Coverage for tests / test_tutorial / test_handling_errors / test_tutorial006.py: 100%
20 statements
« prev ^ index » next coverage.py v7.13.3, created at 2026-02-12 18:15 +0000
« prev ^ index » next coverage.py v7.13.3, created at 2026-02-12 18:15 +0000
1from fastapi.testclient import TestClient 1abcd
2from inline_snapshot import snapshot 1abcd
4from docs_src.handling_errors.tutorial006_py310 import app 1abcd
6client = TestClient(app) 1abcd
9def test_get_validation_error(): 1abcd
10 response = client.get("/items/foo") 1efg
11 assert response.status_code == 422, response.text 1efg
12 assert response.json() == { 1efg
13 "detail": [
14 {
15 "type": "int_parsing",
16 "loc": ["path", "item_id"],
17 "msg": "Input should be a valid integer, unable to parse string as an integer",
18 "input": "foo",
19 }
20 ]
21 }
24def test_get_http_error(): 1abcd
25 response = client.get("/items/3") 1hij
26 assert response.status_code == 418, response.text 1hij
27 assert response.json() == {"detail": "Nope! I don't like 3."} 1hij
30def test_get(): 1abcd
31 response = client.get("/items/2") 1klm
32 assert response.status_code == 200, response.text 1klm
33 assert response.json() == {"item_id": 2} 1klm
36def test_openapi_schema(): 1abcd
37 response = client.get("/openapi.json") 1nop
38 assert response.status_code == 200, response.text 1nop
39 assert response.json() == snapshot( 1nop
40 {
41 "openapi": "3.1.0",
42 "info": {"title": "FastAPI", "version": "0.1.0"},
43 "paths": {
44 "/items/{item_id}": {
45 "get": {
46 "responses": {
47 "200": {
48 "description": "Successful Response",
49 "content": {"application/json": {"schema": {}}},
50 },
51 "422": {
52 "description": "Validation Error",
53 "content": {
54 "application/json": {
55 "schema": {
56 "$ref": "#/components/schemas/HTTPValidationError"
57 }
58 }
59 },
60 },
61 },
62 "summary": "Read Item",
63 "operationId": "read_item_items__item_id__get",
64 "parameters": [
65 {
66 "required": True,
67 "schema": {"title": "Item Id", "type": "integer"},
68 "name": "item_id",
69 "in": "path",
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 )