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