| 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 | 4d611d3 | 2018-05-09 19:20:37 +0100 | [diff] [blame] | 20 | import http |
| tierno | bd5a402 | 2019-01-30 09:48:38 +0000 | [diff] [blame] | 21 | from http import HTTPStatus |
| aticig | 3dd0db6 | 2022-03-04 19:35:45 +0300 | [diff] [blame^] | 22 | from os import urandom |
| 23 | import unittest |
| 24 | |
| 25 | from osm_common.dbbase import DbBase, DbException, deep_update |
| 26 | import pytest |
| Eduardo Sousa | 4d611d3 | 2018-05-09 19:20:37 +0100 | [diff] [blame] | 27 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 28 | |
| Eduardo Sousa | 4d611d3 | 2018-05-09 19:20:37 +0100 | [diff] [blame] | 29 | def exception_message(message): |
| 30 | return "database exception " + message |
| 31 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 32 | |
| Eduardo Sousa | 4d611d3 | 2018-05-09 19:20:37 +0100 | [diff] [blame] | 33 | @pytest.fixture |
| 34 | def db_base(): |
| 35 | return DbBase() |
| 36 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 37 | |
| Eduardo Sousa | 4d611d3 | 2018-05-09 19:20:37 +0100 | [diff] [blame] | 38 | def test_constructor(): |
| 39 | db_base = DbBase() |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 40 | assert db_base is not None |
| Eduardo Sousa | 4d611d3 | 2018-05-09 19:20:37 +0100 | [diff] [blame] | 41 | assert isinstance(db_base, DbBase) |
| 42 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 43 | |
| Eduardo Sousa | 4d611d3 | 2018-05-09 19:20:37 +0100 | [diff] [blame] | 44 | def test_db_connect(db_base): |
| tierno | 136f295 | 2018-10-19 13:01:03 +0200 | [diff] [blame] | 45 | with pytest.raises(DbException) as excinfo: |
| 46 | db_base.db_connect(None) |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 47 | assert str(excinfo.value).startswith( |
| 48 | exception_message("Method 'db_connect' not implemented") |
| 49 | ) |
| Eduardo Sousa | 4d611d3 | 2018-05-09 19:20:37 +0100 | [diff] [blame] | 50 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 51 | |
| Eduardo Sousa | 4d611d3 | 2018-05-09 19:20:37 +0100 | [diff] [blame] | 52 | def test_db_disconnect(db_base): |
| 53 | db_base.db_disconnect() |
| 54 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 55 | |
| Eduardo Sousa | 4d611d3 | 2018-05-09 19:20:37 +0100 | [diff] [blame] | 56 | def test_get_list(db_base): |
| 57 | with pytest.raises(DbException) as excinfo: |
| 58 | db_base.get_list(None, None) |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 59 | assert str(excinfo.value).startswith( |
| 60 | exception_message("Method 'get_list' not implemented") |
| 61 | ) |
| Eduardo Sousa | 4d611d3 | 2018-05-09 19:20:37 +0100 | [diff] [blame] | 62 | assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND |
| 63 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 64 | |
| Eduardo Sousa | 4d611d3 | 2018-05-09 19:20:37 +0100 | [diff] [blame] | 65 | def test_get_one(db_base): |
| 66 | with pytest.raises(DbException) as excinfo: |
| 67 | db_base.get_one(None, None, None, None) |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 68 | assert str(excinfo.value).startswith( |
| 69 | exception_message("Method 'get_one' not implemented") |
| 70 | ) |
| Eduardo Sousa | 4d611d3 | 2018-05-09 19:20:37 +0100 | [diff] [blame] | 71 | assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND |
| 72 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 73 | |
| Eduardo Sousa | 4d611d3 | 2018-05-09 19:20:37 +0100 | [diff] [blame] | 74 | def test_create(db_base): |
| 75 | with pytest.raises(DbException) as excinfo: |
| 76 | db_base.create(None, None) |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 77 | assert str(excinfo.value).startswith( |
| 78 | exception_message("Method 'create' not implemented") |
| 79 | ) |
| Eduardo Sousa | 4d611d3 | 2018-05-09 19:20:37 +0100 | [diff] [blame] | 80 | assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND |
| 81 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 82 | |
| tierno | 2c9794c | 2020-04-29 10:24:28 +0000 | [diff] [blame] | 83 | def test_create_list(db_base): |
| 84 | with pytest.raises(DbException) as excinfo: |
| 85 | db_base.create_list(None, None) |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 86 | assert str(excinfo.value).startswith( |
| 87 | exception_message("Method 'create_list' not implemented") |
| 88 | ) |
| tierno | 2c9794c | 2020-04-29 10:24:28 +0000 | [diff] [blame] | 89 | assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND |
| 90 | |
| 91 | |
| Eduardo Sousa | 4d611d3 | 2018-05-09 19:20:37 +0100 | [diff] [blame] | 92 | def test_del_list(db_base): |
| 93 | with pytest.raises(DbException) as excinfo: |
| 94 | db_base.del_list(None, None) |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 95 | assert str(excinfo.value).startswith( |
| 96 | exception_message("Method 'del_list' not implemented") |
| 97 | ) |
| Eduardo Sousa | 4d611d3 | 2018-05-09 19:20:37 +0100 | [diff] [blame] | 98 | assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND |
| 99 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 100 | |
| Eduardo Sousa | 4d611d3 | 2018-05-09 19:20:37 +0100 | [diff] [blame] | 101 | def test_del_one(db_base): |
| 102 | with pytest.raises(DbException) as excinfo: |
| 103 | db_base.del_one(None, None, None) |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 104 | assert str(excinfo.value).startswith( |
| 105 | exception_message("Method 'del_one' not implemented") |
| 106 | ) |
| Eduardo Sousa | 4d611d3 | 2018-05-09 19:20:37 +0100 | [diff] [blame] | 107 | assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND |
| tierno | b3e750b | 2018-09-05 11:25:23 +0200 | [diff] [blame] | 108 | |
| 109 | |
| tierno | 136f295 | 2018-10-19 13:01:03 +0200 | [diff] [blame] | 110 | class TestEncryption(unittest.TestCase): |
| 111 | def setUp(self): |
| tierno | eef7cb7 | 2018-11-12 11:51:49 +0100 | [diff] [blame] | 112 | master_key = "Setting a long master key with numbers 123 and capitals AGHBNHD and symbols %&8)!'" |
| tierno | cfc5272 | 2018-10-23 11:41:49 +0200 | [diff] [blame] | 113 | db_base1 = DbBase() |
| tierno | 136f295 | 2018-10-19 13:01:03 +0200 | [diff] [blame] | 114 | db_base2 = DbBase() |
| tierno | eef7cb7 | 2018-11-12 11:51:49 +0100 | [diff] [blame] | 115 | db_base3 = DbBase() |
| tierno | 136f295 | 2018-10-19 13:01:03 +0200 | [diff] [blame] | 116 | # set self.secret_key obtained when connect |
| tierno | eef7cb7 | 2018-11-12 11:51:49 +0100 | [diff] [blame] | 117 | db_base1.set_secret_key(master_key, replace=True) |
| 118 | db_base1.set_secret_key(urandom(32)) |
| 119 | db_base2.set_secret_key(None, replace=True) |
| 120 | db_base2.set_secret_key(urandom(30)) |
| 121 | db_base3.set_secret_key(master_key) |
| 122 | self.db_bases = [db_base1, db_base2, db_base3] |
| tierno | 136f295 | 2018-10-19 13:01:03 +0200 | [diff] [blame] | 123 | |
| 124 | def test_encrypt_decrypt(self): |
| 125 | TEST = ( |
| 126 | ("plain text 1 ! ", None), |
| 127 | ("plain text 2 with salt ! ", "1afd5d1a-4a7e-4d9c-8c65-251290183106"), |
| tierno | 136f295 | 2018-10-19 13:01:03 +0200 | [diff] [blame] | 128 | ) |
| tierno | eef7cb7 | 2018-11-12 11:51:49 +0100 | [diff] [blame] | 129 | for db_base in self.db_bases: |
| tierno | 136f295 | 2018-10-19 13:01:03 +0200 | [diff] [blame] | 130 | for value, salt in TEST: |
| 131 | # no encryption |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 132 | encrypted = db_base.encrypt(value, schema_version="1.0", salt=salt) |
| 133 | self.assertEqual( |
| 134 | encrypted, value, "value '{}' has been encrypted".format(value) |
| 135 | ) |
| 136 | decrypted = db_base.decrypt(encrypted, schema_version="1.0", salt=salt) |
| 137 | self.assertEqual( |
| 138 | decrypted, value, "value '{}' has been decrypted".format(value) |
| 139 | ) |
| tierno | 136f295 | 2018-10-19 13:01:03 +0200 | [diff] [blame] | 140 | |
| 141 | # encrypt/decrypt |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 142 | encrypted = db_base.encrypt(value, schema_version="1.1", salt=salt) |
| 143 | self.assertNotEqual( |
| 144 | encrypted, value, "value '{}' has not been encrypted".format(value) |
| 145 | ) |
| tierno | 136f295 | 2018-10-19 13:01:03 +0200 | [diff] [blame] | 146 | self.assertIsInstance(encrypted, str, "Encrypted is not ascii text") |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 147 | decrypted = db_base.decrypt(encrypted, schema_version="1.1", salt=salt) |
| 148 | self.assertEqual( |
| 149 | decrypted, value, "value is not equal after encryption/decryption" |
| 150 | ) |
| tierno | 136f295 | 2018-10-19 13:01:03 +0200 | [diff] [blame] | 151 | |
| 152 | def test_encrypt_decrypt_salt(self): |
| 153 | value = "value to be encrypted!" |
| 154 | encrypted = [] |
| tierno | eef7cb7 | 2018-11-12 11:51:49 +0100 | [diff] [blame] | 155 | for db_base in self.db_bases: |
| tierno | 136f295 | 2018-10-19 13:01:03 +0200 | [diff] [blame] | 156 | for salt in (None, "salt 1", "1afd5d1a-4a7e-4d9c-8c65-251290183106"): |
| 157 | # encrypt/decrypt |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 158 | encrypted.append( |
| 159 | db_base.encrypt(value, schema_version="1.1", salt=salt) |
| 160 | ) |
| 161 | self.assertNotEqual( |
| 162 | encrypted[-1], |
| 163 | value, |
| 164 | "value '{}' has not been encrypted".format(value), |
| 165 | ) |
| tierno | 136f295 | 2018-10-19 13:01:03 +0200 | [diff] [blame] | 166 | self.assertIsInstance(encrypted[-1], str, "Encrypted is not ascii text") |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 167 | decrypted = db_base.decrypt( |
| 168 | encrypted[-1], schema_version="1.1", salt=salt |
| 169 | ) |
| 170 | self.assertEqual( |
| 171 | decrypted, value, "value is not equal after encryption/decryption" |
| 172 | ) |
| tierno | 136f295 | 2018-10-19 13:01:03 +0200 | [diff] [blame] | 173 | for i in range(0, len(encrypted)): |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 174 | for j in range(i + 1, len(encrypted)): |
| 175 | self.assertNotEqual( |
| 176 | encrypted[i], |
| 177 | encrypted[j], |
| 178 | "encryption with different salt must contain different result", |
| 179 | ) |
| tierno | bd5a402 | 2019-01-30 09:48:38 +0000 | [diff] [blame] | 180 | # decrypt with a different master key |
| 181 | try: |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 182 | decrypted = self.db_bases[-1].decrypt( |
| 183 | encrypted[0], schema_version="1.1", salt=None |
| 184 | ) |
| 185 | self.assertNotEqual( |
| 186 | encrypted[0], |
| 187 | decrypted, |
| 188 | "Decryption with different KEY must generate different result", |
| 189 | ) |
| tierno | bd5a402 | 2019-01-30 09:48:38 +0000 | [diff] [blame] | 190 | except DbException as e: |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 191 | self.assertEqual( |
| 192 | e.http_code, |
| 193 | HTTPStatus.INTERNAL_SERVER_ERROR, |
| 194 | "Decryption with different KEY does not provide expected http_code", |
| 195 | ) |
| tierno | 136f295 | 2018-10-19 13:01:03 +0200 | [diff] [blame] | 196 | |
| 197 | |
| tierno | b3e750b | 2018-09-05 11:25:23 +0200 | [diff] [blame] | 198 | class TestDeepUpdate(unittest.TestCase): |
| 199 | def test_update_dict(self): |
| 200 | # Original, patch, expected result |
| 201 | TEST = ( |
| 202 | ({"a": "b"}, {"a": "c"}, {"a": "c"}), |
| 203 | ({"a": "b"}, {"b": "c"}, {"a": "b", "b": "c"}), |
| 204 | ({"a": "b"}, {"a": None}, {}), |
| 205 | ({"a": "b", "b": "c"}, {"a": None}, {"b": "c"}), |
| 206 | ({"a": ["b"]}, {"a": "c"}, {"a": "c"}), |
| 207 | ({"a": "c"}, {"a": ["b"]}, {"a": ["b"]}), |
| 208 | ({"a": {"b": "c"}}, {"a": {"b": "d", "c": None}}, {"a": {"b": "d"}}), |
| 209 | ({"a": [{"b": "c"}]}, {"a": [1]}, {"a": [1]}), |
| 210 | ({1: ["a", "b"]}, {1: ["c", "d"]}, {1: ["c", "d"]}), |
| 211 | ({1: {"a": "b"}}, {1: ["c"]}, {1: ["c"]}), |
| 212 | ({1: {"a": "foo"}}, {1: None}, {}), |
| 213 | ({1: {"a": "foo"}}, {1: "bar"}, {1: "bar"}), |
| 214 | ({"e": None}, {"a": 1}, {"e": None, "a": 1}), |
| 215 | ({1: [1, 2]}, {1: {"a": "b", "c": None}}, {1: {"a": "b"}}), |
| 216 | ({}, {"a": {"bb": {"ccc": None}}}, {"a": {"bb": {}}}), |
| 217 | ) |
| 218 | for t in TEST: |
| 219 | deep_update(t[0], t[1]) |
| 220 | self.assertEqual(t[0], t[2]) |
| 221 | # test deepcopy is done. So that original dictionary does not reference the pach |
| 222 | test_original = {1: {"a": "b"}} |
| 223 | test_patch = {1: {"c": {"d": "e"}}} |
| 224 | test_result = {1: {"a": "b", "c": {"d": "e"}}} |
| 225 | deep_update(test_original, test_patch) |
| 226 | self.assertEqual(test_original, test_result) |
| 227 | test_patch[1]["c"]["f"] = "edition of patch, must not modify original" |
| 228 | self.assertEqual(test_original, test_result) |
| 229 | |
| 230 | def test_update_array(self): |
| 231 | # This TEST contains a list with the the Original, patch, and expected result |
| 232 | TEST = ( |
| 233 | # delete all instances of "a"/"d" |
| 234 | ({"A": ["a", "b", "a"]}, {"A": {"$a": None}}, {"A": ["b"]}), |
| 235 | ({"A": ["a", "b", "a"]}, {"A": {"$d": None}}, {"A": ["a", "b", "a"]}), |
| 236 | # delete and insert at 0 |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 237 | ( |
| 238 | {"A": ["a", "b", "c"]}, |
| 239 | {"A": {"$b": None, "$+[0]": "b"}}, |
| 240 | {"A": ["b", "a", "c"]}, |
| 241 | ), |
| tierno | b3e750b | 2018-09-05 11:25:23 +0200 | [diff] [blame] | 242 | # delete and edit |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 243 | ( |
| 244 | {"A": ["a", "b", "a"]}, |
| 245 | {"A": {"$a": None, "$[1]": {"c": "d"}}}, |
| 246 | {"A": [{"c": "d"}]}, |
| 247 | ), |
| tierno | b3e750b | 2018-09-05 11:25:23 +0200 | [diff] [blame] | 248 | # insert if not exist |
| 249 | ({"A": ["a", "b", "c"]}, {"A": {"$+b": "b"}}, {"A": ["a", "b", "c"]}), |
| 250 | ({"A": ["a", "b", "c"]}, {"A": {"$+d": "f"}}, {"A": ["a", "b", "c", "f"]}), |
| 251 | # edit by filter |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 252 | ( |
| 253 | {"A": ["a", "b", "a"]}, |
| 254 | {"A": {"$b": {"c": "d"}}}, |
| 255 | {"A": ["a", {"c": "d"}, "a"]}, |
| 256 | ), |
| 257 | ( |
| 258 | {"A": ["a", "b", "a"]}, |
| 259 | {"A": {"$b": None, "$+[0]": "b", "$+": "c"}}, |
| 260 | {"A": ["b", "a", "a", "c"]}, |
| 261 | ), |
| tierno | b3e750b | 2018-09-05 11:25:23 +0200 | [diff] [blame] | 262 | ({"A": ["a", "b", "a"]}, {"A": {"$c": None}}, {"A": ["a", "b", "a"]}), |
| 263 | # index deletion out of range |
| 264 | ({"A": ["a", "b", "a"]}, {"A": {"$[5]": None}}, {"A": ["a", "b", "a"]}), |
| 265 | # nested array->dict |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 266 | ( |
| 267 | {"A": ["a", "b", {"id": "1", "c": {"d": 2}}]}, |
| 268 | {"A": {"$id: '1'": {"h": None, "c": {"d": "e", "f": "g"}}}}, |
| 269 | {"A": ["a", "b", {"id": "1", "c": {"d": "e", "f": "g"}}]}, |
| 270 | ), |
| 271 | ( |
| 272 | {"A": [{"id": 1, "c": {"d": 2}}, {"id": 1, "c": {"f": []}}]}, |
| 273 | {"A": {"$id: 1": {"h": None, "c": {"d": "e", "f": "g"}}}}, |
| 274 | { |
| 275 | "A": [ |
| 276 | {"id": 1, "c": {"d": "e", "f": "g"}}, |
| 277 | {"id": 1, "c": {"d": "e", "f": "g"}}, |
| 278 | ] |
| 279 | }, |
| 280 | ), |
| tierno | b3e750b | 2018-09-05 11:25:23 +0200 | [diff] [blame] | 281 | # nested array->array |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 282 | ( |
| 283 | {"A": ["a", "b", ["a", "b"]]}, |
| 284 | {"A": {"$b": None, "$[2]": {"$b": {}, "$+": "c"}}}, |
| 285 | {"A": ["a", ["a", {}, "c"]]}, |
| 286 | ), |
| tierno | b3e750b | 2018-09-05 11:25:23 +0200 | [diff] [blame] | 287 | # types str and int different, so not found |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 288 | ( |
| 289 | {"A": ["a", {"id": "1", "c": "d"}]}, |
| 290 | {"A": {"$id: 1": {"c": "e"}}}, |
| 291 | {"A": ["a", {"id": "1", "c": "d"}]}, |
| 292 | ), |
| tierno | b3e750b | 2018-09-05 11:25:23 +0200 | [diff] [blame] | 293 | ) |
| 294 | for t in TEST: |
| 295 | print(t) |
| 296 | deep_update(t[0], t[1]) |
| 297 | self.assertEqual(t[0], t[2]) |
| 298 | |
| 299 | def test_update_badformat(self): |
| 300 | # This TEST contains original, incorrect patch and #TODO text that must be present |
| 301 | TEST = ( |
| 302 | # conflict, index 0 is edited twice |
| 303 | ({"A": ["a", "b", "a"]}, {"A": {"$a": None, "$[0]": {"c": "d"}}}), |
| 304 | # conflict, two insertions at same index |
| 305 | ({"A": ["a", "b", "a"]}, {"A": {"$[1]": "c", "$[-2]": "d"}}), |
| 306 | ({"A": ["a", "b", "a"]}, {"A": {"$[1]": "c", "$[+1]": "d"}}), |
| 307 | # bad format keys with and without $ |
| 308 | ({"A": ["a", "b", "a"]}, {"A": {"$b": {"c": "d"}, "c": 3}}), |
| 309 | # bad format empty $ and yaml incorrect |
| 310 | ({"A": ["a", "b", "a"]}, {"A": {"$": 3}}), |
| 311 | ({"A": ["a", "b", "a"]}, {"A": {"$a: b: c": 3}}), |
| 312 | ({"A": ["a", "b", "a"]}, {"A": {"$a: b, c: d": 3}}), |
| 313 | # insertion of None |
| 314 | ({"A": ["a", "b", "a"]}, {"A": {"$+": None}}), |
| 315 | # Not found, insertion of None |
| 316 | ({"A": ["a", "b", "a"]}, {"A": {"$+c": None}}), |
| 317 | # index edition out of range |
| 318 | ({"A": ["a", "b", "a"]}, {"A": {"$[5]": 6}}), |
| 319 | # conflict, two editions on index 2 |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 320 | ( |
| 321 | {"A": ["a", {"id": "1", "c": "d"}]}, |
| 322 | {"A": {"$id: '1'": {"c": "e"}, "$c: d": {"c": "f"}}}, |
| 323 | ), |
| tierno | b3e750b | 2018-09-05 11:25:23 +0200 | [diff] [blame] | 324 | ) |
| 325 | for t in TEST: |
| 326 | print(t) |
| 327 | self.assertRaises(DbException, deep_update, t[0], t[1]) |
| 328 | try: |
| 329 | deep_update(t[0], t[1]) |
| 330 | except DbException as e: |
| 331 | print(e) |
| tierno | 136f295 | 2018-10-19 13:01:03 +0200 | [diff] [blame] | 332 | |
| 333 | |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 334 | if __name__ == "__main__": |
| tierno | 136f295 | 2018-10-19 13:01:03 +0200 | [diff] [blame] | 335 | unittest.main() |