Coverage for tests / test_tutorial / test_fastapi / test_simple_hero_api / test_tutorial001.py: 100%
43 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( 1jklmnopqr
21 f"docs_src.tutorial.fastapi.simple_hero_api.{request.param}"
22 )
23 mod.sqlite_url = "sqlite://" 1jklmnopqr
24 mod.engine = create_engine( 1jklmnopqr
25 mod.sqlite_url, connect_args=mod.connect_args, poolclass=StaticPool
26 )
27 return mod 1jklmnopqr
30def test_tutorial(module: ModuleType): 1jklmnopqr
31 with TestClient(module.app) as client: 1abcdefghi
32 hero1_data = {"name": "Deadpond", "secret_name": "Dive Wilson"} 1abcdefghi
33 hero2_data = { 1abcdefghi
34 "name": "Spider-Boy",
35 "secret_name": "Pedro Parqueador",
36 "id": 9000,
37 }
38 response = client.post("/heroes/", json=hero1_data) 1abcdefghi
39 data = response.json() 1abcdefghi
41 assert response.status_code == 200, response.text 1abcdefghi
42 assert data["name"] == hero1_data["name"] 1abcdefghi
43 assert data["secret_name"] == hero1_data["secret_name"] 1abcdefghi
44 assert data["id"] is not None 1abcdefghi
45 assert data["age"] is None 1abcdefghi
47 response = client.post("/heroes/", json=hero2_data) 1abcdefghi
48 data = response.json() 1abcdefghi
50 assert response.status_code == 200, response.text 1abcdefghi
51 assert data["name"] == hero2_data["name"] 1abcdefghi
52 assert data["secret_name"] == hero2_data["secret_name"] 1abcdefghi
53 assert data["id"] == hero2_data["id"], ( 1abcdefghi
54 "Up to this point it's still possible to "
55 "set the ID of the hero in the request"
56 )
57 assert data["age"] is None 1abcdefghi
59 response = client.get("/heroes/") 1abcdefghi
60 data = response.json() 1abcdefghi
62 assert response.status_code == 200, response.text 1abcdefghi
63 assert len(data) == 2 1abcdefghi
64 assert data[0]["name"] == hero1_data["name"] 1abcdefghi
65 assert data[0]["secret_name"] == hero1_data["secret_name"] 1abcdefghi
66 assert data[1]["name"] == hero2_data["name"] 1abcdefghi
67 assert data[1]["secret_name"] == hero2_data["secret_name"] 1abcdefghi
68 assert data[1]["id"] == hero2_data["id"] 1abcdefghi
70 response = client.get("/openapi.json") 1abcdefghi
72 assert response.status_code == 200, response.text 1abcdefghi
74 assert response.json() == { 1abcdefghi
75 "openapi": "3.1.0",
76 "info": {"title": "FastAPI", "version": "0.1.0"},
77 "paths": {
78 "/heroes/": {
79 "get": {
80 "summary": "Read Heroes",
81 "operationId": "read_heroes_heroes__get",
82 "responses": {
83 "200": {
84 "description": "Successful Response",
85 "content": {"application/json": {"schema": {}}},
86 }
87 },
88 },
89 "post": {
90 "summary": "Create Hero",
91 "operationId": "create_hero_heroes__post",
92 "requestBody": {
93 "content": {
94 "application/json": {
95 "schema": {"$ref": "#/components/schemas/Hero"}
96 }
97 },
98 "required": True,
99 },
100 "responses": {
101 "200": {
102 "description": "Successful Response",
103 "content": {"application/json": {"schema": {}}},
104 },
105 "422": {
106 "description": "Validation Error",
107 "content": {
108 "application/json": {
109 "schema": {
110 "$ref": "#/components/schemas/HTTPValidationError"
111 }
112 }
113 },
114 },
115 },
116 },
117 }
118 },
119 "components": {
120 "schemas": {
121 "HTTPValidationError": {
122 "title": "HTTPValidationError",
123 "type": "object",
124 "properties": {
125 "detail": {
126 "title": "Detail",
127 "type": "array",
128 "items": {
129 "$ref": "#/components/schemas/ValidationError"
130 },
131 }
132 },
133 },
134 "Hero": {
135 "title": "Hero",
136 "required": ["name", "secret_name"],
137 "type": "object",
138 "properties": {
139 "id": {
140 "title": "Id",
141 "anyOf": [{"type": "integer"}, {"type": "null"}],
142 },
143 "name": {"title": "Name", "type": "string"},
144 "secret_name": {"title": "Secret Name", "type": "string"},
145 "age": {
146 "title": "Age",
147 "anyOf": [{"type": "integer"}, {"type": "null"}],
148 },
149 },
150 },
151 "ValidationError": {
152 "title": "ValidationError",
153 "required": ["loc", "msg", "type"],
154 "type": "object",
155 "properties": {
156 "loc": {
157 "title": "Location",
158 "type": "array",
159 "items": {
160 "anyOf": [{"type": "string"}, {"type": "integer"}]
161 },
162 },
163 "msg": {"title": "Message", "type": "string"},
164 "type": {"title": "Error Type", "type": "string"},
165 },
166 },
167 }
168 },
169 }