|
|
@ -2,6 +2,7 @@ import pytest |
|
|
|
from fastapi.testclient import TestClient |
|
|
|
|
|
|
|
from docs_src.path_params.tutorial002 import app |
|
|
|
from tests.utils import PYDANTIC_V2 |
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(name="client") |
|
|
@ -17,12 +18,23 @@ def test_with_integer_parameters(client, parameter_value): |
|
|
|
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]) |
|
|
|
def test_with_integers_converted_to_floats(client, parameter_value): |
|
|
|
parameter: float = parameter_value |
|
|
|
response = client.get(f"/items/{parameter}") |
|
|
|
assert response.status_code == 200 |
|
|
|
assert response.json() == {"item_id": int(parameter)} |
|
|
|
assert response.status_code == 422 |
|
|
|
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]) |
|
|
@ -30,6 +42,7 @@ def test_with_float_parameters(client, parameter_value): |
|
|
|
parameter: float = parameter_value |
|
|
|
response = client.get(f"/items/{parameter}") |
|
|
|
assert response.status_code == 422 |
|
|
|
if PYDANTIC_V2: |
|
|
|
assert response.json() == { |
|
|
|
"detail": [ |
|
|
|
{ |
|
|
@ -47,6 +60,7 @@ def test_with_string_parameters(client, parameter_value): |
|
|
|
parameter: str = parameter_value |
|
|
|
response = client.get(f"/items/{parameter}") |
|
|
|
assert response.status_code == 422 |
|
|
|
if PYDANTIC_V2: |
|
|
|
assert response.json() == { |
|
|
|
"detail": [ |
|
|
|
{ |
|
|
@ -66,6 +80,7 @@ def test_with_list_parameters(client, parameter_value): |
|
|
|
parameter: list = parameter_value |
|
|
|
response = client.get(f"/items/{parameter}") |
|
|
|
assert response.status_code == 422 |
|
|
|
if PYDANTIC_V2: |
|
|
|
assert response.json() == { |
|
|
|
"detail": [ |
|
|
|
{ |
|
|
@ -81,6 +96,7 @@ def test_with_list_parameters(client, parameter_value): |
|
|
|
def test_openapi_schema(client): |
|
|
|
response = client.get("/openapi.json") |
|
|
|
assert response.status_code == 200 |
|
|
|
if PYDANTIC_V2: |
|
|
|
assert response.json() == { |
|
|
|
"openapi": "3.1.0", |
|
|
|
"info": {"title": "FastAPI", "version": "0.1.0"}, |
|
|
|