3a8861de3dbcd328080116b5332c4f6c99ae6a77
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_key
= "Setting a long master key with numbers 123 and capitals AGHBNHD and symbols %&8)!'"
74 # set self.secret_key obtained when connect
75 db_base1
.set_secret_key(master_key
, replace
=True)
76 db_base1
.set_secret_key(urandom(32))
77 db_base2
.set_secret_key(None, replace
=True)
78 db_base2
.set_secret_key(urandom(30))
79 db_base3
.set_secret_key(master_key
)
80 self
.db_bases
= [db_base1
, db_base2
, db_base3
]
82 def test_encrypt_decrypt(self
):
84 ("plain text 1 ! ", None),
85 ("plain text 2 with salt ! ", "1afd5d1a-4a7e-4d9c-8c65-251290183106"),
86 ("plain text 3 with usalt ! ", u
"1afd5d1a-4a7e-4d9c-8c65-251290183106"),
87 (u
"plain unicode 4 ! ", None),
88 (u
"plain unicode 5 with salt ! ", "1a000d1a-4a7e-4d9c-8c65-251290183106"),
89 (u
"plain unicode 6 with usalt ! ", u
"1abcdd1a-4a7e-4d9c-8c65-251290183106"),
91 for db_base
in self
.db_bases
:
92 for value
, salt
in TEST
:
94 encrypted
= db_base
.encrypt(value
, schema_version
='1.0', salt
=salt
)
95 self
.assertEqual(encrypted
, value
, "value '{}' has been encrypted".format(value
))
96 decrypted
= db_base
.decrypt(encrypted
, schema_version
='1.0', salt
=salt
)
97 self
.assertEqual(decrypted
, value
, "value '{}' has been decrypted".format(value
))
100 encrypted
= db_base
.encrypt(value
, schema_version
='1.1', salt
=salt
)
101 self
.assertNotEqual(encrypted
, value
, "value '{}' has not been encrypted".format(value
))
102 self
.assertIsInstance(encrypted
, str, "Encrypted is not ascii text")
103 decrypted
= db_base
.decrypt(encrypted
, schema_version
='1.1', salt
=salt
)
104 self
.assertEqual(decrypted
, value
, "value is not equal after encryption/decryption")
106 def test_encrypt_decrypt_salt(self
):
107 value
= "value to be encrypted!"
109 for db_base
in self
.db_bases
:
110 for salt
in (None, "salt 1", "1afd5d1a-4a7e-4d9c-8c65-251290183106"):
112 encrypted
.append(db_base
.encrypt(value
, schema_version
='1.1', salt
=salt
))
113 self
.assertNotEqual(encrypted
[-1], value
, "value '{}' has not been encrypted".format(value
))
114 self
.assertIsInstance(encrypted
[-1], str, "Encrypted is not ascii text")
115 decrypted
= db_base
.decrypt(encrypted
[-1], schema_version
='1.1', salt
=salt
)
116 self
.assertEqual(decrypted
, value
, "value is not equal after encryption/decryption")
117 for i
in range(0, len(encrypted
)):
118 for j
in range(i
+1, len(encrypted
)):
119 self
.assertNotEqual(encrypted
[i
], encrypted
[j
],
120 "encryption with different salt must contain different result")
123 class TestDeepUpdate(unittest
.TestCase
):
124 def test_update_dict(self
):
125 # Original, patch, expected result
127 ({"a": "b"}, {"a": "c"}, {"a": "c"}),
128 ({"a": "b"}, {"b": "c"}, {"a": "b", "b": "c"}),
129 ({"a": "b"}, {"a": None}, {}),
130 ({"a": "b", "b": "c"}, {"a": None}, {"b": "c"}),
131 ({"a": ["b"]}, {"a": "c"}, {"a": "c"}),
132 ({"a": "c"}, {"a": ["b"]}, {"a": ["b"]}),
133 ({"a": {"b": "c"}}, {"a": {"b": "d", "c": None}}, {"a": {"b": "d"}}),
134 ({"a": [{"b": "c"}]}, {"a": [1]}, {"a": [1]}),
135 ({1: ["a", "b"]}, {1: ["c", "d"]}, {1: ["c", "d"]}),
136 ({1: {"a": "b"}}, {1: ["c"]}, {1: ["c"]}),
137 ({1: {"a": "foo"}}, {1: None}, {}),
138 ({1: {"a": "foo"}}, {1: "bar"}, {1: "bar"}),
139 ({"e": None}, {"a": 1}, {"e": None, "a": 1}),
140 ({1: [1, 2]}, {1: {"a": "b", "c": None}}, {1: {"a": "b"}}),
141 ({}, {"a": {"bb": {"ccc": None}}}, {"a": {"bb": {}}}),
144 deep_update(t
[0], t
[1])
145 self
.assertEqual(t
[0], t
[2])
146 # test deepcopy is done. So that original dictionary does not reference the pach
147 test_original
= {1: {"a": "b"}}
148 test_patch
= {1: {"c": {"d": "e"}}}
149 test_result
= {1: {"a": "b", "c": {"d": "e"}}}
150 deep_update(test_original
, test_patch
)
151 self
.assertEqual(test_original
, test_result
)
152 test_patch
[1]["c"]["f"] = "edition of patch, must not modify original"
153 self
.assertEqual(test_original
, test_result
)
155 def test_update_array(self
):
156 # This TEST contains a list with the the Original, patch, and expected result
158 # delete all instances of "a"/"d"
159 ({"A": ["a", "b", "a"]}, {"A": {"$a": None}}, {"A": ["b"]}),
160 ({"A": ["a", "b", "a"]}, {"A": {"$d": None}}, {"A": ["a", "b", "a"]}),
161 # delete and insert at 0
162 ({"A": ["a", "b", "c"]}, {"A": {"$b": None, "$+[0]": "b"}}, {"A": ["b", "a", "c"]}),
164 ({"A": ["a", "b", "a"]}, {"A": {"$a": None, "$[1]": {"c": "d"}}}, {"A": [{"c": "d"}]}),
165 # insert if not exist
166 ({"A": ["a", "b", "c"]}, {"A": {"$+b": "b"}}, {"A": ["a", "b", "c"]}),
167 ({"A": ["a", "b", "c"]}, {"A": {"$+d": "f"}}, {"A": ["a", "b", "c", "f"]}),
169 ({"A": ["a", "b", "a"]}, {"A": {"$b": {"c": "d"}}}, {"A": ["a", {"c": "d"}, "a"]}),
170 ({"A": ["a", "b", "a"]}, {"A": {"$b": None, "$+[0]": "b", "$+": "c"}}, {"A": ["b", "a", "a", "c"]}),
171 ({"A": ["a", "b", "a"]}, {"A": {"$c": None}}, {"A": ["a", "b", "a"]}),
172 # index deletion out of range
173 ({"A": ["a", "b", "a"]}, {"A": {"$[5]": None}}, {"A": ["a", "b", "a"]}),
175 ({"A": ["a", "b", {"id": "1", "c": {"d": 2}}]}, {"A": {"$id: '1'": {"h": None, "c": {"d": "e", "f": "g"}}}},
176 {"A": ["a", "b", {"id": "1", "c": {"d": "e", "f": "g"}}]}),
177 ({"A": [{"id": 1, "c": {"d": 2}}, {"id": 1, "c": {"f": []}}]},
178 {"A": {"$id: 1": {"h": None, "c": {"d": "e", "f": "g"}}}},
179 {"A": [{"id": 1, "c": {"d": "e", "f": "g"}}, {"id": 1, "c": {"d": "e", "f": "g"}}]}),
180 # nested array->array
181 ({"A": ["a", "b", ["a", "b"]]}, {"A": {"$b": None, "$[2]": {"$b": {}, "$+": "c"}}},
182 {"A": ["a", ["a", {}, "c"]]}),
183 # types str and int different, so not found
184 ({"A": ["a", {"id": "1", "c": "d"}]}, {"A": {"$id: 1": {"c": "e"}}}, {"A": ["a", {"id": "1", "c": "d"}]}),
189 deep_update(t
[0], t
[1])
190 self
.assertEqual(t
[0], t
[2])
192 def test_update_badformat(self
):
193 # This TEST contains original, incorrect patch and #TODO text that must be present
195 # conflict, index 0 is edited twice
196 ({"A": ["a", "b", "a"]}, {"A": {"$a": None, "$[0]": {"c": "d"}}}),
197 # conflict, two insertions at same index
198 ({"A": ["a", "b", "a"]}, {"A": {"$[1]": "c", "$[-2]": "d"}}),
199 ({"A": ["a", "b", "a"]}, {"A": {"$[1]": "c", "$[+1]": "d"}}),
200 # bad format keys with and without $
201 ({"A": ["a", "b", "a"]}, {"A": {"$b": {"c": "d"}, "c": 3}}),
202 # bad format empty $ and yaml incorrect
203 ({"A": ["a", "b", "a"]}, {"A": {"$": 3}}),
204 ({"A": ["a", "b", "a"]}, {"A": {"$a: b: c": 3}}),
205 ({"A": ["a", "b", "a"]}, {"A": {"$a: b, c: d": 3}}),
207 ({"A": ["a", "b", "a"]}, {"A": {"$+": None}}),
208 # Not found, insertion of None
209 ({"A": ["a", "b", "a"]}, {"A": {"$+c": None}}),
210 # index edition out of range
211 ({"A": ["a", "b", "a"]}, {"A": {"$[5]": 6}}),
212 # conflict, two editions on index 2
213 ({"A": ["a", {"id": "1", "c": "d"}]}, {"A": {"$id: '1'": {"c": "e"}, "$c: d": {"c": "f"}}}),
217 self
.assertRaises(DbException
, deep_update
, t
[0], t
[1])
219 deep_update(t
[0], t
[1])
220 except DbException
as e
:
224 if __name__
== '__main__':