Skip to content

Commit d3e761e

Browse files
committed
fix: Addressed yurii's comments
1 parent 382a52d commit d3e761e

File tree

1 file changed

+37
-32
lines changed

1 file changed

+37
-32
lines changed

tests/test_aliases.py

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55
from pydantic import Field as PField
66
from sqlmodel import Field, SQLModel
77

8-
# -----------------------------------------------------------------------------------
9-
# Models
8+
from tests.conftest import needs_pydanticv2
9+
10+
"""
11+
Alias tests for SQLModel and Pydantic compatibility
12+
"""
1013

1114

1215
class PydanticUser(BaseModel):
@@ -39,12 +42,6 @@ class Config:
3942
allow_population_by_field_name = True
4043

4144

42-
# -----------------------------------------------------------------------------------
43-
# Tests
44-
45-
# Test validate by name
46-
47-
4845
@pytest.mark.parametrize("model", [PydanticUser, SQLModelUser])
4946
def test_create_with_field_name(model: Union[Type[PydanticUser], Type[SQLModelUser]]):
5047
with pytest.raises(ValidationError):
@@ -59,9 +56,6 @@ def test_create_with_field_name_with_config(
5956
assert user.full_name == "Alice"
6057

6158

62-
# Test validate by alias
63-
64-
6559
@pytest.mark.parametrize(
6660
"model",
6761
[PydanticUser, SQLModelUser, PydanticUserWithConfig, SQLModelUserWithConfig],
@@ -78,9 +72,6 @@ def test_create_with_alias(
7872
assert user.full_name == "Bob"
7973

8074

81-
# Test validate by name and alias
82-
83-
8475
@pytest.mark.parametrize("model", [PydanticUserWithConfig, SQLModelUserWithConfig])
8576
def test_create_with_both_prefers_alias(
8677
model: Union[Type[PydanticUserWithConfig], Type[SQLModelUserWithConfig]],
@@ -89,48 +80,47 @@ def test_create_with_both_prefers_alias(
8980
assert user.full_name == "Charlie" # alias should take precedence
9081

9182

92-
# Test serialize
93-
94-
9583
@pytest.mark.parametrize("model", [PydanticUser, SQLModelUser])
9684
def test_dict_default_uses_field_names(
9785
model: Union[Type[PydanticUser], Type[SQLModelUser]],
9886
):
9987
user = model(fullName="Dana")
100-
data = user.dict()
88+
if VERSION.startswith("2."):
89+
data = user.model_dump()
90+
else:
91+
data = user.dict()
10192
assert "full_name" in data
10293
assert "fullName" not in data
10394
assert data["full_name"] == "Dana"
10495

10596

106-
# Test serialize by alias
107-
108-
10997
@pytest.mark.parametrize("model", [PydanticUser, SQLModelUser])
11098
def test_dict_default_uses_aliases(
11199
model: Union[Type[PydanticUser], Type[SQLModelUser]],
112100
):
113101
user = model(fullName="Dana")
114-
data = user.dict(by_alias=True)
102+
if VERSION.startswith("2."):
103+
data = user.model_dump(by_alias=True)
104+
else:
105+
data = user.dict(by_alias=True)
115106
assert "fullName" in data
116107
assert "full_name" not in data
117108
assert data["fullName"] == "Dana"
118109

119110

120-
# Test json by alias
121-
122-
123111
@pytest.mark.parametrize("model", [PydanticUser, SQLModelUser])
124112
def test_json_by_alias(
125113
model: Union[Type[PydanticUser], Type[SQLModelUser]],
126114
):
127115
user = model(fullName="Frank")
128-
json_data = user.json(by_alias=True)
116+
if VERSION.startswith("2."):
117+
json_data = user.model_dump_json(by_alias=True)
118+
else:
119+
json_data = user.json(by_alias=True)
129120
assert ('"fullName":"Frank"' in json_data) or ('"fullName": "Frank"' in json_data)
130121
assert "full_name" not in json_data
131122

132123

133-
# Pydantic v2 specific models - only define if we're running Pydantic v2
134124
if VERSION.startswith("2."):
135125

136126
class PydanticUserV2(BaseModel):
@@ -148,6 +138,24 @@ class SQLModelUserV2(SQLModel):
148138
SQLModelUserV2 = None
149139

150140

141+
def test_validation_alias_runtimeerror_pydantic_v1():
142+
if VERSION.startswith("2."):
143+
pytest.skip("Only relevant for Pydantic v1")
144+
with pytest.raises(
145+
RuntimeError, match="validation_alias is not supported in Pydantic v1"
146+
):
147+
Field(validation_alias="foo")
148+
149+
150+
def test_serialization_alias_runtimeerror_pydantic_v1():
151+
if VERSION.startswith("2."):
152+
pytest.skip("Only relevant for Pydantic v1")
153+
with pytest.raises(
154+
RuntimeError, match="serialization_alias is not supported in Pydantic v1"
155+
):
156+
Field(serialization_alias="bar")
157+
158+
151159
@needs_pydanticv2
152160
@pytest.mark.parametrize("model", [PydanticUserV2, SQLModelUserV2])
153161
def test_create_with_validation_alias(
@@ -157,16 +165,13 @@ def test_create_with_validation_alias(
157165
assert user.first_name == "John"
158166

159167

160-
@pytest.mark.skipif(
161-
not VERSION.startswith("2."),
162-
reason="validation_alias and serialization_alias are not supported in Pydantic v1",
163-
)
168+
@needs_pydanticv2
164169
@pytest.mark.parametrize("model", [PydanticUserV2, SQLModelUserV2])
165170
def test_serialize_with_serialization_alias(
166171
model: Union[Type[PydanticUserV2], Type[SQLModelUserV2]],
167172
):
168173
user = model(firstName="Jane")
169-
data = user.dict(by_alias=True)
174+
data = user.model_dump(by_alias=True)
170175
assert "f_name" in data
171176
assert "firstName" not in data
172177
assert "first_name" not in data

0 commit comments

Comments
 (0)