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