Coverage for tests/test_tutorial/test_dataclasses/test_tutorial003.py: 100%
22 statements
« prev ^ index » next coverage.py v7.6.1, created at 2024-08-08 03:53 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2024-08-08 03:53 +0000
1from fastapi.testclient import TestClient 1abcde
3from docs_src.dataclasses.tutorial003 import app 1abcde
5from ...utils import needs_pydanticv1, needs_pydanticv2 1abcde
7client = TestClient(app) 1abcde
10def test_post_authors_item(): 1abcde
11 response = client.post( 1abcde
12 "/authors/foo/items/",
13 json=[{"name": "Bar"}, {"name": "Baz", "description": "Drop the Baz"}],
14 )
15 assert response.status_code == 200 1abcde
16 assert response.json() == { 1abcde
17 "name": "foo",
18 "items": [
19 {"name": "Bar", "description": None},
20 {"name": "Baz", "description": "Drop the Baz"},
21 ],
22 }
25def test_get_authors(): 1abcde
26 response = client.get("/authors/") 1abcde
27 assert response.status_code == 200 1abcde
28 assert response.json() == [ 1abcde
29 {
30 "name": "Breaters",
31 "items": [
32 {
33 "name": "Island In The Moon",
34 "description": "A place to be playin' and havin' fun",
35 },
36 {"name": "Holy Buddies", "description": None},
37 ],
38 },
39 {
40 "name": "System of an Up",
41 "items": [
42 {
43 "name": "Salt",
44 "description": "The kombucha mushroom people's favorite",
45 },
46 {"name": "Pad Thai", "description": None},
47 {
48 "name": "Lonely Night",
49 "description": "The mostests lonliest nightiest of allest",
50 },
51 ],
52 },
53 ]
56@needs_pydanticv2 1abcde
57def test_openapi_schema(): 1abcde
58 response = client.get("/openapi.json") 1abcde
59 assert response.status_code == 200 1abcde
60 assert response.json() == { 1abcde
61 "openapi": "3.1.0",
62 "info": {"title": "FastAPI", "version": "0.1.0"},
63 "paths": {
64 "/authors/{author_id}/items/": {
65 "post": {
66 "summary": "Create Author Items",
67 "operationId": "create_author_items_authors__author_id__items__post",
68 "parameters": [
69 {
70 "required": True,
71 "schema": {"title": "Author Id", "type": "string"},
72 "name": "author_id",
73 "in": "path",
74 }
75 ],
76 "requestBody": {
77 "content": {
78 "application/json": {
79 "schema": {
80 "title": "Items",
81 "type": "array",
82 "items": {"$ref": "#/components/schemas/Item"},
83 }
84 }
85 },
86 "required": True,
87 },
88 "responses": {
89 "200": {
90 "description": "Successful Response",
91 "content": {
92 "application/json": {
93 "schema": {"$ref": "#/components/schemas/Author"}
94 }
95 },
96 },
97 "422": {
98 "description": "Validation Error",
99 "content": {
100 "application/json": {
101 "schema": {
102 "$ref": "#/components/schemas/HTTPValidationError"
103 }
104 }
105 },
106 },
107 },
108 }
109 },
110 "/authors/": {
111 "get": {
112 "summary": "Get Authors",
113 "operationId": "get_authors_authors__get",
114 "responses": {
115 "200": {
116 "description": "Successful Response",
117 "content": {
118 "application/json": {
119 "schema": {
120 "title": "Response Get Authors Authors Get",
121 "type": "array",
122 "items": {
123 "$ref": "#/components/schemas/Author"
124 },
125 }
126 }
127 },
128 }
129 },
130 }
131 },
132 },
133 "components": {
134 "schemas": {
135 "Author": {
136 "title": "Author",
137 "required": ["name"],
138 "type": "object",
139 "properties": {
140 "name": {"title": "Name", "type": "string"},
141 "items": {
142 "title": "Items",
143 "type": "array",
144 "items": {"$ref": "#/components/schemas/Item"},
145 },
146 },
147 },
148 "HTTPValidationError": {
149 "title": "HTTPValidationError",
150 "type": "object",
151 "properties": {
152 "detail": {
153 "title": "Detail",
154 "type": "array",
155 "items": {"$ref": "#/components/schemas/ValidationError"},
156 }
157 },
158 },
159 "Item": {
160 "title": "Item",
161 "required": ["name"],
162 "type": "object",
163 "properties": {
164 "name": {"title": "Name", "type": "string"},
165 "description": {
166 "anyOf": [{"type": "string"}, {"type": "null"}],
167 "title": "Description",
168 },
169 },
170 },
171 "ValidationError": {
172 "title": "ValidationError",
173 "required": ["loc", "msg", "type"],
174 "type": "object",
175 "properties": {
176 "loc": {
177 "title": "Location",
178 "type": "array",
179 "items": {
180 "anyOf": [{"type": "string"}, {"type": "integer"}]
181 },
182 },
183 "msg": {"title": "Message", "type": "string"},
184 "type": {"title": "Error Type", "type": "string"},
185 },
186 },
187 }
188 },
189 }
192# TODO: remove when deprecating Pydantic v1
193@needs_pydanticv1 1abcde
194def test_openapi_schema_pv1(): 1abcde
195 response = client.get("/openapi.json") 1abcde
196 assert response.status_code == 200 1abcde
197 assert response.json() == { 1abcde
198 "openapi": "3.1.0",
199 "info": {"title": "FastAPI", "version": "0.1.0"},
200 "paths": {
201 "/authors/{author_id}/items/": {
202 "post": {
203 "summary": "Create Author Items",
204 "operationId": "create_author_items_authors__author_id__items__post",
205 "parameters": [
206 {
207 "required": True,
208 "schema": {"title": "Author Id", "type": "string"},
209 "name": "author_id",
210 "in": "path",
211 }
212 ],
213 "requestBody": {
214 "content": {
215 "application/json": {
216 "schema": {
217 "title": "Items",
218 "type": "array",
219 "items": {"$ref": "#/components/schemas/Item"},
220 }
221 }
222 },
223 "required": True,
224 },
225 "responses": {
226 "200": {
227 "description": "Successful Response",
228 "content": {
229 "application/json": {
230 "schema": {"$ref": "#/components/schemas/Author"}
231 }
232 },
233 },
234 "422": {
235 "description": "Validation Error",
236 "content": {
237 "application/json": {
238 "schema": {
239 "$ref": "#/components/schemas/HTTPValidationError"
240 }
241 }
242 },
243 },
244 },
245 }
246 },
247 "/authors/": {
248 "get": {
249 "summary": "Get Authors",
250 "operationId": "get_authors_authors__get",
251 "responses": {
252 "200": {
253 "description": "Successful Response",
254 "content": {
255 "application/json": {
256 "schema": {
257 "title": "Response Get Authors Authors Get",
258 "type": "array",
259 "items": {
260 "$ref": "#/components/schemas/Author"
261 },
262 }
263 }
264 },
265 }
266 },
267 }
268 },
269 },
270 "components": {
271 "schemas": {
272 "Author": {
273 "title": "Author",
274 "required": ["name"],
275 "type": "object",
276 "properties": {
277 "name": {"title": "Name", "type": "string"},
278 "items": {
279 "title": "Items",
280 "type": "array",
281 "items": {"$ref": "#/components/schemas/Item"},
282 },
283 },
284 },
285 "HTTPValidationError": {
286 "title": "HTTPValidationError",
287 "type": "object",
288 "properties": {
289 "detail": {
290 "title": "Detail",
291 "type": "array",
292 "items": {"$ref": "#/components/schemas/ValidationError"},
293 }
294 },
295 },
296 "Item": {
297 "title": "Item",
298 "required": ["name"],
299 "type": "object",
300 "properties": {
301 "name": {"title": "Name", "type": "string"},
302 "description": {"title": "Description", "type": "string"},
303 },
304 },
305 "ValidationError": {
306 "title": "ValidationError",
307 "required": ["loc", "msg", "type"],
308 "type": "object",
309 "properties": {
310 "loc": {
311 "title": "Location",
312 "type": "array",
313 "items": {
314 "anyOf": [{"type": "string"}, {"type": "integer"}]
315 },
316 },
317 "msg": {"title": "Message", "type": "string"},
318 "type": {"title": "Error Type", "type": "string"},
319 },
320 },
321 }
322 },
323 }