Coverage for tests / test_tutorial / test_fastapi / test_teams / test_tutorial001.py: 100%
83 statements
« prev ^ index » next coverage.py v7.13.0, created at 2026-06-10 09:40 +0000
« prev ^ index » next coverage.py v7.13.0, created at 2026-06-10 09:40 +0000
1import importlib 1ijklmnop
2from types import ModuleType 1ijklmnop
4import pytest 1ijklmnop
5from dirty_equals import IsOneOf 1ijklmnop
6from fastapi.testclient import TestClient 1ijklmnop
7from sqlmodel import create_engine 1ijklmnop
8from sqlmodel.pool import StaticPool 1ijklmnop
11@pytest.fixture( 1ijklmnop
12 name="module",
13 params=[
14 pytest.param("tutorial001_py310"),
15 ],
16)
17def get_module(request: pytest.FixtureRequest) -> ModuleType: 1ijklmnop
18 mod = importlib.import_module(f"docs_src.tutorial.fastapi.teams.{request.param}") 1ijklmnop
19 mod.sqlite_url = "sqlite://" 1ijklmnop
20 mod.engine = create_engine( 1ijklmnop
21 mod.sqlite_url, connect_args=mod.connect_args, poolclass=StaticPool
22 )
23 return mod 1ijklmnop
26def test_tutorial(module: ModuleType): 1ijklmnop
27 with TestClient(module.app) as client: 1abcdefgh
28 hero1_data = {"name": "Deadpond", "secret_name": "Dive Wilson"} 1abcdefgh
29 hero2_data = { 1abcdefgh
30 "name": "Spider-Boy",
31 "secret_name": "Pedro Parqueador",
32 "id": 9000,
33 }
34 hero3_data = { 1abcdefgh
35 "name": "Rusty-Man",
36 "secret_name": "Tommy Sharp",
37 "age": 48,
38 }
39 response = client.post("/heroes/", json=hero1_data) 1abcdefgh
40 assert response.status_code == 200, response.text 1abcdefgh
41 response = client.post("/heroes/", json=hero2_data) 1abcdefgh
42 assert response.status_code == 200, response.text 1abcdefgh
43 hero2 = response.json() 1abcdefgh
44 hero2_id = hero2["id"] 1abcdefgh
45 response = client.post("/heroes/", json=hero3_data) 1abcdefgh
46 assert response.status_code == 200, response.text 1abcdefgh
47 response = client.get(f"/heroes/{hero2_id}") 1abcdefgh
48 assert response.status_code == 200, response.text 1abcdefgh
49 response = client.get("/heroes/9000") 1abcdefgh
50 assert response.status_code == 404, response.text 1abcdefgh
51 response = client.get("/heroes/") 1abcdefgh
52 assert response.status_code == 200, response.text 1abcdefgh
53 data = response.json() 1abcdefgh
54 assert len(data) == 3 1abcdefgh
55 response = client.patch( 1abcdefgh
56 f"/heroes/{hero2_id}", json={"secret_name": "Spider-Youngster"}
57 )
58 assert response.status_code == 200, response.text 1abcdefgh
59 response = client.patch("/heroes/9001", json={"name": "Dragon Cube X"}) 1abcdefgh
60 assert response.status_code == 404, response.text 1abcdefgh
61 response = client.delete(f"/heroes/{hero2_id}") 1abcdefgh
62 assert response.status_code == 200, response.text 1abcdefgh
63 response = client.get("/heroes/") 1abcdefgh
64 assert response.status_code == 200, response.text 1abcdefgh
65 data = response.json() 1abcdefgh
66 assert len(data) == 2 1abcdefgh
67 response = client.delete("/heroes/9000") 1abcdefgh
68 assert response.status_code == 404, response.text 1abcdefgh
70 team_preventers = {"name": "Preventers", "headquarters": "Sharp Tower"} 1abcdefgh
71 team_z_force = {"name": "Z-Force", "headquarters": "Sister Margaret's Bar"} 1abcdefgh
72 response = client.post("/teams/", json=team_preventers) 1abcdefgh
73 assert response.status_code == 200, response.text 1abcdefgh
74 team_preventers_data = response.json() 1abcdefgh
75 team_preventers_id = team_preventers_data["id"] 1abcdefgh
76 response = client.post("/teams/", json=team_z_force) 1abcdefgh
77 assert response.status_code == 200, response.text 1abcdefgh
78 team_z_force_data = response.json() 1abcdefgh
79 team_z_force_data["id"] 1abcdefgh
80 response = client.get("/teams/") 1abcdefgh
81 data = response.json() 1abcdefgh
82 assert len(data) == 2 1abcdefgh
83 response = client.get(f"/teams/{team_preventers_id}") 1abcdefgh
84 data = response.json() 1abcdefgh
85 assert response.status_code == 200, response.text 1abcdefgh
86 assert data == team_preventers_data 1abcdefgh
87 response = client.get("/teams/9000") 1abcdefgh
88 assert response.status_code == 404, response.text 1abcdefgh
89 response = client.patch( 1abcdefgh
90 f"/teams/{team_preventers_id}", json={"headquarters": "Preventers Tower"}
91 )
92 data = response.json() 1abcdefgh
93 assert response.status_code == 200, response.text 1abcdefgh
94 assert data["name"] == team_preventers["name"] 1abcdefgh
95 assert data["headquarters"] == "Preventers Tower" 1abcdefgh
96 response = client.patch("/teams/9000", json={"name": "Freedom League"}) 1abcdefgh
97 assert response.status_code == 404, response.text 1abcdefgh
98 response = client.delete(f"/teams/{team_preventers_id}") 1abcdefgh
99 assert response.status_code == 200, response.text 1abcdefgh
100 response = client.delete("/teams/9000") 1abcdefgh
101 assert response.status_code == 404, response.text 1abcdefgh
102 response = client.get("/teams/") 1abcdefgh
103 assert response.status_code == 200, response.text 1abcdefgh
104 data = response.json() 1abcdefgh
105 assert len(data) == 1 1abcdefgh
107 response = client.get("/openapi.json") 1abcdefgh
108 assert response.status_code == 200, response.text 1abcdefgh
109 assert response.json() == { 1abcdefgh
110 "openapi": "3.1.0",
111 "info": {"title": "FastAPI", "version": "0.1.0"},
112 "paths": {
113 "/heroes/": {
114 "get": {
115 "summary": "Read Heroes",
116 "operationId": "read_heroes_heroes__get",
117 "parameters": [
118 {
119 "required": False,
120 "schema": {
121 "title": "Offset",
122 "type": "integer",
123 "default": 0,
124 },
125 "name": "offset",
126 "in": "query",
127 },
128 {
129 "required": False,
130 "schema": {
131 "title": "Limit",
132 "maximum": 100.0,
133 "type": "integer",
134 "default": 100,
135 },
136 "name": "limit",
137 "in": "query",
138 },
139 ],
140 "responses": {
141 "200": {
142 "description": "Successful Response",
143 "content": {
144 "application/json": {
145 "schema": {
146 "title": "Response Read Heroes Heroes Get",
147 "type": "array",
148 "items": {
149 "$ref": "#/components/schemas/HeroPublic"
150 },
151 }
152 }
153 },
154 },
155 "422": {
156 "description": "Validation Error",
157 "content": {
158 "application/json": {
159 "schema": {
160 "$ref": "#/components/schemas/HTTPValidationError"
161 }
162 }
163 },
164 },
165 },
166 },
167 "post": {
168 "summary": "Create Hero",
169 "operationId": "create_hero_heroes__post",
170 "requestBody": {
171 "content": {
172 "application/json": {
173 "schema": {
174 "$ref": "#/components/schemas/HeroCreate"
175 }
176 }
177 },
178 "required": True,
179 },
180 "responses": {
181 "200": {
182 "description": "Successful Response",
183 "content": {
184 "application/json": {
185 "schema": {
186 "$ref": "#/components/schemas/HeroPublic"
187 }
188 }
189 },
190 },
191 "422": {
192 "description": "Validation Error",
193 "content": {
194 "application/json": {
195 "schema": {
196 "$ref": "#/components/schemas/HTTPValidationError"
197 }
198 }
199 },
200 },
201 },
202 },
203 },
204 "/heroes/{hero_id}": {
205 "get": {
206 "summary": "Read Hero",
207 "operationId": "read_hero_heroes__hero_id__get",
208 "parameters": [
209 {
210 "required": True,
211 "schema": {"title": "Hero Id", "type": "integer"},
212 "name": "hero_id",
213 "in": "path",
214 }
215 ],
216 "responses": {
217 "200": {
218 "description": "Successful Response",
219 "content": {
220 "application/json": {
221 "schema": {
222 "$ref": "#/components/schemas/HeroPublic"
223 }
224 }
225 },
226 },
227 "422": {
228 "description": "Validation Error",
229 "content": {
230 "application/json": {
231 "schema": {
232 "$ref": "#/components/schemas/HTTPValidationError"
233 }
234 }
235 },
236 },
237 },
238 },
239 "delete": {
240 "summary": "Delete Hero",
241 "operationId": "delete_hero_heroes__hero_id__delete",
242 "parameters": [
243 {
244 "required": True,
245 "schema": {"title": "Hero Id", "type": "integer"},
246 "name": "hero_id",
247 "in": "path",
248 }
249 ],
250 "responses": {
251 "200": {
252 "description": "Successful Response",
253 "content": {"application/json": {"schema": {}}},
254 },
255 "422": {
256 "description": "Validation Error",
257 "content": {
258 "application/json": {
259 "schema": {
260 "$ref": "#/components/schemas/HTTPValidationError"
261 }
262 }
263 },
264 },
265 },
266 },
267 "patch": {
268 "summary": "Update Hero",
269 "operationId": "update_hero_heroes__hero_id__patch",
270 "parameters": [
271 {
272 "required": True,
273 "schema": {"title": "Hero Id", "type": "integer"},
274 "name": "hero_id",
275 "in": "path",
276 }
277 ],
278 "requestBody": {
279 "content": {
280 "application/json": {
281 "schema": {
282 "$ref": "#/components/schemas/HeroUpdate"
283 }
284 }
285 },
286 "required": True,
287 },
288 "responses": {
289 "200": {
290 "description": "Successful Response",
291 "content": {
292 "application/json": {
293 "schema": {
294 "$ref": "#/components/schemas/HeroPublic"
295 }
296 }
297 },
298 },
299 "422": {
300 "description": "Validation Error",
301 "content": {
302 "application/json": {
303 "schema": {
304 "$ref": "#/components/schemas/HTTPValidationError"
305 }
306 }
307 },
308 },
309 },
310 },
311 },
312 "/teams/": {
313 "get": {
314 "summary": "Read Teams",
315 "operationId": "read_teams_teams__get",
316 "parameters": [
317 {
318 "required": False,
319 "schema": {
320 "title": "Offset",
321 "type": "integer",
322 "default": 0,
323 },
324 "name": "offset",
325 "in": "query",
326 },
327 {
328 "required": False,
329 "schema": {
330 "title": "Limit",
331 "maximum": 100.0,
332 "type": "integer",
333 "default": 100,
334 },
335 "name": "limit",
336 "in": "query",
337 },
338 ],
339 "responses": {
340 "200": {
341 "description": "Successful Response",
342 "content": {
343 "application/json": {
344 "schema": {
345 "title": "Response Read Teams Teams Get",
346 "type": "array",
347 "items": {
348 "$ref": "#/components/schemas/TeamPublic"
349 },
350 }
351 }
352 },
353 },
354 "422": {
355 "description": "Validation Error",
356 "content": {
357 "application/json": {
358 "schema": {
359 "$ref": "#/components/schemas/HTTPValidationError"
360 }
361 }
362 },
363 },
364 },
365 },
366 "post": {
367 "summary": "Create Team",
368 "operationId": "create_team_teams__post",
369 "requestBody": {
370 "content": {
371 "application/json": {
372 "schema": {
373 "$ref": "#/components/schemas/TeamCreate"
374 }
375 }
376 },
377 "required": True,
378 },
379 "responses": {
380 "200": {
381 "description": "Successful Response",
382 "content": {
383 "application/json": {
384 "schema": {
385 "$ref": "#/components/schemas/TeamPublic"
386 }
387 }
388 },
389 },
390 "422": {
391 "description": "Validation Error",
392 "content": {
393 "application/json": {
394 "schema": {
395 "$ref": "#/components/schemas/HTTPValidationError"
396 }
397 }
398 },
399 },
400 },
401 },
402 },
403 "/teams/{team_id}": {
404 "get": {
405 "summary": "Read Team",
406 "operationId": "read_team_teams__team_id__get",
407 "parameters": [
408 {
409 "required": True,
410 "schema": {"title": "Team Id", "type": "integer"},
411 "name": "team_id",
412 "in": "path",
413 }
414 ],
415 "responses": {
416 "200": {
417 "description": "Successful Response",
418 "content": {
419 "application/json": {
420 "schema": {
421 "$ref": "#/components/schemas/TeamPublic"
422 }
423 }
424 },
425 },
426 "422": {
427 "description": "Validation Error",
428 "content": {
429 "application/json": {
430 "schema": {
431 "$ref": "#/components/schemas/HTTPValidationError"
432 }
433 }
434 },
435 },
436 },
437 },
438 "delete": {
439 "summary": "Delete Team",
440 "operationId": "delete_team_teams__team_id__delete",
441 "parameters": [
442 {
443 "required": True,
444 "schema": {"title": "Team Id", "type": "integer"},
445 "name": "team_id",
446 "in": "path",
447 }
448 ],
449 "responses": {
450 "200": {
451 "description": "Successful Response",
452 "content": {"application/json": {"schema": {}}},
453 },
454 "422": {
455 "description": "Validation Error",
456 "content": {
457 "application/json": {
458 "schema": {
459 "$ref": "#/components/schemas/HTTPValidationError"
460 }
461 }
462 },
463 },
464 },
465 },
466 "patch": {
467 "summary": "Update Team",
468 "operationId": "update_team_teams__team_id__patch",
469 "parameters": [
470 {
471 "required": True,
472 "schema": {"title": "Team Id", "type": "integer"},
473 "name": "team_id",
474 "in": "path",
475 }
476 ],
477 "requestBody": {
478 "content": {
479 "application/json": {
480 "schema": {
481 "$ref": "#/components/schemas/TeamUpdate"
482 }
483 }
484 },
485 "required": True,
486 },
487 "responses": {
488 "200": {
489 "description": "Successful Response",
490 "content": {
491 "application/json": {
492 "schema": {
493 "$ref": "#/components/schemas/TeamPublic"
494 }
495 }
496 },
497 },
498 "422": {
499 "description": "Validation Error",
500 "content": {
501 "application/json": {
502 "schema": {
503 "$ref": "#/components/schemas/HTTPValidationError"
504 }
505 }
506 },
507 },
508 },
509 },
510 },
511 },
512 "components": {
513 "schemas": {
514 "HTTPValidationError": {
515 "title": "HTTPValidationError",
516 "type": "object",
517 "properties": {
518 "detail": {
519 "title": "Detail",
520 "type": "array",
521 "items": {
522 "$ref": "#/components/schemas/ValidationError"
523 },
524 }
525 },
526 },
527 "HeroCreate": {
528 "title": "HeroCreate",
529 "required": ["name", "secret_name"],
530 "type": "object",
531 "properties": {
532 "name": {"title": "Name", "type": "string"},
533 "secret_name": {"title": "Secret Name", "type": "string"},
534 "age": {
535 "title": "Age",
536 "anyOf": [{"type": "integer"}, {"type": "null"}],
537 },
538 "team_id": {
539 "title": "Team Id",
540 "anyOf": [{"type": "integer"}, {"type": "null"}],
541 },
542 },
543 },
544 "HeroPublic": {
545 "title": "HeroPublic",
546 "required": ["name", "secret_name", "id"],
547 "type": "object",
548 "properties": {
549 "name": {"title": "Name", "type": "string"},
550 "secret_name": {"title": "Secret Name", "type": "string"},
551 "age": {
552 "title": "Age",
553 "anyOf": [{"type": "integer"}, {"type": "null"}],
554 },
555 "team_id": {
556 "title": "Team Id",
557 "anyOf": [{"type": "integer"}, {"type": "null"}],
558 },
559 "id": {"title": "Id", "type": "integer"},
560 },
561 },
562 "HeroUpdate": {
563 "title": "HeroUpdate",
564 "type": "object",
565 "properties": {
566 "name": {
567 "title": "Name",
568 "anyOf": [{"type": "string"}, {"type": "null"}],
569 },
570 "secret_name": {
571 "title": "Secret Name",
572 "anyOf": [{"type": "string"}, {"type": "null"}],
573 },
574 "age": {
575 "title": "Age",
576 "anyOf": [{"type": "integer"}, {"type": "null"}],
577 },
578 "team_id": {
579 "title": "Team Id",
580 "anyOf": [{"type": "integer"}, {"type": "null"}],
581 },
582 },
583 },
584 "TeamCreate": {
585 "title": "TeamCreate",
586 "required": ["name", "headquarters"],
587 "type": "object",
588 "properties": {
589 "name": {"title": "Name", "type": "string"},
590 "headquarters": {"title": "Headquarters", "type": "string"},
591 },
592 },
593 "TeamPublic": {
594 "title": "TeamPublic",
595 "required": ["name", "headquarters", "id"],
596 "type": "object",
597 "properties": {
598 "name": {"title": "Name", "type": "string"},
599 "headquarters": {"title": "Headquarters", "type": "string"},
600 "id": {"title": "Id", "type": "integer"},
601 },
602 },
603 "TeamUpdate": {
604 "title": "TeamUpdate",
605 "type": "object",
606 "properties": {
607 "name": {
608 "title": "Name",
609 "anyOf": [{"type": "string"}, {"type": "null"}],
610 },
611 "headquarters": {
612 "title": "Headquarters",
613 "anyOf": [{"type": "string"}, {"type": "null"}],
614 },
615 },
616 },
617 "ValidationError": {
618 "title": "ValidationError",
619 "required": ["loc", "msg", "type"],
620 "type": "object",
621 "properties": IsOneOf(
622 {
623 "loc": {
624 "title": "Location",
625 "type": "array",
626 "items": {
627 "anyOf": [
628 {"type": "string"},
629 {"type": "integer"},
630 ]
631 },
632 },
633 "msg": {"title": "Message", "type": "string"},
634 "type": {"title": "Error Type", "type": "string"},
635 },
636 {
637 "loc": {
638 "title": "Location",
639 "type": "array",
640 "items": {
641 "anyOf": [
642 {"type": "string"},
643 {"type": "integer"},
644 ]
645 },
646 },
647 "msg": {"title": "Message", "type": "string"},
648 "type": {"title": "Error Type", "type": "string"},
649 "ctx": {"title": "Context", "type": "object"},
650 "input": {"title": "Input"},
651 },
652 ),
653 },
654 }
655 },
656 }