Skip to content

Commit 3ba0e8e

Browse files
committed
✅ Tweak test to use Pydantic v1 compat utils
1 parent 84cd604 commit 3ba0e8e

1 file changed

Lines changed: 7 additions & 6 deletions

File tree

tests/test_aliases.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
from typing import Type, Union
22

33
import pytest
4-
from pydantic import VERSION, BaseModel, ValidationError
4+
from pydantic import BaseModel, ValidationError
55
from pydantic import Field as PField
66
from sqlmodel import Field, SQLModel
7+
from sqlmodel._compat import IS_PYDANTIC_V2
78

89
from tests.conftest import needs_pydanticv1, needs_pydanticv2
910

@@ -21,7 +22,7 @@ class SQLModelUser(SQLModel):
2122

2223

2324
# Models with config (validate_by_name=True)
24-
if VERSION.startswith("2."):
25+
if IS_PYDANTIC_V2:
2526

2627
class PydanticUserWithConfig(PydanticUser):
2728
model_config = {"validate_by_name": True}
@@ -83,7 +84,7 @@ def test_dict_default_uses_field_names(
8384
model: Union[Type[PydanticUser], Type[SQLModelUser]],
8485
):
8586
user = model(fullName="Dana")
86-
if VERSION.startswith("2.") or isinstance(user, SQLModel):
87+
if IS_PYDANTIC_V2 or isinstance(user, SQLModel):
8788
data = user.model_dump()
8889
else:
8990
data = user.dict()
@@ -97,7 +98,7 @@ def test_dict_by_alias_uses_aliases(
9798
model: Union[Type[PydanticUser], Type[SQLModelUser]],
9899
):
99100
user = model(fullName="Dana")
100-
if VERSION.startswith("2.") or isinstance(user, SQLModel):
101+
if IS_PYDANTIC_V2 or isinstance(user, SQLModel):
101102
data = user.model_dump(by_alias=True)
102103
else:
103104
data = user.dict(by_alias=True)
@@ -111,15 +112,15 @@ def test_json_by_alias(
111112
model: Union[Type[PydanticUser], Type[SQLModelUser]],
112113
):
113114
user = model(fullName="Frank")
114-
if VERSION.startswith("2."):
115+
if IS_PYDANTIC_V2:
115116
json_data = user.model_dump_json(by_alias=True)
116117
else:
117118
json_data = user.json(by_alias=True)
118119
assert ('"fullName":"Frank"' in json_data) or ('"fullName": "Frank"' in json_data)
119120
assert "full_name" not in json_data
120121

121122

122-
if VERSION.startswith("2."):
123+
if IS_PYDANTIC_V2:
123124

124125
class PydanticUserV2(BaseModel):
125126
first_name: str = PField(

0 commit comments

Comments
 (0)