-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtest_park.py
283 lines (211 loc) · 8.61 KB
/
test_park.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# coding: utf-8
# Copyright 2012-2015 litl, LLC. All Rights Reserved.
import operator
import os
import unittest2 as unittest
import park
class KVStoreBase(object):
"""Base tests for KV Stores"""
def test_get_default(self):
self.assertIsNone(self.store.get("missing"))
def test_put_get(self):
key, value = "test_key", "test_value"
self.assertIsNone(self.store.get(key, None))
self.store.put(key, value)
self.assertTrue(self.store.contains(key))
self.assertEqual(value, self.store.get(key))
def test_delete(self):
self.store.put("test_key1", "test_value1")
self.store.put("test_key2", "test_value2")
self.store.delete("test_key1")
self.assertEqual(["test_key2"], list(self.store.keys()))
def test_delete_many(self):
self.store.put("test_key1", "test_value1")
self.store.put("test_key2", "test_value2")
self.store.delete_many(["test_key1", "test_key2"])
self.assertEqual([], list(self.store.keys()))
def test_null_key(self):
key, value = "\x00", "test_value"
self.assertIsNone(self.store.get(key, None))
self.store.put(key, value)
self.assertEqual(value, self.store.get(key))
self.assertEqual([key], list(self.store.keys()))
self.assertEqual([(key, value)], list(self.store.items()))
def test_null_value(self):
key, value = "test_key", "\x00"
self.assertIsNone(self.store.get(key, None))
self.store.put(key, value)
self.assertEqual(value, self.store.get(key))
self.assertEqual([key], list(self.store.keys()))
self.assertEqual([(key, value)], list(self.store.items()))
def test_replace(self):
key = "foo"
self.assertIsNone(self.store.get(key, None))
self.store.put(key, "bar")
self.assertEqual("bar", self.store.get(key))
self.store.put(key, "baz")
self.assertEqual("baz", self.store.get(key))
def test_put_many(self):
items = [
("one", "value1"),
("two", "value2"),
("three", "value3"),
("four", "value4"),
("five", "value5"),
("six", "value6"),
("seven", "value7"),
("eight", "value8"),
("nine", "value9")
]
self.store.put_many(items)
for key, value in items:
self.assertEqual(value, self.store.get(key))
def test_no_keys(self):
self.assertEqual([], list(self.store.keys()))
self.assertEqual([], list(self.store.keys(key_from="foo")))
self.assertEqual([], list(self.store.keys(key_to="bar")))
self.assertEqual([], list(self.store.keys(key_from="foo",
key_to="bar")))
def test_no_items(self):
self.assertEqual([], list(self.store.items()))
self.assertEqual([], list(self.store.items(key_from="foo")))
self.assertEqual([], list(self.store.items(key_to="bar")))
self.assertEqual([], list(self.store.items(key_from="foo",
key_to="bar")))
def test_keys(self):
items = [
("one", "value1"),
("two", "value2"),
("three", "value3"),
("four", "value4"),
("five", "value5"),
("six", "value6"),
("seven", "value7"),
("eight", "value8"),
("nine", "value9")
]
for key, value in items:
self.store.put(key, value)
# Sorted order is: eight five four nine one seven six three two
keys = list(self.store.keys())
expected = "eight five four nine one seven six three two".split()
self.assertEqual(expected, keys)
# Test key_from on keys that are present and missing in the db
keys = list(self.store.keys(key_from="four"))
expected = "four nine one seven six three two".split()
self.assertEqual(expected, keys)
keys = list(self.store.keys(key_from="fo"))
expected = "four nine one seven six three two".split()
self.assertEqual(expected, keys)
# Test key_to
keys = list(self.store.keys(key_to="six"))
expected = "eight five four nine one seven six".split()
self.assertEqual(expected, keys)
keys = list(self.store.keys(key_to="si"))
expected = "eight five four nine one seven".split()
self.assertEqual(expected, keys)
# And test them both together
keys = list(self.store.keys(key_from="five", key_to="three"))
expected = "five four nine one seven six three".split()
self.assertEqual(expected, keys)
def test_prefix_keys(self):
# Fake some interesting keys and values to make sure the
# prefix iterators are working
store = self.store
store.put("a/", "a")
store.put("a/b", "b")
store.put("a/c", "c")
store.put("a/d", "d")
store.put("a/e", "e")
store.put("a/f", "f")
store.put("b/", "b")
store.put("c/", "c")
store.put("d/", "d")
a_list = list(store.prefix_keys("a/"))
self.assertEqual("a/ a/b a/c a/d a/e a/f".split(), a_list)
a_list = list(store.prefix_keys("a/", strip_prefix=True))
self.assertEqual(["", "b", "c", "d", "e", "f"], a_list)
self.assertEqual(["b/"], list(store.prefix_keys("b/")))
self.assertEqual(["c/"], list(store.prefix_keys("c/")))
self.assertEqual(["d/"], list(store.prefix_keys("d/")))
def test_items(self):
put_items = dict([
("one", "value1"),
("two", "value2"),
("three", "value3"),
("four", "value4"),
("five", "value5"),
("six", "value6"),
("seven", "value7"),
("eight", "value8"),
("nine", "value9")
])
for key, value in list(put_items.items()):
self.store.put(key, value)
# Sorted order is: eight five four nine one seven six three two
keys = list(self.store.items())
expected = sorted(list(put_items.items()), key=operator.itemgetter(0))
self.assertEqual(expected, keys)
# Test key_from on keys that are present and missing in the db
keys = list(self.store.items(key_from="four"))
self.assertEqual(expected[2:], keys)
keys = list(self.store.items(key_from="fo"))
self.assertEqual(expected[2:], keys)
# Test key_to
keys = list(self.store.items(key_to="six"))
self.assertEqual(expected[:7], keys)
keys = list(self.store.items(key_to="si"))
self.assertEqual(expected[:6], keys)
# And test them both together
keys = list(self.store.items(key_from="five", key_to="three"))
self.assertEqual(expected[1:8], keys)
def test_prefix_items(self):
# Fake some interesting keys and values to make sure the
# prefix iterators are working
store = self.store
store.put("a/", "a")
store.put("a/b", "b")
store.put("a/c", "c")
store.put("a/d", "d")
store.put("a/e", "e")
store.put("a/f", "f")
store.put("b/", "b")
store.put("c/", "c")
store.put("d/", "d")
expected = [("a/", "a"),
("a/b", "b"),
("a/c", "c"),
("a/d", "d"),
("a/e", "e"),
("a/f", "f")]
a_list = list(store.prefix_items("a/"))
self.assertEqual(expected, a_list)
expected = [("", "a"),
("b", "b"),
("c", "c"),
("d", "d"),
("e", "e"),
("f", "f")]
a_list = list(store.prefix_items("a/", strip_prefix=True))
self.assertEqual(expected, a_list)
def test_context_manager(self):
with self.store as kv:
kv.put("foo", "bar")
kv.put("baz", "quux")
self.assertEqual("bar", kv.get("foo"))
class TestIbatch(unittest.TestCase):
def test_ibatch(self):
items = list(range(10))
batches = park.ibatch(items, 3)
self.assertEqual([0, 1, 2], list(next(batches)))
self.assertEqual([3, 4, 5], list(next(batches)))
self.assertEqual([6, 7, 8], list(next(batches)))
self.assertEqual([9], list(next(batches)))
class TestSQLiteStore(unittest.TestCase, KVStoreBase):
DB = "tests.test_sqlite_store"
def setUp(self):
self.store = park.SQLiteStore(self.DB)
def cleanup():
if os.path.exists(self.DB):
os.unlink(self.DB)
self.addCleanup(cleanup)