|
|
@ -1,12 +1,8 @@ |
|
|
from typing import List |
|
|
|
|
|
|
|
|
|
|
|
import pytest |
|
|
import pytest |
|
|
from fastapi import FastAPI |
|
|
from fastapi import FastAPI |
|
|
from fastapi.testclient import TestClient |
|
|
from fastapi.testclient import TestClient |
|
|
from pydantic import BaseModel |
|
|
from pydantic import BaseModel |
|
|
|
|
|
|
|
|
from .utils import needs_pydanticv2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(name="client", params=[True, False]) |
|
|
@pytest.fixture(name="client", params=[True, False]) |
|
|
def get_client(request: pytest.FixtureRequest): |
|
|
def get_client(request: pytest.FixtureRequest): |
|
|
@ -25,7 +21,7 @@ def get_client(request: pytest.FixtureRequest): |
|
|
app = FastAPI(separate_input_output_schemas=request.param) |
|
|
app = FastAPI(separate_input_output_schemas=request.param) |
|
|
|
|
|
|
|
|
@app.get("/list") |
|
|
@app.get("/list") |
|
|
def get_items() -> List[MyModel]: |
|
|
def get_items() -> list[MyModel]: |
|
|
return [MyModel(id=1, name="Alice", age=30), MyModel(id=2, name="Bob", age=17)] |
|
|
return [MyModel(id=1, name="Alice", age=30), MyModel(id=2, name="Bob", age=17)] |
|
|
|
|
|
|
|
|
@app.post("/item") |
|
|
@app.post("/item") |
|
|
@ -35,7 +31,6 @@ def get_client(request: pytest.FixtureRequest): |
|
|
yield TestClient(app) |
|
|
yield TestClient(app) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@needs_pydanticv2 |
|
|
|
|
|
def test_create_item(client: TestClient): |
|
|
def test_create_item(client: TestClient): |
|
|
response = client.post( |
|
|
response = client.post( |
|
|
"/item", |
|
|
"/item", |
|
|
@ -45,7 +40,6 @@ def test_create_item(client: TestClient): |
|
|
assert response.json() == {"id": 1, "name": "Alice", "age": 30, "is_adult": True} |
|
|
assert response.json() == {"id": 1, "name": "Alice", "age": 30, "is_adult": True} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@needs_pydanticv2 |
|
|
|
|
|
def test_get_items(client: TestClient): |
|
|
def test_get_items(client: TestClient): |
|
|
response = client.get("/list") |
|
|
response = client.get("/list") |
|
|
assert response.status_code == 200, response.text |
|
|
assert response.status_code == 200, response.text |
|
|
@ -55,7 +49,6 @@ def test_get_items(client: TestClient): |
|
|
] |
|
|
] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@needs_pydanticv2 |
|
|
|
|
|
def test_openapi(client: TestClient): |
|
|
def test_openapi(client: TestClient): |
|
|
response = client.get("/openapi.json") |
|
|
response = client.get("/openapi.json") |
|
|
openapi_schema = response.json() |
|
|
openapi_schema = response.json() |
|
|
|