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