Debian Codegraph @

tests/test_tutorial/test_handling_errors/test_tutorial005.py

raw
1 from fastapi.testclient import TestClient
2 from inline_snapshot import snapshot
3
4 from docs_src.handling_errors.tutorial005_py310 import app
5
6 client = TestClient(app)
7
8
9 def test_post_validation_error():
10 response = client.post("/items/", json={"title": "towel", "size": "XL"})
11 assert response.status_code == 422, response.text
12 assert response.json() == {
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 }
23
24
25 def test_post():
26 data = {"title": "towel", "size": 5}
27 response = client.post("/items/", json=data)
28 assert response.status_code == 200, response.text
29 assert response.json() == data
30
31
32 def test_openapi_schema():
33 response = client.get("/openapi.json")
34 assert response.status_code == 200, response.text
35 assert response.json() == snapshot(
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 )