Skip to content

Commit ddf106f

Browse files
committed
Add tests
1 parent dccd26c commit ddf106f

File tree

2 files changed

+122
-1
lines changed

2 files changed

+122
-1
lines changed

tests/unit/providers/configuration/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import os
44

55
from dependency_injector import providers
6-
from pytest import fixture, mark
6+
from pytest import fixture
77

88

99
@fixture
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
"""Configuration.from_pydantic() tests."""
2+
3+
import pydantic
4+
from dependency_injector import providers
5+
from pytest import fixture, mark, raises
6+
7+
8+
class Section11(pydantic.BaseModel):
9+
value1 = 1
10+
11+
12+
class Section12(pydantic.BaseModel):
13+
value2 = 2
14+
15+
16+
class Settings1(pydantic.BaseSettings):
17+
section1 = Section11()
18+
section2 = Section12()
19+
20+
21+
class Section21(pydantic.BaseModel):
22+
value1 = 11
23+
value11 = 11
24+
25+
26+
class Section3(pydantic.BaseModel):
27+
value3 = 3
28+
29+
30+
class Settings2(pydantic.BaseSettings):
31+
section1 = Section21()
32+
section3 = Section3()
33+
34+
35+
@fixture
36+
def config(config_type, pydantic_settings_1, pydantic_settings_2):
37+
if config_type == "strict":
38+
return providers.Configuration(strict=True)
39+
elif config_type == "default":
40+
return providers.Configuration(pydantic_settings=[pydantic_settings_1, pydantic_settings_2])
41+
else:
42+
raise ValueError("Undefined config type \"{0}\"".format(config_type))
43+
44+
45+
@fixture
46+
def pydantic_settings_1():
47+
return Settings1()
48+
49+
50+
@fixture
51+
def pydantic_settings_2():
52+
return Settings2()
53+
54+
55+
def test_load(config):
56+
config.load()
57+
58+
assert config() == {
59+
"section1": {
60+
"value1": 11,
61+
"value11": 11,
62+
},
63+
"section2": {
64+
"value2": 2,
65+
},
66+
"section3": {
67+
"value3": 3,
68+
},
69+
}
70+
assert config.section1() == {"value1": 11, "value11": 11}
71+
assert config.section1.value1() == 11
72+
assert config.section1.value11() == 11
73+
assert config.section2() == {"value2": 2}
74+
assert config.section2.value2() == 2
75+
assert config.section3() == {"value3": 3}
76+
assert config.section3.value3() == 3
77+
78+
79+
def test_get_pydantic_settings(config, pydantic_settings_1, pydantic_settings_2):
80+
assert config.get_pydantic_settings() == [pydantic_settings_1, pydantic_settings_2]
81+
82+
83+
def test_set_pydantic_settings(config):
84+
class Settings3(pydantic.BaseSettings):
85+
...
86+
87+
class Settings4(pydantic.BaseSettings):
88+
...
89+
90+
settings_3 = Settings3()
91+
settings_4 = Settings4()
92+
93+
config.set_pydantic_settings([settings_3, settings_4])
94+
assert config.get_pydantic_settings() == [settings_3, settings_4]
95+
96+
97+
def test_file_does_not_exist(config):
98+
config.set_pydantic_settings([pydantic.BaseSettings()])
99+
config.load()
100+
assert config() == {}
101+
102+
103+
@mark.parametrize("config_type", ["strict"])
104+
def test_file_does_not_exist_strict_mode(config):
105+
config.set_pydantic_settings([pydantic.BaseSettings()])
106+
with raises(ValueError):
107+
config.load()
108+
assert config() == {}
109+
110+
111+
def test_required_file_does_not_exist(config):
112+
config.set_pydantic_settings([pydantic.BaseSettings()])
113+
with raises(ValueError):
114+
config.load(required=True)
115+
116+
117+
@mark.parametrize("config_type", ["strict"])
118+
def test_not_required_file_does_not_exist_strict_mode(config):
119+
config.set_pydantic_settings([pydantic.BaseSettings()])
120+
config.load(required=False)
121+
assert config() == {}

0 commit comments

Comments
 (0)