Skip to content

Commit b2eb121

Browse files
committed
Add more tests and example
1 parent 3401bda commit b2eb121

File tree

2 files changed

+46
-5
lines changed

2 files changed

+46
-5
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""`Configuration` provider values loading example."""
2+
3+
from dependency_injector import containers, providers
4+
5+
6+
class Container(containers.DeclarativeContainer):
7+
8+
config = providers.Configuration(ini_files=["./config.ini"])
9+
10+
11+
if __name__ == "__main__":
12+
container = Container()
13+
14+
assert container.config() == {
15+
"aws": {
16+
"access_key_id": "KEY",
17+
"secret_access_key": "SECRET",
18+
},
19+
}
20+
assert container.config.aws() == {
21+
"access_key_id": "KEY",
22+
"secret_access_key": "SECRET",
23+
}
24+
assert container.config.aws.access_key_id() == "KEY"
25+
assert container.config.aws.secret_access_key() == "SECRET"

tests/unit/containers/instance/test_load_config_py2_py3.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,37 @@
66

77
@fixture
88
def yaml_config_file(tmp_path):
9-
yaml_config_file = str(tmp_path / "config.yml")
10-
with open(yaml_config_file, "w") as file:
9+
config_file = str(tmp_path / "config.yml")
10+
with open(config_file, "w") as file:
1111
file.write(
1212
"section1:\n"
1313
" value1: yaml-loaded\n"
1414
)
15-
return yaml_config_file
15+
return config_file
1616

1717

18-
def test_auto_load(yaml_config_file):
18+
@fixture
19+
def ini_config_file(tmp_path):
20+
config_file = str(tmp_path / "config.ini")
21+
with open(config_file, "w") as file:
22+
file.write(
23+
"[section2]:\n"
24+
"value2 = ini-loaded\n"
25+
)
26+
return config_file
27+
28+
29+
30+
def test_auto_load(yaml_config_file, ini_config_file):
1931
class ContainerWithConfig(containers.DeclarativeContainer):
20-
config = providers.Configuration(yaml_files=[yaml_config_file])
32+
config = providers.Configuration(
33+
yaml_files=[yaml_config_file],
34+
ini_files=[ini_config_file],
35+
)
2136

2237
container = ContainerWithConfig()
2338
assert container.config.section1.value1() == "yaml-loaded"
39+
assert container.config.section2.value2() == "ini-loaded"
2440

2541

2642
def test_auto_load_and_overriding(yaml_config_file):

0 commit comments

Comments
 (0)