mirror of
https://github.com/fastapi/fastapi.git
synced 2026-05-24 08:16:11 -04:00
🔥 Remove Pydantic v1 specific test variants (#14611)
This commit is contained in:
committed by
GitHub
parent
8322a4445a
commit
44c849c4fc
@@ -2,7 +2,6 @@ import importlib
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
from dirty_equals import IsDict
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from ...utils import needs_py310
|
||||
@@ -74,124 +73,67 @@ def test_post_with_str_float_description_tax(client: TestClient):
|
||||
def test_post_with_only_name(client: TestClient):
|
||||
response = client.post("/items/", json={"name": "Foo"})
|
||||
assert response.status_code == 422
|
||||
assert response.json() == IsDict(
|
||||
{
|
||||
"detail": [
|
||||
{
|
||||
"type": "missing",
|
||||
"loc": ["body", "price"],
|
||||
"msg": "Field required",
|
||||
"input": {"name": "Foo"},
|
||||
}
|
||||
]
|
||||
}
|
||||
) | IsDict(
|
||||
# TODO: remove when deprecating Pydantic v1
|
||||
{
|
||||
"detail": [
|
||||
{
|
||||
"loc": ["body", "price"],
|
||||
"msg": "field required",
|
||||
"type": "value_error.missing",
|
||||
}
|
||||
]
|
||||
}
|
||||
)
|
||||
assert response.json() == {
|
||||
"detail": [
|
||||
{
|
||||
"type": "missing",
|
||||
"loc": ["body", "price"],
|
||||
"msg": "Field required",
|
||||
"input": {"name": "Foo"},
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
def test_post_with_only_name_price(client: TestClient):
|
||||
response = client.post("/items/", json={"name": "Foo", "price": "twenty"})
|
||||
assert response.status_code == 422
|
||||
assert response.json() == IsDict(
|
||||
{
|
||||
"detail": [
|
||||
{
|
||||
"type": "float_parsing",
|
||||
"loc": ["body", "price"],
|
||||
"msg": "Input should be a valid number, unable to parse string as a number",
|
||||
"input": "twenty",
|
||||
}
|
||||
]
|
||||
}
|
||||
) | IsDict(
|
||||
# TODO: remove when deprecating Pydantic v1
|
||||
{
|
||||
"detail": [
|
||||
{
|
||||
"loc": ["body", "price"],
|
||||
"msg": "value is not a valid float",
|
||||
"type": "type_error.float",
|
||||
}
|
||||
]
|
||||
}
|
||||
)
|
||||
assert response.json() == {
|
||||
"detail": [
|
||||
{
|
||||
"type": "float_parsing",
|
||||
"loc": ["body", "price"],
|
||||
"msg": "Input should be a valid number, unable to parse string as a number",
|
||||
"input": "twenty",
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
def test_post_with_no_data(client: TestClient):
|
||||
response = client.post("/items/", json={})
|
||||
assert response.status_code == 422
|
||||
assert response.json() == IsDict(
|
||||
{
|
||||
"detail": [
|
||||
{
|
||||
"type": "missing",
|
||||
"loc": ["body", "name"],
|
||||
"msg": "Field required",
|
||||
"input": {},
|
||||
},
|
||||
{
|
||||
"type": "missing",
|
||||
"loc": ["body", "price"],
|
||||
"msg": "Field required",
|
||||
"input": {},
|
||||
},
|
||||
]
|
||||
}
|
||||
) | IsDict(
|
||||
# TODO: remove when deprecating Pydantic v1
|
||||
{
|
||||
"detail": [
|
||||
{
|
||||
"loc": ["body", "name"],
|
||||
"msg": "field required",
|
||||
"type": "value_error.missing",
|
||||
},
|
||||
{
|
||||
"loc": ["body", "price"],
|
||||
"msg": "field required",
|
||||
"type": "value_error.missing",
|
||||
},
|
||||
]
|
||||
}
|
||||
)
|
||||
assert response.json() == {
|
||||
"detail": [
|
||||
{
|
||||
"type": "missing",
|
||||
"loc": ["body", "name"],
|
||||
"msg": "Field required",
|
||||
"input": {},
|
||||
},
|
||||
{
|
||||
"type": "missing",
|
||||
"loc": ["body", "price"],
|
||||
"msg": "Field required",
|
||||
"input": {},
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
def test_post_with_none(client: TestClient):
|
||||
response = client.post("/items/", json=None)
|
||||
assert response.status_code == 422
|
||||
assert response.json() == IsDict(
|
||||
{
|
||||
"detail": [
|
||||
{
|
||||
"type": "missing",
|
||||
"loc": ["body"],
|
||||
"msg": "Field required",
|
||||
"input": None,
|
||||
}
|
||||
]
|
||||
}
|
||||
) | IsDict(
|
||||
# TODO: remove when deprecating Pydantic v1
|
||||
{
|
||||
"detail": [
|
||||
{
|
||||
"loc": ["body"],
|
||||
"msg": "field required",
|
||||
"type": "value_error.missing",
|
||||
}
|
||||
]
|
||||
}
|
||||
)
|
||||
assert response.json() == {
|
||||
"detail": [
|
||||
{
|
||||
"type": "missing",
|
||||
"loc": ["body"],
|
||||
"msg": "Field required",
|
||||
"input": None,
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
def test_post_broken_body(client: TestClient):
|
||||
@@ -201,67 +143,32 @@ def test_post_broken_body(client: TestClient):
|
||||
content="{some broken json}",
|
||||
)
|
||||
assert response.status_code == 422, response.text
|
||||
assert response.json() == IsDict(
|
||||
{
|
||||
"detail": [
|
||||
{
|
||||
"type": "json_invalid",
|
||||
"loc": ["body", 1],
|
||||
"msg": "JSON decode error",
|
||||
"input": {},
|
||||
"ctx": {
|
||||
"error": "Expecting property name enclosed in double quotes"
|
||||
},
|
||||
}
|
||||
]
|
||||
}
|
||||
) | IsDict(
|
||||
# TODO: remove when deprecating Pydantic v1
|
||||
{
|
||||
"detail": [
|
||||
{
|
||||
"loc": ["body", 1],
|
||||
"msg": "Expecting property name enclosed in double quotes: line 1 column 2 (char 1)",
|
||||
"type": "value_error.jsondecode",
|
||||
"ctx": {
|
||||
"msg": "Expecting property name enclosed in double quotes",
|
||||
"doc": "{some broken json}",
|
||||
"pos": 1,
|
||||
"lineno": 1,
|
||||
"colno": 2,
|
||||
},
|
||||
}
|
||||
]
|
||||
}
|
||||
)
|
||||
assert response.json() == {
|
||||
"detail": [
|
||||
{
|
||||
"type": "json_invalid",
|
||||
"loc": ["body", 1],
|
||||
"msg": "JSON decode error",
|
||||
"input": {},
|
||||
"ctx": {"error": "Expecting property name enclosed in double quotes"},
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
def test_post_form_for_json(client: TestClient):
|
||||
response = client.post("/items/", data={"name": "Foo", "price": 50.5})
|
||||
assert response.status_code == 422, response.text
|
||||
assert response.json() == IsDict(
|
||||
{
|
||||
"detail": [
|
||||
{
|
||||
"type": "model_attributes_type",
|
||||
"loc": ["body"],
|
||||
"msg": "Input should be a valid dictionary or object to extract fields from",
|
||||
"input": "name=Foo&price=50.5",
|
||||
}
|
||||
]
|
||||
}
|
||||
) | IsDict(
|
||||
# TODO: remove when deprecating Pydantic v1
|
||||
{
|
||||
"detail": [
|
||||
{
|
||||
"loc": ["body"],
|
||||
"msg": "value is not a valid dict",
|
||||
"type": "type_error.dict",
|
||||
}
|
||||
]
|
||||
}
|
||||
)
|
||||
assert response.json() == {
|
||||
"detail": [
|
||||
{
|
||||
"type": "model_attributes_type",
|
||||
"loc": ["body"],
|
||||
"msg": "Input should be a valid dictionary or object to extract fields from",
|
||||
"input": "name=Foo&price=50.5",
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
def test_explicit_content_type(client: TestClient):
|
||||
@@ -302,84 +209,46 @@ def test_wrong_headers(client: TestClient):
|
||||
"/items/", content=data, headers={"Content-Type": "text/plain"}
|
||||
)
|
||||
assert response.status_code == 422, response.text
|
||||
assert response.json() == IsDict(
|
||||
{
|
||||
"detail": [
|
||||
{
|
||||
"type": "model_attributes_type",
|
||||
"loc": ["body"],
|
||||
"msg": "Input should be a valid dictionary or object to extract fields from",
|
||||
"input": '{"name": "Foo", "price": 50.5}',
|
||||
}
|
||||
]
|
||||
}
|
||||
) | IsDict(
|
||||
# TODO: remove when deprecating Pydantic v1
|
||||
{
|
||||
"detail": [
|
||||
{
|
||||
"loc": ["body"],
|
||||
"msg": "value is not a valid dict",
|
||||
"type": "type_error.dict",
|
||||
}
|
||||
]
|
||||
}
|
||||
)
|
||||
assert response.json() == {
|
||||
"detail": [
|
||||
{
|
||||
"type": "model_attributes_type",
|
||||
"loc": ["body"],
|
||||
"msg": "Input should be a valid dictionary or object to extract fields from",
|
||||
"input": '{"name": "Foo", "price": 50.5}',
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
response = client.post(
|
||||
"/items/", content=data, headers={"Content-Type": "application/geo+json-seq"}
|
||||
)
|
||||
assert response.status_code == 422, response.text
|
||||
assert response.json() == IsDict(
|
||||
{
|
||||
"detail": [
|
||||
{
|
||||
"type": "model_attributes_type",
|
||||
"loc": ["body"],
|
||||
"msg": "Input should be a valid dictionary or object to extract fields from",
|
||||
"input": '{"name": "Foo", "price": 50.5}',
|
||||
}
|
||||
]
|
||||
}
|
||||
) | IsDict(
|
||||
# TODO: remove when deprecating Pydantic v1
|
||||
{
|
||||
"detail": [
|
||||
{
|
||||
"loc": ["body"],
|
||||
"msg": "value is not a valid dict",
|
||||
"type": "type_error.dict",
|
||||
}
|
||||
]
|
||||
}
|
||||
)
|
||||
assert response.json() == {
|
||||
"detail": [
|
||||
{
|
||||
"type": "model_attributes_type",
|
||||
"loc": ["body"],
|
||||
"msg": "Input should be a valid dictionary or object to extract fields from",
|
||||
"input": '{"name": "Foo", "price": 50.5}',
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
response = client.post(
|
||||
"/items/", content=data, headers={"Content-Type": "application/not-really-json"}
|
||||
)
|
||||
assert response.status_code == 422, response.text
|
||||
assert response.json() == IsDict(
|
||||
{
|
||||
"detail": [
|
||||
{
|
||||
"type": "model_attributes_type",
|
||||
"loc": ["body"],
|
||||
"msg": "Input should be a valid dictionary or object to extract fields from",
|
||||
"input": '{"name": "Foo", "price": 50.5}',
|
||||
}
|
||||
]
|
||||
}
|
||||
) | IsDict(
|
||||
# TODO: remove when deprecating Pydantic v1
|
||||
{
|
||||
"detail": [
|
||||
{
|
||||
"loc": ["body"],
|
||||
"msg": "value is not a valid dict",
|
||||
"type": "type_error.dict",
|
||||
}
|
||||
]
|
||||
}
|
||||
)
|
||||
assert response.json() == {
|
||||
"detail": [
|
||||
{
|
||||
"type": "model_attributes_type",
|
||||
"loc": ["body"],
|
||||
"msg": "Input should be a valid dictionary or object to extract fields from",
|
||||
"input": '{"name": "Foo", "price": 50.5}',
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
def test_other_exceptions(client: TestClient):
|
||||
@@ -435,26 +304,14 @@ def test_openapi_schema(client: TestClient):
|
||||
"properties": {
|
||||
"name": {"title": "Name", "type": "string"},
|
||||
"price": {"title": "Price", "type": "number"},
|
||||
"description": IsDict(
|
||||
{
|
||||
"title": "Description",
|
||||
"anyOf": [{"type": "string"}, {"type": "null"}],
|
||||
}
|
||||
)
|
||||
| IsDict(
|
||||
# TODO: remove when deprecating Pydantic v1
|
||||
{"title": "Description", "type": "string"}
|
||||
),
|
||||
"tax": IsDict(
|
||||
{
|
||||
"title": "Tax",
|
||||
"anyOf": [{"type": "number"}, {"type": "null"}],
|
||||
}
|
||||
)
|
||||
| IsDict(
|
||||
# TODO: remove when deprecating Pydantic v1
|
||||
{"title": "Tax", "type": "number"}
|
||||
),
|
||||
"description": {
|
||||
"title": "Description",
|
||||
"anyOf": [{"type": "string"}, {"type": "null"}],
|
||||
},
|
||||
"tax": {
|
||||
"title": "Tax",
|
||||
"anyOf": [{"type": "number"}, {"type": "null"}],
|
||||
},
|
||||
},
|
||||
},
|
||||
"ValidationError": {
|
||||
|
||||
Reference in New Issue
Block a user