blob: 37c2c837e6b474b7e0f096b9346c2e8f77939821 [file] [log] [blame]
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +01001import http
2import logging
3import pytest
4
5from unittest.mock import MagicMock
6from osm_common.dbbase import DbException
7from osm_common.dbmemory import DbMemory
8
9__author__ = 'Eduardo Sousa <eduardosousa@av.it.pt>'
10
tiernob20a9022018-05-22 12:07:05 +020011
tierno1e9a3292018-11-05 18:18:45 +010012@pytest.fixture(scope="function", params=[True, False])
13def db_memory(request):
14 db = DbMemory(lock=request.param)
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +010015 return db
16
tiernob20a9022018-05-22 12:07:05 +020017
tierno1e9a3292018-11-05 18:18:45 +010018@pytest.fixture(scope="function", params=[True, False])
19def db_memory_with_data(request):
20 db = DbMemory(lock=request.param)
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +010021
22 db.create("test", {"_id": 1, "data": 1})
23 db.create("test", {"_id": 2, "data": 2})
24 db.create("test", {"_id": 3, "data": 3})
25
26 return db
27
tiernob20a9022018-05-22 12:07:05 +020028
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +010029def empty_exception_message():
30 return 'database exception '
31
tiernob20a9022018-05-22 12:07:05 +020032
tierno1e9a3292018-11-05 18:18:45 +010033def get_one_exception_message(db_filter):
34 return "database exception Not found entry with filter='{}'".format(db_filter)
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +010035
tiernob20a9022018-05-22 12:07:05 +020036
tierno1e9a3292018-11-05 18:18:45 +010037def get_one_multiple_exception_message(db_filter):
38 return "database exception Found more than one entry with filter='{}'".format(db_filter)
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +010039
tiernob20a9022018-05-22 12:07:05 +020040
tierno1e9a3292018-11-05 18:18:45 +010041def del_one_exception_message(db_filter):
42 return "database exception Not found entry with filter='{}'".format(db_filter)
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +010043
tiernob20a9022018-05-22 12:07:05 +020044
tierno136f2952018-10-19 13:01:03 +020045def replace_exception_message(value):
46 return "database exception Not found entry with _id='{}'".format(value)
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +010047
tiernob20a9022018-05-22 12:07:05 +020048
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +010049def test_constructor():
50 db = DbMemory()
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +010051 assert db.logger == logging.getLogger('db')
52 assert len(db.db) == 0
53
tiernob20a9022018-05-22 12:07:05 +020054
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +010055def test_constructor_with_logger():
56 logger_name = 'db_local'
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +010057 db = DbMemory(logger_name=logger_name)
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +010058 assert db.logger == logging.getLogger(logger_name)
59 assert len(db.db) == 0
60
tiernob20a9022018-05-22 12:07:05 +020061
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +010062def test_db_connect():
63 logger_name = 'db_local'
64 config = {'logger_name': logger_name}
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +010065 db = DbMemory()
66 db.db_connect(config)
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +010067 assert db.logger == logging.getLogger(logger_name)
68 assert len(db.db) == 0
69
tiernob20a9022018-05-22 12:07:05 +020070
Eduardo Sousa5ffda642018-05-09 19:42:00 +010071def test_db_disconnect(db_memory):
72 db_memory.db_disconnect()
73
tiernob20a9022018-05-22 12:07:05 +020074
tierno1e9a3292018-11-05 18:18:45 +010075@pytest.mark.parametrize("table, db_filter", [
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +010076 ("test", {}),
77 ("test", {"_id": 1}),
78 ("test", {"data": 1}),
Eduardo Sousaacbbdf22018-05-03 15:47:41 +010079 ("test", {"_id": 1, "data": 1})])
tierno1e9a3292018-11-05 18:18:45 +010080def test_get_list_with_empty_db(db_memory, table, db_filter):
81 result = db_memory.get_list(table, db_filter)
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +010082 assert len(result) == 0
83
tiernob20a9022018-05-22 12:07:05 +020084
tierno1e9a3292018-11-05 18:18:45 +010085@pytest.mark.parametrize("table, db_filter, expected_data", [
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +010086 ("test", {}, [{"_id": 1, "data": 1}, {"_id": 2, "data": 2}, {"_id": 3, "data": 3}]),
87 ("test", {"_id": 1}, [{"_id": 1, "data": 1}]),
88 ("test", {"data": 1}, [{"_id": 1, "data": 1}]),
89 ("test", {"_id": 1, "data": 1}, [{"_id": 1, "data": 1}]),
90 ("test", {"_id": 2}, [{"_id": 2, "data": 2}]),
91 ("test", {"data": 2}, [{"_id": 2, "data": 2}]),
92 ("test", {"_id": 2, "data": 2}, [{"_id": 2, "data": 2}]),
93 ("test", {"_id": 4}, []),
94 ("test", {"data": 4}, []),
95 ("test", {"_id": 4, "data": 4}, []),
96 ("test_table", {}, []),
97 ("test_table", {"_id": 1}, []),
98 ("test_table", {"data": 1}, []),
Eduardo Sousaacbbdf22018-05-03 15:47:41 +010099 ("test_table", {"_id": 1, "data": 1}, [])])
tierno1e9a3292018-11-05 18:18:45 +0100100def test_get_list_with_non_empty_db(db_memory_with_data, table, db_filter, expected_data):
101 result = db_memory_with_data.get_list(table, db_filter)
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100102 assert len(result) == len(expected_data)
103 for data in expected_data:
104 assert data in result
105
tiernob20a9022018-05-22 12:07:05 +0200106
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100107def test_get_list_exception(db_memory_with_data):
108 table = 'test'
tierno1e9a3292018-11-05 18:18:45 +0100109 db_filter = {}
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100110 db_memory_with_data._find = MagicMock(side_effect=Exception())
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100111 with pytest.raises(DbException) as excinfo:
tierno1e9a3292018-11-05 18:18:45 +0100112 db_memory_with_data.get_list(table, db_filter)
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100113 assert str(excinfo.value) == empty_exception_message()
114 assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND
115
tiernob20a9022018-05-22 12:07:05 +0200116
tierno1e9a3292018-11-05 18:18:45 +0100117@pytest.mark.parametrize("table, db_filter, expected_data", [
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100118 ("test", {"_id": 1}, {"_id": 1, "data": 1}),
119 ("test", {"_id": 2}, {"_id": 2, "data": 2}),
120 ("test", {"_id": 3}, {"_id": 3, "data": 3}),
121 ("test", {"data": 1}, {"_id": 1, "data": 1}),
122 ("test", {"data": 2}, {"_id": 2, "data": 2}),
123 ("test", {"data": 3}, {"_id": 3, "data": 3}),
124 ("test", {"_id": 1, "data": 1}, {"_id": 1, "data": 1}),
125 ("test", {"_id": 2, "data": 2}, {"_id": 2, "data": 2}),
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100126 ("test", {"_id": 3, "data": 3}, {"_id": 3, "data": 3})])
tierno1e9a3292018-11-05 18:18:45 +0100127def test_get_one(db_memory_with_data, table, db_filter, expected_data):
128 result = db_memory_with_data.get_one(table, db_filter)
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100129 assert result == expected_data
130 assert len(db_memory_with_data.db) == 1
131 assert table in db_memory_with_data.db
132 assert len(db_memory_with_data.db[table]) == 3
133 assert result in db_memory_with_data.db[table]
134
tiernob20a9022018-05-22 12:07:05 +0200135
tierno1e9a3292018-11-05 18:18:45 +0100136@pytest.mark.parametrize("table, db_filter, expected_data", [
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100137 ("test", {}, {"_id": 1, "data": 1})])
tierno1e9a3292018-11-05 18:18:45 +0100138def test_get_one_with_multiple_results(db_memory_with_data, table, db_filter, expected_data):
139 result = db_memory_with_data.get_one(table, db_filter, fail_on_more=False)
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100140 assert result == expected_data
141 assert len(db_memory_with_data.db) == 1
142 assert table in db_memory_with_data.db
143 assert len(db_memory_with_data.db[table]) == 3
144 assert result in db_memory_with_data.db[table]
145
tiernob20a9022018-05-22 12:07:05 +0200146
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100147def test_get_one_with_multiple_results_exception(db_memory_with_data):
148 table = "test"
tierno1e9a3292018-11-05 18:18:45 +0100149 db_filter = {}
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100150 with pytest.raises(DbException) as excinfo:
tierno1e9a3292018-11-05 18:18:45 +0100151 db_memory_with_data.get_one(table, db_filter)
152 assert str(excinfo.value) == (empty_exception_message() + get_one_multiple_exception_message(db_filter))
tiernob20a9022018-05-22 12:07:05 +0200153 # assert excinfo.value.http_code == http.HTTPStatus.CONFLICT
154
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100155
tierno1e9a3292018-11-05 18:18:45 +0100156@pytest.mark.parametrize("table, db_filter", [
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100157 ("test", {"_id": 4}),
158 ("test", {"data": 4}),
159 ("test", {"_id": 4, "data": 4}),
160 ("test_table", {"_id": 4}),
161 ("test_table", {"data": 4}),
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100162 ("test_table", {"_id": 4, "data": 4})])
tierno1e9a3292018-11-05 18:18:45 +0100163def test_get_one_with_non_empty_db_exception(db_memory_with_data, table, db_filter):
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100164 with pytest.raises(DbException) as excinfo:
tierno1e9a3292018-11-05 18:18:45 +0100165 db_memory_with_data.get_one(table, db_filter)
166 assert str(excinfo.value) == (empty_exception_message() + get_one_exception_message(db_filter))
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100167 assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND
168
tiernob20a9022018-05-22 12:07:05 +0200169
tierno1e9a3292018-11-05 18:18:45 +0100170@pytest.mark.parametrize("table, db_filter", [
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100171 ("test", {"_id": 4}),
172 ("test", {"data": 4}),
173 ("test", {"_id": 4, "data": 4}),
174 ("test_table", {"_id": 4}),
175 ("test_table", {"data": 4}),
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100176 ("test_table", {"_id": 4, "data": 4})])
tierno1e9a3292018-11-05 18:18:45 +0100177def test_get_one_with_non_empty_db_none(db_memory_with_data, table, db_filter):
178 result = db_memory_with_data.get_one(table, db_filter, fail_on_empty=False)
tiernob20a9022018-05-22 12:07:05 +0200179 assert result is None
180
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100181
tierno1e9a3292018-11-05 18:18:45 +0100182@pytest.mark.parametrize("table, db_filter", [
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100183 ("test", {"_id": 4}),
184 ("test", {"data": 4}),
185 ("test", {"_id": 4, "data": 4}),
186 ("test_table", {"_id": 4}),
187 ("test_table", {"data": 4}),
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100188 ("test_table", {"_id": 4, "data": 4})])
tierno1e9a3292018-11-05 18:18:45 +0100189def test_get_one_with_empty_db_exception(db_memory, table, db_filter):
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100190 with pytest.raises(DbException) as excinfo:
tierno1e9a3292018-11-05 18:18:45 +0100191 db_memory.get_one(table, db_filter)
192 assert str(excinfo.value) == (empty_exception_message() + get_one_exception_message(db_filter))
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100193 assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND
194
tiernob20a9022018-05-22 12:07:05 +0200195
tierno1e9a3292018-11-05 18:18:45 +0100196@pytest.mark.parametrize("table, db_filter", [
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100197 ("test", {"_id": 4}),
198 ("test", {"data": 4}),
199 ("test", {"_id": 4, "data": 4}),
200 ("test_table", {"_id": 4}),
201 ("test_table", {"data": 4}),
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100202 ("test_table", {"_id": 4, "data": 4})])
tierno1e9a3292018-11-05 18:18:45 +0100203def test_get_one_with_empty_db_none(db_memory, table, db_filter):
204 result = db_memory.get_one(table, db_filter, fail_on_empty=False)
tiernob20a9022018-05-22 12:07:05 +0200205 assert result is None
206
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100207
208def test_get_one_generic_exception(db_memory_with_data):
209 table = 'test'
tierno1e9a3292018-11-05 18:18:45 +0100210 db_filter = {}
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100211 db_memory_with_data._find = MagicMock(side_effect=Exception())
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100212 with pytest.raises(DbException) as excinfo:
tierno1e9a3292018-11-05 18:18:45 +0100213 db_memory_with_data.get_one(table, db_filter)
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100214 assert str(excinfo.value) == empty_exception_message()
215 assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND
216
tiernob20a9022018-05-22 12:07:05 +0200217
tierno1e9a3292018-11-05 18:18:45 +0100218@pytest.mark.parametrize("table, db_filter, expected_data", [
Eduardo Sousa857731b2018-04-26 15:55:05 +0100219 ("test", {}, []),
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100220 ("test", {"_id": 1}, [{"_id": 2, "data": 2}, {"_id": 3, "data": 3}]),
221 ("test", {"_id": 2}, [{"_id": 1, "data": 1}, {"_id": 3, "data": 3}]),
222 ("test", {"_id": 1, "data": 1}, [{"_id": 2, "data": 2}, {"_id": 3, "data": 3}]),
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100223 ("test", {"_id": 2, "data": 2}, [{"_id": 1, "data": 1}, {"_id": 3, "data": 3}])])
tierno1e9a3292018-11-05 18:18:45 +0100224def test_del_list_with_non_empty_db(db_memory_with_data, table, db_filter, expected_data):
225 result = db_memory_with_data.del_list(table, db_filter)
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100226 assert result["deleted"] == (3 - len(expected_data))
227 assert len(db_memory_with_data.db) == 1
228 assert table in db_memory_with_data.db
229 assert len(db_memory_with_data.db[table]) == len(expected_data)
230 for data in expected_data:
231 assert data in db_memory_with_data.db[table]
232
tiernob20a9022018-05-22 12:07:05 +0200233
tierno1e9a3292018-11-05 18:18:45 +0100234@pytest.mark.parametrize("table, db_filter", [
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100235 ("test", {}),
236 ("test", {"_id": 1}),
237 ("test", {"_id": 2}),
238 ("test", {"data": 1}),
239 ("test", {"data": 2}),
240 ("test", {"_id": 1, "data": 1}),
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100241 ("test", {"_id": 2, "data": 2})])
tierno1e9a3292018-11-05 18:18:45 +0100242def test_del_list_with_empty_db(db_memory, table, db_filter):
243 result = db_memory.del_list(table, db_filter)
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100244 assert result['deleted'] == 0
245
tiernob20a9022018-05-22 12:07:05 +0200246
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100247def test_del_list_generic_exception(db_memory_with_data):
248 table = 'test'
tierno1e9a3292018-11-05 18:18:45 +0100249 db_filter = {}
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100250 db_memory_with_data._find = MagicMock(side_effect=Exception())
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100251 with pytest.raises(DbException) as excinfo:
tierno1e9a3292018-11-05 18:18:45 +0100252 db_memory_with_data.del_list(table, db_filter)
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100253 assert str(excinfo.value) == empty_exception_message()
254 assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND
255
tiernob20a9022018-05-22 12:07:05 +0200256
tierno1e9a3292018-11-05 18:18:45 +0100257@pytest.mark.parametrize("table, db_filter, data", [
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100258 ("test", {}, {"_id": 1, "data": 1}),
259 ("test", {"_id": 1}, {"_id": 1, "data": 1}),
260 ("test", {"data": 1}, {"_id": 1, "data": 1}),
261 ("test", {"_id": 1, "data": 1}, {"_id": 1, "data": 1}),
262 ("test", {"_id": 2}, {"_id": 2, "data": 2}),
263 ("test", {"data": 2}, {"_id": 2, "data": 2}),
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100264 ("test", {"_id": 2, "data": 2}, {"_id": 2, "data": 2})])
tierno1e9a3292018-11-05 18:18:45 +0100265def test_del_one(db_memory_with_data, table, db_filter, data):
266 result = db_memory_with_data.del_one(table, db_filter)
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100267 assert result == {"deleted": 1}
268 assert len(db_memory_with_data.db) == 1
269 assert table in db_memory_with_data.db
270 assert len(db_memory_with_data.db[table]) == 2
271 assert data not in db_memory_with_data.db[table]
272
tiernob20a9022018-05-22 12:07:05 +0200273
tierno1e9a3292018-11-05 18:18:45 +0100274@pytest.mark.parametrize("table, db_filter", [
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100275 ("test", {}),
276 ("test", {"_id": 1}),
277 ("test", {"_id": 2}),
278 ("test", {"data": 1}),
279 ("test", {"data": 2}),
280 ("test", {"_id": 1, "data": 1}),
281 ("test", {"_id": 2, "data": 2}),
282 ("test_table", {}),
283 ("test_table", {"_id": 1}),
284 ("test_table", {"_id": 2}),
285 ("test_table", {"data": 1}),
286 ("test_table", {"data": 2}),
287 ("test_table", {"_id": 1, "data": 1}),
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100288 ("test_table", {"_id": 2, "data": 2})])
tierno1e9a3292018-11-05 18:18:45 +0100289def test_del_one_with_empty_db_exception(db_memory, table, db_filter):
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100290 with pytest.raises(DbException) as excinfo:
tierno1e9a3292018-11-05 18:18:45 +0100291 db_memory.del_one(table, db_filter)
292 assert str(excinfo.value) == (empty_exception_message() + del_one_exception_message(db_filter))
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100293 assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND
294
tiernob20a9022018-05-22 12:07:05 +0200295
tierno1e9a3292018-11-05 18:18:45 +0100296@pytest.mark.parametrize("table, db_filter", [
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100297 ("test", {}),
298 ("test", {"_id": 1}),
299 ("test", {"_id": 2}),
300 ("test", {"data": 1}),
301 ("test", {"data": 2}),
302 ("test", {"_id": 1, "data": 1}),
303 ("test", {"_id": 2, "data": 2}),
304 ("test_table", {}),
305 ("test_table", {"_id": 1}),
306 ("test_table", {"_id": 2}),
307 ("test_table", {"data": 1}),
308 ("test_table", {"data": 2}),
309 ("test_table", {"_id": 1, "data": 1}),
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100310 ("test_table", {"_id": 2, "data": 2})])
tierno1e9a3292018-11-05 18:18:45 +0100311def test_del_one_with_empty_db_none(db_memory, table, db_filter):
312 result = db_memory.del_one(table, db_filter, fail_on_empty=False)
tiernob20a9022018-05-22 12:07:05 +0200313 assert result is None
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100314
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100315
tierno1e9a3292018-11-05 18:18:45 +0100316@pytest.mark.parametrize("table, db_filter", [
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100317 ("test", {"_id": 4}),
318 ("test", {"_id": 5}),
319 ("test", {"data": 4}),
320 ("test", {"data": 5}),
321 ("test", {"_id": 1, "data": 2}),
322 ("test", {"_id": 2, "data": 3}),
323 ("test_table", {}),
324 ("test_table", {"_id": 1}),
325 ("test_table", {"_id": 2}),
326 ("test_table", {"data": 1}),
327 ("test_table", {"data": 2}),
328 ("test_table", {"_id": 1, "data": 1}),
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100329 ("test_table", {"_id": 2, "data": 2})])
tierno1e9a3292018-11-05 18:18:45 +0100330def test_del_one_with_non_empty_db_exception(db_memory_with_data, table, db_filter):
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100331 with pytest.raises(DbException) as excinfo:
tierno1e9a3292018-11-05 18:18:45 +0100332 db_memory_with_data.del_one(table, db_filter)
333 assert str(excinfo.value) == (empty_exception_message() + del_one_exception_message(db_filter))
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100334 assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND
335
tiernob20a9022018-05-22 12:07:05 +0200336
tierno1e9a3292018-11-05 18:18:45 +0100337@pytest.mark.parametrize("table, db_filter", [
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100338 ("test", {"_id": 4}),
339 ("test", {"_id": 5}),
340 ("test", {"data": 4}),
341 ("test", {"data": 5}),
342 ("test", {"_id": 1, "data": 2}),
343 ("test", {"_id": 2, "data": 3}),
344 ("test_table", {}),
345 ("test_table", {"_id": 1}),
346 ("test_table", {"_id": 2}),
347 ("test_table", {"data": 1}),
348 ("test_table", {"data": 2}),
349 ("test_table", {"_id": 1, "data": 1}),
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100350 ("test_table", {"_id": 2, "data": 2})])
tierno1e9a3292018-11-05 18:18:45 +0100351def test_del_one_with_non_empty_db_none(db_memory_with_data, table, db_filter):
352 result = db_memory_with_data.del_one(table, db_filter, fail_on_empty=False)
tiernob20a9022018-05-22 12:07:05 +0200353 assert result is None
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100354
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100355
356@pytest.mark.parametrize("fail_on_empty", [
357 (True),
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100358 (False)])
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100359def test_del_one_generic_exception(db_memory_with_data, fail_on_empty):
360 table = 'test'
tierno1e9a3292018-11-05 18:18:45 +0100361 db_filter = {}
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100362 db_memory_with_data._find = MagicMock(side_effect=Exception())
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100363 with pytest.raises(DbException) as excinfo:
tierno1e9a3292018-11-05 18:18:45 +0100364 db_memory_with_data.del_one(table, db_filter, fail_on_empty=fail_on_empty)
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100365 assert str(excinfo.value) == empty_exception_message()
366 assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND
367
tiernob20a9022018-05-22 12:07:05 +0200368
tierno136f2952018-10-19 13:01:03 +0200369@pytest.mark.parametrize("table, _id, indata", [
370 ("test", 1, {"_id": 1, "data": 42}),
371 ("test", 1, {"_id": 1, "data": 42, "kk": 34}),
372 ("test", 1, {"_id": 1}),
373 ("test", 2, {"_id": 2, "data": 42}),
374 ("test", 2, {"_id": 2, "data": 42, "kk": 34}),
375 ("test", 2, {"_id": 2}),
376 ("test", 3, {"_id": 3, "data": 42}),
377 ("test", 3, {"_id": 3, "data": 42, "kk": 34}),
378 ("test", 3, {"_id": 3})])
379def test_replace(db_memory_with_data, table, _id, indata):
380 result = db_memory_with_data.replace(table, _id, indata)
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100381 assert result == {"updated": 1}
382 assert len(db_memory_with_data.db) == 1
383 assert table in db_memory_with_data.db
384 assert len(db_memory_with_data.db[table]) == 3
385 assert indata in db_memory_with_data.db[table]
386
tiernob20a9022018-05-22 12:07:05 +0200387
tierno136f2952018-10-19 13:01:03 +0200388@pytest.mark.parametrize("table, _id, indata", [
389 ("test", 1, {"_id": 1, "data": 42}),
390 ("test", 2, {"_id": 2}),
391 ("test", 3, {"_id": 3})])
392def test_replace_without_data_exception(db_memory, table, _id, indata):
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100393 with pytest.raises(DbException) as excinfo:
tierno136f2952018-10-19 13:01:03 +0200394 db_memory.replace(table, _id, indata, fail_on_empty=True)
395 assert str(excinfo.value) == (replace_exception_message(_id))
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100396 assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND
397
tiernob20a9022018-05-22 12:07:05 +0200398
tierno136f2952018-10-19 13:01:03 +0200399@pytest.mark.parametrize("table, _id, indata", [
400 ("test", 1, {"_id": 1, "data": 42}),
401 ("test", 2, {"_id": 2}),
402 ("test", 3, {"_id": 3})])
403def test_replace_without_data_none(db_memory, table, _id, indata):
404 result = db_memory.replace(table, _id, indata, fail_on_empty=False)
tiernob20a9022018-05-22 12:07:05 +0200405 assert result is None
406
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100407
tierno136f2952018-10-19 13:01:03 +0200408@pytest.mark.parametrize("table, _id, indata", [
409 ("test", 11, {"_id": 11, "data": 42}),
410 ("test", 12, {"_id": 12}),
411 ("test", 33, {"_id": 33})])
412def test_replace_with_data_exception(db_memory_with_data, table, _id, indata):
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100413 with pytest.raises(DbException) as excinfo:
tierno136f2952018-10-19 13:01:03 +0200414 db_memory_with_data.replace(table, _id, indata, fail_on_empty=True)
415 assert str(excinfo.value) == (replace_exception_message(_id))
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100416 assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND
417
tiernob20a9022018-05-22 12:07:05 +0200418
tierno136f2952018-10-19 13:01:03 +0200419@pytest.mark.parametrize("table, _id, indata", [
420 ("test", 11, {"_id": 11, "data": 42}),
421 ("test", 12, {"_id": 12}),
422 ("test", 33, {"_id": 33})])
423def test_replace_with_data_none(db_memory_with_data, table, _id, indata):
424 result = db_memory_with_data.replace(table, _id, indata, fail_on_empty=False)
tiernob20a9022018-05-22 12:07:05 +0200425 assert result is None
426
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100427
428@pytest.mark.parametrize("fail_on_empty", [
tiernob20a9022018-05-22 12:07:05 +0200429 True,
430 False])
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100431def test_replace_generic_exception(db_memory_with_data, fail_on_empty):
432 table = 'test'
tierno136f2952018-10-19 13:01:03 +0200433 _id = {}
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100434 indata = {'_id': 1, 'data': 1}
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100435 db_memory_with_data._find = MagicMock(side_effect=Exception())
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100436 with pytest.raises(DbException) as excinfo:
tierno136f2952018-10-19 13:01:03 +0200437 db_memory_with_data.replace(table, _id, indata, fail_on_empty=fail_on_empty)
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100438 assert str(excinfo.value) == empty_exception_message()
439 assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND
440
tiernob20a9022018-05-22 12:07:05 +0200441
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100442@pytest.mark.parametrize("table, id, data", [
443 ("test", "1", {"data": 1}),
444 ("test", "1", {"data": 2}),
445 ("test", "2", {"data": 1}),
446 ("test", "2", {"data": 2}),
447 ("test_table", "1", {"data": 1}),
448 ("test_table", "1", {"data": 2}),
449 ("test_table", "2", {"data": 1}),
450 ("test_table", "2", {"data": 2}),
451 ("test", "1", {"data_1": 1, "data_2": 2}),
452 ("test", "1", {"data_1": 2, "data_2": 1}),
453 ("test", "2", {"data_1": 1, "data_2": 2}),
454 ("test", "2", {"data_1": 2, "data_2": 1}),
455 ("test_table", "1", {"data_1": 1, "data_2": 2}),
456 ("test_table", "1", {"data_1": 2, "data_2": 1}),
457 ("test_table", "2", {"data_1": 1, "data_2": 2}),
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100458 ("test_table", "2", {"data_1": 2, "data_2": 1})])
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100459def test_create_with_empty_db_with_id(db_memory, table, id, data):
460 data_to_insert = data
461 data_to_insert['_id'] = id
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100462 returned_id = db_memory.create(table, data_to_insert)
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100463 assert returned_id == id
464 assert len(db_memory.db) == 1
465 assert table in db_memory.db
466 assert len(db_memory.db[table]) == 1
467 assert data_to_insert in db_memory.db[table]
468
tiernob20a9022018-05-22 12:07:05 +0200469
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100470@pytest.mark.parametrize("table, id, data", [
471 ("test", "4", {"data": 1}),
472 ("test", "5", {"data": 2}),
473 ("test", "4", {"data": 1}),
474 ("test", "5", {"data": 2}),
475 ("test_table", "4", {"data": 1}),
476 ("test_table", "5", {"data": 2}),
477 ("test_table", "4", {"data": 1}),
478 ("test_table", "5", {"data": 2}),
479 ("test", "4", {"data_1": 1, "data_2": 2}),
480 ("test", "5", {"data_1": 2, "data_2": 1}),
481 ("test", "4", {"data_1": 1, "data_2": 2}),
482 ("test", "5", {"data_1": 2, "data_2": 1}),
483 ("test_table", "4", {"data_1": 1, "data_2": 2}),
484 ("test_table", "5", {"data_1": 2, "data_2": 1}),
485 ("test_table", "4", {"data_1": 1, "data_2": 2}),
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100486 ("test_table", "5", {"data_1": 2, "data_2": 1})])
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100487def test_create_with_non_empty_db_with_id(db_memory_with_data, table, id, data):
488 data_to_insert = data
489 data_to_insert['_id'] = id
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100490 returned_id = db_memory_with_data.create(table, data_to_insert)
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100491 assert returned_id == id
492 assert len(db_memory_with_data.db) == (1 if table == 'test' else 2)
493 assert table in db_memory_with_data.db
494 assert len(db_memory_with_data.db[table]) == (4 if table == 'test' else 1)
495 assert data_to_insert in db_memory_with_data.db[table]
496
tiernob20a9022018-05-22 12:07:05 +0200497
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100498@pytest.mark.parametrize("table, data", [
499 ("test", {"data": 1}),
500 ("test", {"data": 2}),
501 ("test", {"data": 1}),
502 ("test", {"data": 2}),
503 ("test_table", {"data": 1}),
504 ("test_table", {"data": 2}),
505 ("test_table", {"data": 1}),
506 ("test_table", {"data": 2}),
507 ("test", {"data_1": 1, "data_2": 2}),
508 ("test", {"data_1": 2, "data_2": 1}),
509 ("test", {"data_1": 1, "data_2": 2}),
510 ("test", {"data_1": 2, "data_2": 1}),
511 ("test_table", {"data_1": 1, "data_2": 2}),
512 ("test_table", {"data_1": 2, "data_2": 1}),
513 ("test_table", {"data_1": 1, "data_2": 2}),
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100514 ("test_table", {"data_1": 2, "data_2": 1})])
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100515def test_create_with_empty_db_without_id(db_memory, table, data):
516 returned_id = db_memory.create(table, data)
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100517 assert len(db_memory.db) == 1
518 assert table in db_memory.db
519 assert len(db_memory.db[table]) == 1
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100520 data_inserted = data
521 data_inserted['_id'] = returned_id
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100522 assert data_inserted in db_memory.db[table]
523
tiernob20a9022018-05-22 12:07:05 +0200524
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100525@pytest.mark.parametrize("table, data", [
526 ("test", {"data": 1}),
527 ("test", {"data": 2}),
528 ("test", {"data": 1}),
529 ("test", {"data": 2}),
530 ("test_table", {"data": 1}),
531 ("test_table", {"data": 2}),
532 ("test_table", {"data": 1}),
533 ("test_table", {"data": 2}),
534 ("test", {"data_1": 1, "data_2": 2}),
535 ("test", {"data_1": 2, "data_2": 1}),
536 ("test", {"data_1": 1, "data_2": 2}),
537 ("test", {"data_1": 2, "data_2": 1}),
538 ("test_table", {"data_1": 1, "data_2": 2}),
539 ("test_table", {"data_1": 2, "data_2": 1}),
540 ("test_table", {"data_1": 1, "data_2": 2}),
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100541 ("test_table", {"data_1": 2, "data_2": 1})])
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100542def test_create_with_non_empty_db_without_id(db_memory_with_data, table, data):
543 returned_id = db_memory_with_data.create(table, data)
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100544 assert len(db_memory_with_data.db) == (1 if table == 'test' else 2)
545 assert table in db_memory_with_data.db
546 assert len(db_memory_with_data.db[table]) == (4 if table == 'test' else 1)
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100547 data_inserted = data
548 data_inserted['_id'] = returned_id
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100549 assert data_inserted in db_memory_with_data.db[table]
550
tiernob20a9022018-05-22 12:07:05 +0200551
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100552def test_create_with_exception(db_memory):
553 table = "test"
554 data = {"_id": 1, "data": 1}
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100555 db_memory.db = MagicMock()
556 db_memory.db.__contains__.side_effect = Exception()
Eduardo Sousa0cb1b3c2018-04-26 00:36:45 +0100557 with pytest.raises(DbException) as excinfo:
558 db_memory.create(table, data)
559 assert str(excinfo.value) == empty_exception_message()
560 assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND