11"""Configuration.from_pydantic() tests."""
22
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+
518from pytest import fixture , mark , raises
619
20+ from dependency_injector import errors , providers
721
8- class Section11 (pydantic .BaseModel ):
9- value1 = 1
22+ pytestmark = mark .pydantic
1023
1124
12- class Section12 ( pydantic . BaseModel ):
13- value2 = 2
25+ class Section11 ( BaseModel ):
26+ value1 : int = 1
1427
1528
16- class Settings1 (pydantic .BaseSettings ):
17- section1 = Section11 ()
18- section2 = Section12 ()
29+ class Section12 (BaseModel ):
30+ value2 : int = 2
1931
2032
21- class Section21 ( pydantic . BaseModel ):
22- value1 = 11
23- value11 = 11
33+ class Settings1 ( BaseSettings ):
34+ section1 : Section11 = Section11 ()
35+ section2 : Section12 = Section12 ()
2436
2537
26- class Section3 (pydantic .BaseModel ):
27- value3 = 3
38+ class Section21 (BaseModel ):
39+ value1 : int = 11
40+ value11 : int = 11
2841
2942
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+
3351
3452@fixture
3553def no_pydantic_module_installed ():
36- providers .pydantic = None
54+ has_pydantic_settings = providers .has_pydantic_settings
55+ providers .has_pydantic_settings = False
3756 yield
38- providers .pydantic = pydantic
57+ providers .has_pydantic_settings = has_pydantic_settings
3958
4059
4160def test (config ):
@@ -82,66 +101,70 @@ def test_merge(config):
82101
83102
84103def test_empty_settings (config ):
85- config .from_pydantic (pydantic . BaseSettings ())
104+ config .from_pydantic (BaseSettings ())
86105 assert config () == {}
87106
88107
89108@mark .parametrize ("config_type" , ["strict" ])
90109def test_empty_settings_strict_mode (config ):
91110 with raises (ValueError ):
92- config .from_pydantic (pydantic . BaseSettings ())
111+ config .from_pydantic (BaseSettings ())
93112
94113
95114def test_option_empty_settings (config ):
96- config .option .from_pydantic (pydantic . BaseSettings ())
115+ config .option .from_pydantic (BaseSettings ())
97116 assert config .option () == {}
98117
99118
100119@mark .parametrize ("config_type" , ["strict" ])
101120def test_option_empty_settings_strict_mode (config ):
102121 with raises (ValueError ):
103- config .option .from_pydantic (pydantic . BaseSettings ())
122+ config .option .from_pydantic (BaseSettings ())
104123
105124
106125def test_required_empty_settings (config ):
107126 with raises (ValueError ):
108- config .from_pydantic (pydantic . BaseSettings (), required = True )
127+ config .from_pydantic (BaseSettings (), required = True )
109128
110129
111130def test_required_option_empty_settings (config ):
112131 with raises (ValueError ):
113- config .option .from_pydantic (pydantic . BaseSettings (), required = True )
132+ config .option .from_pydantic (BaseSettings (), required = True )
114133
115134
116135@mark .parametrize ("config_type" , ["strict" ])
117136def test_not_required_empty_settings_strict_mode (config ):
118- config .from_pydantic (pydantic . BaseSettings (), required = False )
137+ config .from_pydantic (BaseSettings (), required = False )
119138 assert config () == {}
120139
121140
122141@mark .parametrize ("config_type" , ["strict" ])
123142def 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 )
125144 assert config .option () == {}
126145 assert config () == {"option" : {}}
127146
128147
129148def 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+ ):
131156 config .from_pydantic ({})
132- assert error .value .args [0 ] == (
133- "Unable to recognize settings instance, expect \" pydantic.BaseSettings\" , "
134- "got {0} instead" .format ({})
135- )
136157
137158
138159def 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+ ):
140167 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- )
145168
146169
147170def test_subclass_instead_of_instance (config ):
@@ -164,21 +187,25 @@ def test_option_subclass_instead_of_instance(config):
164187
165188@mark .usefixtures ("no_pydantic_module_installed" )
166189def 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+ ):
168198 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- )
174199
175200
176201@mark .usefixtures ("no_pydantic_module_installed" )
177202def 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+ ):
179211 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