1
1
"""Configuration.from_pydantic() tests."""
2
2
3
- import pydantic
4
- from dependency_injector import providers , errors
3
+ from pydantic import BaseModel
4
+
5
+ try :
6
+ from pydantic_settings import (
7
+ BaseSettings , # type: ignore[import-not-found,unused-ignore]
8
+ )
9
+ except ImportError :
10
+ try :
11
+ from pydantic import BaseSettings # type: ignore[no-redef,unused-ignore]
12
+ except ImportError :
13
+
14
+ class BaseSettings : # type: ignore[no-redef]
15
+ """No-op fallback"""
16
+
17
+
5
18
from pytest import fixture , mark , raises
6
19
20
+ from dependency_injector import errors , providers
7
21
8
- class Section11 (pydantic .BaseModel ):
9
- value1 = 1
22
+ pytestmark = mark .pydantic
10
23
11
24
12
- class Section12 ( pydantic . BaseModel ):
13
- value2 = 2
25
+ class Section11 ( BaseModel ):
26
+ value1 : int = 1
14
27
15
28
16
- class Settings1 (pydantic .BaseSettings ):
17
- section1 = Section11 ()
18
- section2 = Section12 ()
29
+ class Section12 (BaseModel ):
30
+ value2 : int = 2
19
31
20
32
21
- class Section21 ( pydantic . BaseModel ):
22
- value1 = 11
23
- value11 = 11
33
+ class Settings1 ( BaseSettings ):
34
+ section1 : Section11 = Section11 ()
35
+ section2 : Section12 = Section12 ()
24
36
25
37
26
- class Section3 (pydantic .BaseModel ):
27
- value3 = 3
38
+ class Section21 (BaseModel ):
39
+ value1 : int = 11
40
+ value11 : int = 11
28
41
29
42
30
- class Settings2 (pydantic .BaseSettings ):
31
- section1 = Section21 ()
32
- section3 = Section3 ()
43
+ class Section3 (BaseModel ):
44
+ value3 : int = 3
45
+
46
+
47
+ class Settings2 (BaseSettings ):
48
+ section1 : Section21 = Section21 ()
49
+ section3 : Section3 = Section3 ()
50
+
33
51
34
52
@fixture
35
53
def no_pydantic_module_installed ():
36
- providers .pydantic = None
54
+ has_pydantic_settings = providers .has_pydantic_settings
55
+ providers .has_pydantic_settings = False
37
56
yield
38
- providers .pydantic = pydantic
57
+ providers .has_pydantic_settings = has_pydantic_settings
39
58
40
59
41
60
def test (config ):
@@ -82,66 +101,70 @@ def test_merge(config):
82
101
83
102
84
103
def test_empty_settings (config ):
85
- config .from_pydantic (pydantic . BaseSettings ())
104
+ config .from_pydantic (BaseSettings ())
86
105
assert config () == {}
87
106
88
107
89
108
@mark .parametrize ("config_type" , ["strict" ])
90
109
def test_empty_settings_strict_mode (config ):
91
110
with raises (ValueError ):
92
- config .from_pydantic (pydantic . BaseSettings ())
111
+ config .from_pydantic (BaseSettings ())
93
112
94
113
95
114
def test_option_empty_settings (config ):
96
- config .option .from_pydantic (pydantic . BaseSettings ())
115
+ config .option .from_pydantic (BaseSettings ())
97
116
assert config .option () == {}
98
117
99
118
100
119
@mark .parametrize ("config_type" , ["strict" ])
101
120
def test_option_empty_settings_strict_mode (config ):
102
121
with raises (ValueError ):
103
- config .option .from_pydantic (pydantic . BaseSettings ())
122
+ config .option .from_pydantic (BaseSettings ())
104
123
105
124
106
125
def test_required_empty_settings (config ):
107
126
with raises (ValueError ):
108
- config .from_pydantic (pydantic . BaseSettings (), required = True )
127
+ config .from_pydantic (BaseSettings (), required = True )
109
128
110
129
111
130
def test_required_option_empty_settings (config ):
112
131
with raises (ValueError ):
113
- config .option .from_pydantic (pydantic . BaseSettings (), required = True )
132
+ config .option .from_pydantic (BaseSettings (), required = True )
114
133
115
134
116
135
@mark .parametrize ("config_type" , ["strict" ])
117
136
def test_not_required_empty_settings_strict_mode (config ):
118
- config .from_pydantic (pydantic . BaseSettings (), required = False )
137
+ config .from_pydantic (BaseSettings (), required = False )
119
138
assert config () == {}
120
139
121
140
122
141
@mark .parametrize ("config_type" , ["strict" ])
123
142
def test_not_required_option_empty_settings_strict_mode (config ):
124
- config .option .from_pydantic (pydantic . BaseSettings (), required = False )
143
+ config .option .from_pydantic (BaseSettings (), required = False )
125
144
assert config .option () == {}
126
145
assert config () == {"option" : {}}
127
146
128
147
129
148
def test_not_instance_of_settings (config ):
130
- with raises (errors .Error ) as error :
149
+ with raises (
150
+ errors .Error ,
151
+ match = (
152
+ r"Unable to recognize settings instance, expect \"pydantic(?:_settings)?\.BaseSettings\", "
153
+ r"got {0} instead" .format ({})
154
+ ),
155
+ ):
131
156
config .from_pydantic ({})
132
- assert error .value .args [0 ] == (
133
- "Unable to recognize settings instance, expect \" pydantic.BaseSettings\" , "
134
- "got {0} instead" .format ({})
135
- )
136
157
137
158
138
159
def test_option_not_instance_of_settings (config ):
139
- with raises (errors .Error ) as error :
160
+ with raises (
161
+ errors .Error ,
162
+ match = (
163
+ r"Unable to recognize settings instance, expect \"pydantic(?:_settings)?\.BaseSettings\", "
164
+ "got {0} instead" .format ({})
165
+ ),
166
+ ):
140
167
config .option .from_pydantic ({})
141
- assert error .value .args [0 ] == (
142
- "Unable to recognize settings instance, expect \" pydantic.BaseSettings\" , "
143
- "got {0} instead" .format ({})
144
- )
145
168
146
169
147
170
def test_subclass_instead_of_instance (config ):
@@ -164,21 +187,25 @@ def test_option_subclass_instead_of_instance(config):
164
187
165
188
@mark .usefixtures ("no_pydantic_module_installed" )
166
189
def test_no_pydantic_installed (config ):
167
- with raises (errors .Error ) as error :
190
+ with raises (
191
+ errors .Error ,
192
+ match = (
193
+ r"Unable to load pydantic configuration - pydantic(?:_settings)? is not installed\. "
194
+ r"Install pydantic or install Dependency Injector with pydantic extras: "
195
+ r"\"pip install dependency-injector\[pydantic2?\]\""
196
+ ),
197
+ ):
168
198
config .from_pydantic (Settings1 ())
169
- assert error .value .args [0 ] == (
170
- "Unable to load pydantic configuration - pydantic is not installed. "
171
- "Install pydantic or install Dependency Injector with pydantic extras: "
172
- "\" pip install dependency-injector[pydantic]\" "
173
- )
174
199
175
200
176
201
@mark .usefixtures ("no_pydantic_module_installed" )
177
202
def test_option_no_pydantic_installed (config ):
178
- with raises (errors .Error ) as error :
203
+ with raises (
204
+ errors .Error ,
205
+ match = (
206
+ r"Unable to load pydantic configuration - pydantic(?:_settings)? is not installed\. "
207
+ r"Install pydantic or install Dependency Injector with pydantic extras: "
208
+ r"\"pip install dependency-injector\[pydantic2?\]\""
209
+ ),
210
+ ):
179
211
config .option .from_pydantic (Settings1 ())
180
- assert error .value .args [0 ] == (
181
- "Unable to load pydantic configuration - pydantic is not installed. "
182
- "Install pydantic or install Dependency Injector with pydantic extras: "
183
- "\" pip install dependency-injector[pydantic]\" "
184
- )
0 commit comments