Skip to content

Commit 824b5c1

Browse files
committed
Change unittests to have different expected schema depending on the version of pydantic
1 parent 317607a commit 824b5c1

File tree

7 files changed

+56
-11
lines changed

7 files changed

+56
-11
lines changed

tests/unit_tests/helpers.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
import pydantic.version
2+
3+
PYDANTIC_VERSION = pydantic.version.version_short()
4+
5+
16
def assert_equal_ignore_key(expected, actual, ignore_keys):
27
def deep_remove_keys(d, keys_to_ignore):
38
if isinstance(d, dict):

tests/unit_tests/test_choice_list.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
from pydantic_forms.core import FormPage
55
from pydantic_forms.validators import Choice, choice_list
66

7+
from tests.unit_tests.helpers import PYDANTIC_VERSION
8+
79

810
def test_choice_list():
911
class LegChoice(Choice):
@@ -149,13 +151,17 @@ def test_choice_list_constraint_at_least_one_item(Form):
149151
Form(choice=[])
150152

151153
errors = exc_info.value.errors(include_url=False, include_context=False)
154+
155+
if PYDANTIC_VERSION == "2.8":
156+
message = "List should have at least 1 item after validation, not 0"
157+
else:
158+
message = "Value should have at least 1 item after validation, not 0"
152159
expected = [
153160
{
154161
"input": [],
155162
"loc": ("choice",),
156-
"msg": "Value should have at least 1 item after validation, not 0",
163+
"msg": message,
157164
"type": "too_short",
158-
# "ctx": {"limit_value": 1},
159165
}
160166
]
161167
assert errors == expected

tests/unit_tests/test_constrained_list.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

44
from pydantic_forms.core import FormPage
55
from pydantic_forms.validators import unique_conlist
6+
from pydantic.version import version_short as pydantic_version_short
7+
8+
from tests.unit_tests.helpers import PYDANTIC_VERSION
69

710

811
def test_constrained_list_good():
@@ -74,12 +77,15 @@ def test_constrained_list_too_short():
7477

7578
errors = exc_info.value.errors(include_url=False, include_context=False)
7679

80+
if PYDANTIC_VERSION == "2.8":
81+
message = "List should have at least 1 item after validation, not 0"
82+
else:
83+
message = "Value should have at least 1 item after validation, not 0"
7784
expected = [
7885
{
79-
# "ctx": {"error": ListMinLengthError(limit_value=1)},
8086
"input": [],
8187
"loc": ("v",),
82-
"msg": "Value should have at least 1 item after validation, not 0",
88+
"msg": message,
8389
"type": "too_short",
8490
}
8591
]
@@ -111,11 +117,15 @@ class UniqueConListModel(FormPage):
111117
UniqueConListModel(v=[])
112118

113119
errors = exc_info.value.errors(include_url=False, include_context=False)
120+
if PYDANTIC_VERSION == "2.8":
121+
message = "List should have at least 1 item after validation, not 0"
122+
else:
123+
message = "Value should have at least 1 item after validation, not 0"
114124
assert errors == [
115125
{
116126
"input": [],
117127
"loc": ("v",),
118-
"msg": "Value should have at least 1 item after validation, not 0",
128+
"msg": message,
119129
"type": "too_short",
120130
# "ctx": {"limit_value": 1},
121131
}

tests/unit_tests/test_display_subscription.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from pydantic_forms.core import FormPage
44
from pydantic_forms.validators import DisplaySubscription, Label, migration_summary
5+
from tests.unit_tests.helpers import PYDANTIC_VERSION
56

67

78
def test_display_subscription():
@@ -35,6 +36,11 @@ class Form(FormPage):
3536
label: Label
3637
summary: Summary
3738

39+
if PYDANTIC_VERSION == "2.8":
40+
summary_ref = {"allOf": [{"$ref": "#/$defs/MigrationSummaryValue"}]}
41+
else:
42+
summary_ref = {"$ref": "#/$defs/MigrationSummaryValue"}
43+
3844
expected = {
3945
"$defs": {"MigrationSummaryValue": {"properties": {}, "title": "MigrationSummaryValue", "type": "object"}},
4046
"additionalProperties": False,
@@ -53,7 +59,7 @@ class Form(FormPage):
5359
"type": "string",
5460
},
5561
"summary": {
56-
"$ref": "#/$defs/MigrationSummaryValue",
62+
**summary_ref,
5763
"default": None,
5864
"format": "summary",
5965
"type": "string",

tests/unit_tests/test_list_of_two.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from pydantic_forms.core import FormPage
55
from pydantic_forms.validators import ListOfTwo
6+
from tests.unit_tests.helpers import PYDANTIC_VERSION
67

78

89
@pytest.fixture(name="Form")
@@ -22,11 +23,15 @@ def test_list_of_two_min_items(Form):
2223
assert Form(two=[1])
2324

2425
errors = error_info.value.errors(include_url=False, include_context=False)
26+
if PYDANTIC_VERSION == "2.8":
27+
message = "List should have at least 2 items after validation, not 1"
28+
else:
29+
message = "Value should have at least 2 items after validation, not 1"
2530
expected = [
2631
{
2732
"input": [1],
2833
"loc": ("two",),
29-
"msg": "Value should have at least 2 items after validation, not 1",
34+
"msg": message,
3035
"type": "too_short",
3136
}
3237
]
@@ -38,11 +43,15 @@ def test_list_of_two_max_items(Form):
3843
assert Form(two=[1, 2, 3])
3944

4045
errors = error_info.value.errors(include_url=False, include_context=False)
46+
if PYDANTIC_VERSION == "2.8":
47+
message = "List should have at most 2 items after validation, not 3"
48+
else:
49+
message = "Value should have at most 2 items after validation, not 3"
4150
expected = [
4251
{
4352
"input": [1, 2, 3],
4453
"loc": ("two",),
45-
"msg": "Value should have at most 2 items after validation, not 3",
54+
"msg": message,
4655
"type": "too_long",
4756
},
4857
]

tests/unit_tests/test_migration_summary.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from pydantic_forms.core import FormPage
44
from pydantic_forms.validators import DisplaySubscription, Label, migration_summary
5+
from tests.unit_tests.helpers import PYDANTIC_VERSION
56

67

78
def test_display_default():
@@ -33,12 +34,17 @@ def test_migration_summary_schema():
3334
class Form(FormPage):
3435
ms: Summary
3536

37+
if PYDANTIC_VERSION == "2.8":
38+
ms_ref = {"allOf": [{"$ref": "#/$defs/MigrationSummaryValue"}]}
39+
else:
40+
ms_ref = {"$ref": "#/$defs/MigrationSummaryValue"}
41+
3642
expected = {
3743
"$defs": {"MigrationSummaryValue": {"properties": {}, "title": "MigrationSummaryValue", "type": "object"}},
3844
"additionalProperties": False,
3945
"properties": {
4046
"ms": {
41-
"$ref": "#/$defs/MigrationSummaryValue",
47+
**ms_ref,
4248
"default": None,
4349
"format": "summary",
4450
"type": "string",

tests/unit_tests/test_read_only_field.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from pydantic_forms.types import strEnum
1212
from pydantic_forms.validators import read_only_field, read_only_list, LongText, OrganisationId
1313
from pydantic_forms.utils.schema import merge_json_schema
14+
from tests.unit_tests.helpers import PYDANTIC_VERSION
1415

1516

1617
class TestEnum(strEnum):
@@ -52,9 +53,11 @@ class Form(FormPage):
5253

5354
actual = Form.model_json_schema()
5455

55-
if pydantic_version_short() in ("2.8", "2.9"):
56+
if PYDANTIC_VERSION in ("2.8", "2.9"):
5657
# Behavior that was changed (fixed) in 2.10 https://github.com/pydantic/pydantic/pull/10692
57-
del actual["properties"]["read_only"]["enum"]
58+
expected["properties"]["read_only"]["enum"] = [
59+
str(read_only_value) if isinstance(read_only_value, UUID) else read_only_value
60+
]
5861

5962
assert actual == expected
6063

0 commit comments

Comments
 (0)