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