| 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("tutorial010_py310"), |
| 12 |
], |
| 13 |
) |
| 14 |
def get_client(request: pytest.FixtureRequest): |
| 15 |
mod = importlib.import_module(f"docs_src.custom_response.{request.param}") |
| 16 |
client = TestClient(mod.app) |
| 17 |
return client |
| 18 |
|
| 19 |
|
| 20 |
def test_get_custom_response(client: TestClient): |
| 21 |
response = client.get("/items/") |
| 22 |
assert response.status_code == 200, response.text |
| 23 |
assert response.text == snapshot("<h1>Items</h1><p>This is a list of items.</p>") |
| 24 |
|
| 25 |
|
| 26 |
def test_openapi_schema(client: TestClient): |
| 27 |
response = client.get("/openapi.json") |
| 28 |
assert response.status_code == 200, response.text |
| 29 |
assert response.json() == snapshot( |
| 30 |
{ |
| 31 |
"openapi": "3.1.0", |
| 32 |
"info": {"title": "FastAPI", "version": "0.1.0"}, |
| 33 |
"paths": { |
| 34 |
"/items/": { |
| 35 |
"get": { |
| 36 |
"responses": { |
| 37 |
"200": { |
| 38 |
"description": "Successful Response", |
| 39 |
"content": { |
| 40 |
"text/html": {"schema": {"type": "string"}} |
| 41 |
}, |
| 42 |
} |
| 43 |
}, |
| 44 |
"summary": "Read Items", |
| 45 |
"operationId": "read_items_items__get", |
| 46 |
} |
| 47 |
} |
| 48 |
}, |
| 49 |
} |
| 50 |
) |