| 1 |
import importlib |
| 2 |
|
| 3 |
import pytest |
| 4 |
from fastapi.testclient import TestClient |
| 5 |
from inline_snapshot import snapshot |
| 6 |
|
| 7 |
|
| 8 |
@pytest.fixture( |
| 9 |
name="client", |
| 10 |
params=[ |
| 11 |
pytest.param("tutorial004_py310"), |
| 12 |
], |
| 13 |
) |
| 14 |
def get_client(request: pytest.FixtureRequest): |
| 15 |
mod = importlib.import_module(f"docs_src.extra_models.{request.param}") |
| 16 |
|
| 17 |
client = TestClient(mod.app) |
| 18 |
return client |
| 19 |
|
| 20 |
|
| 21 |
def test_get_items(client: TestClient): |
| 22 |
response = client.get("/items/") |
| 23 |
assert response.status_code == 200, response.text |
| 24 |
assert response.json() == [ |
| 25 |
{"name": "Foo", "description": "There comes my hero"}, |
| 26 |
{"name": "Red", "description": "It's my aeroplane"}, |
| 27 |
] |
| 28 |
|
| 29 |
|
| 30 |
def test_openapi_schema(client: TestClient): |
| 31 |
response = client.get("/openapi.json") |
| 32 |
assert response.status_code == 200, response.text |
| 33 |
assert response.json() == snapshot( |
| 34 |
{ |
| 35 |
"openapi": "3.1.0", |
| 36 |
"info": {"title": "FastAPI", "version": "0.1.0"}, |
| 37 |
"paths": { |
| 38 |
"/items/": { |
| 39 |
"get": { |
| 40 |
"responses": { |
| 41 |
"200": { |
| 42 |
"description": "Successful Response", |
| 43 |
"content": { |
| 44 |
"application/json": { |
| 45 |
"schema": { |
| 46 |
"title": "Response Read Items Items Get", |
| 47 |
"type": "array", |
| 48 |
"items": { |
| 49 |
"$ref": "#/components/schemas/Item" |
| 50 |
}, |
| 51 |
} |
| 52 |
} |
| 53 |
}, |
| 54 |
} |
| 55 |
}, |
| 56 |
"summary": "Read Items", |
| 57 |
"operationId": "read_items_items__get", |
| 58 |
} |
| 59 |
} |
| 60 |
}, |
| 61 |
"components": { |
| 62 |
"schemas": { |
| 63 |
"Item": { |
| 64 |
"title": "Item", |
| 65 |
"required": ["name", "description"], |
| 66 |
"type": "object", |
| 67 |
"properties": { |
| 68 |
"name": {"title": "Name", "type": "string"}, |
| 69 |
"description": {"title": "Description", "type": "string"}, |
| 70 |
}, |
| 71 |
} |
| 72 |
} |
| 73 |
}, |
| 74 |
} |
| 75 |
) |