Browse Source

Corrections added for compatibility with Pydantic v1: tests/test_tutorial/test_path/params/test_tutorial002.py

pull/13476/head
alv2017 4 weeks ago
parent
commit
299e79deea
  1. 198
      tests/test_tutorial/test_path_params/test_tutorial002.py

198
tests/test_tutorial/test_path_params/test_tutorial002.py

@ -2,6 +2,7 @@ import pytest
from fastapi.testclient import TestClient from fastapi.testclient import TestClient
from docs_src.path_params.tutorial002 import app from docs_src.path_params.tutorial002 import app
from tests.utils import PYDANTIC_V2
@pytest.fixture(name="client") @pytest.fixture(name="client")
@ -17,12 +18,23 @@ def test_with_integer_parameters(client, parameter_value):
assert response.json() == {"item_id": parameter} assert response.json() == {"item_id": parameter}
@pytest.mark.xfail(PYDANTIC_V2, reason="Validation bug in Pydantic v2")
@pytest.mark.parametrize("parameter_value", [-10.00, 0.00, 10.00]) @pytest.mark.parametrize("parameter_value", [-10.00, 0.00, 10.00])
def test_with_integers_converted_to_floats(client, parameter_value): def test_with_integers_converted_to_floats(client, parameter_value):
parameter: float = parameter_value parameter: float = parameter_value
response = client.get(f"/items/{parameter}") response = client.get(f"/items/{parameter}")
assert response.status_code == 200 assert response.status_code == 422
assert response.json() == {"item_id": int(parameter)} if PYDANTIC_V2:
assert response.json() == {
"detail": [
{
"type": "int_parsing",
"loc": ["path", "item_id"],
"msg": "Input should be a valid integer, unable to parse string as an integer",
"input": f"{parameter}",
}
]
}
@pytest.mark.parametrize("parameter_value", [-3.14, 3.14]) @pytest.mark.parametrize("parameter_value", [-3.14, 3.14])
@ -30,16 +42,17 @@ def test_with_float_parameters(client, parameter_value):
parameter: float = parameter_value parameter: float = parameter_value
response = client.get(f"/items/{parameter}") response = client.get(f"/items/{parameter}")
assert response.status_code == 422 assert response.status_code == 422
assert response.json() == { if PYDANTIC_V2:
"detail": [ assert response.json() == {
{ "detail": [
"type": "int_parsing", {
"loc": ["path", "item_id"], "type": "int_parsing",
"msg": "Input should be a valid integer, unable to parse string as an integer", "loc": ["path", "item_id"],
"input": f"{parameter}", "msg": "Input should be a valid integer, unable to parse string as an integer",
} "input": f"{parameter}",
] }
} ]
}
@pytest.mark.parametrize("parameter_value", ["foo", "foo bar", "foo bar baz"]) @pytest.mark.parametrize("parameter_value", ["foo", "foo bar", "foo bar baz"])
@ -47,16 +60,17 @@ def test_with_string_parameters(client, parameter_value):
parameter: str = parameter_value parameter: str = parameter_value
response = client.get(f"/items/{parameter}") response = client.get(f"/items/{parameter}")
assert response.status_code == 422 assert response.status_code == 422
assert response.json() == { if PYDANTIC_V2:
"detail": [ assert response.json() == {
{ "detail": [
"type": "int_parsing", {
"loc": ["path", "item_id"], "type": "int_parsing",
"msg": "Input should be a valid integer, unable to parse string as an integer", "loc": ["path", "item_id"],
"input": f"{parameter}", "msg": "Input should be a valid integer, unable to parse string as an integer",
} "input": f"{parameter}",
] }
} ]
}
@pytest.mark.parametrize( @pytest.mark.parametrize(
@ -66,85 +80,87 @@ def test_with_list_parameters(client, parameter_value):
parameter: list = parameter_value parameter: list = parameter_value
response = client.get(f"/items/{parameter}") response = client.get(f"/items/{parameter}")
assert response.status_code == 422 assert response.status_code == 422
assert response.json() == { if PYDANTIC_V2:
"detail": [ assert response.json() == {
{ "detail": [
"type": "int_parsing", {
"loc": ["path", "item_id"], "type": "int_parsing",
"msg": "Input should be a valid integer, unable to parse string as an integer", "loc": ["path", "item_id"],
"input": f"{parameter}", "msg": "Input should be a valid integer, unable to parse string as an integer",
} "input": f"{parameter}",
] }
} ]
}
def test_openapi_schema(client): def test_openapi_schema(client):
response = client.get("/openapi.json") response = client.get("/openapi.json")
assert response.status_code == 200 assert response.status_code == 200
assert response.json() == { if PYDANTIC_V2:
"openapi": "3.1.0", assert response.json() == {
"info": {"title": "FastAPI", "version": "0.1.0"}, "openapi": "3.1.0",
"paths": { "info": {"title": "FastAPI", "version": "0.1.0"},
"/items/{item_id}": { "paths": {
"get": { "/items/{item_id}": {
"summary": "Read Item", "get": {
"operationId": "read_item_items__item_id__get", "summary": "Read Item",
"parameters": [ "operationId": "read_item_items__item_id__get",
{ "parameters": [
"name": "item_id", {
"in": "path", "name": "item_id",
"required": True, "in": "path",
"schema": {"type": "integer", "title": "Item Id"}, "required": True,
} "schema": {"type": "integer", "title": "Item Id"},
], }
"responses": { ],
"200": { "responses": {
"description": "Successful Response", "200": {
"content": {"application/json": {"schema": {}}}, "description": "Successful Response",
}, "content": {"application/json": {"schema": {}}},
"422": { },
"description": "Validation Error", "422": {
"content": { "description": "Validation Error",
"application/json": { "content": {
"schema": { "application/json": {
"$ref": "#/components/schemas/HTTPValidationError" "schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
} }
} },
}, },
}, },
}, }
} }
} },
}, "components": {
"components": { "schemas": {
"schemas": { "HTTPValidationError": {
"HTTPValidationError": { "properties": {
"properties": { "detail": {
"detail": { "items": {"$ref": "#/components/schemas/ValidationError"},
"items": {"$ref": "#/components/schemas/ValidationError"}, "type": "array",
"type": "array", "title": "Detail",
"title": "Detail", }
} },
"type": "object",
"title": "HTTPValidationError",
}, },
"type": "object", "ValidationError": {
"title": "HTTPValidationError", "properties": {
}, "loc": {
"ValidationError": { "items": {
"properties": { "anyOf": [{"type": "string"}, {"type": "integer"}]
"loc": { },
"items": { "type": "array",
"anyOf": [{"type": "string"}, {"type": "integer"}] "title": "Location",
}, },
"type": "array", "msg": {"type": "string", "title": "Message"},
"title": "Location", "type": {"type": "string", "title": "Error Type"},
}, },
"msg": {"type": "string", "title": "Message"}, "type": "object",
"type": {"type": "string", "title": "Error Type"}, "required": ["loc", "msg", "type"],
"title": "ValidationError",
}, },
"type": "object", }
"required": ["loc", "msg", "type"], },
"title": "ValidationError", }
},
}
},
}

Loading…
Cancel
Save