| 1 |
import pytest |
| 2 |
from fastapi.testclient import TestClient |
| 3 |
from inline_snapshot import snapshot |
| 4 |
|
| 5 |
from docs_src.path_params.tutorial001_py310 import app |
| 6 |
|
| 7 |
client = TestClient(app) |
| 8 |
|
| 9 |
|
| 10 |
@pytest.mark.parametrize( |
| 11 |
("item_id", "expected_response"), |
| 12 |
[ |
| 13 |
(1, {"item_id": "1"}), |
| 14 |
("alice", {"item_id": "alice"}), |
| 15 |
], |
| 16 |
) |
| 17 |
def test_get_items(item_id, expected_response): |
| 18 |
response = client.get(f"/items/{item_id}") |
| 19 |
assert response.status_code == 200, response.text |
| 20 |
assert response.json() == expected_response |
| 21 |
|
| 22 |
|
| 23 |
def test_openapi_schema(): |
| 24 |
response = client.get("/openapi.json") |
| 25 |
assert response.status_code == 200, response.text |
| 26 |
assert response.json() == snapshot( |
| 27 |
{ |
| 28 |
"openapi": "3.1.0", |
| 29 |
"info": {"title": "FastAPI", "version": "0.1.0"}, |
| 30 |
"paths": { |
| 31 |
"/items/{item_id}": { |
| 32 |
"get": { |
| 33 |
"operationId": "read_item_items__item_id__get", |
| 34 |
"parameters": [ |
| 35 |
{ |
| 36 |
"in": "path", |
| 37 |
"name": "item_id", |
| 38 |
"required": True, |
| 39 |
"schema": { |
| 40 |
"title": "Item Id", |
| 41 |
}, |
| 42 |
}, |
| 43 |
], |
| 44 |
"responses": { |
| 45 |
"200": { |
| 46 |
"content": { |
| 47 |
"application/json": { |
| 48 |
"schema": {}, |
| 49 |
}, |
| 50 |
}, |
| 51 |
"description": "Successful Response", |
| 52 |
}, |
| 53 |
"422": { |
| 54 |
"content": { |
| 55 |
"application/json": { |
| 56 |
"schema": { |
| 57 |
"$ref": "#/components/schemas/HTTPValidationError", |
| 58 |
}, |
| 59 |
}, |
| 60 |
}, |
| 61 |
"description": "Validation Error", |
| 62 |
}, |
| 63 |
}, |
| 64 |
"summary": "Read Item", |
| 65 |
}, |
| 66 |
}, |
| 67 |
}, |
| 68 |
"components": { |
| 69 |
"schemas": { |
| 70 |
"HTTPValidationError": { |
| 71 |
"properties": { |
| 72 |
"detail": { |
| 73 |
"items": { |
| 74 |
"$ref": "#/components/schemas/ValidationError", |
| 75 |
}, |
| 76 |
"title": "Detail", |
| 77 |
"type": "array", |
| 78 |
}, |
| 79 |
}, |
| 80 |
"title": "HTTPValidationError", |
| 81 |
"type": "object", |
| 82 |
}, |
| 83 |
"ValidationError": { |
| 84 |
"properties": { |
| 85 |
"ctx": {"title": "Context", "type": "object"}, |
| 86 |
"input": {"title": "Input"}, |
| 87 |
"loc": { |
| 88 |
"items": { |
| 89 |
"anyOf": [ |
| 90 |
{ |
| 91 |
"type": "string", |
| 92 |
}, |
| 93 |
{ |
| 94 |
"type": "integer", |
| 95 |
}, |
| 96 |
], |
| 97 |
}, |
| 98 |
"title": "Location", |
| 99 |
"type": "array", |
| 100 |
}, |
| 101 |
"msg": { |
| 102 |
"title": "Message", |
| 103 |
"type": "string", |
| 104 |
}, |
| 105 |
"type": { |
| 106 |
"title": "Error Type", |
| 107 |
"type": "string", |
| 108 |
}, |
| 109 |
}, |
| 110 |
"required": [ |
| 111 |
"loc", |
| 112 |
"msg", |
| 113 |
"type", |
| 114 |
], |
| 115 |
"title": "ValidationError", |
| 116 |
"type": "object", |
| 117 |
}, |
| 118 |
}, |
| 119 |
}, |
| 120 |
} |
| 121 |
) |