Add more tests and example

This commit is contained in:
Roman Mogylatov 2021-10-26 20:17:13 -04:00
parent 3401bdaab6
commit b2eb1217c2
2 changed files with 46 additions and 5 deletions

View File

@ -0,0 +1,25 @@
"""`Configuration` provider values loading example."""
from dependency_injector import containers, providers
class Container(containers.DeclarativeContainer):
config = providers.Configuration(ini_files=["./config.ini"])
if __name__ == "__main__":
container = Container()
assert container.config() == {
"aws": {
"access_key_id": "KEY",
"secret_access_key": "SECRET",
},
}
assert container.config.aws() == {
"access_key_id": "KEY",
"secret_access_key": "SECRET",
}
assert container.config.aws.access_key_id() == "KEY"
assert container.config.aws.secret_access_key() == "SECRET"

View File

@ -6,21 +6,37 @@ from pytest import fixture
@fixture
def yaml_config_file(tmp_path):
yaml_config_file = str(tmp_path / "config.yml")
with open(yaml_config_file, "w") as file:
config_file = str(tmp_path / "config.yml")
with open(config_file, "w") as file:
file.write(
"section1:\n"
" value1: yaml-loaded\n"
)
return yaml_config_file
return config_file
def test_auto_load(yaml_config_file):
@fixture
def ini_config_file(tmp_path):
config_file = str(tmp_path / "config.ini")
with open(config_file, "w") as file:
file.write(
"[section2]:\n"
"value2 = ini-loaded\n"
)
return config_file
def test_auto_load(yaml_config_file, ini_config_file):
class ContainerWithConfig(containers.DeclarativeContainer):
config = providers.Configuration(yaml_files=[yaml_config_file])
config = providers.Configuration(
yaml_files=[yaml_config_file],
ini_files=[ini_config_file],
)
container = ContainerWithConfig()
assert container.config.section1.value1() == "yaml-loaded"
assert container.config.section2.value2() == "ini-loaded"
def test_auto_load_and_overriding(yaml_config_file):