4 from osm_common
.dbbase
import DbBase
, DbException
, deep_update
7 def exception_message(message
):
8 return "database exception " + message
16 def test_constructor():
18 assert db_base
is not None
19 assert isinstance(db_base
, DbBase
)
22 def test_db_connect(db_base
):
23 db_base
.db_connect(None)
26 def test_db_disconnect(db_base
):
27 db_base
.db_disconnect()
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
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
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
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
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
65 class TestDeepUpdate(unittest
.TestCase
):
66 def test_update_dict(self
):
67 # Original, patch, expected result
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": {}}}),
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
)
97 def test_update_array(self
):
98 # This TEST contains a list with the the Original, patch, and expected result
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"]}),
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"]}),
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"]}),
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"}]}),
131 deep_update(t
[0], t
[1])
132 self
.assertEqual(t
[0], t
[2])
134 def test_update_badformat(self
):
135 # This TEST contains original, incorrect patch and #TODO text that must be present
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}}),
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"}}}),
159 self
.assertRaises(DbException
, deep_update
, t
[0], t
[1])
161 deep_update(t
[0], t
[1])
162 except DbException
as e
: