Coverage for tests/test_tutorial/test_sql_databases/test_sql_databases_middleware_py39.py: 100%
89 statements
« prev ^ index » next coverage.py v7.6.1, created at 2024-08-08 03:53 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2024-08-08 03:53 +0000
1import importlib 1eabcd
2import os 1eabcd
3from pathlib import Path 1eabcd
5import pytest 1eabcd
6from dirty_equals import IsDict 1eabcd
7from fastapi.testclient import TestClient 1eabcd
9from ...utils import needs_py39, needs_pydanticv1 1eabcd
12@pytest.fixture(scope="module") 1eabcd
13def client(tmp_path_factory: pytest.TempPathFactory): 1eabcd
14 tmp_path = tmp_path_factory.mktemp("data") 1abcd
15 cwd = os.getcwd() 1abcd
16 os.chdir(tmp_path) 1abcd
17 test_db = Path("./sql_app.db") 1abcd
18 if test_db.is_file(): # pragma: nocover 1abcd
19 test_db.unlink()
20 # Import while creating the client to create the DB after starting the test session
21 from docs_src.sql_databases.sql_app_py39 import alt_main 1abcd
23 # Ensure import side effects are re-executed
24 importlib.reload(alt_main) 1abcd
26 with TestClient(alt_main.app) as c: 1abcd
27 yield c 1abcd
28 if test_db.is_file(): # pragma: nocover 1abcd
29 test_db.unlink() 1abcd
30 os.chdir(cwd) 1abcd
33@needs_py39 1eabcd
34# TODO: pv2 add version with Pydantic v2
35@needs_pydanticv1 1eabcd
36def test_create_user(client): 1eabcd
37 test_user = {"email": "johndoe@example.com", "password": "secret"} 1abcd
38 response = client.post("/users/", json=test_user) 1abcd
39 assert response.status_code == 200, response.text 1abcd
40 data = response.json() 1abcd
41 assert test_user["email"] == data["email"] 1abcd
42 assert "id" in data 1abcd
43 response = client.post("/users/", json=test_user) 1abcd
44 assert response.status_code == 400, response.text 1abcd
47@needs_py39 1eabcd
48# TODO: pv2 add version with Pydantic v2
49@needs_pydanticv1 1eabcd
50def test_get_user(client): 1eabcd
51 response = client.get("/users/1") 1abcd
52 assert response.status_code == 200, response.text 1abcd
53 data = response.json() 1abcd
54 assert "email" in data 1abcd
55 assert "id" in data 1abcd
58@needs_py39 1eabcd
59# TODO: pv2 add version with Pydantic v2
60@needs_pydanticv1 1eabcd
61def test_nonexistent_user(client): 1eabcd
62 response = client.get("/users/999") 1abcd
63 assert response.status_code == 404, response.text 1abcd
66@needs_py39 1eabcd
67# TODO: pv2 add version with Pydantic v2
68@needs_pydanticv1 1eabcd
69def test_get_users(client): 1eabcd
70 response = client.get("/users/") 1abcd
71 assert response.status_code == 200, response.text 1abcd
72 data = response.json() 1abcd
73 assert "email" in data[0] 1abcd
74 assert "id" in data[0] 1abcd
77@needs_py39 1eabcd
78# TODO: pv2 add version with Pydantic v2
79@needs_pydanticv1 1eabcd
80def test_create_item(client): 1eabcd
81 item = {"title": "Foo", "description": "Something that fights"} 1abcd
82 response = client.post("/users/1/items/", json=item) 1abcd
83 assert response.status_code == 200, response.text 1abcd
84 item_data = response.json() 1abcd
85 assert item["title"] == item_data["title"] 1abcd
86 assert item["description"] == item_data["description"] 1abcd
87 assert "id" in item_data 1abcd
88 assert "owner_id" in item_data 1abcd
89 response = client.get("/users/1") 1abcd
90 assert response.status_code == 200, response.text 1abcd
91 user_data = response.json() 1abcd
92 item_to_check = [it for it in user_data["items"] if it["id"] == item_data["id"]][0] 1abcd
93 assert item_to_check["title"] == item["title"] 1abcd
94 assert item_to_check["description"] == item["description"] 1abcd
95 response = client.get("/users/1") 1abcd
96 assert response.status_code == 200, response.text 1abcd
97 user_data = response.json() 1abcd
98 item_to_check = [it for it in user_data["items"] if it["id"] == item_data["id"]][0] 1abcd
99 assert item_to_check["title"] == item["title"] 1abcd
100 assert item_to_check["description"] == item["description"] 1abcd
103@needs_py39 1eabcd
104# TODO: pv2 add version with Pydantic v2
105@needs_pydanticv1 1eabcd
106def test_read_items(client): 1eabcd
107 response = client.get("/items/") 1abcd
108 assert response.status_code == 200, response.text 1abcd
109 data = response.json() 1abcd
110 assert data 1abcd
111 first_item = data[0] 1abcd
112 assert "title" in first_item 1abcd
113 assert "description" in first_item 1abcd
116@needs_py39 1eabcd
117# TODO: pv2 add version with Pydantic v2
118@needs_pydanticv1 1eabcd
119def test_openapi_schema(client: TestClient): 1eabcd
120 response = client.get("/openapi.json") 1abcd
121 assert response.status_code == 200, response.text 1abcd
122 assert response.json() == { 1abcd
123 "openapi": "3.1.0",
124 "info": {"title": "FastAPI", "version": "0.1.0"},
125 "paths": {
126 "/users/": {
127 "get": {
128 "responses": {
129 "200": {
130 "description": "Successful Response",
131 "content": {
132 "application/json": {
133 "schema": {
134 "title": "Response Read Users Users Get",
135 "type": "array",
136 "items": {"$ref": "#/components/schemas/User"},
137 }
138 }
139 },
140 },
141 "422": {
142 "description": "Validation Error",
143 "content": {
144 "application/json": {
145 "schema": {
146 "$ref": "#/components/schemas/HTTPValidationError"
147 }
148 }
149 },
150 },
151 },
152 "summary": "Read Users",
153 "operationId": "read_users_users__get",
154 "parameters": [
155 {
156 "required": False,
157 "schema": {
158 "title": "Skip",
159 "type": "integer",
160 "default": 0,
161 },
162 "name": "skip",
163 "in": "query",
164 },
165 {
166 "required": False,
167 "schema": {
168 "title": "Limit",
169 "type": "integer",
170 "default": 100,
171 },
172 "name": "limit",
173 "in": "query",
174 },
175 ],
176 },
177 "post": {
178 "responses": {
179 "200": {
180 "description": "Successful Response",
181 "content": {
182 "application/json": {
183 "schema": {"$ref": "#/components/schemas/User"}
184 }
185 },
186 },
187 "422": {
188 "description": "Validation Error",
189 "content": {
190 "application/json": {
191 "schema": {
192 "$ref": "#/components/schemas/HTTPValidationError"
193 }
194 }
195 },
196 },
197 },
198 "summary": "Create User",
199 "operationId": "create_user_users__post",
200 "requestBody": {
201 "content": {
202 "application/json": {
203 "schema": {"$ref": "#/components/schemas/UserCreate"}
204 }
205 },
206 "required": True,
207 },
208 },
209 },
210 "/users/{user_id}": {
211 "get": {
212 "responses": {
213 "200": {
214 "description": "Successful Response",
215 "content": {
216 "application/json": {
217 "schema": {"$ref": "#/components/schemas/User"}
218 }
219 },
220 },
221 "422": {
222 "description": "Validation Error",
223 "content": {
224 "application/json": {
225 "schema": {
226 "$ref": "#/components/schemas/HTTPValidationError"
227 }
228 }
229 },
230 },
231 },
232 "summary": "Read User",
233 "operationId": "read_user_users__user_id__get",
234 "parameters": [
235 {
236 "required": True,
237 "schema": {"title": "User Id", "type": "integer"},
238 "name": "user_id",
239 "in": "path",
240 }
241 ],
242 }
243 },
244 "/users/{user_id}/items/": {
245 "post": {
246 "responses": {
247 "200": {
248 "description": "Successful Response",
249 "content": {
250 "application/json": {
251 "schema": {"$ref": "#/components/schemas/Item"}
252 }
253 },
254 },
255 "422": {
256 "description": "Validation Error",
257 "content": {
258 "application/json": {
259 "schema": {
260 "$ref": "#/components/schemas/HTTPValidationError"
261 }
262 }
263 },
264 },
265 },
266 "summary": "Create Item For User",
267 "operationId": "create_item_for_user_users__user_id__items__post",
268 "parameters": [
269 {
270 "required": True,
271 "schema": {"title": "User Id", "type": "integer"},
272 "name": "user_id",
273 "in": "path",
274 }
275 ],
276 "requestBody": {
277 "content": {
278 "application/json": {
279 "schema": {"$ref": "#/components/schemas/ItemCreate"}
280 }
281 },
282 "required": True,
283 },
284 }
285 },
286 "/items/": {
287 "get": {
288 "responses": {
289 "200": {
290 "description": "Successful Response",
291 "content": {
292 "application/json": {
293 "schema": {
294 "title": "Response Read Items Items Get",
295 "type": "array",
296 "items": {"$ref": "#/components/schemas/Item"},
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 "summary": "Read Items",
313 "operationId": "read_items_items__get",
314 "parameters": [
315 {
316 "required": False,
317 "schema": {
318 "title": "Skip",
319 "type": "integer",
320 "default": 0,
321 },
322 "name": "skip",
323 "in": "query",
324 },
325 {
326 "required": False,
327 "schema": {
328 "title": "Limit",
329 "type": "integer",
330 "default": 100,
331 },
332 "name": "limit",
333 "in": "query",
334 },
335 ],
336 }
337 },
338 },
339 "components": {
340 "schemas": {
341 "ItemCreate": {
342 "title": "ItemCreate",
343 "required": ["title"],
344 "type": "object",
345 "properties": {
346 "title": {"title": "Title", "type": "string"},
347 "description": IsDict(
348 {
349 "title": "Description",
350 "anyOf": [{"type": "string"}, {"type": "null"}],
351 }
352 )
353 | IsDict(
354 # TODO: remove when deprecating Pydantic v1
355 {"title": "Description", "type": "string"}
356 ),
357 },
358 },
359 "Item": {
360 "title": "Item",
361 "required": ["title", "id", "owner_id"],
362 "type": "object",
363 "properties": {
364 "title": {"title": "Title", "type": "string"},
365 "description": IsDict(
366 {
367 "title": "Description",
368 "anyOf": [{"type": "string"}, {"type": "null"}],
369 }
370 )
371 | IsDict(
372 # TODO: remove when deprecating Pydantic v1
373 {"title": "Description", "type": "string"},
374 ),
375 "id": {"title": "Id", "type": "integer"},
376 "owner_id": {"title": "Owner Id", "type": "integer"},
377 },
378 },
379 "User": {
380 "title": "User",
381 "required": ["email", "id", "is_active"],
382 "type": "object",
383 "properties": {
384 "email": {"title": "Email", "type": "string"},
385 "id": {"title": "Id", "type": "integer"},
386 "is_active": {"title": "Is Active", "type": "boolean"},
387 "items": {
388 "title": "Items",
389 "type": "array",
390 "items": {"$ref": "#/components/schemas/Item"},
391 "default": [],
392 },
393 },
394 },
395 "UserCreate": {
396 "title": "UserCreate",
397 "required": ["email", "password"],
398 "type": "object",
399 "properties": {
400 "email": {"title": "Email", "type": "string"},
401 "password": {"title": "Password", "type": "string"},
402 },
403 },
404 "ValidationError": {
405 "title": "ValidationError",
406 "required": ["loc", "msg", "type"],
407 "type": "object",
408 "properties": {
409 "loc": {
410 "title": "Location",
411 "type": "array",
412 "items": {
413 "anyOf": [{"type": "string"}, {"type": "integer"}]
414 },
415 },
416 "msg": {"title": "Message", "type": "string"},
417 "type": {"title": "Error Type", "type": "string"},
418 },
419 },
420 "HTTPValidationError": {
421 "title": "HTTPValidationError",
422 "type": "object",
423 "properties": {
424 "detail": {
425 "title": "Detail",
426 "type": "array",
427 "items": {"$ref": "#/components/schemas/ValidationError"},
428 }
429 },
430 },
431 }
432 },
433 }