| 1 |
import pytest |
| 2 |
from fastapi import FastAPI |
| 3 |
from fastapi.testclient import TestClient |
| 4 |
from inline_snapshot import snapshot |
| 5 |
|
| 6 |
|
| 7 |
@pytest.fixture(name="client") |
| 8 |
def get_client(request): |
| 9 |
separate_input_output_schemas = request.param |
| 10 |
app = FastAPI(separate_input_output_schemas=separate_input_output_schemas) |
| 11 |
|
| 12 |
from pydantic import BaseModel, computed_field |
| 13 |
|
| 14 |
class Rectangle(BaseModel): |
| 15 |
width: int |
| 16 |
length: int |
| 17 |
|
| 18 |
@computed_field |
| 19 |
@property |
| 20 |
def area(self) -> int: |
| 21 |
return self.width * self.length |
| 22 |
|
| 23 |
@app.get("/") |
| 24 |
def read_root() -> Rectangle: |
| 25 |
return Rectangle(width=3, length=4) |
| 26 |
|
| 27 |
@app.get("/responses", responses={200: {"model": Rectangle}}) |
| 28 |
def read_responses() -> Rectangle: |
| 29 |
return Rectangle(width=3, length=4) |
| 30 |
|
| 31 |
client = TestClient(app) |
| 32 |
return client |
| 33 |
|
| 34 |
|
| 35 |
@pytest.mark.parametrize("client", [True, False], indirect=True) |
| 36 |
@pytest.mark.parametrize("path", ["/", "/responses"]) |
| 37 |
def test_get(client: TestClient, path: str): |
| 38 |
response = client.get(path) |
| 39 |
assert response.status_code == 200, response.text |
| 40 |
assert response.json() == {"width": 3, "length": 4, "area": 12} |
| 41 |
|
| 42 |
|
| 43 |
@pytest.mark.parametrize("client", [True, False], indirect=True) |
| 44 |
def test_openapi_schema(client: TestClient): |
| 45 |
response = client.get("/openapi.json") |
| 46 |
assert response.status_code == 200, response.text |
| 47 |
assert response.json() == snapshot( |
| 48 |
{ |
| 49 |
"openapi": "3.1.0", |
| 50 |
"info": {"title": "FastAPI", "version": "0.1.0"}, |
| 51 |
"paths": { |
| 52 |
"/": { |
| 53 |
"get": { |
| 54 |
"summary": "Read Root", |
| 55 |
"operationId": "read_root__get", |
| 56 |
"responses": { |
| 57 |
"200": { |
| 58 |
"description": "Successful Response", |
| 59 |
"content": { |
| 60 |
"application/json": { |
| 61 |
"schema": { |
| 62 |
"$ref": "#/components/schemas/Rectangle" |
| 63 |
} |
| 64 |
} |
| 65 |
}, |
| 66 |
} |
| 67 |
}, |
| 68 |
} |
| 69 |
}, |
| 70 |
"/responses": { |
| 71 |
"get": { |
| 72 |
"summary": "Read Responses", |
| 73 |
"operationId": "read_responses_responses_get", |
| 74 |
"responses": { |
| 75 |
"200": { |
| 76 |
"description": "Successful Response", |
| 77 |
"content": { |
| 78 |
"application/json": { |
| 79 |
"schema": { |
| 80 |
"$ref": "#/components/schemas/Rectangle" |
| 81 |
} |
| 82 |
} |
| 83 |
}, |
| 84 |
} |
| 85 |
}, |
| 86 |
} |
| 87 |
}, |
| 88 |
}, |
| 89 |
"components": { |
| 90 |
"schemas": { |
| 91 |
"Rectangle": { |
| 92 |
"properties": { |
| 93 |
"width": {"type": "integer", "title": "Width"}, |
| 94 |
"length": {"type": "integer", "title": "Length"}, |
| 95 |
"area": { |
| 96 |
"type": "integer", |
| 97 |
"title": "Area", |
| 98 |
"readOnly": True, |
| 99 |
}, |
| 100 |
}, |
| 101 |
"type": "object", |
| 102 |
"required": ["width", "length", "area"], |
| 103 |
"title": "Rectangle", |
| 104 |
} |
| 105 |
} |
| 106 |
}, |
| 107 |
} |
| 108 |
) |