forked from Attumm/redis-dict
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_redis_dict.py
370 lines (282 loc) · 12.9 KB
/
test_redis_dict.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
import unittest
import redis
from redis_dict import RedisDict
# !! Make sure you don't have keys named like this, they will be deleted.
TEST_NAMESPACE_PREFIX = 'test_rd'
redis_config = {
'host': 'localhost',
'port': 6379,
'db': 0,
}
class TestRedisDict(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.redisdb = redis.StrictRedis(**redis_config)
cls.r = cls.create_redis_dict()
@classmethod
def tearDownClass(cls):
cls.clear_test_namespace()
@classmethod
def create_redis_dict(cls, namespace=TEST_NAMESPACE_PREFIX, **kwargs):
config = redis_config.copy()
config.update(kwargs)
return RedisDict(namespace=namespace, **config)
@classmethod
def clear_test_namespace(cls):
for key in cls.redisdb.scan_iter('{}:*'.format(TEST_NAMESPACE_PREFIX)):
cls.redisdb.delete(key)
def setUp(self):
self.clear_test_namespace()
def test_keys_empty(self):
"""Calling RedisDict.keys() should return an empty list."""
keys = self.r.keys()
self.assertEqual(keys, [])
def test_keys_nonempty(self):
"""Calling RedisDict.keys() with various keys set should return these keys unaltered."""
self.r['foo'] = 'bar'
self.r[None] = 'baz'
self.r[True] = 'baq'
self.r[False] = 'bax'
expected_keys = sorted(['foo', None, True, False])
actual_keys = sorted(self.r.keys())
self.assertEqual(expected_keys, actual_keys)
def test_set_namespace(self):
"""Test that RedisDict keys are inserted with the given namespace."""
self.r['foo'] = 'bar'
expected_keys = ['{}:foo'.format(TEST_NAMESPACE_PREFIX)]
actual_keys = self.redisdb.keys('{}:*'.format(TEST_NAMESPACE_PREFIX))
self.assertEqual(expected_keys, actual_keys)
def test_set_and_get(self):
"""Test setting a key and retrieving it."""
self.r['foobar'] = 'barbar'
self.assertEqual(self.r['foobar'], 'barbar')
def test_set_and_get_intkey(self):
"""Test setting an integer key and retrieving it."""
self.r[1] = 'foobar'
self.assertEqual(self.r[1], 'foobar')
def test_set_and_get_nonekey(self):
"""Test setting a None key and retrieving it."""
self.r[None] = 'foobar'
self.assertEqual(self.r[None], 'foobar')
def test_set_and_get_boolkey(self):
"""Test setting a boolean key and retrieving it."""
self.r[True] = 'foobar1'
self.r[False] = 'foobar2'
self.assertEqual(self.r[True], 'foobar1')
self.assertEqual(self.r[False], 'foobar2')
def test_set_none_and_get_none(self):
"""Test setting a key with None as value and retrieving it."""
self.r['foobar'] = None
self.assertIsNone(self.r['foobar'])
def test_set_bool_and_get_bool(self):
"""Test setting a key with no value and retrieving it."""
self.r['foobar'] = True
self.assertEqual(self.r['foobar'], True)
def test_set_and_get_multiple(self):
"""Test setting two different keys with two different values, and reading them."""
self.r['foobar1'] = 'barbar1'
self.r['foobar2'] = 'barbar2'
self.assertEqual(self.r['foobar1'], 'barbar1')
self.assertEqual(self.r['foobar2'], 'barbar2')
def test_get_nonexisting(self):
"""Test that retrieving a non-existing key raises a KeyError."""
with self.assertRaises(KeyError):
_ = self.r['nonexistingkey']
def test_delete(self):
"""Test deleting a key."""
self.r['foobargone'] = 'bars'
del self.r['foobargone']
self.assertEqual(self.redisdb.get('foobargone'), None)
def test_contains_empty(self):
"""Tests the __contains__ function with no keys set."""
self.assertFalse('foobar' in self.r)
def test_contains_nonempty(self):
"""Tests the __contains__ function with keys set."""
self.r['foobar'] = 'barbar'
self.assertTrue('foobar' in self.r)
def test_contains_nonekey_notset(self):
"""Tests the __contains__ function with key None when it's not set."""
self.assertFalse(None in self.r)
def test_contains_nonekey_isset(self):
"""Tests the __contains__ function with key None when it's set."""
self.r[None] = 'foobar'
self.assertTrue(None in self.r)
def test_contains_boolkey_notset(self):
"""Tests the __contains__ function with key True when it's not set."""
self.assertFalse(True in self.r)
def test_contains_boolkey_isset(self):
"""Tests the __contains__ function with key True when it's set."""
self.r[True] = 'foobar'
self.assertTrue(True in self.r)
def test_repr_empty(self):
"""Tests the __repr__ function with no keys set."""
expected_repr = str({})
actual_repr = repr(self.r)
self.assertEqual(actual_repr, expected_repr)
def test_repr_nonempty(self):
"""Tests the __repr__ function with keys set."""
self.r['foobars'] = 'barrbars'
expected_repr = str({u'foobars': u'barrbars'})
actual_repr = repr(self.r)
self.assertEqual(actual_repr, expected_repr)
def test_str_nonempty(self):
"""Tests the __repr__ function with keys set."""
self.r['foobars'] = 'barrbars'
expected_str = str({u'foobars': u'barrbars'})
actual_str = str(self.r)
self.assertEqual(actual_str, expected_str)
def test_len_empty(self):
"""Tests the __repr__ function with no keys set."""
self.assertEqual(len(self.r), 0)
def test_len_nonempty(self):
"""Tests the __repr__ function with keys set."""
self.r['foobar1'] = 'barbar1'
self.r['foobar2'] = 'barbar2'
self.assertEqual(len(self.r), 2)
def test_to_dict_empty(self):
"""Tests the to_dict function with no keys set."""
expected_dict = {}
actual_dict = self.r.to_dict()
self.assertEqual(actual_dict, expected_dict)
def test_to_dict_nonempty(self):
"""Tests the to_dict function with keys set."""
self.r['foobar'] = 'barbaros'
expected_dict = {u'foobar': u'barbaros'}
actual_dict = self.r.to_dict()
self.assertEqual(actual_dict, expected_dict)
def test_chain_set_1(self):
"""Test setting a chain with 1 element."""
self.r.chain_set(['foo'], 'melons')
expected_key = '{}:foo'.format(TEST_NAMESPACE_PREFIX)
self.assertEqual(self.redisdb.get(expected_key), 'melons')
def test_chain_set_2(self):
"""Test setting a chain with 2 elements."""
self.r.chain_set(['foo', 'bar'], 'melons')
expected_key = '{}:foo:bar'.format(TEST_NAMESPACE_PREFIX)
self.assertEqual(self.redisdb.get(expected_key), 'melons')
def test_chain_set_overwrite(self):
"""Test setting a chain with 1 element and then overwriting it."""
self.r.chain_set(['foo'], 'melons')
self.r.chain_set(['foo'], 'bananas')
expected_key = '{}:foo'.format(TEST_NAMESPACE_PREFIX)
self.assertEqual(self.redisdb.get(expected_key), 'bananas')
def test_chain_get_1(self):
"""Test setting and getting a chain with 1 element."""
self.r.chain_set(['foo'], 'melons')
self.assertEqual(self.r.chain_get(['foo']), 'melons')
def test_chain_get_empty(self):
"""Test getting a chain that has not been set."""
with self.assertRaises(KeyError):
_ = self.r.chain_get(['foo'])
def test_chain_get_2(self):
"""Test setting and getting a chain with 2 elements."""
self.r.chain_set(['foo', 'bar'], 'melons')
self.assertEqual(self.r.chain_get(['foo', 'bar']), 'melons')
def test_chain_del_1(self):
"""Test setting and deleting a chain with 1 element."""
self.r.chain_set(['foo'], 'melons')
self.r.chain_del(['foo'])
with self.assertRaises(KeyError):
_ = self.r.chain_get(['foo'])
def test_chain_del_2(self):
"""Test setting and deleting a chain with 2 elements."""
self.r.chain_set(['foo', 'bar'], 'melons')
self.r.chain_del(['foo', 'bar'])
with self.assertRaises(KeyError):
_ = self.r.chain_get(['foo', 'bar'])
def test_expire_context(self):
"""Test adding keys with an expire value by using the contextmanager."""
with self.r.expire_at(3600):
self.r['foobar'] = 'barbar'
actual_ttl = self.redisdb.ttl('{}:foobar'.format(TEST_NAMESPACE_PREFIX))
self.assertAlmostEqual(3600, actual_ttl, delta=2)
def test_expire_keyword(self):
"""Test ading keys with an expire value by using the expire config keyword."""
r = self.create_redis_dict(expire=3600)
r['foobar'] = 'barbar'
actual_ttl = self.redisdb.ttl('{}:foobar'.format(TEST_NAMESPACE_PREFIX))
self.assertAlmostEqual(3600, actual_ttl, delta=2)
def test_iter(self):
"""Tests the __iter__ function."""
key_values = {
'foobar1': 'barbar1',
'foobar2': 'barbar2',
}
for key, value in key_values.items():
self.r[key] = value
# TODO made the assumption that iterating the redisdict should return keys, like a normal dict
for key in self.r:
self.assertEqual(self.r[key], key_values[key])
def test_multi_get_empty(self):
"""Tests the multi_get function with no keys set."""
self.assertEqual(self.r.multi_get('foo'), [])
def test_multi_get_nonempty(self):
"""Tests the multi_get function with 3 keys set, get 2 of them."""
self.r['foobar'] = 'barbar'
self.r['foobaz'] = 'bazbaz'
self.r['goobar'] = 'borbor'
expected_result = sorted(['barbar', 'bazbaz'])
actual_result = sorted(self.r.multi_get('foo'))
self.assertEqual(actual_result, expected_result)
def test_multi_get_chain_with_key_none(self):
"""Tests that multi_chain_get with key None raises TypeError."""
with self.assertRaises(TypeError):
self.r.multi_chain_get(None)
def test_multi_chain_get_empty(self):
"""Tests the multi_chain_get function with no keys set."""
self.assertEqual(self.r.multi_chain_get(['foo']), [])
def test_multi_chain_get_nonempty(self):
"""Tests the multi_chain_get function with keys set."""
self.r.chain_set(['foo', 'bar', 'bar'], 'barbar')
self.r.chain_set(['foo', 'bar', 'baz'], 'bazbaz')
self.r.chain_set(['foo', 'baz'], 'borbor')
expected_result = sorted([u'barbar', u'bazbaz'])
actual_result = sorted(self.r.multi_chain_get(['foo', 'bar']))
self.assertEqual(actual_result, expected_result)
def test_multi_dict_empty(self):
"""Tests the multi_dict function with no keys set."""
self.assertEqual(self.r.multi_dict('foo'), {})
def test_multi_dict_one_key(self):
"""Tests the multi_dict function with 1 key set."""
self.r['foobar'] = 'barbar'
expected_dict = {u'foobar': u'barbar'}
self.assertEqual(self.r.multi_dict('foo'), expected_dict)
def test_multi_dict_two_keys(self):
"""Tests the multi_dict function with 2 keys set."""
self.r['foobar'] = 'barbar'
self.r['foobaz'] = 'bazbaz'
expected_dict = {u'foobar': u'barbar', u'foobaz': u'bazbaz'}
self.assertEqual(self.r.multi_dict('foo'), expected_dict)
def test_multi_dict_complex(self):
"""Tests the multi_dict function by setting 3 keys and matching 2."""
self.r['foobar'] = 'barbar'
self.r['foobaz'] = 'bazbaz'
self.r['goobar'] = 'borbor'
expected_dict = {u'foobar': u'barbar', u'foobaz': u'bazbaz'}
self.assertEqual(self.r.multi_dict('foo'), expected_dict)
def test_multi_del_empty(self):
"""Tests the multi_del function with no keys set."""
self.assertEqual(self.r.multi_del('foobar'), 0)
def test_multi_del_one_key(self):
"""Tests the multi_del function with 1 key set."""
self.r['foobar'] = 'barbar'
self.assertEqual(self.r.multi_del('foobar'), 1)
self.assertIsNone(self.redisdb.get('foobar'))
def test_multi_del_two_keys(self):
"""Tests the multi_del function with 2 keys set."""
self.r['foobar'] = 'barbar'
self.r['foobaz'] = 'bazbaz'
self.assertEqual(self.r.multi_del('foo'), 2)
self.assertIsNone(self.redisdb.get('foobar'))
self.assertIsNone(self.redisdb.get('foobaz'))
def test_multi_del_complex(self):
"""Tests the multi_del function by setting 3 keys and deleting 2."""
self.r['foobar'] = 'barbar'
self.r['foobaz'] = 'bazbaz'
self.r['goobar'] = 'borbor'
self.assertEqual(self.r.multi_del('foo'), 2)
self.assertIsNone(self.redisdb.get('foobar'))
self.assertIsNone(self.redisdb.get('foobaz'))
self.assertEqual(self.r['goobar'], 'borbor')
if __name__ == '__main__':
unittest.main()