mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2025-03-03 11:15:47 +03:00
Add more tests and example
This commit is contained in:
parent
3401bdaab6
commit
b2eb1217c2
25
examples/providers/configuration/configuration_ini_init.py
Normal file
25
examples/providers/configuration/configuration_ini_init.py
Normal 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"
|
|
@ -6,21 +6,37 @@ from pytest import fixture
|
||||||
|
|
||||||
@fixture
|
@fixture
|
||||||
def yaml_config_file(tmp_path):
|
def yaml_config_file(tmp_path):
|
||||||
yaml_config_file = str(tmp_path / "config.yml")
|
config_file = str(tmp_path / "config.yml")
|
||||||
with open(yaml_config_file, "w") as file:
|
with open(config_file, "w") as file:
|
||||||
file.write(
|
file.write(
|
||||||
"section1:\n"
|
"section1:\n"
|
||||||
" value1: yaml-loaded\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):
|
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()
|
container = ContainerWithConfig()
|
||||||
assert container.config.section1.value1() == "yaml-loaded"
|
assert container.config.section1.value1() == "yaml-loaded"
|
||||||
|
assert container.config.section2.value2() == "ini-loaded"
|
||||||
|
|
||||||
|
|
||||||
def test_auto_load_and_overriding(yaml_config_file):
|
def test_auto_load_and_overriding(yaml_config_file):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user