Coverage for tests / test_tutorial / test_handling_errors / test_tutorial005.py: 100%
17 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.tutorial005_py310 import app 1abcd
6client = TestClient(app) 1abcd
9def test_post_validation_error(): 1abcd
10 response = client.post("/items/", json={"title": "towel", "size": "XL"}) 1hij
11 assert response.status_code == 422, response.text 1hij
12 assert response.json() == { 1hij
13 "detail": [
14 {
15 "type": "int_parsing",
16 "loc": ["body", "size"],
17 "msg": "Input should be a valid integer, unable to parse string as an integer",
18 "input": "XL",
19 }
20 ],
21 "body": {"title": "towel", "size": "XL"},
22 }
25def test_post(): 1abcd
26 data = {"title": "towel", "size": 5} 1efg
27 response = client.post("/items/", json=data) 1efg
28 assert response.status_code == 200, response.text 1efg
29 assert response.json() == data 1efg
32def test_openapi_schema(): 1abcd
33 response = client.get("/openapi.json") 1klm
34 assert response.status_code == 200, response.text 1klm
35 assert response.json() == snapshot( 1klm
36 {
37 "openapi": "3.1.0",
38 "info": {"title": "FastAPI", "version": "0.1.0"},
39 "paths": {
40 "/items/": {
41 "post": {
42 "summary": "Create Item",
43 "operationId": "create_item_items__post",
44 "requestBody": {
45 "content": {
46 "application/json": {
47 "schema": {"$ref": "#/components/schemas/Item"}
48 }
49 },
50 "required": True,
51 },
52 "responses": {
53 "200": {
54 "description": "Successful Response",
55 "content": {"application/json": {"schema": {}}},
56 },
57 "422": {
58 "description": "Validation Error",
59 "content": {
60 "application/json": {
61 "schema": {
62 "$ref": "#/components/schemas/HTTPValidationError"
63 }
64 }
65 },
66 },
67 },
68 }
69 }
70 },
71 "components": {
72 "schemas": {
73 "HTTPValidationError": {
74 "title": "HTTPValidationError",
75 "type": "object",
76 "properties": {
77 "detail": {
78 "title": "Detail",
79 "type": "array",
80 "items": {
81 "$ref": "#/components/schemas/ValidationError"
82 },
83 }
84 },
85 },
86 "Item": {
87 "title": "Item",
88 "required": ["title", "size"],
89 "type": "object",
90 "properties": {
91 "title": {"title": "Title", "type": "string"},
92 "size": {"title": "Size", "type": "integer"},
93 },
94 },
95 "ValidationError": {
96 "title": "ValidationError",
97 "required": ["loc", "msg", "type"],
98 "type": "object",
99 "properties": {
100 "loc": {
101 "title": "Location",
102 "type": "array",
103 "items": {
104 "anyOf": [{"type": "string"}, {"type": "integer"}]
105 },
106 },
107 "msg": {"title": "Message", "type": "string"},
108 "type": {"title": "Error Type", "type": "string"},
109 "input": {"title": "Input"},
110 "ctx": {"title": "Context", "type": "object"},
111 },
112 },
113 }
114 },
115 }
116 )