| Eduardo Sousa | 4d611d3 | 2018-05-09 19:20:37 +0100 | [diff] [blame] | 1 | import http |
| 2 | import pytest |
| tierno | b3e750b | 2018-09-05 11:25:23 +0200 | [diff] [blame] | 3 | import unittest |
| 4 | from osm_common.dbbase import DbBase, DbException, deep_update |
| Eduardo Sousa | 4d611d3 | 2018-05-09 19:20:37 +0100 | [diff] [blame] | 5 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 6 | |
| Eduardo Sousa | 4d611d3 | 2018-05-09 19:20:37 +0100 | [diff] [blame] | 7 | def exception_message(message): |
| 8 | return "database exception " + message |
| 9 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 10 | |
| Eduardo Sousa | 4d611d3 | 2018-05-09 19:20:37 +0100 | [diff] [blame] | 11 | @pytest.fixture |
| 12 | def db_base(): |
| 13 | return DbBase() |
| 14 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 15 | |
| Eduardo Sousa | 4d611d3 | 2018-05-09 19:20:37 +0100 | [diff] [blame] | 16 | def test_constructor(): |
| 17 | db_base = DbBase() |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 18 | assert db_base is not None |
| Eduardo Sousa | 4d611d3 | 2018-05-09 19:20:37 +0100 | [diff] [blame] | 19 | assert isinstance(db_base, DbBase) |
| 20 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 21 | |
| Eduardo Sousa | 4d611d3 | 2018-05-09 19:20:37 +0100 | [diff] [blame] | 22 | def test_db_connect(db_base): |
| 23 | db_base.db_connect(None) |
| 24 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 25 | |
| Eduardo Sousa | 4d611d3 | 2018-05-09 19:20:37 +0100 | [diff] [blame] | 26 | def test_db_disconnect(db_base): |
| 27 | db_base.db_disconnect() |
| 28 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 29 | |
| Eduardo Sousa | 4d611d3 | 2018-05-09 19:20:37 +0100 | [diff] [blame] | 30 | def test_get_list(db_base): |
| 31 | with pytest.raises(DbException) as excinfo: |
| 32 | db_base.get_list(None, None) |
| 33 | assert str(excinfo.value).startswith(exception_message("Method 'get_list' not implemented")) |
| 34 | assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND |
| 35 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 36 | |
| Eduardo Sousa | 4d611d3 | 2018-05-09 19:20:37 +0100 | [diff] [blame] | 37 | def test_get_one(db_base): |
| 38 | with pytest.raises(DbException) as excinfo: |
| 39 | db_base.get_one(None, None, None, None) |
| 40 | assert str(excinfo.value).startswith(exception_message("Method 'get_one' not implemented")) |
| 41 | assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND |
| 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_create(db_base): |
| 45 | with pytest.raises(DbException) as excinfo: |
| 46 | db_base.create(None, None) |
| 47 | assert str(excinfo.value).startswith(exception_message("Method 'create' not implemented")) |
| 48 | assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND |
| 49 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 50 | |
| Eduardo Sousa | 4d611d3 | 2018-05-09 19:20:37 +0100 | [diff] [blame] | 51 | def test_del_list(db_base): |
| 52 | with pytest.raises(DbException) as excinfo: |
| 53 | db_base.del_list(None, None) |
| 54 | assert str(excinfo.value).startswith(exception_message("Method 'del_list' not implemented")) |
| 55 | assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND |
| 56 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 57 | |
| Eduardo Sousa | 4d611d3 | 2018-05-09 19:20:37 +0100 | [diff] [blame] | 58 | def test_del_one(db_base): |
| 59 | with pytest.raises(DbException) as excinfo: |
| 60 | db_base.del_one(None, None, None) |
| 61 | assert str(excinfo.value).startswith(exception_message("Method 'del_one' not implemented")) |
| 62 | assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND |
| tierno | b3e750b | 2018-09-05 11:25:23 +0200 | [diff] [blame] | 63 | |
| 64 | |
| 65 | class TestDeepUpdate(unittest.TestCase): |
| 66 | def test_update_dict(self): |
| 67 | # Original, patch, expected result |
| 68 | TEST = ( |
| 69 | ({"a": "b"}, {"a": "c"}, {"a": "c"}), |
| 70 | ({"a": "b"}, {"b": "c"}, {"a": "b", "b": "c"}), |
| 71 | ({"a": "b"}, {"a": None}, {}), |
| 72 | ({"a": "b", "b": "c"}, {"a": None}, {"b": "c"}), |
| 73 | ({"a": ["b"]}, {"a": "c"}, {"a": "c"}), |
| 74 | ({"a": "c"}, {"a": ["b"]}, {"a": ["b"]}), |
| 75 | ({"a": {"b": "c"}}, {"a": {"b": "d", "c": None}}, {"a": {"b": "d"}}), |
| 76 | ({"a": [{"b": "c"}]}, {"a": [1]}, {"a": [1]}), |
| 77 | ({1: ["a", "b"]}, {1: ["c", "d"]}, {1: ["c", "d"]}), |
| 78 | ({1: {"a": "b"}}, {1: ["c"]}, {1: ["c"]}), |
| 79 | ({1: {"a": "foo"}}, {1: None}, {}), |
| 80 | ({1: {"a": "foo"}}, {1: "bar"}, {1: "bar"}), |
| 81 | ({"e": None}, {"a": 1}, {"e": None, "a": 1}), |
| 82 | ({1: [1, 2]}, {1: {"a": "b", "c": None}}, {1: {"a": "b"}}), |
| 83 | ({}, {"a": {"bb": {"ccc": None}}}, {"a": {"bb": {}}}), |
| 84 | ) |
| 85 | for t in TEST: |
| 86 | deep_update(t[0], t[1]) |
| 87 | self.assertEqual(t[0], t[2]) |
| 88 | # test deepcopy is done. So that original dictionary does not reference the pach |
| 89 | test_original = {1: {"a": "b"}} |
| 90 | test_patch = {1: {"c": {"d": "e"}}} |
| 91 | test_result = {1: {"a": "b", "c": {"d": "e"}}} |
| 92 | deep_update(test_original, test_patch) |
| 93 | self.assertEqual(test_original, test_result) |
| 94 | test_patch[1]["c"]["f"] = "edition of patch, must not modify original" |
| 95 | self.assertEqual(test_original, test_result) |
| 96 | |
| 97 | def test_update_array(self): |
| 98 | # This TEST contains a list with the the Original, patch, and expected result |
| 99 | TEST = ( |
| 100 | # delete all instances of "a"/"d" |
| 101 | ({"A": ["a", "b", "a"]}, {"A": {"$a": None}}, {"A": ["b"]}), |
| 102 | ({"A": ["a", "b", "a"]}, {"A": {"$d": None}}, {"A": ["a", "b", "a"]}), |
| 103 | # delete and insert at 0 |
| 104 | ({"A": ["a", "b", "c"]}, {"A": {"$b": None, "$+[0]": "b"}}, {"A": ["b", "a", "c"]}), |
| 105 | # delete and edit |
| 106 | ({"A": ["a", "b", "a"]}, {"A": {"$a": None, "$[1]": {"c": "d"}}}, {"A": [{"c": "d"}]}), |
| 107 | # insert if not exist |
| 108 | ({"A": ["a", "b", "c"]}, {"A": {"$+b": "b"}}, {"A": ["a", "b", "c"]}), |
| 109 | ({"A": ["a", "b", "c"]}, {"A": {"$+d": "f"}}, {"A": ["a", "b", "c", "f"]}), |
| 110 | # edit by filter |
| 111 | ({"A": ["a", "b", "a"]}, {"A": {"$b": {"c": "d"}}}, {"A": ["a", {"c": "d"}, "a"]}), |
| 112 | ({"A": ["a", "b", "a"]}, {"A": {"$b": None, "$+[0]": "b", "$+": "c"}}, {"A": ["b", "a", "a", "c"]}), |
| 113 | ({"A": ["a", "b", "a"]}, {"A": {"$c": None}}, {"A": ["a", "b", "a"]}), |
| 114 | # index deletion out of range |
| 115 | ({"A": ["a", "b", "a"]}, {"A": {"$[5]": None}}, {"A": ["a", "b", "a"]}), |
| 116 | # nested array->dict |
| 117 | ({"A": ["a", "b", {"id": "1", "c": {"d": 2}}]}, {"A": {"$id: '1'": {"h": None, "c": {"d": "e", "f": "g"}}}}, |
| 118 | {"A": ["a", "b", {"id": "1", "c": {"d": "e", "f": "g"}}]}), |
| 119 | ({"A": [{"id": 1, "c": {"d": 2}}, {"id": 1, "c": {"f": []}}]}, |
| 120 | {"A": {"$id: 1": {"h": None, "c": {"d": "e", "f": "g"}}}}, |
| 121 | {"A": [{"id": 1, "c": {"d": "e", "f": "g"}}, {"id": 1, "c": {"d": "e", "f": "g"}}]}), |
| 122 | # nested array->array |
| 123 | ({"A": ["a", "b", ["a", "b"]]}, {"A": {"$b": None, "$[2]": {"$b": {}, "$+": "c"}}}, |
| 124 | {"A": ["a", ["a", {}, "c"]]}), |
| 125 | # types str and int different, so not found |
| 126 | ({"A": ["a", {"id": "1", "c": "d"}]}, {"A": {"$id: 1": {"c": "e"}}}, {"A": ["a", {"id": "1", "c": "d"}]}), |
| 127 | |
| 128 | ) |
| 129 | for t in TEST: |
| 130 | print(t) |
| 131 | deep_update(t[0], t[1]) |
| 132 | self.assertEqual(t[0], t[2]) |
| 133 | |
| 134 | def test_update_badformat(self): |
| 135 | # This TEST contains original, incorrect patch and #TODO text that must be present |
| 136 | TEST = ( |
| 137 | # conflict, index 0 is edited twice |
| 138 | ({"A": ["a", "b", "a"]}, {"A": {"$a": None, "$[0]": {"c": "d"}}}), |
| 139 | # conflict, two insertions at same index |
| 140 | ({"A": ["a", "b", "a"]}, {"A": {"$[1]": "c", "$[-2]": "d"}}), |
| 141 | ({"A": ["a", "b", "a"]}, {"A": {"$[1]": "c", "$[+1]": "d"}}), |
| 142 | # bad format keys with and without $ |
| 143 | ({"A": ["a", "b", "a"]}, {"A": {"$b": {"c": "d"}, "c": 3}}), |
| 144 | # bad format empty $ and yaml incorrect |
| 145 | ({"A": ["a", "b", "a"]}, {"A": {"$": 3}}), |
| 146 | ({"A": ["a", "b", "a"]}, {"A": {"$a: b: c": 3}}), |
| 147 | ({"A": ["a", "b", "a"]}, {"A": {"$a: b, c: d": 3}}), |
| 148 | # insertion of None |
| 149 | ({"A": ["a", "b", "a"]}, {"A": {"$+": None}}), |
| 150 | # Not found, insertion of None |
| 151 | ({"A": ["a", "b", "a"]}, {"A": {"$+c": None}}), |
| 152 | # index edition out of range |
| 153 | ({"A": ["a", "b", "a"]}, {"A": {"$[5]": 6}}), |
| 154 | # conflict, two editions on index 2 |
| 155 | ({"A": ["a", {"id": "1", "c": "d"}]}, {"A": {"$id: '1'": {"c": "e"}, "$c: d": {"c": "f"}}}), |
| 156 | ) |
| 157 | for t in TEST: |
| 158 | print(t) |
| 159 | self.assertRaises(DbException, deep_update, t[0], t[1]) |
| 160 | try: |
| 161 | deep_update(t[0], t[1]) |
| 162 | except DbException as e: |
| 163 | print(e) |