Fixing bug in DbMemory.del_list(), where if you passed a filter to delete the whole...
[osm/common.git] / osm_common / tests / test_dbmemory.py
1 import http
2 import logging
3 import pytest
4
5 from unittest.mock import MagicMock
6 from osm_common.dbbase import DbException
7 from osm_common.dbmemory import DbMemory
8
9 __author__ = 'Eduardo Sousa <eduardosousa@av.it.pt>'
10
11 @pytest.fixture
12 def db_memory():
13 db = DbMemory()
14 return db
15
16 @pytest.fixture
17 def db_memory_with_data():
18 db = DbMemory()
19
20 db.create("test", {"_id": 1, "data": 1})
21 db.create("test", {"_id": 2, "data": 2})
22 db.create("test", {"_id": 3, "data": 3})
23
24 return db
25
26
27
28 def empty_exception_message():
29 return 'database exception '
30
31 def get_one_exception_message(filter):
32 return "database exception Not found entry with filter='{}'".format(filter)
33
34 def get_one_multiple_exception_message(filter):
35 return "database exception Found more than one entry with filter='{}'".format(filter)
36
37 def del_one_exception_message(filter):
38 return "database exception Not found entry with filter='{}'".format(filter)
39
40 def replace_exception_message(filter):
41 return "database exception Not found entry with filter='{}'".format(filter)
42
43 def test_constructor():
44 db = DbMemory()
45
46 assert db.logger == logging.getLogger('db')
47 assert len(db.db) == 0
48
49 def test_constructor_with_logger():
50 logger_name = 'db_local'
51
52 db = DbMemory(logger_name=logger_name)
53
54 assert db.logger == logging.getLogger(logger_name)
55 assert len(db.db) == 0
56
57 def test_db_connect():
58 logger_name = 'db_local'
59 config = {'logger_name': logger_name}
60
61 db = DbMemory()
62 db.db_connect(config)
63
64 assert db.logger == logging.getLogger(logger_name)
65 assert len(db.db) == 0
66
67 @pytest.mark.parametrize("table, filter", [
68 ("test", {}),
69 ("test", {"_id": 1}),
70 ("test", {"data": 1}),
71 ("test", {"_id": 1, "data": 1}),
72 ])
73 def test_get_list_with_empty_db(db_memory, table, filter):
74 result = db_memory.get_list(table, filter)
75
76 assert len(result) == 0
77
78 @pytest.mark.parametrize("table, filter, expected_data", [
79 ("test", {}, [{"_id": 1, "data": 1}, {"_id": 2, "data": 2}, {"_id": 3, "data": 3}]),
80 ("test", {"_id": 1}, [{"_id": 1, "data": 1}]),
81 ("test", {"data": 1}, [{"_id": 1, "data": 1}]),
82 ("test", {"_id": 1, "data": 1}, [{"_id": 1, "data": 1}]),
83 ("test", {"_id": 2}, [{"_id": 2, "data": 2}]),
84 ("test", {"data": 2}, [{"_id": 2, "data": 2}]),
85 ("test", {"_id": 2, "data": 2}, [{"_id": 2, "data": 2}]),
86 ("test", {"_id": 4}, []),
87 ("test", {"data": 4}, []),
88 ("test", {"_id": 4, "data": 4}, []),
89 ("test_table", {}, []),
90 ("test_table", {"_id": 1}, []),
91 ("test_table", {"data": 1}, []),
92 ("test_table", {"_id": 1, "data": 1}, []),
93 ])
94 def test_get_list_with_non_empty_db(db_memory_with_data, table, filter, expected_data):
95 result = db_memory_with_data.get_list(table, filter)
96
97 assert len(result) == len(expected_data)
98 for data in expected_data:
99 assert data in result
100
101 def test_get_list_exception(db_memory_with_data):
102 table = 'test'
103 filter = {}
104
105 db_memory_with_data._find = MagicMock(side_effect=Exception())
106
107 with pytest.raises(DbException) as excinfo:
108 db_memory_with_data.get_list(table, filter)
109 assert str(excinfo.value) == empty_exception_message()
110 assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND
111
112 @pytest.mark.parametrize("table, filter, expected_data", [
113 ("test", {"_id": 1}, {"_id": 1, "data": 1}),
114 ("test", {"_id": 2}, {"_id": 2, "data": 2}),
115 ("test", {"_id": 3}, {"_id": 3, "data": 3}),
116 ("test", {"data": 1}, {"_id": 1, "data": 1}),
117 ("test", {"data": 2}, {"_id": 2, "data": 2}),
118 ("test", {"data": 3}, {"_id": 3, "data": 3}),
119 ("test", {"_id": 1, "data": 1}, {"_id": 1, "data": 1}),
120 ("test", {"_id": 2, "data": 2}, {"_id": 2, "data": 2}),
121 ("test", {"_id": 3, "data": 3}, {"_id": 3, "data": 3}),
122 ])
123 def test_get_one(db_memory_with_data, table, filter, expected_data):
124 result = db_memory_with_data.get_one(table, filter)
125
126 assert result == expected_data
127 assert len(db_memory_with_data.db) == 1
128 assert table in db_memory_with_data.db
129 assert len(db_memory_with_data.db[table]) == 3
130 assert result in db_memory_with_data.db[table]
131
132 @pytest.mark.parametrize("table, filter, expected_data", [
133 ("test", {}, {"_id": 1, "data": 1}),
134 ])
135 def test_get_one_with_multiple_results(db_memory_with_data, table, filter, expected_data):
136 result = db_memory_with_data.get_one(table, filter, fail_on_more=False)
137
138 assert result == expected_data
139 assert len(db_memory_with_data.db) == 1
140 assert table in db_memory_with_data.db
141 assert len(db_memory_with_data.db[table]) == 3
142 assert result in db_memory_with_data.db[table]
143
144 def test_get_one_with_multiple_results_exception(db_memory_with_data):
145 table = "test"
146 filter = {}
147
148 with pytest.raises(DbException) as excinfo:
149 db_memory_with_data.get_one(table, filter)
150
151 assert str(excinfo.value) == (empty_exception_message() + get_one_multiple_exception_message(filter))
152 # assert excinfo.value.http_code == http.HTTPStatus.CONFLICT
153
154 @pytest.mark.parametrize("table, filter", [
155 ("test", {"_id": 4}),
156 ("test", {"data": 4}),
157 ("test", {"_id": 4, "data": 4}),
158 ("test_table", {"_id": 4}),
159 ("test_table", {"data": 4}),
160 ("test_table", {"_id": 4, "data": 4}),
161 ])
162 def test_get_one_with_non_empty_db_exception(db_memory_with_data, table, filter):
163 with pytest.raises(DbException) as excinfo:
164 db_memory_with_data.get_one(table, filter)
165 assert str(excinfo.value) == (empty_exception_message() + get_one_exception_message(filter))
166 assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND
167
168 @pytest.mark.parametrize("table, filter", [
169 ("test", {"_id": 4}),
170 ("test", {"data": 4}),
171 ("test", {"_id": 4, "data": 4}),
172 ("test_table", {"_id": 4}),
173 ("test_table", {"data": 4}),
174 ("test_table", {"_id": 4, "data": 4}),
175 ])
176 def test_get_one_with_non_empty_db_none(db_memory_with_data, table, filter):
177 result = db_memory_with_data.get_one(table, filter, fail_on_empty=False)
178
179 assert result == None
180
181 @pytest.mark.parametrize("table, filter", [
182 ("test", {"_id": 4}),
183 ("test", {"data": 4}),
184 ("test", {"_id": 4, "data": 4}),
185 ("test_table", {"_id": 4}),
186 ("test_table", {"data": 4}),
187 ("test_table", {"_id": 4, "data": 4}),
188 ])
189 def test_get_one_with_empty_db_exception(db_memory, table, filter):
190 with pytest.raises(DbException) as excinfo:
191 db_memory.get_one(table, filter)
192 assert str(excinfo.value) == (empty_exception_message() + get_one_exception_message(filter))
193 assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND
194
195 @pytest.mark.parametrize("table, filter", [
196 ("test", {"_id": 4}),
197 ("test", {"data": 4}),
198 ("test", {"_id": 4, "data": 4}),
199 ("test_table", {"_id": 4}),
200 ("test_table", {"data": 4}),
201 ("test_table", {"_id": 4, "data": 4}),
202 ])
203 def test_get_one_with_empty_db_none(db_memory, table, filter):
204 result = db_memory.get_one(table, filter, fail_on_empty=False)
205
206 assert result == None
207
208 def test_get_one_generic_exception(db_memory_with_data):
209 table = 'test'
210 filter = {}
211
212 db_memory_with_data._find = MagicMock(side_effect=Exception())
213
214 with pytest.raises(DbException) as excinfo:
215 db_memory_with_data.get_one(table, filter)
216 assert str(excinfo.value) == empty_exception_message()
217 assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND
218
219 @pytest.mark.parametrize("table, filter, expected_data", [
220 ("test", {}, []),
221 ("test", {"_id": 1}, [{"_id": 2, "data": 2}, {"_id": 3, "data": 3}]),
222 ("test", {"_id": 2}, [{"_id": 1, "data": 1}, {"_id": 3, "data": 3}]),
223 ("test", {"_id": 1, "data": 1}, [{"_id": 2, "data": 2}, {"_id": 3, "data": 3}]),
224 ("test", {"_id": 2, "data": 2}, [{"_id": 1, "data": 1}, {"_id": 3, "data": 3}]),
225 ])
226 def test_del_list_with_non_empty_db(db_memory_with_data, table, filter, expected_data):
227 result = db_memory_with_data.del_list(table, filter)
228
229 assert result["deleted"] == (3 - len(expected_data))
230 assert len(db_memory_with_data.db) == 1
231 assert table in db_memory_with_data.db
232 assert len(db_memory_with_data.db[table]) == len(expected_data)
233 for data in expected_data:
234 assert data in db_memory_with_data.db[table]
235
236 @pytest.mark.parametrize("table, filter", [
237 ("test", {}),
238 ("test", {"_id": 1}),
239 ("test", {"_id": 2}),
240 ("test", {"data": 1}),
241 ("test", {"data": 2}),
242 ("test", {"_id": 1, "data": 1}),
243 ("test", {"_id": 2, "data": 2}),
244 ])
245 def test_del_list_with_empty_db(db_memory, table, filter):
246 result = db_memory.del_list(table, filter)
247 assert result['deleted'] == 0
248
249 def test_del_list_generic_exception(db_memory_with_data):
250 table = 'test'
251 filter = {}
252
253 db_memory_with_data._find = MagicMock(side_effect=Exception())
254
255 with pytest.raises(DbException) as excinfo:
256 db_memory_with_data.del_list(table, filter)
257 assert str(excinfo.value) == empty_exception_message()
258 assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND
259
260 @pytest.mark.parametrize("table, filter, data", [
261 ("test", {}, {"_id": 1, "data": 1}),
262 ("test", {"_id": 1}, {"_id": 1, "data": 1}),
263 ("test", {"data": 1}, {"_id": 1, "data": 1}),
264 ("test", {"_id": 1, "data": 1}, {"_id": 1, "data": 1}),
265 ("test", {"_id": 2}, {"_id": 2, "data": 2}),
266 ("test", {"data": 2}, {"_id": 2, "data": 2}),
267 ("test", {"_id": 2, "data": 2}, {"_id": 2, "data": 2}),
268 ])
269 def test_del_one(db_memory_with_data, table, filter, data):
270 result = db_memory_with_data.del_one(table, filter)
271
272 assert result == {"deleted": 1}
273 assert len(db_memory_with_data.db) == 1
274 assert table in db_memory_with_data.db
275 assert len(db_memory_with_data.db[table]) == 2
276 assert data not in db_memory_with_data.db[table]
277
278 @pytest.mark.parametrize("table, filter", [
279 ("test", {}),
280 ("test", {"_id": 1}),
281 ("test", {"_id": 2}),
282 ("test", {"data": 1}),
283 ("test", {"data": 2}),
284 ("test", {"_id": 1, "data": 1}),
285 ("test", {"_id": 2, "data": 2}),
286 ("test_table", {}),
287 ("test_table", {"_id": 1}),
288 ("test_table", {"_id": 2}),
289 ("test_table", {"data": 1}),
290 ("test_table", {"data": 2}),
291 ("test_table", {"_id": 1, "data": 1}),
292 ("test_table", {"_id": 2, "data": 2}),
293 ])
294 def test_del_one_with_empty_db_exception(db_memory, table, filter):
295 with pytest.raises(DbException) as excinfo:
296 db_memory.del_one(table, filter)
297 assert str(excinfo.value) == (empty_exception_message() + del_one_exception_message(filter))
298 assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND
299
300 @pytest.mark.parametrize("table, filter", [
301 ("test", {}),
302 ("test", {"_id": 1}),
303 ("test", {"_id": 2}),
304 ("test", {"data": 1}),
305 ("test", {"data": 2}),
306 ("test", {"_id": 1, "data": 1}),
307 ("test", {"_id": 2, "data": 2}),
308 ("test_table", {}),
309 ("test_table", {"_id": 1}),
310 ("test_table", {"_id": 2}),
311 ("test_table", {"data": 1}),
312 ("test_table", {"data": 2}),
313 ("test_table", {"_id": 1, "data": 1}),
314 ("test_table", {"_id": 2, "data": 2}),
315 ])
316 def test_del_one_with_empty_db_none(db_memory, table, filter):
317 result = db_memory.del_one(table, filter, fail_on_empty=False)
318
319 assert result == None
320
321 @pytest.mark.parametrize("table, filter", [
322 ("test", {"_id": 4}),
323 ("test", {"_id": 5}),
324 ("test", {"data": 4}),
325 ("test", {"data": 5}),
326 ("test", {"_id": 1, "data": 2}),
327 ("test", {"_id": 2, "data": 3}),
328 ("test_table", {}),
329 ("test_table", {"_id": 1}),
330 ("test_table", {"_id": 2}),
331 ("test_table", {"data": 1}),
332 ("test_table", {"data": 2}),
333 ("test_table", {"_id": 1, "data": 1}),
334 ("test_table", {"_id": 2, "data": 2}),
335 ])
336 def test_del_one_with_non_empty_db_exception(db_memory_with_data, table, filter):
337 with pytest.raises(DbException) as excinfo:
338 db_memory_with_data.del_one(table, filter)
339 assert str(excinfo.value) == (empty_exception_message() + del_one_exception_message(filter))
340 assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND
341
342 @pytest.mark.parametrize("table, filter", [
343 ("test", {"_id": 4}),
344 ("test", {"_id": 5}),
345 ("test", {"data": 4}),
346 ("test", {"data": 5}),
347 ("test", {"_id": 1, "data": 2}),
348 ("test", {"_id": 2, "data": 3}),
349 ("test_table", {}),
350 ("test_table", {"_id": 1}),
351 ("test_table", {"_id": 2}),
352 ("test_table", {"data": 1}),
353 ("test_table", {"data": 2}),
354 ("test_table", {"_id": 1, "data": 1}),
355 ("test_table", {"_id": 2, "data": 2}),
356 ])
357 def test_del_one_with_non_empty_db_none(db_memory_with_data, table, filter):
358 result = db_memory_with_data.del_one(table, filter, fail_on_empty=False)
359
360 assert result == None
361
362 @pytest.mark.parametrize("fail_on_empty", [
363 (True),
364 (False),
365 ])
366 def test_del_one_generic_exception(db_memory_with_data, fail_on_empty):
367 table = 'test'
368 filter = {}
369
370 db_memory_with_data._find = MagicMock(side_effect=Exception())
371
372 with pytest.raises(DbException) as excinfo:
373 db_memory_with_data.del_one(table, filter, fail_on_empty=fail_on_empty)
374 assert str(excinfo.value) == empty_exception_message()
375 assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND
376
377 @pytest.mark.parametrize("table, filter, indata", [
378 ("test", {}, {"_id": 1, "data": 42}),
379 ("test", {}, {"_id": 3, "data": 42}),
380 ("test", {"_id": 1}, {"_id": 3, "data": 42}),
381 ("test", {"_id": 3}, {"_id": 3, "data": 42}),
382 ("test", {"data": 1}, {"_id": 3, "data": 42}),
383 ("test", {"data": 3}, {"_id": 3, "data": 42}),
384 ("test", {"_id": 1, "data": 1}, {"_id": 3, "data": 42}),
385 ("test", {"_id": 3, "data": 3}, {"_id": 3, "data": 42}),
386 ])
387 def test_replace(db_memory_with_data, table, filter, indata):
388 result = db_memory_with_data.replace(table, filter, indata)
389
390 assert result == {"updated": 1}
391 assert len(db_memory_with_data.db) == 1
392 assert table in db_memory_with_data.db
393 assert len(db_memory_with_data.db[table]) == 3
394 assert indata in db_memory_with_data.db[table]
395
396 @pytest.mark.parametrize("table, filter, indata", [
397 ("test", {}, {'_id': 1, 'data': 1}),
398 ("test", {}, {'_id': 2, 'data': 1}),
399 ("test", {}, {'_id': 1, 'data': 2}),
400 ("test", {'_id': 1}, {'_id': 1, 'data': 1}),
401 ("test", {'_id': 1, 'data': 1}, {'_id': 1, 'data': 1}),
402 ("test_table", {}, {'_id': 1, 'data': 1}),
403 ("test_table", {}, {'_id': 2, 'data': 1}),
404 ("test_table", {}, {'_id': 1, 'data': 2}),
405 ("test_table", {'_id': 1}, {'_id': 1, 'data': 1}),
406 ("test_table", {'_id': 1, 'data': 1}, {'_id': 1, 'data': 1}),
407 ])
408 def test_replace_without_data_exception(db_memory, table, filter, indata):
409 with pytest.raises(DbException) as excinfo:
410 db_memory.replace(table, filter, indata, fail_on_empty=True)
411 assert str(excinfo.value) == (empty_exception_message() + replace_exception_message(filter))
412 assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND
413
414 @pytest.mark.parametrize("table, filter, indata", [
415 ("test", {}, {'_id': 1, 'data': 1}),
416 ("test", {}, {'_id': 2, 'data': 1}),
417 ("test", {}, {'_id': 1, 'data': 2}),
418 ("test", {'_id': 1}, {'_id': 1, 'data': 1}),
419 ("test", {'_id': 1, 'data': 1}, {'_id': 1, 'data': 1}),
420 ("test_table", {}, {'_id': 1, 'data': 1}),
421 ("test_table", {}, {'_id': 2, 'data': 1}),
422 ("test_table", {}, {'_id': 1, 'data': 2}),
423 ("test_table", {'_id': 1}, {'_id': 1, 'data': 1}),
424 ("test_table", {'_id': 1, 'data': 1}, {'_id': 1, 'data': 1}),
425 ])
426 def test_replace_without_data_none(db_memory, table, filter, indata):
427 result = db_memory.replace(table, filter, indata, fail_on_empty=False)
428 assert result == None
429
430 @pytest.mark.parametrize("table, filter, indata", [
431 ("test_table", {}, {'_id': 1, 'data': 1}),
432 ("test_table", {}, {'_id': 2, 'data': 1}),
433 ("test_table", {}, {'_id': 1, 'data': 2}),
434 ("test_table", {'_id': 1}, {'_id': 1, 'data': 1}),
435 ("test_table", {'_id': 1, 'data': 1}, {'_id': 1, 'data': 1}),
436 ])
437 def test_replace_with_data_exception(db_memory_with_data, table, filter, indata):
438 with pytest.raises(DbException) as excinfo:
439 db_memory_with_data.replace(table, filter, indata, fail_on_empty=True)
440 assert str(excinfo.value) == (empty_exception_message() + replace_exception_message(filter))
441 assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND
442
443 @pytest.mark.parametrize("table, filter, indata", [
444 ("test_table", {}, {'_id': 1, 'data': 1}),
445 ("test_table", {}, {'_id': 2, 'data': 1}),
446 ("test_table", {}, {'_id': 1, 'data': 2}),
447 ("test_table", {'_id': 1}, {'_id': 1, 'data': 1}),
448 ("test_table", {'_id': 1, 'data': 1}, {'_id': 1, 'data': 1}),
449 ])
450 def test_replace_with_data_none(db_memory_with_data, table, filter, indata):
451 result = db_memory_with_data.replace(table, filter, indata, fail_on_empty=False)
452 assert result == None
453
454 @pytest.mark.parametrize("fail_on_empty", [
455 (True),
456 (False),
457 ])
458 def test_replace_generic_exception(db_memory_with_data, fail_on_empty):
459 table = 'test'
460 filter = {}
461 indata = {'_id': 1, 'data': 1}
462
463 db_memory_with_data._find = MagicMock(side_effect=Exception())
464
465 with pytest.raises(DbException) as excinfo:
466 db_memory_with_data.replace(table, filter, indata, fail_on_empty=fail_on_empty)
467 assert str(excinfo.value) == empty_exception_message()
468 assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND
469
470 @pytest.mark.parametrize("table, id, data", [
471 ("test", "1", {"data": 1}),
472 ("test", "1", {"data": 2}),
473 ("test", "2", {"data": 1}),
474 ("test", "2", {"data": 2}),
475 ("test_table", "1", {"data": 1}),
476 ("test_table", "1", {"data": 2}),
477 ("test_table", "2", {"data": 1}),
478 ("test_table", "2", {"data": 2}),
479 ("test", "1", {"data_1": 1, "data_2": 2}),
480 ("test", "1", {"data_1": 2, "data_2": 1}),
481 ("test", "2", {"data_1": 1, "data_2": 2}),
482 ("test", "2", {"data_1": 2, "data_2": 1}),
483 ("test_table", "1", {"data_1": 1, "data_2": 2}),
484 ("test_table", "1", {"data_1": 2, "data_2": 1}),
485 ("test_table", "2", {"data_1": 1, "data_2": 2}),
486 ("test_table", "2", {"data_1": 2, "data_2": 1}),
487 ])
488 def test_create_with_empty_db_with_id(db_memory, table, id, data):
489 data_to_insert = data
490 data_to_insert['_id'] = id
491
492 returned_id = db_memory.create(table, data_to_insert)
493
494 assert returned_id == id
495 assert len(db_memory.db) == 1
496 assert table in db_memory.db
497 assert len(db_memory.db[table]) == 1
498 assert data_to_insert in db_memory.db[table]
499
500 @pytest.mark.parametrize("table, id, data", [
501 ("test", "4", {"data": 1}),
502 ("test", "5", {"data": 2}),
503 ("test", "4", {"data": 1}),
504 ("test", "5", {"data": 2}),
505 ("test_table", "4", {"data": 1}),
506 ("test_table", "5", {"data": 2}),
507 ("test_table", "4", {"data": 1}),
508 ("test_table", "5", {"data": 2}),
509 ("test", "4", {"data_1": 1, "data_2": 2}),
510 ("test", "5", {"data_1": 2, "data_2": 1}),
511 ("test", "4", {"data_1": 1, "data_2": 2}),
512 ("test", "5", {"data_1": 2, "data_2": 1}),
513 ("test_table", "4", {"data_1": 1, "data_2": 2}),
514 ("test_table", "5", {"data_1": 2, "data_2": 1}),
515 ("test_table", "4", {"data_1": 1, "data_2": 2}),
516 ("test_table", "5", {"data_1": 2, "data_2": 1}),
517 ])
518 def test_create_with_non_empty_db_with_id(db_memory_with_data, table, id, data):
519 data_to_insert = data
520 data_to_insert['_id'] = id
521
522 returned_id = db_memory_with_data.create(table, data_to_insert)
523
524 assert returned_id == id
525 assert len(db_memory_with_data.db) == (1 if table == 'test' else 2)
526 assert table in db_memory_with_data.db
527 assert len(db_memory_with_data.db[table]) == (4 if table == 'test' else 1)
528 assert data_to_insert in db_memory_with_data.db[table]
529
530 @pytest.mark.parametrize("table, data", [
531 ("test", {"data": 1}),
532 ("test", {"data": 2}),
533 ("test", {"data": 1}),
534 ("test", {"data": 2}),
535 ("test_table", {"data": 1}),
536 ("test_table", {"data": 2}),
537 ("test_table", {"data": 1}),
538 ("test_table", {"data": 2}),
539 ("test", {"data_1": 1, "data_2": 2}),
540 ("test", {"data_1": 2, "data_2": 1}),
541 ("test", {"data_1": 1, "data_2": 2}),
542 ("test", {"data_1": 2, "data_2": 1}),
543 ("test_table", {"data_1": 1, "data_2": 2}),
544 ("test_table", {"data_1": 2, "data_2": 1}),
545 ("test_table", {"data_1": 1, "data_2": 2}),
546 ("test_table", {"data_1": 2, "data_2": 1}),
547 ])
548 def test_create_with_empty_db_without_id(db_memory, table, data):
549 returned_id = db_memory.create(table, data)
550
551 assert len(db_memory.db) == 1
552 assert table in db_memory.db
553 assert len(db_memory.db[table]) == 1
554
555 data_inserted = data
556 data_inserted['_id'] = returned_id
557
558 assert data_inserted in db_memory.db[table]
559
560 @pytest.mark.parametrize("table, data", [
561 ("test", {"data": 1}),
562 ("test", {"data": 2}),
563 ("test", {"data": 1}),
564 ("test", {"data": 2}),
565 ("test_table", {"data": 1}),
566 ("test_table", {"data": 2}),
567 ("test_table", {"data": 1}),
568 ("test_table", {"data": 2}),
569 ("test", {"data_1": 1, "data_2": 2}),
570 ("test", {"data_1": 2, "data_2": 1}),
571 ("test", {"data_1": 1, "data_2": 2}),
572 ("test", {"data_1": 2, "data_2": 1}),
573 ("test_table", {"data_1": 1, "data_2": 2}),
574 ("test_table", {"data_1": 2, "data_2": 1}),
575 ("test_table", {"data_1": 1, "data_2": 2}),
576 ("test_table", {"data_1": 2, "data_2": 1}),
577 ])
578 def test_create_with_non_empty_db_without_id(db_memory_with_data, table, data):
579 returned_id = db_memory_with_data.create(table, data)
580
581 assert len(db_memory_with_data.db) == (1 if table == 'test' else 2)
582 assert table in db_memory_with_data.db
583 assert len(db_memory_with_data.db[table]) == (4 if table == 'test' else 1)
584
585 data_inserted = data
586 data_inserted['_id'] = returned_id
587
588 assert data_inserted in db_memory_with_data.db[table]
589
590 def test_create_with_exception(db_memory):
591 table = "test"
592 data = {"_id": 1, "data": 1}
593
594 db_memory.db = MagicMock()
595 db_memory.db.__contains__.side_effect = Exception()
596
597 with pytest.raises(DbException) as excinfo:
598 db_memory.create(table, data)
599 assert str(excinfo.value) == empty_exception_message()
600 assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND