4 from osm_common
.dbbase
import DbBase
, DbException
, deep_update
8 def exception_message(message
):
9 return "database exception " + message
17 def test_constructor():
19 assert db_base
is not None
20 assert isinstance(db_base
, DbBase
)
23 def test_db_connect(db_base
):
24 with pytest
.raises(DbException
) as excinfo
:
25 db_base
.db_connect(None)
26 assert str(excinfo
.value
).startswith(exception_message("Method 'db_connect' not implemented"))
29 def test_db_disconnect(db_base
):
30 db_base
.db_disconnect()
33 def test_get_list(db_base
):
34 with pytest
.raises(DbException
) as excinfo
:
35 db_base
.get_list(None, None)
36 assert str(excinfo
.value
).startswith(exception_message("Method 'get_list' not implemented"))
37 assert excinfo
.value
.http_code
== http
.HTTPStatus
.NOT_FOUND
40 def test_get_one(db_base
):
41 with pytest
.raises(DbException
) as excinfo
:
42 db_base
.get_one(None, None, None, None)
43 assert str(excinfo
.value
).startswith(exception_message("Method 'get_one' not implemented"))
44 assert excinfo
.value
.http_code
== http
.HTTPStatus
.NOT_FOUND
47 def test_create(db_base
):
48 with pytest
.raises(DbException
) as excinfo
:
49 db_base
.create(None, None)
50 assert str(excinfo
.value
).startswith(exception_message("Method 'create' not implemented"))
51 assert excinfo
.value
.http_code
== http
.HTTPStatus
.NOT_FOUND
54 def test_del_list(db_base
):
55 with pytest
.raises(DbException
) as excinfo
:
56 db_base
.del_list(None, None)
57 assert str(excinfo
.value
).startswith(exception_message("Method 'del_list' not implemented"))
58 assert excinfo
.value
.http_code
== http
.HTTPStatus
.NOT_FOUND
61 def test_del_one(db_base
):
62 with pytest
.raises(DbException
) as excinfo
:
63 db_base
.del_one(None, None, None)
64 assert str(excinfo
.value
).startswith(exception_message("Method 'del_one' not implemented"))
65 assert excinfo
.value
.http_code
== http
.HTTPStatus
.NOT_FOUND
68 class TestEncryption(unittest
.TestCase
):
70 master_password
= "Setting a long master password with numbers 123 and capitals AGHBNHD and symbols %&8)!'"
73 # set self.secret_key obtained when connect
74 db_base1
.secret_key
= DbBase
._join
_passwords
(urandom(32), master_password
)
75 db_base2
.secret_key
= DbBase
._join
_passwords
(urandom(32), None)
76 self
.db_base
= [db_base1
, db_base2
]
78 def test_encrypt_decrypt(self
):
80 ("plain text 1 ! ", None),
81 ("plain text 2 with salt ! ", "1afd5d1a-4a7e-4d9c-8c65-251290183106"),
82 ("plain text 3 with usalt ! ", u
"1afd5d1a-4a7e-4d9c-8c65-251290183106"),
83 (u
"plain unicode 4 ! ", None),
84 (u
"plain unicode 5 with salt ! ", "1a000d1a-4a7e-4d9c-8c65-251290183106"),
85 (u
"plain unicode 6 with usalt ! ", u
"1abcdd1a-4a7e-4d9c-8c65-251290183106"),
87 for db_base
in self
.db_base
:
88 for value
, salt
in TEST
:
90 encrypted
= db_base
.encrypt(value
, schema_version
='1.0', salt
=salt
)
91 self
.assertEqual(encrypted
, value
, "value '{}' has been encrypted".format(value
))
92 decrypted
= db_base
.decrypt(encrypted
, schema_version
='1.0', salt
=salt
)
93 self
.assertEqual(decrypted
, value
, "value '{}' has been decrypted".format(value
))
96 encrypted
= db_base
.encrypt(value
, schema_version
='1.1', salt
=salt
)
97 self
.assertNotEqual(encrypted
, value
, "value '{}' has not been encrypted".format(value
))
98 self
.assertIsInstance(encrypted
, str, "Encrypted is not ascii text")
99 decrypted
= db_base
.decrypt(encrypted
, schema_version
='1.1', salt
=salt
)
100 self
.assertEqual(decrypted
, value
, "value is not equal after encryption/decryption")
102 def test_encrypt_decrypt_salt(self
):
103 value
= "value to be encrypted!"
105 for db_base
in self
.db_base
:
106 for salt
in (None, "salt 1", "1afd5d1a-4a7e-4d9c-8c65-251290183106"):
108 encrypted
.append(db_base
.encrypt(value
, schema_version
='1.1', salt
=salt
))
109 self
.assertNotEqual(encrypted
[-1], value
, "value '{}' has not been encrypted".format(value
))
110 self
.assertIsInstance(encrypted
[-1], str, "Encrypted is not ascii text")
111 decrypted
= db_base
.decrypt(encrypted
[-1], schema_version
='1.1', salt
=salt
)
112 self
.assertEqual(decrypted
, value
, "value is not equal after encryption/decryption")
113 for i
in range(0, len(encrypted
)):
114 for j
in range(i
+1, len(encrypted
)):
115 self
.assertNotEqual(encrypted
[i
], encrypted
[j
],
116 "encryption with different salt contains different result")
119 class TestDeepUpdate(unittest
.TestCase
):
120 def test_update_dict(self
):
121 # Original, patch, expected result
123 ({"a": "b"}, {"a": "c"}, {"a": "c"}),
124 ({"a": "b"}, {"b": "c"}, {"a": "b", "b": "c"}),
125 ({"a": "b"}, {"a": None}, {}),
126 ({"a": "b", "b": "c"}, {"a": None}, {"b": "c"}),
127 ({"a": ["b"]}, {"a": "c"}, {"a": "c"}),
128 ({"a": "c"}, {"a": ["b"]}, {"a": ["b"]}),
129 ({"a": {"b": "c"}}, {"a": {"b": "d", "c": None}}, {"a": {"b": "d"}}),
130 ({"a": [{"b": "c"}]}, {"a": [1]}, {"a": [1]}),
131 ({1: ["a", "b"]}, {1: ["c", "d"]}, {1: ["c", "d"]}),
132 ({1: {"a": "b"}}, {1: ["c"]}, {1: ["c"]}),
133 ({1: {"a": "foo"}}, {1: None}, {}),
134 ({1: {"a": "foo"}}, {1: "bar"}, {1: "bar"}),
135 ({"e": None}, {"a": 1}, {"e": None, "a": 1}),
136 ({1: [1, 2]}, {1: {"a": "b", "c": None}}, {1: {"a": "b"}}),
137 ({}, {"a": {"bb": {"ccc": None}}}, {"a": {"bb": {}}}),
140 deep_update(t
[0], t
[1])
141 self
.assertEqual(t
[0], t
[2])
142 # test deepcopy is done. So that original dictionary does not reference the pach
143 test_original
= {1: {"a": "b"}}
144 test_patch
= {1: {"c": {"d": "e"}}}
145 test_result
= {1: {"a": "b", "c": {"d": "e"}}}
146 deep_update(test_original
, test_patch
)
147 self
.assertEqual(test_original
, test_result
)
148 test_patch
[1]["c"]["f"] = "edition of patch, must not modify original"
149 self
.assertEqual(test_original
, test_result
)
151 def test_update_array(self
):
152 # This TEST contains a list with the the Original, patch, and expected result
154 # delete all instances of "a"/"d"
155 ({"A": ["a", "b", "a"]}, {"A": {"$a": None}}, {"A": ["b"]}),
156 ({"A": ["a", "b", "a"]}, {"A": {"$d": None}}, {"A": ["a", "b", "a"]}),
157 # delete and insert at 0
158 ({"A": ["a", "b", "c"]}, {"A": {"$b": None, "$+[0]": "b"}}, {"A": ["b", "a", "c"]}),
160 ({"A": ["a", "b", "a"]}, {"A": {"$a": None, "$[1]": {"c": "d"}}}, {"A": [{"c": "d"}]}),
161 # insert if not exist
162 ({"A": ["a", "b", "c"]}, {"A": {"$+b": "b"}}, {"A": ["a", "b", "c"]}),
163 ({"A": ["a", "b", "c"]}, {"A": {"$+d": "f"}}, {"A": ["a", "b", "c", "f"]}),
165 ({"A": ["a", "b", "a"]}, {"A": {"$b": {"c": "d"}}}, {"A": ["a", {"c": "d"}, "a"]}),
166 ({"A": ["a", "b", "a"]}, {"A": {"$b": None, "$+[0]": "b", "$+": "c"}}, {"A": ["b", "a", "a", "c"]}),
167 ({"A": ["a", "b", "a"]}, {"A": {"$c": None}}, {"A": ["a", "b", "a"]}),
168 # index deletion out of range
169 ({"A": ["a", "b", "a"]}, {"A": {"$[5]": None}}, {"A": ["a", "b", "a"]}),
171 ({"A": ["a", "b", {"id": "1", "c": {"d": 2}}]}, {"A": {"$id: '1'": {"h": None, "c": {"d": "e", "f": "g"}}}},
172 {"A": ["a", "b", {"id": "1", "c": {"d": "e", "f": "g"}}]}),
173 ({"A": [{"id": 1, "c": {"d": 2}}, {"id": 1, "c": {"f": []}}]},
174 {"A": {"$id: 1": {"h": None, "c": {"d": "e", "f": "g"}}}},
175 {"A": [{"id": 1, "c": {"d": "e", "f": "g"}}, {"id": 1, "c": {"d": "e", "f": "g"}}]}),
176 # nested array->array
177 ({"A": ["a", "b", ["a", "b"]]}, {"A": {"$b": None, "$[2]": {"$b": {}, "$+": "c"}}},
178 {"A": ["a", ["a", {}, "c"]]}),
179 # types str and int different, so not found
180 ({"A": ["a", {"id": "1", "c": "d"}]}, {"A": {"$id: 1": {"c": "e"}}}, {"A": ["a", {"id": "1", "c": "d"}]}),
185 deep_update(t
[0], t
[1])
186 self
.assertEqual(t
[0], t
[2])
188 def test_update_badformat(self
):
189 # This TEST contains original, incorrect patch and #TODO text that must be present
191 # conflict, index 0 is edited twice
192 ({"A": ["a", "b", "a"]}, {"A": {"$a": None, "$[0]": {"c": "d"}}}),
193 # conflict, two insertions at same index
194 ({"A": ["a", "b", "a"]}, {"A": {"$[1]": "c", "$[-2]": "d"}}),
195 ({"A": ["a", "b", "a"]}, {"A": {"$[1]": "c", "$[+1]": "d"}}),
196 # bad format keys with and without $
197 ({"A": ["a", "b", "a"]}, {"A": {"$b": {"c": "d"}, "c": 3}}),
198 # bad format empty $ and yaml incorrect
199 ({"A": ["a", "b", "a"]}, {"A": {"$": 3}}),
200 ({"A": ["a", "b", "a"]}, {"A": {"$a: b: c": 3}}),
201 ({"A": ["a", "b", "a"]}, {"A": {"$a: b, c: d": 3}}),
203 ({"A": ["a", "b", "a"]}, {"A": {"$+": None}}),
204 # Not found, insertion of None
205 ({"A": ["a", "b", "a"]}, {"A": {"$+c": None}}),
206 # index edition out of range
207 ({"A": ["a", "b", "a"]}, {"A": {"$[5]": 6}}),
208 # conflict, two editions on index 2
209 ({"A": ["a", {"id": "1", "c": "d"}]}, {"A": {"$id: '1'": {"c": "e"}, "$c: d": {"c": "f"}}}),
213 self
.assertRaises(DbException
, deep_update
, t
[0], t
[1])
215 deep_update(t
[0], t
[1])
216 except DbException
as e
:
220 if __name__
== '__main__':