44from pydantic import BaseModel , ValidationError
55from pydantic import Field as PField
66from sqlmodel import Field , SQLModel
7- from sqlmodel ._compat import IS_PYDANTIC_V2
8-
9- from tests .conftest import needs_pydanticv1 , needs_pydanticv2
107
118"""
129Alias tests for SQLModel and Pydantic compatibility
@@ -22,23 +19,12 @@ class SQLModelUser(SQLModel):
2219
2320
2421# Models with config (validate_by_name=True)
25- if IS_PYDANTIC_V2 :
26-
27- class PydanticUserWithConfig (PydanticUser ):
28- model_config = {"validate_by_name" : True }
29-
30- class SQLModelUserWithConfig (SQLModelUser ):
31- model_config = {"validate_by_name" : True }
32-
33- else :
22+ class PydanticUserWithConfig (PydanticUser ):
23+ model_config = {"validate_by_name" : True }
3424
35- class PydanticUserWithConfig (PydanticUser ):
36- class Config :
37- allow_population_by_field_name = True
3825
39- class SQLModelUserWithConfig (SQLModelUser ):
40- class Config :
41- allow_population_by_field_name = True
26+ class SQLModelUserWithConfig (SQLModelUser ):
27+ model_config = {"validate_by_name" : True }
4228
4329
4430@pytest .mark .parametrize ("model" , [PydanticUser , SQLModelUser ])
@@ -84,10 +70,7 @@ def test_dict_default_uses_field_names(
8470 model : Union [type [PydanticUser ], type [SQLModelUser ]],
8571):
8672 user = model (fullName = "Dana" )
87- if IS_PYDANTIC_V2 or isinstance (user , SQLModel ):
88- data = user .model_dump ()
89- else :
90- data = user .dict ()
73+ data = user .model_dump ()
9174 assert "full_name" in data
9275 assert "fullName" not in data
9376 assert data ["full_name" ] == "Dana"
@@ -98,10 +81,7 @@ def test_dict_by_alias_uses_aliases(
9881 model : Union [type [PydanticUser ], type [SQLModelUser ]],
9982):
10083 user = model (fullName = "Dana" )
101- if IS_PYDANTIC_V2 or isinstance (user , SQLModel ):
102- data = user .model_dump (by_alias = True )
103- else :
104- data = user .dict (by_alias = True )
84+ data = user .model_dump (by_alias = True )
10585 assert "fullName" in data
10686 assert "full_name" not in data
10787 assert data ["fullName" ] == "Dana"
@@ -112,48 +92,19 @@ def test_json_by_alias(
11292 model : Union [type [PydanticUser ], type [SQLModelUser ]],
11393):
11494 user = model (fullName = "Frank" )
115- if IS_PYDANTIC_V2 :
116- json_data = user .model_dump_json (by_alias = True )
117- else :
118- json_data = user .json (by_alias = True )
95+ json_data = user .model_dump_json (by_alias = True )
11996 assert ('"fullName":"Frank"' in json_data ) or ('"fullName": "Frank"' in json_data )
12097 assert "full_name" not in json_data
12198
12299
123- if IS_PYDANTIC_V2 :
124-
125- class PydanticUserV2 (BaseModel ):
126- first_name : str = PField (
127- validation_alias = "firstName" , serialization_alias = "f_name"
128- )
129-
130- class SQLModelUserV2 (SQLModel ):
131- first_name : str = Field (
132- validation_alias = "firstName" , serialization_alias = "f_name"
133- )
134- else :
135- # Dummy classes for Pydantic v1 to prevent import errors
136- PydanticUserV2 = None
137- SQLModelUserV2 = None
100+ class PydanticUserV2 (BaseModel ):
101+ first_name : str = PField (validation_alias = "firstName" , serialization_alias = "f_name" )
138102
139103
140- @needs_pydanticv1
141- def test_validation_alias_runtimeerror_pydantic_v1 ():
142- with pytest .raises (
143- RuntimeError , match = "validation_alias is not supported in Pydantic v1"
144- ):
145- Field (validation_alias = "foo" )
104+ class SQLModelUserV2 (SQLModel ):
105+ first_name : str = Field (validation_alias = "firstName" , serialization_alias = "f_name" )
146106
147107
148- @needs_pydanticv1
149- def test_serialization_alias_runtimeerror_pydantic_v1 ():
150- with pytest .raises (
151- RuntimeError , match = "serialization_alias is not supported in Pydantic v1"
152- ):
153- Field (serialization_alias = "bar" )
154-
155-
156- @needs_pydanticv2
157108@pytest .mark .parametrize ("model" , [PydanticUserV2 , SQLModelUserV2 ])
158109def test_create_with_validation_alias (
159110 model : Union [type [PydanticUserV2 ], type [SQLModelUserV2 ]],
@@ -162,7 +113,6 @@ def test_create_with_validation_alias(
162113 assert user .first_name == "John"
163114
164115
165- @needs_pydanticv2
166116@pytest .mark .parametrize ("model" , [PydanticUserV2 , SQLModelUserV2 ])
167117def test_serialize_with_serialization_alias (
168118 model : Union [type [PydanticUserV2 ], type [SQLModelUserV2 ]],
@@ -175,7 +125,6 @@ def test_serialize_with_serialization_alias(
175125 assert data ["f_name" ] == "Jane"
176126
177127
178- @needs_pydanticv2
179128def test_schema_extra_validation_alias_sqlmodel_v2 ():
180129 class M (SQLModel ):
181130 f : str = Field (schema_extra = {"validation_alias" : "f_alias" })
@@ -184,7 +133,6 @@ class M(SQLModel):
184133 assert m .f == "asd"
185134
186135
187- @needs_pydanticv2
188136def test_schema_extra_serialization_alias_sqlmodel_v2 ():
189137 class M (SQLModel ):
190138 f : str = Field (schema_extra = {"serialization_alias" : "f_out" })
@@ -196,23 +144,6 @@ class M(SQLModel):
196144 assert data ["f_out" ] == "x"
197145
198146
199- @needs_pydanticv1
200- def test_schema_extra_validation_alias_runtimeerror_pydantic_v1 ():
201- with pytest .raises (
202- RuntimeError , match = "validation_alias is not supported in Pydantic v1"
203- ):
204- Field (schema_extra = {"validation_alias" : "x" })
205-
206-
207- @needs_pydanticv1
208- def test_schema_extra_serialization_alias_runtimeerror_pydantic_v1 ():
209- with pytest .raises (
210- RuntimeError , match = "serialization_alias is not supported in Pydantic v1"
211- ):
212- Field (schema_extra = {"serialization_alias" : "y" })
213-
214-
215- @needs_pydanticv2
216147def test_alias_plus_validation_alias_prefers_validation_alias_sqlmodel_v2 ():
217148 class M (SQLModel ):
218149 first_name : str = Field (alias = "fullName" , validation_alias = "v_name" )
@@ -221,7 +152,6 @@ class M(SQLModel):
221152 assert m .first_name == "B"
222153
223154
224- @needs_pydanticv2
225155def test_alias_plus_serialization_alias_prefers_serialization_alias_sqlmodel_v2 ():
226156 class M (SQLModel ):
227157 first_name : str = Field (alias = "fullName" , serialization_alias = "f_name" )
@@ -233,7 +163,6 @@ class M(SQLModel):
233163 assert data ["f_name" ] == "Z"
234164
235165
236- @needs_pydanticv2
237166def test_alias_generator_works_sqlmodel_v2 ():
238167 class M (SQLModel ):
239168 model_config = {"alias_generator" : lambda s : "gen_" + s }
@@ -245,7 +174,6 @@ class M(SQLModel):
245174 assert "gen_f" in data and data ["gen_f" ] == "ok"
246175
247176
248- @needs_pydanticv2
249177def test_alias_generator_with_explicit_alias_prefers_field_alias_sqlmodel_v2 ():
250178 class M (SQLModel ):
251179 model_config = {"alias_generator" : lambda s : "gen_" + s }
0 commit comments