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