| Eduardo Sousa | a011781 | 2019-02-05 15:57:09 +0000 | [diff] [blame] | 1 | # Copyright 2018 Whitestack, LLC |
| 2 | # Copyright 2018 Telefonica S.A. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 | # not use this file except in compliance with the License. You may obtain |
| 6 | # a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | # License for the specific language governing permissions and limitations |
| 14 | # under the License. |
| 15 | # |
| 16 | # For those usages not covered by the Apache License, Version 2.0 please |
| 17 | # contact: esousa@whitestack.com or alfonso.tiernosepulveda@telefonica.com |
| 18 | ## |
| 19 | |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 20 | import http |
| 21 | import logging |
| 22 | import pytest |
| tierno | 6472e2b | 2019-09-02 16:04:16 +0000 | [diff] [blame] | 23 | import unittest |
| 24 | from unittest.mock import Mock |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 25 | |
| 26 | from unittest.mock import MagicMock |
| 27 | from osm_common.dbbase import DbException |
| 28 | from osm_common.dbmemory import DbMemory |
| tierno | 7fc50dd | 2020-02-17 12:01:38 +0000 | [diff] [blame] | 29 | from copy import deepcopy |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 30 | |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 31 | __author__ = "Eduardo Sousa <eduardosousa@av.it.pt>" |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 32 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 33 | |
| tierno | 1e9a329 | 2018-11-05 18:18:45 +0100 | [diff] [blame] | 34 | @pytest.fixture(scope="function", params=[True, False]) |
| 35 | def db_memory(request): |
| 36 | db = DbMemory(lock=request.param) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 37 | return db |
| 38 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 39 | |
| tierno | 1e9a329 | 2018-11-05 18:18:45 +0100 | [diff] [blame] | 40 | @pytest.fixture(scope="function", params=[True, False]) |
| 41 | def db_memory_with_data(request): |
| 42 | db = DbMemory(lock=request.param) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 43 | |
| 44 | db.create("test", {"_id": 1, "data": 1}) |
| 45 | db.create("test", {"_id": 2, "data": 2}) |
| 46 | db.create("test", {"_id": 3, "data": 3}) |
| 47 | |
| 48 | return db |
| 49 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 50 | |
| tierno | 6472e2b | 2019-09-02 16:04:16 +0000 | [diff] [blame] | 51 | @pytest.fixture(scope="function") |
| 52 | def db_memory_with_many_data(request): |
| 53 | db = DbMemory(lock=False) |
| 54 | |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 55 | db.create_list( |
| 56 | "test", |
| 57 | [ |
| 58 | { |
| 59 | "_id": 1, |
| 60 | "data": {"data2": {"data3": 1}}, |
| 61 | "list": [{"a": 1}], |
| 62 | "text": "sometext", |
| 63 | }, |
| 64 | { |
| 65 | "_id": 2, |
| 66 | "data": {"data2": {"data3": 2}}, |
| 67 | "list": [{"a": 2}], |
| 68 | "list2": [1, 2, 3], |
| 69 | }, |
| 70 | {"_id": 3, "data": {"data2": {"data3": 3}}, "list": [{"a": 3}]}, |
| 71 | {"_id": 4, "data": {"data2": {"data3": 4}}, "list": [{"a": 4}, {"a": 0}]}, |
| 72 | {"_id": 5, "data": {"data2": {"data3": 5}}, "list": [{"a": 5}]}, |
| 73 | {"_id": 6, "data": {"data2": {"data3": 6}}, "list": [{"0": {"a": 1}}]}, |
| 74 | {"_id": 7, "data": {"data2": {"data3": 7}}, "0": {"a": 0}}, |
| 75 | { |
| 76 | "_id": 8, |
| 77 | "list": [ |
| 78 | {"a": 3, "b": 0, "c": [{"a": 3, "b": 1}, {"a": 0, "b": "v"}]}, |
| 79 | {"a": 0, "b": 1}, |
| 80 | ], |
| 81 | }, |
| 82 | ], |
| 83 | ) |
| tierno | 6472e2b | 2019-09-02 16:04:16 +0000 | [diff] [blame] | 84 | return db |
| 85 | |
| 86 | |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 87 | def empty_exception_message(): |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 88 | return "database exception " |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 89 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 90 | |
| tierno | 1e9a329 | 2018-11-05 18:18:45 +0100 | [diff] [blame] | 91 | def get_one_exception_message(db_filter): |
| 92 | return "database exception Not found entry with filter='{}'".format(db_filter) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 93 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 94 | |
| tierno | 1e9a329 | 2018-11-05 18:18:45 +0100 | [diff] [blame] | 95 | def get_one_multiple_exception_message(db_filter): |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 96 | return "database exception Found more than one entry with filter='{}'".format( |
| 97 | db_filter |
| 98 | ) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 99 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 100 | |
| tierno | 1e9a329 | 2018-11-05 18:18:45 +0100 | [diff] [blame] | 101 | def del_one_exception_message(db_filter): |
| 102 | return "database exception Not found entry with filter='{}'".format(db_filter) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 103 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 104 | |
| tierno | 136f295 | 2018-10-19 13:01:03 +0200 | [diff] [blame] | 105 | def replace_exception_message(value): |
| 106 | return "database exception Not found entry with _id='{}'".format(value) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 107 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 108 | |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 109 | def test_constructor(): |
| 110 | db = DbMemory() |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 111 | assert db.logger == logging.getLogger("db") |
| tierno | 6472e2b | 2019-09-02 16:04:16 +0000 | [diff] [blame] | 112 | assert db.db == {} |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 113 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 114 | |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 115 | def test_constructor_with_logger(): |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 116 | logger_name = "db_local" |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 117 | db = DbMemory(logger_name=logger_name) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 118 | assert db.logger == logging.getLogger(logger_name) |
| tierno | 6472e2b | 2019-09-02 16:04:16 +0000 | [diff] [blame] | 119 | assert db.db == {} |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 120 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 121 | |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 122 | def test_db_connect(): |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 123 | logger_name = "db_local" |
| 124 | config = {"logger_name": logger_name} |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 125 | db = DbMemory() |
| 126 | db.db_connect(config) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 127 | assert db.logger == logging.getLogger(logger_name) |
| tierno | 6472e2b | 2019-09-02 16:04:16 +0000 | [diff] [blame] | 128 | assert db.db == {} |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 129 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 130 | |
| Eduardo Sousa | 5ffda64 | 2018-05-09 19:42:00 +0100 | [diff] [blame] | 131 | def test_db_disconnect(db_memory): |
| 132 | db_memory.db_disconnect() |
| 133 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 134 | |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 135 | @pytest.mark.parametrize( |
| 136 | "table, db_filter", |
| 137 | [ |
| 138 | ("test", {}), |
| 139 | ("test", {"_id": 1}), |
| 140 | ("test", {"data": 1}), |
| 141 | ("test", {"_id": 1, "data": 1}), |
| 142 | ], |
| 143 | ) |
| tierno | 1e9a329 | 2018-11-05 18:18:45 +0100 | [diff] [blame] | 144 | def test_get_list_with_empty_db(db_memory, table, db_filter): |
| 145 | result = db_memory.get_list(table, db_filter) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 146 | assert len(result) == 0 |
| 147 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 148 | |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 149 | @pytest.mark.parametrize( |
| 150 | "table, db_filter, expected_data", |
| 151 | [ |
| 152 | ( |
| 153 | "test", |
| 154 | {}, |
| 155 | [{"_id": 1, "data": 1}, {"_id": 2, "data": 2}, {"_id": 3, "data": 3}], |
| 156 | ), |
| 157 | ("test", {"_id": 1}, [{"_id": 1, "data": 1}]), |
| 158 | ("test", {"data": 1}, [{"_id": 1, "data": 1}]), |
| 159 | ("test", {"_id": 1, "data": 1}, [{"_id": 1, "data": 1}]), |
| 160 | ("test", {"_id": 2}, [{"_id": 2, "data": 2}]), |
| 161 | ("test", {"data": 2}, [{"_id": 2, "data": 2}]), |
| 162 | ("test", {"_id": 2, "data": 2}, [{"_id": 2, "data": 2}]), |
| 163 | ("test", {"_id": 4}, []), |
| 164 | ("test", {"data": 4}, []), |
| 165 | ("test", {"_id": 4, "data": 4}, []), |
| 166 | ("test_table", {}, []), |
| 167 | ("test_table", {"_id": 1}, []), |
| 168 | ("test_table", {"data": 1}, []), |
| 169 | ("test_table", {"_id": 1, "data": 1}, []), |
| 170 | ], |
| 171 | ) |
| 172 | def test_get_list_with_non_empty_db( |
| 173 | db_memory_with_data, table, db_filter, expected_data |
| 174 | ): |
| tierno | 1e9a329 | 2018-11-05 18:18:45 +0100 | [diff] [blame] | 175 | result = db_memory_with_data.get_list(table, db_filter) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 176 | assert len(result) == len(expected_data) |
| 177 | for data in expected_data: |
| 178 | assert data in result |
| 179 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 180 | |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 181 | def test_get_list_exception(db_memory_with_data): |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 182 | table = "test" |
| tierno | 1e9a329 | 2018-11-05 18:18:45 +0100 | [diff] [blame] | 183 | db_filter = {} |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 184 | db_memory_with_data._find = MagicMock(side_effect=Exception()) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 185 | with pytest.raises(DbException) as excinfo: |
| tierno | 1e9a329 | 2018-11-05 18:18:45 +0100 | [diff] [blame] | 186 | db_memory_with_data.get_list(table, db_filter) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 187 | assert str(excinfo.value) == empty_exception_message() |
| 188 | assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND |
| 189 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 190 | |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 191 | @pytest.mark.parametrize( |
| 192 | "table, db_filter, expected_data", |
| 193 | [ |
| 194 | ("test", {"_id": 1}, {"_id": 1, "data": 1}), |
| 195 | ("test", {"_id": 2}, {"_id": 2, "data": 2}), |
| 196 | ("test", {"_id": 3}, {"_id": 3, "data": 3}), |
| 197 | ("test", {"data": 1}, {"_id": 1, "data": 1}), |
| 198 | ("test", {"data": 2}, {"_id": 2, "data": 2}), |
| 199 | ("test", {"data": 3}, {"_id": 3, "data": 3}), |
| 200 | ("test", {"_id": 1, "data": 1}, {"_id": 1, "data": 1}), |
| 201 | ("test", {"_id": 2, "data": 2}, {"_id": 2, "data": 2}), |
| 202 | ("test", {"_id": 3, "data": 3}, {"_id": 3, "data": 3}), |
| 203 | ], |
| 204 | ) |
| tierno | 1e9a329 | 2018-11-05 18:18:45 +0100 | [diff] [blame] | 205 | def test_get_one(db_memory_with_data, table, db_filter, expected_data): |
| 206 | result = db_memory_with_data.get_one(table, db_filter) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 207 | assert result == expected_data |
| 208 | assert len(db_memory_with_data.db) == 1 |
| 209 | assert table in db_memory_with_data.db |
| 210 | assert len(db_memory_with_data.db[table]) == 3 |
| 211 | assert result in db_memory_with_data.db[table] |
| 212 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 213 | |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 214 | @pytest.mark.parametrize( |
| 215 | "db_filter, expected_ids", |
| 216 | [ |
| 217 | ({}, [1, 2, 3, 4, 5, 6, 7, 8]), |
| 218 | ({"_id": 1}, [1]), |
| 219 | ({"data.data2.data3": 2}, [2]), |
| 220 | ({"data.data2.data3.eq": 2}, [2]), |
| 221 | ({"data.data2.data3": [2]}, [2]), |
| 222 | ({"data.data2.data3.cont": [2]}, [2]), |
| 223 | ({"data.data2.data3.neq": 2}, [1, 3, 4, 5, 6, 7, 8]), |
| 224 | ({"data.data2.data3.neq": [2]}, [1, 3, 4, 5, 6, 7, 8]), |
| 225 | ({"data.data2.data3.ncont": [2]}, [1, 3, 4, 5, 6, 7, 8]), |
| 226 | ({"data.data2.data3": [2, 3]}, [2, 3]), |
| 227 | ({"data.data2.data3.gt": 4}, [5, 6, 7]), |
| 228 | ({"data.data2.data3.gte": 4}, [4, 5, 6, 7]), |
| 229 | ({"data.data2.data3.lt": 4}, [1, 2, 3]), |
| 230 | ({"data.data2.data3.lte": 4}, [1, 2, 3, 4]), |
| 231 | ({"data.data2.data3.lte": 4.5}, [1, 2, 3, 4]), |
| 232 | ({"data.data2.data3.gt": "text"}, []), |
| 233 | ({"nonexist.nonexist": "4"}, []), |
| 234 | ({"nonexist.nonexist": None}, [1, 2, 3, 4, 5, 6, 7, 8]), |
| 235 | ({"nonexist.nonexist.neq": "4"}, [1, 2, 3, 4, 5, 6, 7, 8]), |
| 236 | ({"nonexist.nonexist.neq": None}, []), |
| 237 | ({"text.eq": "sometext"}, [1]), |
| 238 | ({"text.neq": "sometext"}, [2, 3, 4, 5, 6, 7, 8]), |
| 239 | ({"text.eq": "somet"}, []), |
| 240 | ({"text.gte": "a"}, [1]), |
| 241 | ({"text.gte": "somet"}, [1]), |
| 242 | ({"text.gte": "sometext"}, [1]), |
| 243 | ({"text.lt": "somet"}, []), |
| 244 | ({"data.data2.data3": 2, "data.data2.data4": None}, [2]), |
| 245 | ({"data.data2.data3": 2, "data.data2.data4": 5}, []), |
| 246 | ({"data.data2.data3": 4}, [4]), |
| 247 | ({"data.data2.data3": [3, 4, "e"]}, [3, 4]), |
| 248 | ({"data.data2.data3": None}, [8]), |
| 249 | ({"data.data2": "4"}, []), |
| 250 | ({"list.0.a": 1}, [1, 6]), |
| 251 | ({"list2": 1}, [2]), |
| 252 | ({"list2": [1, 5]}, [2]), |
| 253 | ({"list2": [1, 2]}, [2]), |
| 254 | ({"list2": [5, 7]}, []), |
| 255 | ({"list.ANYINDEX.a": 1}, [1]), |
| 256 | ({"list.a": 3, "list.b": 1}, [8]), |
| 257 | ({"list.ANYINDEX.a": 3, "list.ANYINDEX.b": 1}, []), |
| 258 | ({"list.ANYINDEX.a": 3, "list.ANYINDEX.c.a": 3}, [8]), |
| 259 | ({"list.ANYINDEX.a": 3, "list.ANYINDEX.b": 0}, [8]), |
| 260 | ( |
| 261 | { |
| 262 | "list.ANYINDEX.a": 3, |
| 263 | "list.ANYINDEX.c.ANYINDEX.a": 0, |
| 264 | "list.ANYINDEX.c.ANYINDEX.b": "v", |
| 265 | }, |
| 266 | [8], |
| 267 | ), |
| 268 | ( |
| 269 | { |
| 270 | "list.ANYINDEX.a": 3, |
| 271 | "list.ANYINDEX.c.ANYINDEX.a": 0, |
| 272 | "list.ANYINDEX.c.ANYINDEX.b": 1, |
| 273 | }, |
| 274 | [], |
| 275 | ), |
| 276 | ({"list.c.b": 1}, [8]), |
| 277 | ({"list.c.b": None}, [1, 2, 3, 4, 5, 6, 7]), |
| 278 | # ({"data.data2.data3": 4}, []), |
| 279 | # ({"data.data2.data3": 4}, []), |
| 280 | ], |
| 281 | ) |
| tierno | 6472e2b | 2019-09-02 16:04:16 +0000 | [diff] [blame] | 282 | def test_get_list(db_memory_with_many_data, db_filter, expected_ids): |
| 283 | result = db_memory_with_many_data.get_list("test", db_filter) |
| 284 | assert isinstance(result, list) |
| 285 | result_ids = [item["_id"] for item in result] |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 286 | assert len(result) == len( |
| 287 | expected_ids |
| 288 | ), "for db_filter={} result={} expected_ids={}".format( |
| 289 | db_filter, result, result_ids |
| 290 | ) |
| tierno | 6472e2b | 2019-09-02 16:04:16 +0000 | [diff] [blame] | 291 | assert result_ids == expected_ids |
| 292 | for i in range(len(result)): |
| 293 | assert result[i] in db_memory_with_many_data.db["test"] |
| 294 | |
| 295 | assert len(db_memory_with_many_data.db) == 1 |
| 296 | assert "test" in db_memory_with_many_data.db |
| 297 | assert len(db_memory_with_many_data.db["test"]) == 8 |
| delacruzramo | ae049d8 | 2019-09-17 16:05:17 +0200 | [diff] [blame] | 298 | result = db_memory_with_many_data.count("test", db_filter) |
| 299 | assert result == len(expected_ids) |
| tierno | 6472e2b | 2019-09-02 16:04:16 +0000 | [diff] [blame] | 300 | |
| 301 | |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 302 | @pytest.mark.parametrize( |
| 303 | "table, db_filter, expected_data", [("test", {}, {"_id": 1, "data": 1})] |
| 304 | ) |
| 305 | def test_get_one_with_multiple_results( |
| 306 | db_memory_with_data, table, db_filter, expected_data |
| 307 | ): |
| tierno | 1e9a329 | 2018-11-05 18:18:45 +0100 | [diff] [blame] | 308 | result = db_memory_with_data.get_one(table, db_filter, fail_on_more=False) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 309 | assert result == expected_data |
| 310 | assert len(db_memory_with_data.db) == 1 |
| 311 | assert table in db_memory_with_data.db |
| 312 | assert len(db_memory_with_data.db[table]) == 3 |
| 313 | assert result in db_memory_with_data.db[table] |
| 314 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 315 | |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 316 | def test_get_one_with_multiple_results_exception(db_memory_with_data): |
| 317 | table = "test" |
| tierno | 1e9a329 | 2018-11-05 18:18:45 +0100 | [diff] [blame] | 318 | db_filter = {} |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 319 | with pytest.raises(DbException) as excinfo: |
| tierno | 1e9a329 | 2018-11-05 18:18:45 +0100 | [diff] [blame] | 320 | db_memory_with_data.get_one(table, db_filter) |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 321 | assert str(excinfo.value) == ( |
| 322 | empty_exception_message() + get_one_multiple_exception_message(db_filter) |
| 323 | ) |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 324 | # assert excinfo.value.http_code == http.HTTPStatus.CONFLICT |
| 325 | |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 326 | |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 327 | @pytest.mark.parametrize( |
| 328 | "table, db_filter", |
| 329 | [ |
| 330 | ("test", {"_id": 4}), |
| 331 | ("test", {"data": 4}), |
| 332 | ("test", {"_id": 4, "data": 4}), |
| 333 | ("test_table", {"_id": 4}), |
| 334 | ("test_table", {"data": 4}), |
| 335 | ("test_table", {"_id": 4, "data": 4}), |
| 336 | ], |
| 337 | ) |
| tierno | 1e9a329 | 2018-11-05 18:18:45 +0100 | [diff] [blame] | 338 | def test_get_one_with_non_empty_db_exception(db_memory_with_data, table, db_filter): |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 339 | with pytest.raises(DbException) as excinfo: |
| tierno | 1e9a329 | 2018-11-05 18:18:45 +0100 | [diff] [blame] | 340 | db_memory_with_data.get_one(table, db_filter) |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 341 | assert str(excinfo.value) == ( |
| 342 | empty_exception_message() + get_one_exception_message(db_filter) |
| 343 | ) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 344 | assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND |
| 345 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 346 | |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 347 | @pytest.mark.parametrize( |
| 348 | "table, db_filter", |
| 349 | [ |
| 350 | ("test", {"_id": 4}), |
| 351 | ("test", {"data": 4}), |
| 352 | ("test", {"_id": 4, "data": 4}), |
| 353 | ("test_table", {"_id": 4}), |
| 354 | ("test_table", {"data": 4}), |
| 355 | ("test_table", {"_id": 4, "data": 4}), |
| 356 | ], |
| 357 | ) |
| tierno | 1e9a329 | 2018-11-05 18:18:45 +0100 | [diff] [blame] | 358 | def test_get_one_with_non_empty_db_none(db_memory_with_data, table, db_filter): |
| 359 | result = db_memory_with_data.get_one(table, db_filter, fail_on_empty=False) |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 360 | assert result is None |
| 361 | |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 362 | |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 363 | @pytest.mark.parametrize( |
| 364 | "table, db_filter", |
| 365 | [ |
| 366 | ("test", {"_id": 4}), |
| 367 | ("test", {"data": 4}), |
| 368 | ("test", {"_id": 4, "data": 4}), |
| 369 | ("test_table", {"_id": 4}), |
| 370 | ("test_table", {"data": 4}), |
| 371 | ("test_table", {"_id": 4, "data": 4}), |
| 372 | ], |
| 373 | ) |
| tierno | 1e9a329 | 2018-11-05 18:18:45 +0100 | [diff] [blame] | 374 | def test_get_one_with_empty_db_exception(db_memory, table, db_filter): |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 375 | with pytest.raises(DbException) as excinfo: |
| tierno | 1e9a329 | 2018-11-05 18:18:45 +0100 | [diff] [blame] | 376 | db_memory.get_one(table, db_filter) |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 377 | assert str(excinfo.value) == ( |
| 378 | empty_exception_message() + get_one_exception_message(db_filter) |
| 379 | ) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 380 | assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND |
| 381 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 382 | |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 383 | @pytest.mark.parametrize( |
| 384 | "table, db_filter", |
| 385 | [ |
| 386 | ("test", {"_id": 4}), |
| 387 | ("test", {"data": 4}), |
| 388 | ("test", {"_id": 4, "data": 4}), |
| 389 | ("test_table", {"_id": 4}), |
| 390 | ("test_table", {"data": 4}), |
| 391 | ("test_table", {"_id": 4, "data": 4}), |
| 392 | ], |
| 393 | ) |
| tierno | 1e9a329 | 2018-11-05 18:18:45 +0100 | [diff] [blame] | 394 | def test_get_one_with_empty_db_none(db_memory, table, db_filter): |
| 395 | result = db_memory.get_one(table, db_filter, fail_on_empty=False) |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 396 | assert result is None |
| 397 | |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 398 | |
| 399 | def test_get_one_generic_exception(db_memory_with_data): |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 400 | table = "test" |
| tierno | 1e9a329 | 2018-11-05 18:18:45 +0100 | [diff] [blame] | 401 | db_filter = {} |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 402 | db_memory_with_data._find = MagicMock(side_effect=Exception()) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 403 | with pytest.raises(DbException) as excinfo: |
| tierno | 1e9a329 | 2018-11-05 18:18:45 +0100 | [diff] [blame] | 404 | db_memory_with_data.get_one(table, db_filter) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 405 | assert str(excinfo.value) == empty_exception_message() |
| 406 | assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND |
| 407 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 408 | |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 409 | @pytest.mark.parametrize( |
| 410 | "table, db_filter, expected_data", |
| 411 | [ |
| 412 | ("test", {}, []), |
| 413 | ("test", {"_id": 1}, [{"_id": 2, "data": 2}, {"_id": 3, "data": 3}]), |
| 414 | ("test", {"_id": 2}, [{"_id": 1, "data": 1}, {"_id": 3, "data": 3}]), |
| 415 | ("test", {"_id": 1, "data": 1}, [{"_id": 2, "data": 2}, {"_id": 3, "data": 3}]), |
| 416 | ("test", {"_id": 2, "data": 2}, [{"_id": 1, "data": 1}, {"_id": 3, "data": 3}]), |
| 417 | ], |
| 418 | ) |
| 419 | def test_del_list_with_non_empty_db( |
| 420 | db_memory_with_data, table, db_filter, expected_data |
| 421 | ): |
| tierno | 1e9a329 | 2018-11-05 18:18:45 +0100 | [diff] [blame] | 422 | result = db_memory_with_data.del_list(table, db_filter) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 423 | assert result["deleted"] == (3 - len(expected_data)) |
| 424 | assert len(db_memory_with_data.db) == 1 |
| 425 | assert table in db_memory_with_data.db |
| 426 | assert len(db_memory_with_data.db[table]) == len(expected_data) |
| 427 | for data in expected_data: |
| 428 | assert data in db_memory_with_data.db[table] |
| 429 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 430 | |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 431 | @pytest.mark.parametrize( |
| 432 | "table, db_filter", |
| 433 | [ |
| 434 | ("test", {}), |
| 435 | ("test", {"_id": 1}), |
| 436 | ("test", {"_id": 2}), |
| 437 | ("test", {"data": 1}), |
| 438 | ("test", {"data": 2}), |
| 439 | ("test", {"_id": 1, "data": 1}), |
| 440 | ("test", {"_id": 2, "data": 2}), |
| 441 | ], |
| 442 | ) |
| tierno | 1e9a329 | 2018-11-05 18:18:45 +0100 | [diff] [blame] | 443 | def test_del_list_with_empty_db(db_memory, table, db_filter): |
| 444 | result = db_memory.del_list(table, db_filter) |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 445 | assert result["deleted"] == 0 |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 446 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 447 | |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 448 | def test_del_list_generic_exception(db_memory_with_data): |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 449 | table = "test" |
| tierno | 1e9a329 | 2018-11-05 18:18:45 +0100 | [diff] [blame] | 450 | db_filter = {} |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 451 | db_memory_with_data._find = MagicMock(side_effect=Exception()) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 452 | with pytest.raises(DbException) as excinfo: |
| tierno | 1e9a329 | 2018-11-05 18:18:45 +0100 | [diff] [blame] | 453 | db_memory_with_data.del_list(table, db_filter) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 454 | assert str(excinfo.value) == empty_exception_message() |
| 455 | assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND |
| 456 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 457 | |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 458 | @pytest.mark.parametrize( |
| 459 | "table, db_filter, data", |
| 460 | [ |
| 461 | ("test", {}, {"_id": 1, "data": 1}), |
| 462 | ("test", {"_id": 1}, {"_id": 1, "data": 1}), |
| 463 | ("test", {"data": 1}, {"_id": 1, "data": 1}), |
| 464 | ("test", {"_id": 1, "data": 1}, {"_id": 1, "data": 1}), |
| 465 | ("test", {"_id": 2}, {"_id": 2, "data": 2}), |
| 466 | ("test", {"data": 2}, {"_id": 2, "data": 2}), |
| 467 | ("test", {"_id": 2, "data": 2}, {"_id": 2, "data": 2}), |
| 468 | ], |
| 469 | ) |
| tierno | 1e9a329 | 2018-11-05 18:18:45 +0100 | [diff] [blame] | 470 | def test_del_one(db_memory_with_data, table, db_filter, data): |
| 471 | result = db_memory_with_data.del_one(table, db_filter) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 472 | assert result == {"deleted": 1} |
| 473 | assert len(db_memory_with_data.db) == 1 |
| 474 | assert table in db_memory_with_data.db |
| 475 | assert len(db_memory_with_data.db[table]) == 2 |
| 476 | assert data not in db_memory_with_data.db[table] |
| 477 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 478 | |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 479 | @pytest.mark.parametrize( |
| 480 | "table, db_filter", |
| 481 | [ |
| 482 | ("test", {}), |
| 483 | ("test", {"_id": 1}), |
| 484 | ("test", {"_id": 2}), |
| 485 | ("test", {"data": 1}), |
| 486 | ("test", {"data": 2}), |
| 487 | ("test", {"_id": 1, "data": 1}), |
| 488 | ("test", {"_id": 2, "data": 2}), |
| 489 | ("test_table", {}), |
| 490 | ("test_table", {"_id": 1}), |
| 491 | ("test_table", {"_id": 2}), |
| 492 | ("test_table", {"data": 1}), |
| 493 | ("test_table", {"data": 2}), |
| 494 | ("test_table", {"_id": 1, "data": 1}), |
| 495 | ("test_table", {"_id": 2, "data": 2}), |
| 496 | ], |
| 497 | ) |
| tierno | 1e9a329 | 2018-11-05 18:18:45 +0100 | [diff] [blame] | 498 | def test_del_one_with_empty_db_exception(db_memory, table, db_filter): |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 499 | with pytest.raises(DbException) as excinfo: |
| tierno | 1e9a329 | 2018-11-05 18:18:45 +0100 | [diff] [blame] | 500 | db_memory.del_one(table, db_filter) |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 501 | assert str(excinfo.value) == ( |
| 502 | empty_exception_message() + del_one_exception_message(db_filter) |
| 503 | ) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 504 | assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND |
| 505 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 506 | |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 507 | @pytest.mark.parametrize( |
| 508 | "table, db_filter", |
| 509 | [ |
| 510 | ("test", {}), |
| 511 | ("test", {"_id": 1}), |
| 512 | ("test", {"_id": 2}), |
| 513 | ("test", {"data": 1}), |
| 514 | ("test", {"data": 2}), |
| 515 | ("test", {"_id": 1, "data": 1}), |
| 516 | ("test", {"_id": 2, "data": 2}), |
| 517 | ("test_table", {}), |
| 518 | ("test_table", {"_id": 1}), |
| 519 | ("test_table", {"_id": 2}), |
| 520 | ("test_table", {"data": 1}), |
| 521 | ("test_table", {"data": 2}), |
| 522 | ("test_table", {"_id": 1, "data": 1}), |
| 523 | ("test_table", {"_id": 2, "data": 2}), |
| 524 | ], |
| 525 | ) |
| tierno | 1e9a329 | 2018-11-05 18:18:45 +0100 | [diff] [blame] | 526 | def test_del_one_with_empty_db_none(db_memory, table, db_filter): |
| 527 | result = db_memory.del_one(table, db_filter, fail_on_empty=False) |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 528 | assert result is None |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 529 | |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 530 | |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 531 | @pytest.mark.parametrize( |
| 532 | "table, db_filter", |
| 533 | [ |
| 534 | ("test", {"_id": 4}), |
| 535 | ("test", {"_id": 5}), |
| 536 | ("test", {"data": 4}), |
| 537 | ("test", {"data": 5}), |
| 538 | ("test", {"_id": 1, "data": 2}), |
| 539 | ("test", {"_id": 2, "data": 3}), |
| 540 | ("test_table", {}), |
| 541 | ("test_table", {"_id": 1}), |
| 542 | ("test_table", {"_id": 2}), |
| 543 | ("test_table", {"data": 1}), |
| 544 | ("test_table", {"data": 2}), |
| 545 | ("test_table", {"_id": 1, "data": 1}), |
| 546 | ("test_table", {"_id": 2, "data": 2}), |
| 547 | ], |
| 548 | ) |
| tierno | 1e9a329 | 2018-11-05 18:18:45 +0100 | [diff] [blame] | 549 | def test_del_one_with_non_empty_db_exception(db_memory_with_data, table, db_filter): |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 550 | with pytest.raises(DbException) as excinfo: |
| tierno | 1e9a329 | 2018-11-05 18:18:45 +0100 | [diff] [blame] | 551 | db_memory_with_data.del_one(table, db_filter) |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 552 | assert str(excinfo.value) == ( |
| 553 | empty_exception_message() + del_one_exception_message(db_filter) |
| 554 | ) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 555 | assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND |
| 556 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 557 | |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 558 | @pytest.mark.parametrize( |
| 559 | "table, db_filter", |
| 560 | [ |
| 561 | ("test", {"_id": 4}), |
| 562 | ("test", {"_id": 5}), |
| 563 | ("test", {"data": 4}), |
| 564 | ("test", {"data": 5}), |
| 565 | ("test", {"_id": 1, "data": 2}), |
| 566 | ("test", {"_id": 2, "data": 3}), |
| 567 | ("test_table", {}), |
| 568 | ("test_table", {"_id": 1}), |
| 569 | ("test_table", {"_id": 2}), |
| 570 | ("test_table", {"data": 1}), |
| 571 | ("test_table", {"data": 2}), |
| 572 | ("test_table", {"_id": 1, "data": 1}), |
| 573 | ("test_table", {"_id": 2, "data": 2}), |
| 574 | ], |
| 575 | ) |
| tierno | 1e9a329 | 2018-11-05 18:18:45 +0100 | [diff] [blame] | 576 | def test_del_one_with_non_empty_db_none(db_memory_with_data, table, db_filter): |
| 577 | result = db_memory_with_data.del_one(table, db_filter, fail_on_empty=False) |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 578 | assert result is None |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 579 | |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 580 | |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 581 | @pytest.mark.parametrize("fail_on_empty", [(True), (False)]) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 582 | def test_del_one_generic_exception(db_memory_with_data, fail_on_empty): |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 583 | table = "test" |
| tierno | 1e9a329 | 2018-11-05 18:18:45 +0100 | [diff] [blame] | 584 | db_filter = {} |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 585 | db_memory_with_data._find = MagicMock(side_effect=Exception()) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 586 | with pytest.raises(DbException) as excinfo: |
| tierno | 1e9a329 | 2018-11-05 18:18:45 +0100 | [diff] [blame] | 587 | db_memory_with_data.del_one(table, db_filter, fail_on_empty=fail_on_empty) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 588 | assert str(excinfo.value) == empty_exception_message() |
| 589 | assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND |
| 590 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 591 | |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 592 | @pytest.mark.parametrize( |
| 593 | "table, _id, indata", |
| 594 | [ |
| 595 | ("test", 1, {"_id": 1, "data": 42}), |
| 596 | ("test", 1, {"_id": 1, "data": 42, "kk": 34}), |
| 597 | ("test", 1, {"_id": 1}), |
| 598 | ("test", 2, {"_id": 2, "data": 42}), |
| 599 | ("test", 2, {"_id": 2, "data": 42, "kk": 34}), |
| 600 | ("test", 2, {"_id": 2}), |
| 601 | ("test", 3, {"_id": 3, "data": 42}), |
| 602 | ("test", 3, {"_id": 3, "data": 42, "kk": 34}), |
| 603 | ("test", 3, {"_id": 3}), |
| 604 | ], |
| 605 | ) |
| tierno | 136f295 | 2018-10-19 13:01:03 +0200 | [diff] [blame] | 606 | def test_replace(db_memory_with_data, table, _id, indata): |
| 607 | result = db_memory_with_data.replace(table, _id, indata) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 608 | assert result == {"updated": 1} |
| 609 | assert len(db_memory_with_data.db) == 1 |
| 610 | assert table in db_memory_with_data.db |
| 611 | assert len(db_memory_with_data.db[table]) == 3 |
| 612 | assert indata in db_memory_with_data.db[table] |
| 613 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 614 | |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 615 | @pytest.mark.parametrize( |
| 616 | "table, _id, indata", |
| 617 | [ |
| 618 | ("test", 1, {"_id": 1, "data": 42}), |
| 619 | ("test", 2, {"_id": 2}), |
| 620 | ("test", 3, {"_id": 3}), |
| 621 | ], |
| 622 | ) |
| tierno | 136f295 | 2018-10-19 13:01:03 +0200 | [diff] [blame] | 623 | def test_replace_without_data_exception(db_memory, table, _id, indata): |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 624 | with pytest.raises(DbException) as excinfo: |
| tierno | 136f295 | 2018-10-19 13:01:03 +0200 | [diff] [blame] | 625 | db_memory.replace(table, _id, indata, fail_on_empty=True) |
| 626 | assert str(excinfo.value) == (replace_exception_message(_id)) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 627 | assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND |
| 628 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 629 | |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 630 | @pytest.mark.parametrize( |
| 631 | "table, _id, indata", |
| 632 | [ |
| 633 | ("test", 1, {"_id": 1, "data": 42}), |
| 634 | ("test", 2, {"_id": 2}), |
| 635 | ("test", 3, {"_id": 3}), |
| 636 | ], |
| 637 | ) |
| tierno | 136f295 | 2018-10-19 13:01:03 +0200 | [diff] [blame] | 638 | def test_replace_without_data_none(db_memory, table, _id, indata): |
| 639 | result = db_memory.replace(table, _id, indata, fail_on_empty=False) |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 640 | assert result is None |
| 641 | |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 642 | |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 643 | @pytest.mark.parametrize( |
| 644 | "table, _id, indata", |
| 645 | [ |
| 646 | ("test", 11, {"_id": 11, "data": 42}), |
| 647 | ("test", 12, {"_id": 12}), |
| 648 | ("test", 33, {"_id": 33}), |
| 649 | ], |
| 650 | ) |
| tierno | 136f295 | 2018-10-19 13:01:03 +0200 | [diff] [blame] | 651 | def test_replace_with_data_exception(db_memory_with_data, table, _id, indata): |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 652 | with pytest.raises(DbException) as excinfo: |
| tierno | 136f295 | 2018-10-19 13:01:03 +0200 | [diff] [blame] | 653 | db_memory_with_data.replace(table, _id, indata, fail_on_empty=True) |
| 654 | assert str(excinfo.value) == (replace_exception_message(_id)) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 655 | assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND |
| 656 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 657 | |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 658 | @pytest.mark.parametrize( |
| 659 | "table, _id, indata", |
| 660 | [ |
| 661 | ("test", 11, {"_id": 11, "data": 42}), |
| 662 | ("test", 12, {"_id": 12}), |
| 663 | ("test", 33, {"_id": 33}), |
| 664 | ], |
| 665 | ) |
| tierno | 136f295 | 2018-10-19 13:01:03 +0200 | [diff] [blame] | 666 | def test_replace_with_data_none(db_memory_with_data, table, _id, indata): |
| 667 | result = db_memory_with_data.replace(table, _id, indata, fail_on_empty=False) |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 668 | assert result is None |
| 669 | |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 670 | |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 671 | @pytest.mark.parametrize("fail_on_empty", [True, False]) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 672 | def test_replace_generic_exception(db_memory_with_data, fail_on_empty): |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 673 | table = "test" |
| tierno | 136f295 | 2018-10-19 13:01:03 +0200 | [diff] [blame] | 674 | _id = {} |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 675 | indata = {"_id": 1, "data": 1} |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 676 | db_memory_with_data._find = MagicMock(side_effect=Exception()) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 677 | with pytest.raises(DbException) as excinfo: |
| tierno | 136f295 | 2018-10-19 13:01:03 +0200 | [diff] [blame] | 678 | db_memory_with_data.replace(table, _id, indata, fail_on_empty=fail_on_empty) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 679 | assert str(excinfo.value) == empty_exception_message() |
| 680 | assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND |
| 681 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 682 | |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 683 | @pytest.mark.parametrize( |
| 684 | "table, id, data", |
| 685 | [ |
| 686 | ("test", "1", {"data": 1}), |
| 687 | ("test", "1", {"data": 2}), |
| 688 | ("test", "2", {"data": 1}), |
| 689 | ("test", "2", {"data": 2}), |
| 690 | ("test_table", "1", {"data": 1}), |
| 691 | ("test_table", "1", {"data": 2}), |
| 692 | ("test_table", "2", {"data": 1}), |
| 693 | ("test_table", "2", {"data": 2}), |
| 694 | ("test", "1", {"data_1": 1, "data_2": 2}), |
| 695 | ("test", "1", {"data_1": 2, "data_2": 1}), |
| 696 | ("test", "2", {"data_1": 1, "data_2": 2}), |
| 697 | ("test", "2", {"data_1": 2, "data_2": 1}), |
| 698 | ("test_table", "1", {"data_1": 1, "data_2": 2}), |
| 699 | ("test_table", "1", {"data_1": 2, "data_2": 1}), |
| 700 | ("test_table", "2", {"data_1": 1, "data_2": 2}), |
| 701 | ("test_table", "2", {"data_1": 2, "data_2": 1}), |
| 702 | ], |
| 703 | ) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 704 | def test_create_with_empty_db_with_id(db_memory, table, id, data): |
| 705 | data_to_insert = data |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 706 | data_to_insert["_id"] = id |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 707 | returned_id = db_memory.create(table, data_to_insert) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 708 | assert returned_id == id |
| 709 | assert len(db_memory.db) == 1 |
| 710 | assert table in db_memory.db |
| 711 | assert len(db_memory.db[table]) == 1 |
| 712 | assert data_to_insert in db_memory.db[table] |
| 713 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 714 | |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 715 | @pytest.mark.parametrize( |
| 716 | "table, id, data", |
| 717 | [ |
| 718 | ("test", "4", {"data": 1}), |
| 719 | ("test", "5", {"data": 2}), |
| 720 | ("test", "4", {"data": 1}), |
| 721 | ("test", "5", {"data": 2}), |
| 722 | ("test_table", "4", {"data": 1}), |
| 723 | ("test_table", "5", {"data": 2}), |
| 724 | ("test_table", "4", {"data": 1}), |
| 725 | ("test_table", "5", {"data": 2}), |
| 726 | ("test", "4", {"data_1": 1, "data_2": 2}), |
| 727 | ("test", "5", {"data_1": 2, "data_2": 1}), |
| 728 | ("test", "4", {"data_1": 1, "data_2": 2}), |
| 729 | ("test", "5", {"data_1": 2, "data_2": 1}), |
| 730 | ("test_table", "4", {"data_1": 1, "data_2": 2}), |
| 731 | ("test_table", "5", {"data_1": 2, "data_2": 1}), |
| 732 | ("test_table", "4", {"data_1": 1, "data_2": 2}), |
| 733 | ("test_table", "5", {"data_1": 2, "data_2": 1}), |
| 734 | ], |
| 735 | ) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 736 | def test_create_with_non_empty_db_with_id(db_memory_with_data, table, id, data): |
| 737 | data_to_insert = data |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 738 | data_to_insert["_id"] = id |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 739 | returned_id = db_memory_with_data.create(table, data_to_insert) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 740 | assert returned_id == id |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 741 | assert len(db_memory_with_data.db) == (1 if table == "test" else 2) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 742 | assert table in db_memory_with_data.db |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 743 | 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] | 744 | assert data_to_insert in db_memory_with_data.db[table] |
| 745 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 746 | |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 747 | @pytest.mark.parametrize( |
| 748 | "table, data", |
| 749 | [ |
| 750 | ("test", {"data": 1}), |
| 751 | ("test", {"data": 2}), |
| 752 | ("test", {"data": 1}), |
| 753 | ("test", {"data": 2}), |
| 754 | ("test_table", {"data": 1}), |
| 755 | ("test_table", {"data": 2}), |
| 756 | ("test_table", {"data": 1}), |
| 757 | ("test_table", {"data": 2}), |
| 758 | ("test", {"data_1": 1, "data_2": 2}), |
| 759 | ("test", {"data_1": 2, "data_2": 1}), |
| 760 | ("test", {"data_1": 1, "data_2": 2}), |
| 761 | ("test", {"data_1": 2, "data_2": 1}), |
| 762 | ("test_table", {"data_1": 1, "data_2": 2}), |
| 763 | ("test_table", {"data_1": 2, "data_2": 1}), |
| 764 | ("test_table", {"data_1": 1, "data_2": 2}), |
| 765 | ("test_table", {"data_1": 2, "data_2": 1}), |
| 766 | ], |
| 767 | ) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 768 | def test_create_with_empty_db_without_id(db_memory, table, data): |
| 769 | returned_id = db_memory.create(table, data) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 770 | assert len(db_memory.db) == 1 |
| 771 | assert table in db_memory.db |
| 772 | assert len(db_memory.db[table]) == 1 |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 773 | data_inserted = data |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 774 | data_inserted["_id"] = returned_id |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 775 | assert data_inserted in db_memory.db[table] |
| 776 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 777 | |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 778 | @pytest.mark.parametrize( |
| 779 | "table, data", |
| 780 | [ |
| 781 | ("test", {"data": 1}), |
| 782 | ("test", {"data": 2}), |
| 783 | ("test", {"data": 1}), |
| 784 | ("test", {"data": 2}), |
| 785 | ("test_table", {"data": 1}), |
| 786 | ("test_table", {"data": 2}), |
| 787 | ("test_table", {"data": 1}), |
| 788 | ("test_table", {"data": 2}), |
| 789 | ("test", {"data_1": 1, "data_2": 2}), |
| 790 | ("test", {"data_1": 2, "data_2": 1}), |
| 791 | ("test", {"data_1": 1, "data_2": 2}), |
| 792 | ("test", {"data_1": 2, "data_2": 1}), |
| 793 | ("test_table", {"data_1": 1, "data_2": 2}), |
| 794 | ("test_table", {"data_1": 2, "data_2": 1}), |
| 795 | ("test_table", {"data_1": 1, "data_2": 2}), |
| 796 | ("test_table", {"data_1": 2, "data_2": 1}), |
| 797 | ], |
| 798 | ) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 799 | def test_create_with_non_empty_db_without_id(db_memory_with_data, table, data): |
| 800 | returned_id = db_memory_with_data.create(table, data) |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 801 | assert len(db_memory_with_data.db) == (1 if table == "test" else 2) |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 802 | assert table in db_memory_with_data.db |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 803 | 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] | 804 | data_inserted = data |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 805 | data_inserted["_id"] = returned_id |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 806 | assert data_inserted in db_memory_with_data.db[table] |
| 807 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 808 | |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 809 | def test_create_with_exception(db_memory): |
| 810 | table = "test" |
| 811 | data = {"_id": 1, "data": 1} |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 812 | db_memory.db = MagicMock() |
| 813 | db_memory.db.__contains__.side_effect = Exception() |
| Eduardo Sousa | 0cb1b3c | 2018-04-26 00:36:45 +0100 | [diff] [blame] | 814 | with pytest.raises(DbException) as excinfo: |
| 815 | db_memory.create(table, data) |
| 816 | assert str(excinfo.value) == empty_exception_message() |
| 817 | assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND |
| tierno | 6472e2b | 2019-09-02 16:04:16 +0000 | [diff] [blame] | 818 | |
| 819 | |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 820 | @pytest.mark.parametrize( |
| 821 | "db_content, update_dict, expected, message", |
| 822 | [ |
| 823 | ( |
| 824 | {"a": {"none": None}}, |
| 825 | {"a.b.num": "v"}, |
| 826 | {"a": {"none": None, "b": {"num": "v"}}}, |
| 827 | "create dict", |
| 828 | ), |
| 829 | ( |
| 830 | {"a": {"none": None}}, |
| 831 | {"a.none.num": "v"}, |
| 832 | {"a": {"none": {"num": "v"}}}, |
| 833 | "create dict over none", |
| 834 | ), |
| 835 | ( |
| 836 | {"a": {"b": {"num": 4}}}, |
| 837 | {"a.b.num": "v"}, |
| 838 | {"a": {"b": {"num": "v"}}}, |
| 839 | "replace_number", |
| 840 | ), |
| 841 | ( |
| 842 | {"a": {"b": {"num": 4}}}, |
| 843 | {"a.b.num.c.d": "v"}, |
| 844 | None, |
| 845 | "create dict over number should fail", |
| 846 | ), |
| 847 | ( |
| 848 | {"a": {"b": {"num": 4}}}, |
| 849 | {"a.b": "v"}, |
| 850 | {"a": {"b": "v"}}, |
| 851 | "replace dict with a string", |
| 852 | ), |
| 853 | ( |
| 854 | {"a": {"b": {"num": 4}}}, |
| 855 | {"a.b": None}, |
| 856 | {"a": {"b": None}}, |
| 857 | "replace dict with None", |
| 858 | ), |
| 859 | ( |
| 860 | {"a": [{"b": {"num": 4}}]}, |
| 861 | {"a.b.num": "v"}, |
| 862 | None, |
| 863 | "create dict over list should fail", |
| 864 | ), |
| 865 | ( |
| 866 | {"a": [{"b": {"num": 4}}]}, |
| 867 | {"a.0.b.num": "v"}, |
| 868 | {"a": [{"b": {"num": "v"}}]}, |
| 869 | "set list", |
| 870 | ), |
| 871 | ( |
| 872 | {"a": [{"b": {"num": 4}}]}, |
| 873 | {"a.3.b.num": "v"}, |
| 874 | {"a": [{"b": {"num": 4}}, None, None, {"b": {"num": "v"}}]}, |
| 875 | "expand list", |
| 876 | ), |
| 877 | ({"a": [[4]]}, {"a.0.0": "v"}, {"a": [["v"]]}, "set nested list"), |
| 878 | ({"a": [[4]]}, {"a.0.2": "v"}, {"a": [[4, None, "v"]]}, "expand nested list"), |
| 879 | ( |
| 880 | {"a": [[4]]}, |
| 881 | {"a.2.2": "v"}, |
| 882 | {"a": [[4], None, {"2": "v"}]}, |
| 883 | "expand list and add number key", |
| 884 | ), |
| 885 | ], |
| 886 | ) |
| tierno | 6472e2b | 2019-09-02 16:04:16 +0000 | [diff] [blame] | 887 | def test_set_one(db_memory, db_content, update_dict, expected, message): |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 888 | db_memory._find = Mock(return_value=((0, db_content),)) |
| tierno | 6472e2b | 2019-09-02 16:04:16 +0000 | [diff] [blame] | 889 | if expected is None: |
| 890 | with pytest.raises(DbException) as excinfo: |
| 891 | db_memory.set_one("table", {}, update_dict) |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 892 | assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND, message |
| tierno | 6472e2b | 2019-09-02 16:04:16 +0000 | [diff] [blame] | 893 | else: |
| 894 | db_memory.set_one("table", {}, update_dict) |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 895 | assert db_content == expected, message |
| tierno | 6472e2b | 2019-09-02 16:04:16 +0000 | [diff] [blame] | 896 | |
| 897 | |
| 898 | class TestDbMemory(unittest.TestCase): |
| 899 | # TODO to delete. This is cover with pytest test_set_one. |
| 900 | def test_set_one(self): |
| 901 | test_set = ( |
| 902 | # (database content, set-content, expected database content (None=fails), message) |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 903 | ( |
| 904 | {"a": {"none": None}}, |
| 905 | {"a.b.num": "v"}, |
| 906 | {"a": {"none": None, "b": {"num": "v"}}}, |
| 907 | "create dict", |
| 908 | ), |
| 909 | ( |
| 910 | {"a": {"none": None}}, |
| 911 | {"a.none.num": "v"}, |
| 912 | {"a": {"none": {"num": "v"}}}, |
| 913 | "create dict over none", |
| 914 | ), |
| 915 | ( |
| 916 | {"a": {"b": {"num": 4}}}, |
| 917 | {"a.b.num": "v"}, |
| 918 | {"a": {"b": {"num": "v"}}}, |
| 919 | "replace_number", |
| 920 | ), |
| 921 | ( |
| 922 | {"a": {"b": {"num": 4}}}, |
| 923 | {"a.b.num.c.d": "v"}, |
| 924 | None, |
| 925 | "create dict over number should fail", |
| 926 | ), |
| 927 | ( |
| 928 | {"a": {"b": {"num": 4}}}, |
| 929 | {"a.b": "v"}, |
| 930 | {"a": {"b": "v"}}, |
| 931 | "replace dict with a string", |
| 932 | ), |
| 933 | ( |
| 934 | {"a": {"b": {"num": 4}}}, |
| 935 | {"a.b": None}, |
| 936 | {"a": {"b": None}}, |
| 937 | "replace dict with None", |
| 938 | ), |
| 939 | ( |
| 940 | {"a": [{"b": {"num": 4}}]}, |
| 941 | {"a.b.num": "v"}, |
| 942 | None, |
| 943 | "create dict over list should fail", |
| 944 | ), |
| 945 | ( |
| 946 | {"a": [{"b": {"num": 4}}]}, |
| 947 | {"a.0.b.num": "v"}, |
| 948 | {"a": [{"b": {"num": "v"}}]}, |
| 949 | "set list", |
| 950 | ), |
| 951 | ( |
| 952 | {"a": [{"b": {"num": 4}}]}, |
| 953 | {"a.3.b.num": "v"}, |
| 954 | {"a": [{"b": {"num": 4}}, None, None, {"b": {"num": "v"}}]}, |
| 955 | "expand list", |
| 956 | ), |
| tierno | 6472e2b | 2019-09-02 16:04:16 +0000 | [diff] [blame] | 957 | ({"a": [[4]]}, {"a.0.0": "v"}, {"a": [["v"]]}, "set nested list"), |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 958 | ( |
| 959 | {"a": [[4]]}, |
| 960 | {"a.0.2": "v"}, |
| 961 | {"a": [[4, None, "v"]]}, |
| 962 | "expand nested list", |
| 963 | ), |
| 964 | ( |
| 965 | {"a": [[4]]}, |
| 966 | {"a.2.2": "v"}, |
| 967 | {"a": [[4], None, {"2": "v"}]}, |
| 968 | "expand list and add number key", |
| 969 | ), |
| tierno | bf6c572 | 2020-03-12 09:54:35 +0000 | [diff] [blame] | 970 | ({"a": None}, {"b.c": "v"}, {"a": None, "b": {"c": "v"}}, "expand at root"), |
| tierno | 6472e2b | 2019-09-02 16:04:16 +0000 | [diff] [blame] | 971 | ) |
| 972 | db_men = DbMemory() |
| 973 | db_men._find = Mock() |
| 974 | for db_content, update_dict, expected, message in test_set: |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 975 | db_men._find.return_value = ((0, db_content),) |
| tierno | 6472e2b | 2019-09-02 16:04:16 +0000 | [diff] [blame] | 976 | if expected is None: |
| 977 | self.assertRaises(DbException, db_men.set_one, "table", {}, update_dict) |
| 978 | else: |
| 979 | db_men.set_one("table", {}, update_dict) |
| 980 | self.assertEqual(db_content, expected, message) |
| tierno | 7fc50dd | 2020-02-17 12:01:38 +0000 | [diff] [blame] | 981 | |
| 982 | def test_set_one_pull(self): |
| 983 | example = {"a": [1, "1", 1], "d": {}, "n": None} |
| 984 | test_set = ( |
| 985 | # (database content, set-content, expected database content (None=fails), message) |
| 986 | (example, {"a": "1"}, {"a": [1, 1], "d": {}, "n": None}, "pull one item"), |
| 987 | (example, {"a": 1}, {"a": ["1"], "d": {}, "n": None}, "pull two items"), |
| 988 | (example, {"a": "v"}, example, "pull non existing item"), |
| 989 | (example, {"a.6": 1}, example, "pull non existing arrray"), |
| 990 | (example, {"d.b.c": 1}, example, "pull non existing arrray2"), |
| 991 | (example, {"b": 1}, example, "pull non existing arrray3"), |
| 992 | (example, {"d": 1}, None, "pull over dict"), |
| 993 | (example, {"n": 1}, None, "pull over None"), |
| 994 | ) |
| 995 | db_men = DbMemory() |
| 996 | db_men._find = Mock() |
| 997 | for db_content, pull_dict, expected, message in test_set: |
| 998 | db_content = deepcopy(db_content) |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 999 | db_men._find.return_value = ((0, db_content),) |
| tierno | 7fc50dd | 2020-02-17 12:01:38 +0000 | [diff] [blame] | 1000 | if expected is None: |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 1001 | self.assertRaises( |
| 1002 | DbException, |
| 1003 | db_men.set_one, |
| 1004 | "table", |
| 1005 | {}, |
| 1006 | None, |
| 1007 | fail_on_empty=False, |
| 1008 | pull=pull_dict, |
| 1009 | ) |
| tierno | 7fc50dd | 2020-02-17 12:01:38 +0000 | [diff] [blame] | 1010 | else: |
| 1011 | db_men.set_one("table", {}, None, pull=pull_dict) |
| 1012 | self.assertEqual(db_content, expected, message) |
| 1013 | |
| 1014 | def test_set_one_push(self): |
| 1015 | example = {"a": [1, "1", 1], "d": {}, "n": None} |
| 1016 | test_set = ( |
| 1017 | # (database content, set-content, expected database content (None=fails), message) |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 1018 | ( |
| 1019 | example, |
| 1020 | {"d.b.c": 1}, |
| 1021 | {"a": [1, "1", 1], "d": {"b": {"c": [1]}}, "n": None}, |
| 1022 | "push non existing arrray2", |
| 1023 | ), |
| 1024 | ( |
| 1025 | example, |
| 1026 | {"b": 1}, |
| 1027 | {"a": [1, "1", 1], "d": {}, "b": [1], "n": None}, |
| 1028 | "push non existing arrray3", |
| 1029 | ), |
| 1030 | ( |
| 1031 | example, |
| 1032 | {"a.6": 1}, |
| 1033 | {"a": [1, "1", 1, None, None, None, [1]], "d": {}, "n": None}, |
| 1034 | "push non existing arrray", |
| 1035 | ), |
| 1036 | ( |
| 1037 | example, |
| 1038 | {"a": 2}, |
| 1039 | {"a": [1, "1", 1, 2], "d": {}, "n": None}, |
| 1040 | "push one item", |
| 1041 | ), |
| 1042 | ( |
| 1043 | example, |
| 1044 | {"a": {1: 1}}, |
| 1045 | {"a": [1, "1", 1, {1: 1}], "d": {}, "n": None}, |
| 1046 | "push a dict", |
| 1047 | ), |
| tierno | 7fc50dd | 2020-02-17 12:01:38 +0000 | [diff] [blame] | 1048 | (example, {"d": 1}, None, "push over dict"), |
| 1049 | (example, {"n": 1}, None, "push over None"), |
| 1050 | ) |
| 1051 | db_men = DbMemory() |
| 1052 | db_men._find = Mock() |
| 1053 | for db_content, push_dict, expected, message in test_set: |
| 1054 | db_content = deepcopy(db_content) |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 1055 | db_men._find.return_value = ((0, db_content),) |
| tierno | 7fc50dd | 2020-02-17 12:01:38 +0000 | [diff] [blame] | 1056 | if expected is None: |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 1057 | self.assertRaises( |
| 1058 | DbException, |
| 1059 | db_men.set_one, |
| 1060 | "table", |
| 1061 | {}, |
| 1062 | None, |
| 1063 | fail_on_empty=False, |
| 1064 | push=push_dict, |
| 1065 | ) |
| tierno | 7fc50dd | 2020-02-17 12:01:38 +0000 | [diff] [blame] | 1066 | else: |
| 1067 | db_men.set_one("table", {}, None, push=push_dict) |
| 1068 | self.assertEqual(db_content, expected, message) |
| 1069 | |
| tierno | 399f6c3 | 2020-05-12 07:36:41 +0000 | [diff] [blame] | 1070 | def test_set_one_push_list(self): |
| 1071 | example = {"a": [1, "1", 1], "d": {}, "n": None} |
| 1072 | test_set = ( |
| 1073 | # (database content, set-content, expected database content (None=fails), message) |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 1074 | ( |
| 1075 | example, |
| 1076 | {"d.b.c": [1]}, |
| 1077 | {"a": [1, "1", 1], "d": {"b": {"c": [1]}}, "n": None}, |
| 1078 | "push non existing arrray2", |
| 1079 | ), |
| 1080 | ( |
| 1081 | example, |
| 1082 | {"b": [1]}, |
| 1083 | {"a": [1, "1", 1], "d": {}, "b": [1], "n": None}, |
| 1084 | "push non existing arrray3", |
| 1085 | ), |
| 1086 | ( |
| 1087 | example, |
| 1088 | {"a.6": [1]}, |
| 1089 | {"a": [1, "1", 1, None, None, None, [1]], "d": {}, "n": None}, |
| 1090 | "push non existing arrray", |
| 1091 | ), |
| 1092 | ( |
| 1093 | example, |
| 1094 | {"a": [2, 3]}, |
| 1095 | {"a": [1, "1", 1, 2, 3], "d": {}, "n": None}, |
| 1096 | "push two item", |
| 1097 | ), |
| 1098 | ( |
| 1099 | example, |
| 1100 | {"a": [{1: 1}]}, |
| 1101 | {"a": [1, "1", 1, {1: 1}], "d": {}, "n": None}, |
| 1102 | "push a dict", |
| 1103 | ), |
| tierno | 399f6c3 | 2020-05-12 07:36:41 +0000 | [diff] [blame] | 1104 | (example, {"d": [1]}, None, "push over dict"), |
| 1105 | (example, {"n": [1]}, None, "push over None"), |
| 1106 | (example, {"a": 1}, None, "invalid push list non an array"), |
| 1107 | ) |
| 1108 | db_men = DbMemory() |
| 1109 | db_men._find = Mock() |
| 1110 | for db_content, push_list, expected, message in test_set: |
| 1111 | db_content = deepcopy(db_content) |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 1112 | db_men._find.return_value = ((0, db_content),) |
| tierno | 399f6c3 | 2020-05-12 07:36:41 +0000 | [diff] [blame] | 1113 | if expected is None: |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 1114 | self.assertRaises( |
| 1115 | DbException, |
| 1116 | db_men.set_one, |
| 1117 | "table", |
| 1118 | {}, |
| 1119 | None, |
| 1120 | fail_on_empty=False, |
| 1121 | push_list=push_list, |
| 1122 | ) |
| tierno | 399f6c3 | 2020-05-12 07:36:41 +0000 | [diff] [blame] | 1123 | else: |
| 1124 | db_men.set_one("table", {}, None, push_list=push_list) |
| 1125 | self.assertEqual(db_content, expected, message) |
| 1126 | |
| tierno | 7fc50dd | 2020-02-17 12:01:38 +0000 | [diff] [blame] | 1127 | def test_unset_one(self): |
| 1128 | example = {"a": [1, "1", 1], "d": {}, "n": None} |
| 1129 | test_set = ( |
| 1130 | # (database content, set-content, expected database content (None=fails), message) |
| 1131 | (example, {"d.b.c": 1}, example, "unset non existing"), |
| 1132 | (example, {"b": 1}, example, "unset non existing"), |
| 1133 | (example, {"a.6": 1}, example, "unset non existing arrray"), |
| 1134 | (example, {"a": 2}, {"d": {}, "n": None}, "unset array"), |
| 1135 | (example, {"d": 1}, {"a": [1, "1", 1], "n": None}, "unset dict"), |
| 1136 | (example, {"n": 1}, {"a": [1, "1", 1], "d": {}}, "unset None"), |
| 1137 | ) |
| 1138 | db_men = DbMemory() |
| 1139 | db_men._find = Mock() |
| 1140 | for db_content, unset_dict, expected, message in test_set: |
| 1141 | db_content = deepcopy(db_content) |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 1142 | db_men._find.return_value = ((0, db_content),) |
| tierno | 7fc50dd | 2020-02-17 12:01:38 +0000 | [diff] [blame] | 1143 | if expected is None: |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 1144 | self.assertRaises( |
| 1145 | DbException, |
| 1146 | db_men.set_one, |
| 1147 | "table", |
| 1148 | {}, |
| 1149 | None, |
| 1150 | fail_on_empty=False, |
| 1151 | unset=unset_dict, |
| 1152 | ) |
| tierno | 7fc50dd | 2020-02-17 12:01:38 +0000 | [diff] [blame] | 1153 | else: |
| 1154 | db_men.set_one("table", {}, None, unset=unset_dict) |
| 1155 | self.assertEqual(db_content, expected, message) |