| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 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 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 11 | |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 12 | @pytest.fixture |
| 13 | def db_memory(): |
| 14 | db = DbMemory() |
| 15 | return db |
| 16 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 17 | |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 18 | @pytest.fixture |
| 19 | def db_memory_with_data(): |
| 20 | db = DbMemory() |
| 21 | |
| 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 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 28 | |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 29 | def empty_exception_message(): |
| 30 | return 'database exception ' |
| 31 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 32 | |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 33 | def get_one_exception_message(filter): |
| 34 | return "database exception Not found entry with filter='{}'".format(filter) |
| 35 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 36 | |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 37 | def get_one_multiple_exception_message(filter): |
| 38 | return "database exception Found more than one entry with filter='{}'".format(filter) |
| 39 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 40 | |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 41 | def del_one_exception_message(filter): |
| 42 | return "database exception Not found entry with filter='{}'".format(filter) |
| 43 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 44 | |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 45 | def replace_exception_message(filter): |
| 46 | return "database exception Not found entry with filter='{}'".format(filter) |
| 47 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 48 | |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 49 | def test_constructor(): |
| 50 | db = DbMemory() |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 51 | assert db.logger == logging.getLogger('db') |
| 52 | assert len(db.db) == 0 |
| 53 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 54 | |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 55 | def test_constructor_with_logger(): |
| 56 | logger_name = 'db_local' |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 57 | db = DbMemory(logger_name=logger_name) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 58 | assert db.logger == logging.getLogger(logger_name) |
| 59 | assert len(db.db) == 0 |
| 60 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 61 | |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 62 | def test_db_connect(): |
| 63 | logger_name = 'db_local' |
| 64 | config = {'logger_name': logger_name} |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 65 | db = DbMemory() |
| 66 | db.db_connect(config) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 67 | assert db.logger == logging.getLogger(logger_name) |
| 68 | assert len(db.db) == 0 |
| 69 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 70 | |
| Eduardo Sousa | 5ffda64 | 2018-05-09 19:42:00 +0100 | [diff] [blame] | 71 | def test_db_disconnect(db_memory): |
| 72 | db_memory.db_disconnect() |
| 73 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 74 | |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 75 | @pytest.mark.parametrize("table, filter", [ |
| 76 | ("test", {}), |
| 77 | ("test", {"_id": 1}), |
| 78 | ("test", {"data": 1}), |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 79 | ("test", {"_id": 1, "data": 1})]) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 80 | def test_get_list_with_empty_db(db_memory, table, filter): |
| 81 | result = db_memory.get_list(table, filter) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 82 | assert len(result) == 0 |
| 83 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 84 | |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 85 | @pytest.mark.parametrize("table, filter, expected_data", [ |
| 86 | ("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 Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 99 | ("test_table", {"_id": 1, "data": 1}, [])]) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 100 | def test_get_list_with_non_empty_db(db_memory_with_data, table, filter, expected_data): |
| 101 | result = db_memory_with_data.get_list(table, filter) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 102 | assert len(result) == len(expected_data) |
| 103 | for data in expected_data: |
| 104 | assert data in result |
| 105 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 106 | |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 107 | def test_get_list_exception(db_memory_with_data): |
| 108 | table = 'test' |
| 109 | filter = {} |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 110 | db_memory_with_data._find = MagicMock(side_effect=Exception()) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 111 | with pytest.raises(DbException) as excinfo: |
| 112 | db_memory_with_data.get_list(table, filter) |
| 113 | assert str(excinfo.value) == empty_exception_message() |
| 114 | assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND |
| 115 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 116 | |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 117 | @pytest.mark.parametrize("table, filter, expected_data", [ |
| 118 | ("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 Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 126 | ("test", {"_id": 3, "data": 3}, {"_id": 3, "data": 3})]) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 127 | def test_get_one(db_memory_with_data, table, filter, expected_data): |
| 128 | result = db_memory_with_data.get_one(table, filter) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 129 | 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 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 135 | |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 136 | @pytest.mark.parametrize("table, filter, expected_data", [ |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 137 | ("test", {}, {"_id": 1, "data": 1})]) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 138 | def test_get_one_with_multiple_results(db_memory_with_data, table, filter, expected_data): |
| 139 | result = db_memory_with_data.get_one(table, filter, fail_on_more=False) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 140 | 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 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 146 | |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 147 | def test_get_one_with_multiple_results_exception(db_memory_with_data): |
| 148 | table = "test" |
| 149 | filter = {} |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 150 | with pytest.raises(DbException) as excinfo: |
| 151 | db_memory_with_data.get_one(table, filter) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 152 | assert str(excinfo.value) == (empty_exception_message() + get_one_multiple_exception_message(filter)) |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 153 | # assert excinfo.value.http_code == http.HTTPStatus.CONFLICT |
| 154 | |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 155 | |
| 156 | @pytest.mark.parametrize("table, filter", [ |
| 157 | ("test", {"_id": 4}), |
| 158 | ("test", {"data": 4}), |
| 159 | ("test", {"_id": 4, "data": 4}), |
| 160 | ("test_table", {"_id": 4}), |
| 161 | ("test_table", {"data": 4}), |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 162 | ("test_table", {"_id": 4, "data": 4})]) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 163 | def test_get_one_with_non_empty_db_exception(db_memory_with_data, table, filter): |
| 164 | with pytest.raises(DbException) as excinfo: |
| 165 | db_memory_with_data.get_one(table, filter) |
| 166 | assert str(excinfo.value) == (empty_exception_message() + get_one_exception_message(filter)) |
| 167 | assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND |
| 168 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 169 | |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 170 | @pytest.mark.parametrize("table, filter", [ |
| 171 | ("test", {"_id": 4}), |
| 172 | ("test", {"data": 4}), |
| 173 | ("test", {"_id": 4, "data": 4}), |
| 174 | ("test_table", {"_id": 4}), |
| 175 | ("test_table", {"data": 4}), |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 176 | ("test_table", {"_id": 4, "data": 4})]) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 177 | def test_get_one_with_non_empty_db_none(db_memory_with_data, table, filter): |
| 178 | result = db_memory_with_data.get_one(table, filter, fail_on_empty=False) |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 179 | assert result is None |
| 180 | |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 181 | |
| 182 | @pytest.mark.parametrize("table, filter", [ |
| 183 | ("test", {"_id": 4}), |
| 184 | ("test", {"data": 4}), |
| 185 | ("test", {"_id": 4, "data": 4}), |
| 186 | ("test_table", {"_id": 4}), |
| 187 | ("test_table", {"data": 4}), |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 188 | ("test_table", {"_id": 4, "data": 4})]) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 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 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 195 | |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 196 | @pytest.mark.parametrize("table, filter", [ |
| 197 | ("test", {"_id": 4}), |
| 198 | ("test", {"data": 4}), |
| 199 | ("test", {"_id": 4, "data": 4}), |
| 200 | ("test_table", {"_id": 4}), |
| 201 | ("test_table", {"data": 4}), |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 202 | ("test_table", {"_id": 4, "data": 4})]) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 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) |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 205 | assert result is None |
| 206 | |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 207 | |
| 208 | def test_get_one_generic_exception(db_memory_with_data): |
| 209 | table = 'test' |
| 210 | filter = {} |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 211 | db_memory_with_data._find = MagicMock(side_effect=Exception()) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 212 | with pytest.raises(DbException) as excinfo: |
| 213 | db_memory_with_data.get_one(table, filter) |
| 214 | assert str(excinfo.value) == empty_exception_message() |
| 215 | assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND |
| 216 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 217 | |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 218 | @pytest.mark.parametrize("table, filter, expected_data", [ |
| Eduardo Sousa | 857731b | 2018-04-26 15:55:05 +0100 | [diff] [blame] | 219 | ("test", {}, []), |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 220 | ("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 Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 223 | ("test", {"_id": 2, "data": 2}, [{"_id": 1, "data": 1}, {"_id": 3, "data": 3}])]) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 224 | def test_del_list_with_non_empty_db(db_memory_with_data, table, filter, expected_data): |
| 225 | result = db_memory_with_data.del_list(table, filter) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 226 | 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 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 233 | |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 234 | @pytest.mark.parametrize("table, filter", [ |
| 235 | ("test", {}), |
| 236 | ("test", {"_id": 1}), |
| 237 | ("test", {"_id": 2}), |
| 238 | ("test", {"data": 1}), |
| 239 | ("test", {"data": 2}), |
| 240 | ("test", {"_id": 1, "data": 1}), |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 241 | ("test", {"_id": 2, "data": 2})]) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 242 | def test_del_list_with_empty_db(db_memory, table, filter): |
| 243 | result = db_memory.del_list(table, filter) |
| 244 | assert result['deleted'] == 0 |
| 245 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 246 | |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 247 | def test_del_list_generic_exception(db_memory_with_data): |
| 248 | table = 'test' |
| 249 | filter = {} |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 250 | db_memory_with_data._find = MagicMock(side_effect=Exception()) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 251 | with pytest.raises(DbException) as excinfo: |
| 252 | db_memory_with_data.del_list(table, filter) |
| 253 | assert str(excinfo.value) == empty_exception_message() |
| 254 | assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND |
| 255 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 256 | |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 257 | @pytest.mark.parametrize("table, filter, data", [ |
| 258 | ("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 Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 264 | ("test", {"_id": 2, "data": 2}, {"_id": 2, "data": 2})]) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 265 | def test_del_one(db_memory_with_data, table, filter, data): |
| 266 | result = db_memory_with_data.del_one(table, filter) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 267 | 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 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 273 | |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 274 | @pytest.mark.parametrize("table, filter", [ |
| 275 | ("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 Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 288 | ("test_table", {"_id": 2, "data": 2})]) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 289 | def test_del_one_with_empty_db_exception(db_memory, table, filter): |
| 290 | with pytest.raises(DbException) as excinfo: |
| 291 | db_memory.del_one(table, filter) |
| 292 | assert str(excinfo.value) == (empty_exception_message() + del_one_exception_message(filter)) |
| 293 | assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND |
| 294 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 295 | |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 296 | @pytest.mark.parametrize("table, filter", [ |
| 297 | ("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 Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 310 | ("test_table", {"_id": 2, "data": 2})]) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 311 | def test_del_one_with_empty_db_none(db_memory, table, filter): |
| 312 | result = db_memory.del_one(table, filter, fail_on_empty=False) |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 313 | assert result is None |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 314 | |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 315 | |
| 316 | @pytest.mark.parametrize("table, filter", [ |
| 317 | ("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 Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 329 | ("test_table", {"_id": 2, "data": 2})]) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 330 | def test_del_one_with_non_empty_db_exception(db_memory_with_data, table, filter): |
| 331 | with pytest.raises(DbException) as excinfo: |
| 332 | db_memory_with_data.del_one(table, filter) |
| 333 | assert str(excinfo.value) == (empty_exception_message() + del_one_exception_message(filter)) |
| 334 | assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND |
| 335 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 336 | |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 337 | @pytest.mark.parametrize("table, filter", [ |
| 338 | ("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 Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 350 | ("test_table", {"_id": 2, "data": 2})]) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 351 | def test_del_one_with_non_empty_db_none(db_memory_with_data, table, filter): |
| 352 | result = db_memory_with_data.del_one(table, filter, fail_on_empty=False) |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 353 | assert result is None |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 354 | |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 355 | |
| 356 | @pytest.mark.parametrize("fail_on_empty", [ |
| 357 | (True), |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 358 | (False)]) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 359 | def test_del_one_generic_exception(db_memory_with_data, fail_on_empty): |
| 360 | table = 'test' |
| 361 | filter = {} |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 362 | db_memory_with_data._find = MagicMock(side_effect=Exception()) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 363 | with pytest.raises(DbException) as excinfo: |
| 364 | db_memory_with_data.del_one(table, filter, fail_on_empty=fail_on_empty) |
| 365 | assert str(excinfo.value) == empty_exception_message() |
| 366 | assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND |
| 367 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 368 | |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 369 | @pytest.mark.parametrize("table, filter, indata", [ |
| 370 | ("test", {}, {"_id": 1, "data": 42}), |
| 371 | ("test", {}, {"_id": 3, "data": 42}), |
| 372 | ("test", {"_id": 1}, {"_id": 3, "data": 42}), |
| 373 | ("test", {"_id": 3}, {"_id": 3, "data": 42}), |
| 374 | ("test", {"data": 1}, {"_id": 3, "data": 42}), |
| 375 | ("test", {"data": 3}, {"_id": 3, "data": 42}), |
| 376 | ("test", {"_id": 1, "data": 1}, {"_id": 3, "data": 42}), |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 377 | ("test", {"_id": 3, "data": 3}, {"_id": 3, "data": 42})]) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 378 | def test_replace(db_memory_with_data, table, filter, indata): |
| 379 | result = db_memory_with_data.replace(table, filter, indata) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 380 | assert result == {"updated": 1} |
| 381 | assert len(db_memory_with_data.db) == 1 |
| 382 | assert table in db_memory_with_data.db |
| 383 | assert len(db_memory_with_data.db[table]) == 3 |
| 384 | assert indata in db_memory_with_data.db[table] |
| 385 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 386 | |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 387 | @pytest.mark.parametrize("table, filter, indata", [ |
| 388 | ("test", {}, {'_id': 1, 'data': 1}), |
| 389 | ("test", {}, {'_id': 2, 'data': 1}), |
| 390 | ("test", {}, {'_id': 1, 'data': 2}), |
| 391 | ("test", {'_id': 1}, {'_id': 1, 'data': 1}), |
| 392 | ("test", {'_id': 1, 'data': 1}, {'_id': 1, 'data': 1}), |
| 393 | ("test_table", {}, {'_id': 1, 'data': 1}), |
| 394 | ("test_table", {}, {'_id': 2, 'data': 1}), |
| 395 | ("test_table", {}, {'_id': 1, 'data': 2}), |
| 396 | ("test_table", {'_id': 1}, {'_id': 1, 'data': 1}), |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 397 | ("test_table", {'_id': 1, 'data': 1}, {'_id': 1, 'data': 1})]) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 398 | def test_replace_without_data_exception(db_memory, table, filter, indata): |
| 399 | with pytest.raises(DbException) as excinfo: |
| 400 | db_memory.replace(table, filter, indata, fail_on_empty=True) |
| 401 | assert str(excinfo.value) == (empty_exception_message() + replace_exception_message(filter)) |
| 402 | assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND |
| 403 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 404 | |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 405 | @pytest.mark.parametrize("table, filter, indata", [ |
| 406 | ("test", {}, {'_id': 1, 'data': 1}), |
| 407 | ("test", {}, {'_id': 2, 'data': 1}), |
| 408 | ("test", {}, {'_id': 1, 'data': 2}), |
| 409 | ("test", {'_id': 1}, {'_id': 1, 'data': 1}), |
| 410 | ("test", {'_id': 1, 'data': 1}, {'_id': 1, 'data': 1}), |
| 411 | ("test_table", {}, {'_id': 1, 'data': 1}), |
| 412 | ("test_table", {}, {'_id': 2, 'data': 1}), |
| 413 | ("test_table", {}, {'_id': 1, 'data': 2}), |
| 414 | ("test_table", {'_id': 1}, {'_id': 1, 'data': 1}), |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 415 | ("test_table", {'_id': 1, 'data': 1}, {'_id': 1, 'data': 1})]) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 416 | def test_replace_without_data_none(db_memory, table, filter, indata): |
| 417 | result = db_memory.replace(table, filter, indata, fail_on_empty=False) |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 418 | assert result is None |
| 419 | |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 420 | |
| 421 | @pytest.mark.parametrize("table, filter, indata", [ |
| 422 | ("test_table", {}, {'_id': 1, 'data': 1}), |
| 423 | ("test_table", {}, {'_id': 2, 'data': 1}), |
| 424 | ("test_table", {}, {'_id': 1, 'data': 2}), |
| 425 | ("test_table", {'_id': 1}, {'_id': 1, 'data': 1}), |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 426 | ("test_table", {'_id': 1, 'data': 1}, {'_id': 1, 'data': 1})]) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 427 | def test_replace_with_data_exception(db_memory_with_data, table, filter, indata): |
| 428 | with pytest.raises(DbException) as excinfo: |
| 429 | db_memory_with_data.replace(table, filter, indata, fail_on_empty=True) |
| 430 | assert str(excinfo.value) == (empty_exception_message() + replace_exception_message(filter)) |
| 431 | assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND |
| 432 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 433 | |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 434 | @pytest.mark.parametrize("table, filter, indata", [ |
| 435 | ("test_table", {}, {'_id': 1, 'data': 1}), |
| 436 | ("test_table", {}, {'_id': 2, 'data': 1}), |
| 437 | ("test_table", {}, {'_id': 1, 'data': 2}), |
| 438 | ("test_table", {'_id': 1}, {'_id': 1, 'data': 1}), |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 439 | ("test_table", {'_id': 1, 'data': 1}, {'_id': 1, 'data': 1})]) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 440 | def test_replace_with_data_none(db_memory_with_data, table, filter, indata): |
| 441 | result = db_memory_with_data.replace(table, filter, indata, fail_on_empty=False) |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 442 | assert result is None |
| 443 | |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 444 | |
| 445 | @pytest.mark.parametrize("fail_on_empty", [ |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 446 | True, |
| 447 | False]) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 448 | def test_replace_generic_exception(db_memory_with_data, fail_on_empty): |
| 449 | table = 'test' |
| 450 | filter = {} |
| 451 | indata = {'_id': 1, 'data': 1} |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 452 | db_memory_with_data._find = MagicMock(side_effect=Exception()) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 453 | with pytest.raises(DbException) as excinfo: |
| 454 | db_memory_with_data.replace(table, filter, indata, fail_on_empty=fail_on_empty) |
| 455 | assert str(excinfo.value) == empty_exception_message() |
| 456 | assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND |
| 457 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 458 | |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 459 | @pytest.mark.parametrize("table, id, data", [ |
| 460 | ("test", "1", {"data": 1}), |
| 461 | ("test", "1", {"data": 2}), |
| 462 | ("test", "2", {"data": 1}), |
| 463 | ("test", "2", {"data": 2}), |
| 464 | ("test_table", "1", {"data": 1}), |
| 465 | ("test_table", "1", {"data": 2}), |
| 466 | ("test_table", "2", {"data": 1}), |
| 467 | ("test_table", "2", {"data": 2}), |
| 468 | ("test", "1", {"data_1": 1, "data_2": 2}), |
| 469 | ("test", "1", {"data_1": 2, "data_2": 1}), |
| 470 | ("test", "2", {"data_1": 1, "data_2": 2}), |
| 471 | ("test", "2", {"data_1": 2, "data_2": 1}), |
| 472 | ("test_table", "1", {"data_1": 1, "data_2": 2}), |
| 473 | ("test_table", "1", {"data_1": 2, "data_2": 1}), |
| 474 | ("test_table", "2", {"data_1": 1, "data_2": 2}), |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 475 | ("test_table", "2", {"data_1": 2, "data_2": 1})]) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 476 | def test_create_with_empty_db_with_id(db_memory, table, id, data): |
| 477 | data_to_insert = data |
| 478 | data_to_insert['_id'] = id |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 479 | returned_id = db_memory.create(table, data_to_insert) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 480 | assert returned_id == id |
| 481 | assert len(db_memory.db) == 1 |
| 482 | assert table in db_memory.db |
| 483 | assert len(db_memory.db[table]) == 1 |
| 484 | assert data_to_insert in db_memory.db[table] |
| 485 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 486 | |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 487 | @pytest.mark.parametrize("table, id, data", [ |
| 488 | ("test", "4", {"data": 1}), |
| 489 | ("test", "5", {"data": 2}), |
| 490 | ("test", "4", {"data": 1}), |
| 491 | ("test", "5", {"data": 2}), |
| 492 | ("test_table", "4", {"data": 1}), |
| 493 | ("test_table", "5", {"data": 2}), |
| 494 | ("test_table", "4", {"data": 1}), |
| 495 | ("test_table", "5", {"data": 2}), |
| 496 | ("test", "4", {"data_1": 1, "data_2": 2}), |
| 497 | ("test", "5", {"data_1": 2, "data_2": 1}), |
| 498 | ("test", "4", {"data_1": 1, "data_2": 2}), |
| 499 | ("test", "5", {"data_1": 2, "data_2": 1}), |
| 500 | ("test_table", "4", {"data_1": 1, "data_2": 2}), |
| 501 | ("test_table", "5", {"data_1": 2, "data_2": 1}), |
| 502 | ("test_table", "4", {"data_1": 1, "data_2": 2}), |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 503 | ("test_table", "5", {"data_1": 2, "data_2": 1})]) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 504 | def test_create_with_non_empty_db_with_id(db_memory_with_data, table, id, data): |
| 505 | data_to_insert = data |
| 506 | data_to_insert['_id'] = id |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 507 | returned_id = db_memory_with_data.create(table, data_to_insert) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 508 | assert returned_id == id |
| 509 | assert len(db_memory_with_data.db) == (1 if table == 'test' else 2) |
| 510 | assert table in db_memory_with_data.db |
| 511 | assert len(db_memory_with_data.db[table]) == (4 if table == 'test' else 1) |
| 512 | assert data_to_insert in db_memory_with_data.db[table] |
| 513 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 514 | |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 515 | @pytest.mark.parametrize("table, data", [ |
| 516 | ("test", {"data": 1}), |
| 517 | ("test", {"data": 2}), |
| 518 | ("test", {"data": 1}), |
| 519 | ("test", {"data": 2}), |
| 520 | ("test_table", {"data": 1}), |
| 521 | ("test_table", {"data": 2}), |
| 522 | ("test_table", {"data": 1}), |
| 523 | ("test_table", {"data": 2}), |
| 524 | ("test", {"data_1": 1, "data_2": 2}), |
| 525 | ("test", {"data_1": 2, "data_2": 1}), |
| 526 | ("test", {"data_1": 1, "data_2": 2}), |
| 527 | ("test", {"data_1": 2, "data_2": 1}), |
| 528 | ("test_table", {"data_1": 1, "data_2": 2}), |
| 529 | ("test_table", {"data_1": 2, "data_2": 1}), |
| 530 | ("test_table", {"data_1": 1, "data_2": 2}), |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 531 | ("test_table", {"data_1": 2, "data_2": 1})]) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 532 | def test_create_with_empty_db_without_id(db_memory, table, data): |
| 533 | returned_id = db_memory.create(table, data) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 534 | assert len(db_memory.db) == 1 |
| 535 | assert table in db_memory.db |
| 536 | assert len(db_memory.db[table]) == 1 |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 537 | data_inserted = data |
| 538 | data_inserted['_id'] = returned_id |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 539 | assert data_inserted in db_memory.db[table] |
| 540 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 541 | |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 542 | @pytest.mark.parametrize("table, data", [ |
| 543 | ("test", {"data": 1}), |
| 544 | ("test", {"data": 2}), |
| 545 | ("test", {"data": 1}), |
| 546 | ("test", {"data": 2}), |
| 547 | ("test_table", {"data": 1}), |
| 548 | ("test_table", {"data": 2}), |
| 549 | ("test_table", {"data": 1}), |
| 550 | ("test_table", {"data": 2}), |
| 551 | ("test", {"data_1": 1, "data_2": 2}), |
| 552 | ("test", {"data_1": 2, "data_2": 1}), |
| 553 | ("test", {"data_1": 1, "data_2": 2}), |
| 554 | ("test", {"data_1": 2, "data_2": 1}), |
| 555 | ("test_table", {"data_1": 1, "data_2": 2}), |
| 556 | ("test_table", {"data_1": 2, "data_2": 1}), |
| 557 | ("test_table", {"data_1": 1, "data_2": 2}), |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 558 | ("test_table", {"data_1": 2, "data_2": 1})]) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 559 | def test_create_with_non_empty_db_without_id(db_memory_with_data, table, data): |
| 560 | returned_id = db_memory_with_data.create(table, data) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 561 | assert len(db_memory_with_data.db) == (1 if table == 'test' else 2) |
| 562 | assert table in db_memory_with_data.db |
| 563 | assert len(db_memory_with_data.db[table]) == (4 if table == 'test' else 1) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 564 | data_inserted = data |
| 565 | data_inserted['_id'] = returned_id |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 566 | assert data_inserted in db_memory_with_data.db[table] |
| 567 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 568 | |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 569 | def test_create_with_exception(db_memory): |
| 570 | table = "test" |
| 571 | data = {"_id": 1, "data": 1} |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 572 | db_memory.db = MagicMock() |
| 573 | db_memory.db.__contains__.side_effect = Exception() |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 574 | with pytest.raises(DbException) as excinfo: |
| 575 | db_memory.create(table, data) |
| 576 | assert str(excinfo.value) == empty_exception_message() |
| 577 | assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND |