Coverage for tests / test_tutorial / test_fastapi / test_delete / test_tutorial001.py: 100%
49 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.delete.{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
62 response = client.delete(f"/heroes/{hero2_id}") 1abcdefgh
63 assert response.status_code == 200, response.text 1abcdefgh
64 response = client.get("/heroes/") 1abcdefgh
65 assert response.status_code == 200, response.text 1abcdefgh
66 data = response.json() 1abcdefgh
67 assert len(data) == 2 1abcdefgh
69 response = client.delete("/heroes/9000") 1abcdefgh
70 assert response.status_code == 404, response.text 1abcdefgh
72 response = client.get("/openapi.json") 1abcdefgh
73 assert response.status_code == 200, response.text 1abcdefgh
74 assert response.json() == { 1abcdefgh
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 "parameters": [
83 {
84 "required": False,
85 "schema": {
86 "title": "Offset",
87 "type": "integer",
88 "default": 0,
89 },
90 "name": "offset",
91 "in": "query",
92 },
93 {
94 "required": False,
95 "schema": {
96 "title": "Limit",
97 "maximum": 100.0,
98 "type": "integer",
99 "default": 100,
100 },
101 "name": "limit",
102 "in": "query",
103 },
104 ],
105 "responses": {
106 "200": {
107 "description": "Successful Response",
108 "content": {
109 "application/json": {
110 "schema": {
111 "title": "Response Read Heroes Heroes Get",
112 "type": "array",
113 "items": {
114 "$ref": "#/components/schemas/HeroPublic"
115 },
116 }
117 }
118 },
119 },
120 "422": {
121 "description": "Validation Error",
122 "content": {
123 "application/json": {
124 "schema": {
125 "$ref": "#/components/schemas/HTTPValidationError"
126 }
127 }
128 },
129 },
130 },
131 },
132 "post": {
133 "summary": "Create Hero",
134 "operationId": "create_hero_heroes__post",
135 "requestBody": {
136 "content": {
137 "application/json": {
138 "schema": {
139 "$ref": "#/components/schemas/HeroCreate"
140 }
141 }
142 },
143 "required": True,
144 },
145 "responses": {
146 "200": {
147 "description": "Successful Response",
148 "content": {
149 "application/json": {
150 "schema": {
151 "$ref": "#/components/schemas/HeroPublic"
152 }
153 }
154 },
155 },
156 "422": {
157 "description": "Validation Error",
158 "content": {
159 "application/json": {
160 "schema": {
161 "$ref": "#/components/schemas/HTTPValidationError"
162 }
163 }
164 },
165 },
166 },
167 },
168 },
169 "/heroes/{hero_id}": {
170 "get": {
171 "summary": "Read Hero",
172 "operationId": "read_hero_heroes__hero_id__get",
173 "parameters": [
174 {
175 "required": True,
176 "schema": {"title": "Hero Id", "type": "integer"},
177 "name": "hero_id",
178 "in": "path",
179 }
180 ],
181 "responses": {
182 "200": {
183 "description": "Successful Response",
184 "content": {
185 "application/json": {
186 "schema": {
187 "$ref": "#/components/schemas/HeroPublic"
188 }
189 }
190 },
191 },
192 "422": {
193 "description": "Validation Error",
194 "content": {
195 "application/json": {
196 "schema": {
197 "$ref": "#/components/schemas/HTTPValidationError"
198 }
199 }
200 },
201 },
202 },
203 },
204 "delete": {
205 "summary": "Delete Hero",
206 "operationId": "delete_hero_heroes__hero_id__delete",
207 "parameters": [
208 {
209 "required": True,
210 "schema": {"title": "Hero Id", "type": "integer"},
211 "name": "hero_id",
212 "in": "path",
213 }
214 ],
215 "responses": {
216 "200": {
217 "description": "Successful Response",
218 "content": {"application/json": {"schema": {}}},
219 },
220 "422": {
221 "description": "Validation Error",
222 "content": {
223 "application/json": {
224 "schema": {
225 "$ref": "#/components/schemas/HTTPValidationError"
226 }
227 }
228 },
229 },
230 },
231 },
232 "patch": {
233 "summary": "Update Hero",
234 "operationId": "update_hero_heroes__hero_id__patch",
235 "parameters": [
236 {
237 "required": True,
238 "schema": {"title": "Hero Id", "type": "integer"},
239 "name": "hero_id",
240 "in": "path",
241 }
242 ],
243 "requestBody": {
244 "content": {
245 "application/json": {
246 "schema": {
247 "$ref": "#/components/schemas/HeroUpdate"
248 }
249 }
250 },
251 "required": True,
252 },
253 "responses": {
254 "200": {
255 "description": "Successful Response",
256 "content": {
257 "application/json": {
258 "schema": {
259 "$ref": "#/components/schemas/HeroPublic"
260 }
261 }
262 },
263 },
264 "422": {
265 "description": "Validation Error",
266 "content": {
267 "application/json": {
268 "schema": {
269 "$ref": "#/components/schemas/HTTPValidationError"
270 }
271 }
272 },
273 },
274 },
275 },
276 },
277 },
278 "components": {
279 "schemas": {
280 "HTTPValidationError": {
281 "title": "HTTPValidationError",
282 "type": "object",
283 "properties": {
284 "detail": {
285 "title": "Detail",
286 "type": "array",
287 "items": {
288 "$ref": "#/components/schemas/ValidationError"
289 },
290 }
291 },
292 },
293 "HeroCreate": {
294 "title": "HeroCreate",
295 "required": ["name", "secret_name"],
296 "type": "object",
297 "properties": {
298 "name": {"title": "Name", "type": "string"},
299 "secret_name": {"title": "Secret Name", "type": "string"},
300 "age": {
301 "title": "Age",
302 "anyOf": [{"type": "integer"}, {"type": "null"}],
303 },
304 },
305 },
306 "HeroPublic": {
307 "title": "HeroPublic",
308 "required": ["name", "secret_name", "id"],
309 "type": "object",
310 "properties": {
311 "name": {"title": "Name", "type": "string"},
312 "secret_name": {"title": "Secret Name", "type": "string"},
313 "age": {
314 "title": "Age",
315 "anyOf": [{"type": "integer"}, {"type": "null"}],
316 },
317 "id": {"title": "Id", "type": "integer"},
318 },
319 },
320 "HeroUpdate": {
321 "title": "HeroUpdate",
322 "type": "object",
323 "properties": {
324 "name": {
325 "title": "Name",
326 "anyOf": [{"type": "string"}, {"type": "null"}],
327 },
328 "secret_name": {
329 "title": "Secret Name",
330 "anyOf": [{"type": "string"}, {"type": "null"}],
331 },
332 "age": {
333 "title": "Age",
334 "anyOf": [{"type": "integer"}, {"type": "null"}],
335 },
336 },
337 },
338 "ValidationError": {
339 "title": "ValidationError",
340 "required": ["loc", "msg", "type"],
341 "type": "object",
342 "properties": IsOneOf(
343 {
344 "loc": {
345 "title": "Location",
346 "type": "array",
347 "items": {
348 "anyOf": [
349 {"type": "string"},
350 {"type": "integer"},
351 ]
352 },
353 },
354 "msg": {"title": "Message", "type": "string"},
355 "type": {"title": "Error Type", "type": "string"},
356 },
357 {
358 "loc": {
359 "title": "Location",
360 "type": "array",
361 "items": {
362 "anyOf": [
363 {"type": "string"},
364 {"type": "integer"},
365 ]
366 },
367 },
368 "msg": {"title": "Message", "type": "string"},
369 "type": {"title": "Error Type", "type": "string"},
370 "ctx": {"title": "Context", "type": "object"},
371 "input": {"title": "Input"},
372 },
373 ),
374 },
375 }
376 },
377 }