Issue Manager / issue-manager (push) Has been cancelled
Build Docs / changes (push) Has been cancelled
Build Docs / langs (push) Has been cancelled
Build Docs / build-docs (push) Has been cancelled
Build Docs / docs-all-green (push) Has been cancelled
Conflict detector / main (push) Has been cancelled
Test Redistribute / test-redistribute (fastapi) (push) Has been cancelled
Test Redistribute / test-redistribute (fastapi-slim) (push) Has been cancelled
Test Redistribute / test-redistribute-alls-green (push) Has been cancelled
Test / lint (push) Has been cancelled
Test / test (pydantic-v1, 3.10) (push) Has been cancelled
Test / test (pydantic-v1, 3.11) (push) Has been cancelled
Test / test (pydantic-v1, 3.13) (push) Has been cancelled
Test / test (pydantic-v1, 3.8) (push) Has been cancelled
Test / test (pydantic-v1, 3.9) (push) Has been cancelled
Test / test (pydantic-v2, 3.10) (push) Has been cancelled
Test / test (pydantic-v2, 3.11) (push) Has been cancelled
Test / test (pydantic-v2, 3.12) (push) Has been cancelled
Test / test (pydantic-v2, 3.13) (push) Has been cancelled
Test / test (pydantic-v2, 3.14) (push) Has been cancelled
Test / test (pydantic-v2, 3.8) (push) Has been cancelled
Test / test (pydantic-v2, 3.9) (push) Has been cancelled
Test / coverage-combine (push) Has been cancelled
Test / check (push) Has been cancelled
Label Approved / label-approved (push) Has been cancelled
FastAPI People Contributors / job (push) Has been cancelled
FastAPI People Sponsors / job (push) Has been cancelled
Update Topic Repos / topic-repos (push) Has been cancelled
FastAPI People / job (push) Has been cancelled
Test / test (pydantic-v1, 3.12) (push) Has been cancelled
102 lines
3.3 KiB
Python
102 lines
3.3 KiB
Python
import pytest
|
|
from fastapi import FastAPI
|
|
from fastapi.testclient import TestClient
|
|
|
|
from .utils import needs_pydanticv2
|
|
|
|
|
|
@pytest.fixture(name="client")
|
|
def get_client(request):
|
|
separate_input_output_schemas = request.param
|
|
app = FastAPI(separate_input_output_schemas=separate_input_output_schemas)
|
|
|
|
from pydantic import BaseModel, computed_field
|
|
|
|
class Rectangle(BaseModel):
|
|
width: int
|
|
length: int
|
|
|
|
@computed_field
|
|
@property
|
|
def area(self) -> int:
|
|
return self.width * self.length
|
|
|
|
@app.get("/")
|
|
def read_root() -> Rectangle:
|
|
return Rectangle(width=3, length=4)
|
|
|
|
@app.get("/responses", responses={200: {"model": Rectangle}})
|
|
def read_responses() -> Rectangle:
|
|
return Rectangle(width=3, length=4)
|
|
|
|
client = TestClient(app)
|
|
return client
|
|
|
|
|
|
@pytest.mark.parametrize("client", [True, False], indirect=True)
|
|
@pytest.mark.parametrize("path", ["/", "/responses"])
|
|
@needs_pydanticv2
|
|
def test_get(client: TestClient, path: str):
|
|
response = client.get(path)
|
|
assert response.status_code == 200, response.text
|
|
assert response.json() == {"width": 3, "length": 4, "area": 12}
|
|
|
|
|
|
@pytest.mark.parametrize("client", [True, False], indirect=True)
|
|
@needs_pydanticv2
|
|
def test_openapi_schema(client: TestClient):
|
|
response = client.get("/openapi.json")
|
|
assert response.status_code == 200, response.text
|
|
assert response.json() == {
|
|
"openapi": "3.1.0",
|
|
"info": {"title": "FastAPI", "version": "0.1.0"},
|
|
"paths": {
|
|
"/": {
|
|
"get": {
|
|
"summary": "Read Root",
|
|
"operationId": "read_root__get",
|
|
"responses": {
|
|
"200": {
|
|
"description": "Successful Response",
|
|
"content": {
|
|
"application/json": {
|
|
"schema": {"$ref": "#/components/schemas/Rectangle"}
|
|
}
|
|
},
|
|
}
|
|
},
|
|
}
|
|
},
|
|
"/responses": {
|
|
"get": {
|
|
"summary": "Read Responses",
|
|
"operationId": "read_responses_responses_get",
|
|
"responses": {
|
|
"200": {
|
|
"description": "Successful Response",
|
|
"content": {
|
|
"application/json": {
|
|
"schema": {"$ref": "#/components/schemas/Rectangle"}
|
|
}
|
|
},
|
|
}
|
|
},
|
|
}
|
|
},
|
|
},
|
|
"components": {
|
|
"schemas": {
|
|
"Rectangle": {
|
|
"properties": {
|
|
"width": {"type": "integer", "title": "Width"},
|
|
"length": {"type": "integer", "title": "Length"},
|
|
"area": {"type": "integer", "title": "Area", "readOnly": True},
|
|
},
|
|
"type": "object",
|
|
"required": ["width", "length", "area"],
|
|
"title": "Rectangle",
|
|
}
|
|
}
|
|
},
|
|
}
|