| 1 |
import importlib |
| 2 |
|
| 3 |
import pytest |
| 4 |
from fastapi.testclient import TestClient |
| 5 |
from inline_snapshot import snapshot |
| 6 |
|
| 7 |
from tests.utils import needs_py310 |
| 8 |
|
| 9 |
|
| 10 |
@pytest.fixture( |
| 11 |
name="client", |
| 12 |
params=[ |
| 13 |
pytest.param("tutorial002_py310", marks=needs_py310), |
| 14 |
], |
| 15 |
) |
| 16 |
def get_client(request: pytest.FixtureRequest): |
| 17 |
mod = importlib.import_module(f"docs_src.dataclasses_.{request.param}") |
| 18 |
|
| 19 |
client = TestClient(mod.app) |
| 20 |
client.headers.clear() |
| 21 |
return client |
| 22 |
|
| 23 |
|
| 24 |
def test_get_item(client: TestClient): |
| 25 |
response = client.get("/items/next") |
| 26 |
assert response.status_code == 200 |
| 27 |
assert response.json() == { |
| 28 |
"name": "Island In The Moon", |
| 29 |
"price": 12.99, |
| 30 |
"description": "A place to be playin' and havin' fun", |
| 31 |
"tags": ["breater"], |
| 32 |
"tax": None, |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
def test_openapi_schema(client: TestClient): |
| 37 |
response = client.get("/openapi.json") |
| 38 |
assert response.status_code == 200 |
| 39 |
assert response.json() == snapshot( |
| 40 |
{ |
| 41 |
"openapi": "3.1.0", |
| 42 |
"info": {"title": "FastAPI", "version": "0.1.0"}, |
| 43 |
"paths": { |
| 44 |
"/items/next": { |
| 45 |
"get": { |
| 46 |
"summary": "Read Next Item", |
| 47 |
"operationId": "read_next_item_items_next_get", |
| 48 |
"responses": { |
| 49 |
"200": { |
| 50 |
"description": "Successful Response", |
| 51 |
"content": { |
| 52 |
"application/json": { |
| 53 |
"schema": {"$ref": "#/components/schemas/Item"} |
| 54 |
} |
| 55 |
}, |
| 56 |
} |
| 57 |
}, |
| 58 |
} |
| 59 |
} |
| 60 |
}, |
| 61 |
"components": { |
| 62 |
"schemas": { |
| 63 |
"Item": { |
| 64 |
"title": "Item", |
| 65 |
"required": ["name", "price"], |
| 66 |
"type": "object", |
| 67 |
"properties": { |
| 68 |
"name": {"title": "Name", "type": "string"}, |
| 69 |
"price": {"title": "Price", "type": "number"}, |
| 70 |
"tags": { |
| 71 |
"title": "Tags", |
| 72 |
"type": "array", |
| 73 |
"items": {"type": "string"}, |
| 74 |
}, |
| 75 |
"description": { |
| 76 |
"title": "Description", |
| 77 |
"anyOf": [{"type": "string"}, {"type": "null"}], |
| 78 |
}, |
| 79 |
"tax": { |
| 80 |
"title": "Tax", |
| 81 |
"anyOf": [{"type": "number"}, {"type": "null"}], |
| 82 |
}, |
| 83 |
}, |
| 84 |
} |
| 85 |
} |
| 86 |
}, |
| 87 |
} |
| 88 |
) |