Skip to content

Commit 497aeb1

Browse files
committed
Rename test file, fix tests for Pydantic V1
1 parent 522b030 commit 497aeb1

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

tests/test_field_pd_and_json_kwarrgs.py renamed to tests/test_field_json_schema_extra.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import pytest
22
from sqlmodel import Field, SQLModel
3+
from sqlmodel._compat import IS_PYDANTIC_V2
4+
5+
from tests.conftest import needs_pydanticv2
36

47

58
def test_json_schema_extra_applied():
@@ -13,15 +16,18 @@ class Item(SQLModel):
1316
}
1417
)
1518

16-
schema = Item.model_json_schema()
19+
if IS_PYDANTIC_V2:
20+
schema = Item.model_json_schema()
21+
else:
22+
schema = Item.schema()
1723

1824
name_schema = schema["properties"]["name"]
1925

2026
assert name_schema["example"] == "Sword of Power"
2127
assert name_schema["x-custom-key"] == "Important Data"
2228

2329

24-
def test_schema_extra_and_new_param_conflict(caplog):
30+
def test_schema_extra_and_json_schema_extra_conflict(caplog):
2531
"""
2632
Test that passing schema_extra and json_schema_extra at the same time produces
2733
a warning.
@@ -47,19 +53,29 @@ class LegacyItem(SQLModel):
4753
}
4854
)
4955

50-
schema = LegacyItem.model_json_schema()
56+
if IS_PYDANTIC_V2:
57+
schema = LegacyItem.model_json_schema()
58+
else:
59+
schema = LegacyItem.schema()
5160

5261
name_schema = schema["properties"]["name"]
5362

5463
assert name_schema["example"] == "Sword of Old"
5564
assert name_schema["x-custom-key"] == "Important Data"
5665

57-
field_info = LegacyItem.model_fields["name"]
58-
assert field_info.serialization_alias == "id_test"
66+
if IS_PYDANTIC_V2:
67+
# With Pydantic V1 serialization_alias from schema_extra is applied
68+
field_info = LegacyItem.model_fields["name"]
69+
assert field_info.serialization_alias == "id_test"
70+
else: # With Pydantic V1 it just goes to schema
71+
assert name_schema["serialization_alias"] == "id_test"
5972

6073

74+
@needs_pydanticv2
6175
def test_json_schema_extra_mix_in_schema_extra():
62-
"""test that json_schema_extra is applied when it is in schema_extra"""
76+
"""
77+
Test workaround when json_schema_extra was passed via schema_extra with Pydantic v2.
78+
"""
6379

6480
with pytest.warns(DeprecationWarning, match="schema_extra parameter is deprecated"):
6581

0 commit comments

Comments
 (0)