Coverage for tests / test_tutorial / test_fastapi / test_delete / test_tutorial001.py: 100%
47 statements
« prev ^ index » next coverage.py v7.13.0, created at 2026-11-09 00:02 +0000
« prev ^ index » next coverage.py v7.13.0, created at 2026-11-09 00:02 +0000
1import importlib 1jklmnopqr
2from types import ModuleType 1jklmnopqr
4import pytest 1jklmnopqr
5from dirty_equals import IsOneOf 1jklmnopqr
6from fastapi.testclient import TestClient 1jklmnopqr
7from sqlalchemy import Engine 1jklmnopqr
10@pytest.fixture( 1jklmnopqr
11 name="module",
12 params=[
13 pytest.param("tutorial001_py310"),
14 ],
15)
16def get_module(request: pytest.FixtureRequest, database_engine: Engine) -> ModuleType: 1jklmnopqr
17 mod = importlib.import_module(f"docs_src.tutorial.fastapi.delete.{request.param}") 1jklmnopqr
18 mod.engine = database_engine 1jklmnopqr
19 return mod 1jklmnopqr
22def test_tutorial(module: ModuleType): 1jklmnopqr
23 with TestClient(module.app) as client: 1abcdefghi
24 hero1_data = {"name": "Deadpond", "secret_name": "Dive Wilson"} 1abcdefghi
25 hero2_data = { 1abcdefghi
26 "name": "Spider-Boy",
27 "secret_name": "Pedro Parqueador",
28 "id": 9000,
29 }
30 hero3_data = { 1abcdefghi
31 "name": "Rusty-Man",
32 "secret_name": "Tommy Sharp",
33 "age": 48,
34 }
35 response = client.post("/heroes/", json=hero1_data) 1abcdefghi
36 assert response.status_code == 200, response.text 1abcdefghi
37 response = client.post("/heroes/", json=hero2_data) 1abcdefghi
38 assert response.status_code == 200, response.text 1abcdefghi
39 hero2 = response.json() 1abcdefghi
40 hero2_id = hero2["id"] 1abcdefghi
41 response = client.post("/heroes/", json=hero3_data) 1abcdefghi
42 assert response.status_code == 200, response.text 1abcdefghi
43 response = client.get(f"/heroes/{hero2_id}") 1abcdefghi
44 assert response.status_code == 200, response.text 1abcdefghi
45 response = client.get("/heroes/9000") 1abcdefghi
46 assert response.status_code == 404, response.text 1abcdefghi
47 response = client.get("/heroes/") 1abcdefghi
48 assert response.status_code == 200, response.text 1abcdefghi
49 data = response.json() 1abcdefghi
50 assert len(data) == 3 1abcdefghi
51 response = client.patch( 1abcdefghi
52 f"/heroes/{hero2_id}", json={"secret_name": "Spider-Youngster"}
53 )
54 assert response.status_code == 200, response.text 1abcdefghi
55 response = client.patch("/heroes/9001", json={"name": "Dragon Cube X"}) 1abcdefghi
56 assert response.status_code == 404, response.text 1abcdefghi
58 response = client.delete(f"/heroes/{hero2_id}") 1abcdefghi
59 assert response.status_code == 200, response.text 1abcdefghi
60 response = client.get("/heroes/") 1abcdefghi
61 assert response.status_code == 200, response.text 1abcdefghi
62 data = response.json() 1abcdefghi
63 assert len(data) == 2 1abcdefghi
65 response = client.delete("/heroes/9000") 1abcdefghi
66 assert response.status_code == 404, response.text 1abcdefghi
68 response = client.get("/openapi.json") 1abcdefghi
69 assert response.status_code == 200, response.text 1abcdefghi
70 assert response.json() == { 1abcdefghi
71 "openapi": "3.1.0",
72 "info": {"title": "FastAPI", "version": "0.1.0"},
73 "paths": {
74 "/heroes/": {
75 "get": {
76 "summary": "Read Heroes",
77 "operationId": "read_heroes_heroes__get",
78 "parameters": [
79 {
80 "required": False,
81 "schema": {
82 "title": "Offset",
83 "type": "integer",
84 "default": 0,
85 },
86 "name": "offset",
87 "in": "query",
88 },
89 {
90 "required": False,
91 "schema": {
92 "title": "Limit",
93 "maximum": 100.0,
94 "type": "integer",
95 "default": 100,
96 },
97 "name": "limit",
98 "in": "query",
99 },
100 ],
101 "responses": {
102 "200": {
103 "description": "Successful Response",
104 "content": {
105 "application/json": {
106 "schema": {
107 "title": "Response Read Heroes Heroes Get",
108 "type": "array",
109 "items": {
110 "$ref": "#/components/schemas/HeroPublic"
111 },
112 }
113 }
114 },
115 },
116 "422": {
117 "description": "Validation Error",
118 "content": {
119 "application/json": {
120 "schema": {
121 "$ref": "#/components/schemas/HTTPValidationError"
122 }
123 }
124 },
125 },
126 },
127 },
128 "post": {
129 "summary": "Create Hero",
130 "operationId": "create_hero_heroes__post",
131 "requestBody": {
132 "content": {
133 "application/json": {
134 "schema": {
135 "$ref": "#/components/schemas/HeroCreate"
136 }
137 }
138 },
139 "required": True,
140 },
141 "responses": {
142 "200": {
143 "description": "Successful Response",
144 "content": {
145 "application/json": {
146 "schema": {
147 "$ref": "#/components/schemas/HeroPublic"
148 }
149 }
150 },
151 },
152 "422": {
153 "description": "Validation Error",
154 "content": {
155 "application/json": {
156 "schema": {
157 "$ref": "#/components/schemas/HTTPValidationError"
158 }
159 }
160 },
161 },
162 },
163 },
164 },
165 "/heroes/{hero_id}": {
166 "get": {
167 "summary": "Read Hero",
168 "operationId": "read_hero_heroes__hero_id__get",
169 "parameters": [
170 {
171 "required": True,
172 "schema": {"title": "Hero Id", "type": "integer"},
173 "name": "hero_id",
174 "in": "path",
175 }
176 ],
177 "responses": {
178 "200": {
179 "description": "Successful Response",
180 "content": {
181 "application/json": {
182 "schema": {
183 "$ref": "#/components/schemas/HeroPublic"
184 }
185 }
186 },
187 },
188 "422": {
189 "description": "Validation Error",
190 "content": {
191 "application/json": {
192 "schema": {
193 "$ref": "#/components/schemas/HTTPValidationError"
194 }
195 }
196 },
197 },
198 },
199 },
200 "delete": {
201 "summary": "Delete Hero",
202 "operationId": "delete_hero_heroes__hero_id__delete",
203 "parameters": [
204 {
205 "required": True,
206 "schema": {"title": "Hero Id", "type": "integer"},
207 "name": "hero_id",
208 "in": "path",
209 }
210 ],
211 "responses": {
212 "200": {
213 "description": "Successful Response",
214 "content": {"application/json": {"schema": {}}},
215 },
216 "422": {
217 "description": "Validation Error",
218 "content": {
219 "application/json": {
220 "schema": {
221 "$ref": "#/components/schemas/HTTPValidationError"
222 }
223 }
224 },
225 },
226 },
227 },
228 "patch": {
229 "summary": "Update Hero",
230 "operationId": "update_hero_heroes__hero_id__patch",
231 "parameters": [
232 {
233 "required": True,
234 "schema": {"title": "Hero Id", "type": "integer"},
235 "name": "hero_id",
236 "in": "path",
237 }
238 ],
239 "requestBody": {
240 "content": {
241 "application/json": {
242 "schema": {
243 "$ref": "#/components/schemas/HeroUpdate"
244 }
245 }
246 },
247 "required": True,
248 },
249 "responses": {
250 "200": {
251 "description": "Successful Response",
252 "content": {
253 "application/json": {
254 "schema": {
255 "$ref": "#/components/schemas/HeroPublic"
256 }
257 }
258 },
259 },
260 "422": {
261 "description": "Validation Error",
262 "content": {
263 "application/json": {
264 "schema": {
265 "$ref": "#/components/schemas/HTTPValidationError"
266 }
267 }
268 },
269 },
270 },
271 },
272 },
273 },
274 "components": {
275 "schemas": {
276 "HTTPValidationError": {
277 "title": "HTTPValidationError",
278 "type": "object",
279 "properties": {
280 "detail": {
281 "title": "Detail",
282 "type": "array",
283 "items": {
284 "$ref": "#/components/schemas/ValidationError"
285 },
286 }
287 },
288 },
289 "HeroCreate": {
290 "title": "HeroCreate",
291 "required": ["name", "secret_name"],
292 "type": "object",
293 "properties": {
294 "name": {"title": "Name", "type": "string"},
295 "secret_name": {"title": "Secret Name", "type": "string"},
296 "age": {
297 "title": "Age",
298 "anyOf": [{"type": "integer"}, {"type": "null"}],
299 },
300 },
301 },
302 "HeroPublic": {
303 "title": "HeroPublic",
304 "required": ["name", "secret_name", "id"],
305 "type": "object",
306 "properties": {
307 "name": {"title": "Name", "type": "string"},
308 "secret_name": {"title": "Secret Name", "type": "string"},
309 "age": {
310 "title": "Age",
311 "anyOf": [{"type": "integer"}, {"type": "null"}],
312 },
313 "id": {"title": "Id", "type": "integer"},
314 },
315 },
316 "HeroUpdate": {
317 "title": "HeroUpdate",
318 "type": "object",
319 "properties": {
320 "name": {
321 "title": "Name",
322 "anyOf": [{"type": "string"}, {"type": "null"}],
323 },
324 "secret_name": {
325 "title": "Secret Name",
326 "anyOf": [{"type": "string"}, {"type": "null"}],
327 },
328 "age": {
329 "title": "Age",
330 "anyOf": [{"type": "integer"}, {"type": "null"}],
331 },
332 },
333 },
334 "ValidationError": {
335 "title": "ValidationError",
336 "required": ["loc", "msg", "type"],
337 "type": "object",
338 "properties": IsOneOf(
339 {
340 "loc": {
341 "title": "Location",
342 "type": "array",
343 "items": {
344 "anyOf": [
345 {"type": "string"},
346 {"type": "integer"},
347 ]
348 },
349 },
350 "msg": {"title": "Message", "type": "string"},
351 "type": {"title": "Error Type", "type": "string"},
352 },
353 {
354 "loc": {
355 "title": "Location",
356 "type": "array",
357 "items": {
358 "anyOf": [
359 {"type": "string"},
360 {"type": "integer"},
361 ]
362 },
363 },
364 "msg": {"title": "Message", "type": "string"},
365 "type": {"title": "Error Type", "type": "string"},
366 "ctx": {"title": "Context", "type": "object"},
367 "input": {"title": "Input"},
368 },
369 ),
370 },
371 }
372 },
373 }