Browse Source

Update tests for Pydantic v1 and v2, and Python 3.14

pull/14168/head
Sebastián Ramírez 10 months ago
parent
commit
f2e5568308
  1. 2
      tests/test_compat_params_v1.py
  2. 2
      tests/test_pydantic_v1_v2_01.py
  3. 2
      tests/test_pydantic_v1_v2_list.py
  4. 2
      tests/test_pydantic_v1_v2_mixed.py
  5. 648
      tests/test_pydantic_v1_v2_multifile/test_multifile.py
  6. 2
      tests/test_pydantic_v1_v2_noneable.py
  7. 2
      tests/utils.py

2
tests/test_compat_params_v1.py

@ -5,7 +5,7 @@ import pytest
from tests.utils import pydantic_snapshot, skip_module_if_py_gte_314 from tests.utils import pydantic_snapshot, skip_module_if_py_gte_314
if sys.version_info > (3, 13): if sys.version_info >= (3, 14):
skip_module_if_py_gte_314() skip_module_if_py_gte_314()
from fastapi import FastAPI from fastapi import FastAPI

2
tests/test_pydantic_v1_v2_01.py

@ -3,7 +3,7 @@ from typing import Any, List, Union
from tests.utils import pydantic_snapshot, skip_module_if_py_gte_314 from tests.utils import pydantic_snapshot, skip_module_if_py_gte_314
if sys.version_info > (3, 13): if sys.version_info >= (3, 14):
skip_module_if_py_gte_314() skip_module_if_py_gte_314()
from fastapi import FastAPI from fastapi import FastAPI

2
tests/test_pydantic_v1_v2_list.py

@ -3,7 +3,7 @@ from typing import Any, List, Union
from tests.utils import pydantic_snapshot, skip_module_if_py_gte_314 from tests.utils import pydantic_snapshot, skip_module_if_py_gte_314
if sys.version_info > (3, 13): if sys.version_info >= (3, 14):
skip_module_if_py_gte_314() skip_module_if_py_gte_314()
from fastapi import FastAPI from fastapi import FastAPI

2
tests/test_pydantic_v1_v2_mixed.py

@ -3,7 +3,7 @@ from typing import Any, List, Union
from tests.utils import pydantic_snapshot, skip_module_if_py_gte_314 from tests.utils import pydantic_snapshot, skip_module_if_py_gte_314
if sys.version_info > (3, 13): if sys.version_info >= (3, 14):
skip_module_if_py_gte_314() skip_module_if_py_gte_314()
from fastapi import FastAPI from fastapi import FastAPI

648
tests/test_pydantic_v1_v2_multifile/test_multifile.py

@ -2,10 +2,9 @@ import sys
from tests.utils import pydantic_snapshot, skip_module_if_py_gte_314 from tests.utils import pydantic_snapshot, skip_module_if_py_gte_314
if sys.version_info > (3, 13): if sys.version_info >= (3, 14):
skip_module_if_py_gte_314() skip_module_if_py_gte_314()
from dirty_equals import IsPartialDict
from fastapi.testclient import TestClient from fastapi.testclient import TestClient
from inline_snapshot import snapshot from inline_snapshot import snapshot
@ -767,231 +766,472 @@ def test_openapi_schema():
} }
}, },
}, },
"components": IsPartialDict( "components": {
# Pydantic v2 creates a couple of extra models separated for input and output "schemas": pydantic_snapshot(
{ v1=snapshot(
"schemas": { {
"Body_handle_v2_items_in_list_to_v1_item_in_list_v2_to_v1_list_of_items_to_list_of_items_post": { "Body_handle_v2_items_in_list_to_v1_item_in_list_v2_to_v1_list_of_items_to_list_of_items_post": {
"properties": { "properties": {
"data1": { "data1": {
"items": { "items": {
"$ref": "#/components/schemas/tests__test_pydantic_v1_v2_multifile__modelsv2__ItemInList" "$ref": "#/components/schemas/tests__test_pydantic_v1_v2_multifile__modelsv2__ItemInList"
}, },
"type": "array", "type": "array",
"title": "Data1", "title": "Data1",
}, },
"data2": { "data2": {
"items": { "items": {
"$ref": "#/components/schemas/tests__test_pydantic_v1_v2_multifile__modelsv2b__ItemInList" "$ref": "#/components/schemas/tests__test_pydantic_v1_v2_multifile__modelsv2b__ItemInList"
}, },
"type": "array", "type": "array",
"title": "Data2", "title": "Data2",
}, },
},
"type": "object",
"required": ["data1", "data2"],
"title": "Body_handle_v2_items_in_list_to_v1_item_in_list_v2_to_v1_list_of_items_to_list_of_items_post",
},
"Body_handle_v2_same_name_to_v1_v2_to_v1_same_name_post": {
"properties": {
"item1": pydantic_snapshot(
v2=snapshot(
{
"$ref": "#/components/schemas/tests__test_pydantic_v1_v2_multifile__modelsv2__Item-Input"
}
),
v1=snapshot(
{
"$ref": "#/components/schemas/tests__test_pydantic_v1_v2_multifile__modelsv2__Item"
}
),
),
"item2": {
"$ref": "#/components/schemas/tests__test_pydantic_v1_v2_multifile__modelsv2b__Item"
}, },
"type": "object",
"required": ["data1", "data2"],
"title": "Body_handle_v2_items_in_list_to_v1_item_in_list_v2_to_v1_list_of_items_to_list_of_items_post",
}, },
"type": "object", "Body_handle_v2_same_name_to_v1_v2_to_v1_same_name_post": {
"required": ["item1", "item2"], "properties": {
"title": "Body_handle_v2_same_name_to_v1_v2_to_v1_same_name_post", "item1": {
}, "$ref": "#/components/schemas/tests__test_pydantic_v1_v2_multifile__modelsv2__Item"
"HTTPValidationError": { },
"properties": { "item2": {
"detail": { "$ref": "#/components/schemas/tests__test_pydantic_v1_v2_multifile__modelsv2b__Item"
"items": { },
"$ref": "#/components/schemas/ValidationError" },
}, "type": "object",
"type": "array", "required": ["item1", "item2"],
"title": "Detail", "title": "Body_handle_v2_same_name_to_v1_v2_to_v1_same_name_post",
}
}, },
"type": "object", "HTTPValidationError": {
"title": "HTTPValidationError", "properties": {
}, "detail": {
"ValidationError": { "items": {
"properties": { "$ref": "#/components/schemas/ValidationError"
"loc": { },
"items": { "type": "array",
"anyOf": [ "title": "Detail",
{"type": "string"}, }
{"type": "integer"}, },
] "type": "object",
}, "title": "HTTPValidationError",
"type": "array",
"title": "Location",
},
"msg": {"type": "string", "title": "Message"},
"type": {"type": "string", "title": "Error Type"},
},
"type": "object",
"required": ["loc", "msg", "type"],
"title": "ValidationError",
},
"tests__test_pydantic_v1_v2_multifile__modelsv1__SubItem": {
"properties": {"name": {"type": "string", "title": "Name"}},
"type": "object",
"required": ["name"],
"title": "SubItem",
},
"tests__test_pydantic_v1_v2_multifile__modelsv1__ItemInList": {
"properties": {
"name1": {"type": "string", "title": "Name1"}
}, },
"type": "object", "ValidationError": {
"required": ["name1"], "properties": {
"title": "ItemInList", "loc": {
}, "items": {
"tests__test_pydantic_v1_v2_multifile__modelsv1__Item": { "anyOf": [
"properties": { {"type": "string"},
"title": {"type": "string", "title": "Title"}, {"type": "integer"},
"size": {"type": "integer", "title": "Size"}, ]
"description": { },
"type": "string", "type": "array",
"title": "Description", "title": "Location",
}, },
"sub": { "msg": {"type": "string", "title": "Message"},
"$ref": "#/components/schemas/tests__test_pydantic_v1_v2_multifile__modelsv1__SubItem" "type": {"type": "string", "title": "Error Type"},
}, },
"multi": { "type": "object",
"items": { "required": ["loc", "msg", "type"],
"title": "ValidationError",
},
"tests__test_pydantic_v1_v2_multifile__modelsv1__Item": {
"properties": {
"title": {"type": "string", "title": "Title"},
"size": {"type": "integer", "title": "Size"},
"description": {
"type": "string",
"title": "Description",
},
"sub": {
"$ref": "#/components/schemas/tests__test_pydantic_v1_v2_multifile__modelsv1__SubItem" "$ref": "#/components/schemas/tests__test_pydantic_v1_v2_multifile__modelsv1__SubItem"
}, },
"type": "array", "multi": {
"title": "Multi", "items": {
"default": [], "$ref": "#/components/schemas/tests__test_pydantic_v1_v2_multifile__modelsv1__SubItem"
},
"type": "array",
"title": "Multi",
"default": [],
},
}, },
"type": "object",
"required": ["title", "size", "sub"],
"title": "Item",
}, },
"type": "object", "tests__test_pydantic_v1_v2_multifile__modelsv1__ItemInList": {
"required": ["title", "size", "sub"], "properties": {
"title": "Item", "name1": {"type": "string", "title": "Name1"}
},
"tests__test_pydantic_v1_v2_multifile__modelsv2__Item": {
"properties": {
"new_title": {"type": "string", "title": "New Title"},
"new_size": {"type": "integer", "title": "New Size"},
"new_description": pydantic_snapshot(
v2=snapshot(
{
"anyOf": [
{"type": "string"},
{"type": "null"},
],
"title": "New Description",
}
),
v1=snapshot(
{"type": "string", "title": "New Description"}
),
),
"new_sub": {
"$ref": "#/components/schemas/tests__test_pydantic_v1_v2_multifile__modelsv2__SubItem"
}, },
"new_multi": { "type": "object",
"items": { "required": ["name1"],
"title": "ItemInList",
},
"tests__test_pydantic_v1_v2_multifile__modelsv1__SubItem": {
"properties": {
"name": {"type": "string", "title": "Name"}
},
"type": "object",
"required": ["name"],
"title": "SubItem",
},
"tests__test_pydantic_v1_v2_multifile__modelsv2__Item": {
"properties": {
"new_title": {
"type": "string",
"title": "New Title",
},
"new_size": {
"type": "integer",
"title": "New Size",
},
"new_description": {
"type": "string",
"title": "New Description",
},
"new_sub": {
"$ref": "#/components/schemas/tests__test_pydantic_v1_v2_multifile__modelsv2__SubItem" "$ref": "#/components/schemas/tests__test_pydantic_v1_v2_multifile__modelsv2__SubItem"
}, },
"type": "array", "new_multi": {
"title": "New Multi", "items": {
"default": [], "$ref": "#/components/schemas/tests__test_pydantic_v1_v2_multifile__modelsv2__SubItem"
},
"type": "array",
"title": "New Multi",
"default": [],
},
}, },
"type": "object",
"required": ["new_title", "new_size", "new_sub"],
"title": "Item",
}, },
"type": "object", "tests__test_pydantic_v1_v2_multifile__modelsv2__ItemInList": {
"required": ["new_title", "new_size", "new_sub"], "properties": {
"title": "Item", "name2": {"type": "string", "title": "Name2"}
}, },
"tests__test_pydantic_v1_v2_multifile__modelsv2__ItemInList": { "type": "object",
"properties": { "required": ["name2"],
"name2": {"type": "string", "title": "Name2"} "title": "ItemInList",
}, },
"type": "object", "tests__test_pydantic_v1_v2_multifile__modelsv2__SubItem": {
"required": ["name2"], "properties": {
"title": "ItemInList", "new_sub_name": {
}, "type": "string",
"tests__test_pydantic_v1_v2_multifile__modelsv2__SubItem": { "title": "New Sub Name",
"properties": { }
"new_sub_name": { },
"type": "string", "type": "object",
"title": "New Sub Name", "required": ["new_sub_name"],
} "title": "SubItem",
}, },
"type": "object", "tests__test_pydantic_v1_v2_multifile__modelsv2b__Item": {
"required": ["new_sub_name"], "properties": {
"title": "SubItem", "dup_title": {
}, "type": "string",
"tests__test_pydantic_v1_v2_multifile__modelsv2b__Item": { "title": "Dup Title",
"properties": { },
"dup_title": {"type": "string", "title": "Dup Title"}, "dup_size": {
"dup_size": {"type": "integer", "title": "Dup Size"}, "type": "integer",
"dup_description": pydantic_snapshot( "title": "Dup Size",
v2=snapshot( },
{ "dup_description": {
"type": "string",
"title": "Dup Description",
},
"dup_sub": {
"$ref": "#/components/schemas/tests__test_pydantic_v1_v2_multifile__modelsv2b__SubItem"
},
"dup_multi": {
"items": {
"$ref": "#/components/schemas/tests__test_pydantic_v1_v2_multifile__modelsv2b__SubItem"
},
"type": "array",
"title": "Dup Multi",
"default": [],
},
},
"type": "object",
"required": ["dup_title", "dup_size", "dup_sub"],
"title": "Item",
},
"tests__test_pydantic_v1_v2_multifile__modelsv2b__ItemInList": {
"properties": {
"dup_name2": {
"type": "string",
"title": "Dup Name2",
}
},
"type": "object",
"required": ["dup_name2"],
"title": "ItemInList",
},
"tests__test_pydantic_v1_v2_multifile__modelsv2b__SubItem": {
"properties": {
"dup_sub_name": {
"type": "string",
"title": "Dup Sub Name",
}
},
"type": "object",
"required": ["dup_sub_name"],
"title": "SubItem",
},
}
),
v2=snapshot(
{
"Body_handle_v2_items_in_list_to_v1_item_in_list_v2_to_v1_list_of_items_to_list_of_items_post": {
"properties": {
"data1": {
"items": {
"$ref": "#/components/schemas/tests__test_pydantic_v1_v2_multifile__modelsv2__ItemInList"
},
"type": "array",
"title": "Data1",
},
"data2": {
"items": {
"$ref": "#/components/schemas/tests__test_pydantic_v1_v2_multifile__modelsv2b__ItemInList"
},
"type": "array",
"title": "Data2",
},
},
"type": "object",
"required": ["data1", "data2"],
"title": "Body_handle_v2_items_in_list_to_v1_item_in_list_v2_to_v1_list_of_items_to_list_of_items_post",
},
"Body_handle_v2_same_name_to_v1_v2_to_v1_same_name_post": {
"properties": {
"item1": {
"$ref": "#/components/schemas/tests__test_pydantic_v1_v2_multifile__modelsv2__Item-Input"
},
"item2": {
"$ref": "#/components/schemas/tests__test_pydantic_v1_v2_multifile__modelsv2b__Item"
},
},
"type": "object",
"required": ["item1", "item2"],
"title": "Body_handle_v2_same_name_to_v1_v2_to_v1_same_name_post",
},
"HTTPValidationError": {
"properties": {
"detail": {
"items": {
"$ref": "#/components/schemas/ValidationError"
},
"type": "array",
"title": "Detail",
}
},
"type": "object",
"title": "HTTPValidationError",
},
"SubItem-Output": {
"properties": {
"new_sub_name": {
"type": "string",
"title": "New Sub Name",
}
},
"type": "object",
"required": ["new_sub_name"],
"title": "SubItem",
},
"ValidationError": {
"properties": {
"loc": {
"items": {
"anyOf": [ "anyOf": [
{"type": "string"}, {"type": "string"},
{"type": "null"}, {"type": "integer"},
], ]
"title": "Dup Description", },
} "type": "array",
), "title": "Location",
v1=snapshot( },
{"type": "string", "title": "Dup Description"} "msg": {"type": "string", "title": "Message"},
), "type": {"type": "string", "title": "Error Type"},
), },
"dup_sub": { "type": "object",
"$ref": "#/components/schemas/tests__test_pydantic_v1_v2_multifile__modelsv2b__SubItem" "required": ["loc", "msg", "type"],
"title": "ValidationError",
},
"tests__test_pydantic_v1_v2_multifile__modelsv1__Item": {
"properties": {
"title": {"type": "string", "title": "Title"},
"size": {"type": "integer", "title": "Size"},
"description": {
"type": "string",
"title": "Description",
},
"sub": {
"$ref": "#/components/schemas/tests__test_pydantic_v1_v2_multifile__modelsv1__SubItem"
},
"multi": {
"items": {
"$ref": "#/components/schemas/tests__test_pydantic_v1_v2_multifile__modelsv1__SubItem"
},
"type": "array",
"title": "Multi",
"default": [],
},
},
"type": "object",
"required": ["title", "size", "sub"],
"title": "Item",
},
"tests__test_pydantic_v1_v2_multifile__modelsv1__ItemInList": {
"properties": {
"name1": {"type": "string", "title": "Name1"}
},
"type": "object",
"required": ["name1"],
"title": "ItemInList",
},
"tests__test_pydantic_v1_v2_multifile__modelsv1__SubItem": {
"properties": {
"name": {"type": "string", "title": "Name"}
},
"type": "object",
"required": ["name"],
"title": "SubItem",
},
"tests__test_pydantic_v1_v2_multifile__modelsv2__Item": {
"properties": {
"new_title": {
"type": "string",
"title": "New Title",
},
"new_size": {
"type": "integer",
"title": "New Size",
},
"new_description": {
"anyOf": [{"type": "string"}, {"type": "null"}],
"title": "New Description",
},
"new_sub": {
"$ref": "#/components/schemas/SubItem-Output"
},
"new_multi": {
"items": {
"$ref": "#/components/schemas/SubItem-Output"
},
"type": "array",
"title": "New Multi",
"default": [],
},
},
"type": "object",
"required": ["new_title", "new_size", "new_sub"],
"title": "Item",
},
"tests__test_pydantic_v1_v2_multifile__modelsv2__Item-Input": {
"properties": {
"new_title": {
"type": "string",
"title": "New Title",
},
"new_size": {
"type": "integer",
"title": "New Size",
},
"new_description": {
"anyOf": [{"type": "string"}, {"type": "null"}],
"title": "New Description",
},
"new_sub": {
"$ref": "#/components/schemas/tests__test_pydantic_v1_v2_multifile__modelsv2__SubItem"
},
"new_multi": {
"items": {
"$ref": "#/components/schemas/tests__test_pydantic_v1_v2_multifile__modelsv2__SubItem"
},
"type": "array",
"title": "New Multi",
"default": [],
},
},
"type": "object",
"required": ["new_title", "new_size", "new_sub"],
"title": "Item",
},
"tests__test_pydantic_v1_v2_multifile__modelsv2__ItemInList": {
"properties": {
"name2": {"type": "string", "title": "Name2"}
}, },
"dup_multi": { "type": "object",
"items": { "required": ["name2"],
"title": "ItemInList",
},
"tests__test_pydantic_v1_v2_multifile__modelsv2__SubItem": {
"properties": {
"new_sub_name": {
"type": "string",
"title": "New Sub Name",
}
},
"type": "object",
"required": ["new_sub_name"],
"title": "SubItem",
},
"tests__test_pydantic_v1_v2_multifile__modelsv2b__Item": {
"properties": {
"dup_title": {
"type": "string",
"title": "Dup Title",
},
"dup_size": {
"type": "integer",
"title": "Dup Size",
},
"dup_description": {
"anyOf": [{"type": "string"}, {"type": "null"}],
"title": "Dup Description",
},
"dup_sub": {
"$ref": "#/components/schemas/tests__test_pydantic_v1_v2_multifile__modelsv2b__SubItem" "$ref": "#/components/schemas/tests__test_pydantic_v1_v2_multifile__modelsv2b__SubItem"
}, },
"type": "array", "dup_multi": {
"title": "Dup Multi", "items": {
"default": [], "$ref": "#/components/schemas/tests__test_pydantic_v1_v2_multifile__modelsv2b__SubItem"
},
"type": "array",
"title": "Dup Multi",
"default": [],
},
}, },
"type": "object",
"required": ["dup_title", "dup_size", "dup_sub"],
"title": "Item",
}, },
"type": "object", "tests__test_pydantic_v1_v2_multifile__modelsv2b__ItemInList": {
"required": ["dup_title", "dup_size", "dup_sub"], "properties": {
"title": "Item", "dup_name2": {
}, "type": "string",
"tests__test_pydantic_v1_v2_multifile__modelsv2b__ItemInList": { "title": "Dup Name2",
"properties": { }
"dup_name2": {"type": "string", "title": "Dup Name2"} },
"type": "object",
"required": ["dup_name2"],
"title": "ItemInList",
}, },
"type": "object", "tests__test_pydantic_v1_v2_multifile__modelsv2b__SubItem": {
"required": ["dup_name2"], "properties": {
"title": "ItemInList", "dup_sub_name": {
}, "type": "string",
"tests__test_pydantic_v1_v2_multifile__modelsv2b__SubItem": { "title": "Dup Sub Name",
"properties": { }
"dup_sub_name": { },
"type": "string", "type": "object",
"title": "Dup Sub Name", "required": ["dup_sub_name"],
} "title": "SubItem",
}, },
"type": "object", }
"required": ["dup_sub_name"], ),
"title": "SubItem", ),
}, },
}
}
),
} }
) )

2
tests/test_pydantic_v1_v2_noneable.py

@ -3,7 +3,7 @@ from typing import Any, List, Union
from tests.utils import pydantic_snapshot, skip_module_if_py_gte_314 from tests.utils import pydantic_snapshot, skip_module_if_py_gte_314
if sys.version_info > (3, 13): if sys.version_info >= (3, 14):
skip_module_if_py_gte_314() skip_module_if_py_gte_314()
from fastapi import FastAPI from fastapi import FastAPI

2
tests/utils.py

@ -17,7 +17,7 @@ needs_pydanticv1 = pytest.mark.skipif(PYDANTIC_V2, reason="requires Pydantic v1"
def skip_module_if_py_gte_314(): def skip_module_if_py_gte_314():
"""Skip entire module on Python 3.14+ at import time.""" """Skip entire module on Python 3.14+ at import time."""
if sys.version_info > (3, 13): if sys.version_info >= (3, 14):
pytest.skip("requires python3.13-", allow_module_level=True) pytest.skip("requires python3.13-", allow_module_level=True)

Loading…
Cancel
Save