-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_main.py
More file actions
482 lines (397 loc) · 14.5 KB
/
test_main.py
File metadata and controls
482 lines (397 loc) · 14.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
from typing import Any
import pytest
from bson import ObjectId
from conftest import TODO_LISTS
from fastapi.testclient import TestClient
from main import MongoTodoList, RedisTodoList, SqlTodoList
from nqlstore import MongoStore, RedisStore, SQLStore
_SEARCH_TERMS = ["ho", "oo", "work"]
@pytest.mark.asyncio
@pytest.mark.parametrize("todolist", TODO_LISTS)
async def test_create_sql_todolist(
client_with_sql: TestClient,
sql_store: SQLStore,
todolist: dict,
):
"""POST to /todos creates a todolist in sql and returns it"""
with client_with_sql as client:
response = client.post("/todos", json=todolist)
got = response.json()
todolist_id = got["id"]
raw_todos = todolist.get("todos", [])
resp_todos = got["todos"]
expected = {
"id": todolist_id,
"name": todolist["name"],
"description": None,
"todos": [
{
**raw,
"is_complete": "0",
"id": resp["id"],
"parent_id": todolist_id,
}
for raw, resp in zip(raw_todos, resp_todos)
],
}
db_query = {"id": {"$eq": todolist_id}}
db_results = await sql_store.find(SqlTodoList, query=db_query, limit=1)
record_in_db = db_results[0].model_dump()
assert got == expected
assert record_in_db == expected
@pytest.mark.asyncio
@pytest.mark.parametrize("todolist", TODO_LISTS)
async def test_create_redis_todolist(
client_with_redis: TestClient,
redis_store: RedisStore,
todolist: dict,
):
"""POST to /todos creates a todolist in redis and returns it"""
with client_with_redis as client:
response = client.post("/todos", json=todolist)
got = response.json()
todolist_id = got["id"]
raw_todos = todolist.get("todos", [])
resp_todos = got["todos"]
expected = {
"id": todolist_id,
"name": todolist["name"],
"description": None,
"pk": todolist_id,
"todos": [
{
**raw,
"is_complete": "0",
"id": resp["id"],
"pk": resp["pk"],
}
for raw, resp in zip(raw_todos, resp_todos)
],
}
db_query = {"id": {"$eq": todolist_id}}
db_results = await redis_store.find(RedisTodoList, query=db_query, limit=1)
record_in_db = db_results[0].model_dump(mode="json")
assert got == expected
assert record_in_db == expected
@pytest.mark.asyncio
@pytest.mark.parametrize("todolist", TODO_LISTS)
async def test_create_mongo_todolist(
client_with_mongo: TestClient,
mongo_store: MongoStore,
todolist: dict,
):
"""POST to /todos creates a todolist in redis and returns it"""
with client_with_mongo as client:
response = client.post("/todos", json=todolist)
got = response.json()
todolist_id = got["id"]
raw_todos = todolist.get("todos", [])
expected = {
"id": todolist_id,
"name": todolist["name"],
"description": None,
"todos": [
{
**raw,
"is_complete": "0",
}
for raw in raw_todos
],
}
db_query = {"_id": {"$eq": ObjectId(todolist_id)}}
db_results = await mongo_store.find(MongoTodoList, query=db_query, limit=1)
record_in_db = db_results[0].model_dump(mode="json")
assert got == expected
assert record_in_db == expected
@pytest.mark.asyncio
@pytest.mark.parametrize("index", range(len(TODO_LISTS)))
async def test_update_sql_todolist(
client_with_sql: TestClient,
sql_store: SQLStore,
sql_todolists: list[SqlTodoList],
index: int,
):
"""PUT to /todos/{id} updates the sql todolist of given id and returns updated version"""
with client_with_sql as client:
todolist = sql_todolists[index]
id_ = todolist.id
todos = [{**v.model_dump(), "is_complete": "1"} for v in todolist.todos]
update = {
"name": "some other name",
"todos": [*todos, {"title": "another one"}, {"title": "another one again"}],
}
response = client.put(f"/todos/{id_}", json=update)
got = response.json()
expected = {
**todolist.model_dump(mode="json"),
**update,
"todos": [
{
**raw,
"id": final["id"],
"parent_id": final["parent_id"],
"is_complete": raw.get("is_complete", final["is_complete"]),
}
for raw, final in zip(update["todos"], got["todos"])
],
}
db_query = {"id": {"$eq": id_}}
db_results = await sql_store.find(SqlTodoList, query=db_query, limit=1)
record_in_db = db_results[0].model_dump(mode="json")
assert got == expected
assert record_in_db == expected
@pytest.mark.asyncio
@pytest.mark.parametrize("index", range(len(TODO_LISTS)))
async def test_update_redis_todolist(
client_with_redis: TestClient,
redis_store: RedisStore,
redis_todolists: list[RedisTodoList],
index: int,
):
"""PUT to /todos/{id} updates the redis todolist of given id and returns updated version"""
with client_with_redis as client:
todolist = redis_todolists[index]
id_ = todolist.id
todos = [{**v.model_dump(), "is_complete": "1"} for v in todolist.todos]
update = {
"name": "some other name",
"todos": [*todos, {"title": "another one"}, {"title": "another one again"}],
}
response = client.put(f"/todos/{id_}", json=update)
got = response.json()
expected = {
**todolist.model_dump(mode="json"),
**update,
"todos": [
{
**raw,
"id": final["id"],
"pk": final["pk"],
"is_complete": raw.get("is_complete", final["is_complete"]),
}
for raw, final in zip(update["todos"], got["todos"])
],
}
db_query = {"id": {"$eq": id_}}
db_results = await redis_store.find(RedisTodoList, query=db_query, limit=1)
record_in_db = db_results[0].model_dump(mode="json")
expected_in_db = {
**expected,
"todos": [
{
**raw,
"id": final["id"],
"pk": final["pk"],
}
for raw, final in zip(expected["todos"], record_in_db["todos"])
],
}
assert got == expected
assert record_in_db == expected_in_db
@pytest.mark.asyncio
@pytest.mark.parametrize("index", range(len(TODO_LISTS)))
async def test_update_mongo_todolist(
client_with_mongo: TestClient,
mongo_store: MongoStore,
mongo_todolists: list[MongoTodoList],
index: int,
):
"""PUT to /todos/{id} updates the mongo todolist of given id and returns updated version"""
with client_with_mongo as client:
todolist = mongo_todolists[index]
id_ = todolist.id
todos = [{**v.model_dump(), "is_complete": "1"} for v in todolist.todos]
update = {
"name": "some other name",
"todos": [*todos, {"title": "another one"}, {"title": "another one again"}],
}
response = client.put(f"/todos/{id_}", json=update)
got = response.json()
expected = {
**todolist.model_dump(mode="json"),
**update,
"todos": [
{**v, "is_complete": v.get("is_complete", "0")} for v in update["todos"]
],
}
db_query = {"_id": {"$eq": id_}}
db_results = await mongo_store.find(MongoTodoList, query=db_query, limit=1)
record_in_db = db_results[0].model_dump(mode="json")
assert got == expected
assert record_in_db == expected
@pytest.mark.asyncio
@pytest.mark.parametrize("index", range(len(TODO_LISTS)))
async def test_delete_sql_todolist(
client_with_sql: TestClient,
sql_store: SQLStore,
sql_todolists: list[SqlTodoList],
index: int,
):
"""DELETE /todos/{id} deletes the sql todolist of given id and returns deleted version"""
with client_with_sql as client:
todolist = sql_todolists[index]
id_ = todolist.id
response = client.delete(f"/todos/{id_}")
got = response.json()
expected = todolist.model_dump(mode="json")
db_query = {"id": {"$eq": id_}}
db_results = await sql_store.find(SqlTodoList, query=db_query, limit=1)
assert got == expected
assert db_results == []
@pytest.mark.asyncio
@pytest.mark.parametrize("index", range(len(TODO_LISTS)))
async def test_delete_redis_todolist(
client_with_redis: TestClient,
redis_store: RedisStore,
redis_todolists: list[RedisTodoList],
index: int,
):
"""DELETE /todos/{id} deletes the redis todolist of given id and returns deleted version"""
with client_with_redis as client:
todolist = redis_todolists[index]
id_ = todolist.id
response = client.delete(f"/todos/{id_}")
got = response.json()
expected = todolist.model_dump(mode="json")
db_query = {"id": {"$eq": id_}}
db_results = await redis_store.find(RedisTodoList, query=db_query, limit=1)
assert got == expected
assert db_results == []
@pytest.mark.asyncio
@pytest.mark.parametrize("index", range(len(TODO_LISTS)))
async def test_delete_mongo_todolist(
client_with_mongo: TestClient,
mongo_store: MongoStore,
mongo_todolists: list[MongoTodoList],
index: int,
):
"""DELETE /todos/{id} deletes the mongo todolist of given id and returns deleted version"""
with client_with_mongo as client:
todolist = mongo_todolists[index]
id_ = todolist.id
response = client.delete(f"/todos/{id_}")
got = response.json()
expected = todolist.model_dump(mode="json")
db_query = {"_id": {"$eq": id_}}
db_results = await mongo_store.find(MongoTodoList, query=db_query, limit=1)
assert got == expected
assert db_results == []
@pytest.mark.asyncio
@pytest.mark.parametrize("index", range(len(TODO_LISTS)))
async def test_read_one_sql_todolist(
client_with_sql: TestClient,
sql_store: SQLStore,
sql_todolists: list[SqlTodoList],
index: int,
):
"""GET /todos/{id} gets the sql todolist of given id"""
with client_with_sql as client:
todolist = sql_todolists[index]
id_ = todolist.id
response = client.get(f"/todos/{id_}")
got = response.json()
expected = todolist.model_dump(mode="json")
db_query = {"id": {"$eq": id_}}
db_results = await sql_store.find(SqlTodoList, query=db_query, limit=1)
record_in_db = db_results[0].model_dump(mode="json")
assert got == expected
assert record_in_db == expected
@pytest.mark.asyncio
@pytest.mark.parametrize("index", range(len(TODO_LISTS)))
async def test_read_one_redis_todolist(
client_with_redis: TestClient,
redis_store: RedisStore,
redis_todolists: list[RedisTodoList],
index: int,
):
"""GET /todos/{id} gets the redis todolist of given id"""
with client_with_redis as client:
todolist = redis_todolists[index]
id_ = todolist.id
response = client.get(f"/todos/{id_}")
got = response.json()
expected = todolist.model_dump(mode="json")
db_query = {"id": {"$eq": id_}}
db_results = await redis_store.find(RedisTodoList, query=db_query, limit=1)
record_in_db = db_results[0].model_dump(mode="json")
assert got == expected
assert record_in_db == expected
@pytest.mark.asyncio
@pytest.mark.parametrize("index", range(len(TODO_LISTS)))
async def test_read_one_mongo_todolist(
client_with_mongo: TestClient,
mongo_store: MongoStore,
mongo_todolists: list[MongoTodoList],
index: int,
):
"""GET /todos/{id} gets the mongo todolist of given id"""
with client_with_mongo as client:
todolist = mongo_todolists[index]
id_ = todolist.id
response = client.get(f"/todos/{id_}")
got = response.json()
expected = todolist.model_dump(mode="json")
db_query = {"_id": {"$eq": id_}}
db_results = await mongo_store.find(MongoTodoList, query=db_query, limit=1)
record_in_db = db_results[0].model_dump(mode="json")
assert got == expected
assert record_in_db == expected
@pytest.mark.asyncio
@pytest.mark.parametrize("q", _SEARCH_TERMS)
async def test_search_sql_by_name(
client_with_sql: TestClient,
sql_store: SQLStore,
sql_todolists: list[SqlTodoList],
q: str,
):
"""GET /todos?q={} gets all sql todolists with name containing search item"""
with client_with_sql as client:
response = client.get(f"/todos?q={q}")
got = response.json()
expected = [
v.model_dump(mode="json") for v in sql_todolists if q in v.name.lower()
]
assert got == expected
@pytest.mark.asyncio
@pytest.mark.parametrize("q", _SEARCH_TERMS)
async def test_search_redis_by_name(
client_with_redis: TestClient,
redis_store: RedisStore,
redis_todolists: list[RedisTodoList],
q: str,
):
"""GET /todos?q={} gets all redis todolists with name containing search item"""
with client_with_redis as client:
response = client.get(f"/todos?q={q}")
got = response.json()
expected = [
v.model_dump(mode="json") for v in redis_todolists if q in v.name.lower()
]
assert got == expected
@pytest.mark.asyncio
@pytest.mark.parametrize("q", _SEARCH_TERMS)
async def test_search_mongo_by_name(
client_with_mongo: TestClient,
mongo_store: MongoStore,
mongo_todolists: list[MongoTodoList],
q: str,
):
"""GET /todos?q={} gets all mongo todolists with name containing search item"""
with client_with_mongo as client:
response = client.get(f"/todos?q={q}")
got = response.json()
expected = [
v.model_dump(mode="json") for v in mongo_todolists if q in v.name.lower()
]
assert got == expected
def _get_id(item: Any) -> Any:
"""Gets the id of the given record
Args:
item: the record whose id is to be obtained
Returns:
the id of the record
"""
try:
return item.id
except AttributeError:
return item.pk