00001 from distutils.version import StrictVersion
00002 import unittest
00003 import datetime
00004 import time
00005 import binascii
00006
00007 from redis._compat import (unichr, u, b, ascii_letters, iteritems, dictkeys,
00008 dictvalues)
00009 from redis.client import parse_info
00010 import redis
00011
00012 class ServerCommandsTestCase(unittest.TestCase):
00013 def get_client(self, cls=redis.Redis):
00014 return cls(host='localhost', port=6379, db=9)
00015
00016 def setUp(self):
00017 self.client = self.get_client()
00018 self.client.flushdb()
00019
00020 def tearDown(self):
00021 self.client.flushdb()
00022 self.client.connection_pool.disconnect()
00023
00024 def test_response_callbacks(self):
00025 self.assertEquals(
00026 self.client.response_callbacks,
00027 redis.Redis.RESPONSE_CALLBACKS)
00028 self.assertNotEquals(
00029 id(self.client.response_callbacks),
00030 id(redis.Redis.RESPONSE_CALLBACKS))
00031 self.client.set_response_callback('GET', lambda x: 'static')
00032 self.client.set('a', 'foo')
00033 self.assertEquals(self.client.get('a'), 'static')
00034
00035
00036 def test_dbsize(self):
00037 self.client['a'] = 'foo'
00038 self.client['b'] = 'bar'
00039 self.assertEquals(self.client.dbsize(), 2)
00040
00041 def test_get_and_set(self):
00042
00043 client = redis.Redis(host='localhost', port=6379, db=9)
00044 self.assertEquals(client.get('a'), None)
00045 byte_string = b('value')
00046 integer = 5
00047 unicode_string = unichr(3456) + u('abcd') + unichr(3421)
00048 self.assert_(client.set('byte_string', byte_string))
00049 self.assert_(client.set('integer', 5))
00050 self.assert_(client.set('unicode_string', unicode_string))
00051 self.assertEquals(client.get('byte_string'), byte_string)
00052 self.assertEquals(client.get('integer'), b(str(integer)))
00053 self.assertEquals(
00054 client.get('unicode_string').decode('utf-8'),
00055 unicode_string)
00056
00057 def test_getitem_and_setitem(self):
00058 self.client['a'] = 'bar'
00059 self.assertEquals(self.client['a'], b('bar'))
00060 self.assertRaises(KeyError, self.client.__getitem__, 'b')
00061
00062 def test_delete(self):
00063 self.assertEquals(self.client.delete('a'), False)
00064 self.client['a'] = 'foo'
00065 self.assertEquals(self.client.delete('a'), True)
00066
00067 def test_delitem(self):
00068 self.client['a'] = 'foo'
00069 del self.client['a']
00070 self.assertEquals(self.client.get('a'), None)
00071
00072 def test_config_get(self):
00073 data = self.client.config_get()
00074 self.assert_('maxmemory' in data)
00075 self.assert_(data['maxmemory'].isdigit())
00076
00077 def test_config_set(self):
00078 data = self.client.config_get()
00079 rdbname = data['dbfilename']
00080 self.assert_(self.client.config_set('dbfilename', 'redis_py_test.rdb'))
00081 self.assertEquals(
00082 self.client.config_get()['dbfilename'],
00083 'redis_py_test.rdb'
00084 )
00085 self.assert_(self.client.config_set('dbfilename', rdbname))
00086 self.assertEquals(self.client.config_get()['dbfilename'], rdbname)
00087
00088 def test_debug_object(self):
00089 self.client['a'] = 'foo'
00090 debug_info = self.client.debug_object('a')
00091 self.assert_(len(debug_info) > 0)
00092 self.assertEquals(debug_info['refcount'], 1)
00093 self.assert_(debug_info['serializedlength'] > 0)
00094 self.client.rpush('b', 'a1')
00095 debug_info = self.client.debug_object('a')
00096
00097 def test_echo(self):
00098 self.assertEquals(self.client.echo('foo bar'), b('foo bar'))
00099
00100 def test_info(self):
00101 self.client['a'] = 'foo'
00102 self.client['b'] = 'bar'
00103 info = self.client.info()
00104 self.assert_(isinstance(info, dict))
00105 self.assertEquals(info['db9']['keys'], 2)
00106
00107 def test_lastsave(self):
00108 self.assert_(isinstance(self.client.lastsave(), datetime.datetime))
00109
00110 def test_object(self):
00111 self.client['a'] = 'foo'
00112 self.assert_(isinstance(self.client.object('refcount', 'a'), int))
00113 self.assert_(isinstance(self.client.object('idletime', 'a'), int))
00114 self.assertEquals(self.client.object('encoding', 'a'), b('raw'))
00115
00116 def test_ping(self):
00117 self.assertEquals(self.client.ping(), True)
00118
00119 def test_time(self):
00120 version = self.client.info()['redis_version']
00121 if StrictVersion(version) < StrictVersion('2.6.0'):
00122 try:
00123 raise unittest.SkipTest()
00124 except AttributeError:
00125 return
00126
00127 t = self.client.time()
00128 self.assertEquals(len(t), 2)
00129 self.assert_(isinstance(t[0], int))
00130 self.assert_(isinstance(t[1], int))
00131
00132
00133 def test_append(self):
00134
00135 self.client.rpush('a', 'a1')
00136 self.assertRaises(redis.ResponseError, self.client.append, 'a', 'a1')
00137 del self.client['a']
00138
00139 self.assertEquals(self.client.append('a', 'a1'), 2)
00140 self.assertEquals(self.client['a'], b('a1'))
00141 self.assert_(self.client.append('a', 'a2'), 4)
00142 self.assertEquals(self.client['a'], b('a1a2'))
00143
00144 def test_getrange(self):
00145 self.client['a'] = 'foo'
00146 self.assertEquals(self.client.getrange('a', 0, 0), b('f'))
00147 self.assertEquals(self.client.getrange('a', 0, 2), b('foo'))
00148 self.assertEquals(self.client.getrange('a', 3, 4), b(''))
00149
00150 def test_decr(self):
00151 self.assertEquals(self.client.decr('a'), -1)
00152 self.assertEquals(self.client['a'], b('-1'))
00153 self.assertEquals(self.client.decr('a'), -2)
00154 self.assertEquals(self.client['a'], b('-2'))
00155 self.assertEquals(self.client.decr('a', amount=5), -7)
00156 self.assertEquals(self.client['a'], b('-7'))
00157
00158 def test_exists(self):
00159 self.assertEquals(self.client.exists('a'), False)
00160 self.client['a'] = 'foo'
00161 self.assertEquals(self.client.exists('a'), True)
00162
00163 def test_expire(self):
00164 self.assertEquals(self.client.expire('a', 10), False)
00165 self.client['a'] = 'foo'
00166 self.assertEquals(self.client.expire('a', 10), True)
00167 self.assertEquals(self.client.ttl('a'), 10)
00168 self.assertEquals(self.client.persist('a'), True)
00169 self.assertEquals(self.client.ttl('a'), None)
00170
00171 def test_expireat(self):
00172 expire_at = datetime.datetime.now() + datetime.timedelta(minutes=1)
00173 self.assertEquals(self.client.expireat('a', expire_at), False)
00174 self.client['a'] = 'foo'
00175
00176 expire_at_seconds = int(time.mktime(expire_at.timetuple()))
00177 self.assertEquals(self.client.expireat('a', expire_at_seconds), True)
00178 self.assertEquals(self.client.ttl('a'), 60)
00179
00180 self.client['b'] = 'bar'
00181 self.assertEquals(self.client.expireat('b', expire_at), True)
00182 self.assertEquals(self.client.ttl('b'), 60)
00183
00184 def test_get_set_bit(self):
00185 self.assertEquals(self.client.getbit('a', 5), False)
00186 self.assertEquals(self.client.setbit('a', 5, True), False)
00187 self.assertEquals(self.client.getbit('a', 5), True)
00188 self.assertEquals(self.client.setbit('a', 4, False), False)
00189 self.assertEquals(self.client.getbit('a', 4), False)
00190 self.assertEquals(self.client.setbit('a', 4, True), False)
00191 self.assertEquals(self.client.setbit('a', 5, True), True)
00192 self.assertEquals(self.client.getbit('a', 4), True)
00193 self.assertEquals(self.client.getbit('a', 5), True)
00194
00195 def test_bitcount(self):
00196 version = self.client.info()['redis_version']
00197 if StrictVersion(version) < StrictVersion('2.6.0'):
00198 try:
00199 raise unittest.SkipTest()
00200 except AttributeError:
00201 return
00202
00203 self.client.setbit('a', 5, True)
00204 self.assertEquals(self.client.bitcount('a'), 1)
00205 self.client.setbit('a', 6, True)
00206 self.assertEquals(self.client.bitcount('a'), 2)
00207 self.client.setbit('a', 5, False)
00208 self.assertEquals(self.client.bitcount('a'), 1)
00209 self.client.setbit('a', 9, True)
00210 self.client.setbit('a', 17, True)
00211 self.client.setbit('a', 25, True)
00212 self.client.setbit('a', 33, True)
00213 self.assertEquals(self.client.bitcount('a'), 5)
00214 self.assertEquals(self.client.bitcount('a', 2, 3), 2)
00215 self.assertEquals(self.client.bitcount('a', 2, -1), 3)
00216 self.assertEquals(self.client.bitcount('a', -2, -1), 2)
00217 self.assertEquals(self.client.bitcount('a', 1, 1), 1)
00218
00219 def test_bitop_not_empty_string(self):
00220 version = self.client.info()['redis_version']
00221 if StrictVersion(version) < StrictVersion('2.6.0'):
00222 try:
00223 raise unittest.SkipTest()
00224 except AttributeError:
00225 return
00226
00227 self.client.set('a', '')
00228 self.client.bitop('not', 'r', 'a')
00229 self.assertEquals(self.client.get('r'), None)
00230
00231 def test_bitop_not(self):
00232 version = self.client.info()['redis_version']
00233 if StrictVersion(version) < StrictVersion('2.6.0'):
00234 try:
00235 raise unittest.SkipTest()
00236 except AttributeError:
00237 return
00238
00239 test_str = b('\xAA\x00\xFF\x55')
00240 correct = ~0xAA00FF55 & 0xFFFFFFFF
00241 self.client.set('a', test_str)
00242 self.client.bitop('not', 'r', 'a')
00243 self.assertEquals(
00244 int(binascii.hexlify(self.client.get('r')), 16),
00245 correct)
00246
00247 def test_bitop_not_in_place(self):
00248 version = self.client.info()['redis_version']
00249 if StrictVersion(version) < StrictVersion('2.6.0'):
00250 try:
00251 raise unittest.SkipTest()
00252 except AttributeError:
00253 return
00254
00255 test_str = b('\xAA\x00\xFF\x55')
00256 correct = ~0xAA00FF55 & 0xFFFFFFFF
00257 self.client.set('a', test_str)
00258 self.client.bitop('not', 'a', 'a')
00259 self.assertEquals(
00260 int(binascii.hexlify(self.client.get('a')), 16),
00261 correct)
00262
00263 def test_bitop_single_string(self):
00264 version = self.client.info()['redis_version']
00265 if StrictVersion(version) < StrictVersion('2.6.0'):
00266 try:
00267 raise unittest.SkipTest()
00268 except AttributeError:
00269 return
00270
00271 test_str = b('\x01\x02\xFF')
00272 self.client.set('a', test_str)
00273 self.client.bitop('and', 'res1', 'a')
00274 self.client.bitop('or', 'res2', 'a')
00275 self.client.bitop('xor', 'res3', 'a')
00276 self.assertEquals(self.client.get('res1'), test_str)
00277 self.assertEquals(self.client.get('res2'), test_str)
00278 self.assertEquals(self.client.get('res3'), test_str)
00279
00280 def test_bitop_string_operands(self):
00281 version = self.client.info()['redis_version']
00282 if StrictVersion(version) < StrictVersion('2.6.0'):
00283 try:
00284 raise unittest.SkipTest()
00285 except AttributeError:
00286 return
00287
00288 self.client.set('a', b('\x01\x02\xFF\xFF'))
00289 self.client.set('b', b('\x01\x02\xFF'))
00290 self.client.bitop('and', 'res1', 'a', 'b')
00291 self.client.bitop('or', 'res2', 'a', 'b')
00292 self.client.bitop('xor', 'res3', 'a', 'b')
00293 self.assertEquals(
00294 int(binascii.hexlify(self.client.get('res1')), 16),
00295 0x0102FF00)
00296 self.assertEquals(
00297 int(binascii.hexlify(self.client.get('res2')), 16),
00298 0x0102FFFF)
00299 self.assertEquals(
00300 int(binascii.hexlify(self.client.get('res3')), 16),
00301 0x000000FF)
00302
00303 def test_getset(self):
00304 self.assertEquals(self.client.getset('a', 'foo'), None)
00305 self.assertEquals(self.client.getset('a', 'bar'), b('foo'))
00306
00307 def test_incr(self):
00308 self.assertEquals(self.client.incr('a'), 1)
00309 self.assertEquals(self.client['a'], b('1'))
00310 self.assertEquals(self.client.incr('a'), 2)
00311 self.assertEquals(self.client['a'], b('2'))
00312 self.assertEquals(self.client.incr('a', amount=5), 7)
00313 self.assertEquals(self.client['a'], b('7'))
00314
00315 def test_keys(self):
00316 self.assertEquals(self.client.keys(), [])
00317 keys = set([b('test_a'), b('test_b'), b('testc')])
00318 for key in keys:
00319 self.client[key] = 1
00320 self.assertEquals(
00321 set(self.client.keys(pattern='test_*')),
00322 keys - set([b('testc')]))
00323 self.assertEquals(set(self.client.keys(pattern='test*')), keys)
00324
00325 def test_mget(self):
00326 self.assertEquals(self.client.mget(['a', 'b']), [None, None])
00327 self.client['a'] = '1'
00328 self.client['b'] = '2'
00329 self.client['c'] = '3'
00330 self.assertEquals(
00331 self.client.mget(['a', 'other', 'b', 'c']),
00332 [b('1'), None, b('2'), b('3')])
00333
00334 def test_mset(self):
00335 d = {'a': '1', 'b': '2', 'c': '3'}
00336 self.assert_(self.client.mset(d))
00337 for k, v in iteritems(d):
00338 self.assertEquals(self.client[k], b(v))
00339
00340 def test_msetnx(self):
00341 d = {'a': '1', 'b': '2', 'c': '3'}
00342 self.assert_(self.client.msetnx(d))
00343 d2 = {'a': 'x', 'd': '4'}
00344 self.assert_(not self.client.msetnx(d2))
00345 for k, v in iteritems(d):
00346 self.assertEquals(self.client[k], b(v))
00347 self.assertEquals(self.client.get('d'), None)
00348
00349 def test_randomkey(self):
00350 self.assertEquals(self.client.randomkey(), None)
00351 self.client['a'] = '1'
00352 self.client['b'] = '2'
00353 self.client['c'] = '3'
00354 self.assert_(self.client.randomkey() in (b('a'), b('b'), b('c')))
00355
00356 def test_rename(self):
00357 self.client['a'] = '1'
00358 self.assert_(self.client.rename('a', 'b'))
00359 self.assertEquals(self.client.get('a'), None)
00360 self.assertEquals(self.client['b'], b('1'))
00361
00362 def test_renamenx(self):
00363 self.client['a'] = '1'
00364 self.client['b'] = '2'
00365 self.assert_(not self.client.renamenx('a', 'b'))
00366 self.assertEquals(self.client['a'], b('1'))
00367 self.assertEquals(self.client['b'], b('2'))
00368
00369 def test_setex(self):
00370 self.assertEquals(self.client.setex('a', '1', 60), True)
00371 self.assertEquals(self.client['a'], b('1'))
00372 self.assertEquals(self.client.ttl('a'), 60)
00373
00374 def test_setnx(self):
00375 self.assert_(self.client.setnx('a', '1'))
00376 self.assertEquals(self.client['a'], b('1'))
00377 self.assert_(not self.client.setnx('a', '2'))
00378 self.assertEquals(self.client['a'], b('1'))
00379
00380 def test_setrange(self):
00381 self.assertEquals(self.client.setrange('a', 5, 'abcdef'), 11)
00382 self.assertEquals(self.client['a'], b('\0\0\0\0\0abcdef'))
00383 self.client['a'] = 'Hello World'
00384 self.assertEquals(self.client.setrange('a', 6, 'Redis'), 11)
00385 self.assertEquals(self.client['a'], b('Hello Redis'))
00386
00387 def test_strlen(self):
00388 self.client['a'] = 'abcdef'
00389 self.assertEquals(self.client.strlen('a'), 6)
00390
00391 def test_substr(self):
00392
00393 self.client.rpush('a', 'a1')
00394 self.assertRaises(redis.ResponseError, self.client.substr, 'a', 0)
00395 del self.client['a']
00396
00397 self.client['a'] = 'abcdefghi'
00398 self.assertEquals(self.client.substr('a', 0), b('abcdefghi'))
00399 self.assertEquals(self.client.substr('a', 2), b('cdefghi'))
00400 self.assertEquals(self.client.substr('a', 3, 5), b('def'))
00401 self.assertEquals(self.client.substr('a', 3, -2), b('defgh'))
00402 self.client['a'] = 123456
00403 self.assertEquals(self.client.substr('a', 2, -2), b('345'))
00404
00405 def test_type(self):
00406 self.assertEquals(self.client.type('a'), b('none'))
00407 self.client['a'] = '1'
00408 self.assertEquals(self.client.type('a'), b('string'))
00409 del self.client['a']
00410 self.client.lpush('a', '1')
00411 self.assertEquals(self.client.type('a'), b('list'))
00412 del self.client['a']
00413 self.client.sadd('a', '1')
00414 self.assertEquals(self.client.type('a'), b('set'))
00415 del self.client['a']
00416 self.client.zadd('a', **{'1': 1})
00417 self.assertEquals(self.client.type('a'), b('zset'))
00418
00419
00420 def make_list(self, name, l):
00421 for i in l:
00422 self.client.rpush(name, i)
00423
00424 def test_blpop(self):
00425 self.make_list('a', 'ab')
00426 self.make_list('b', 'cd')
00427 self.assertEquals(
00428 self.client.blpop(['b', 'a'], timeout=1),
00429 (b('b'), b('c')))
00430 self.assertEquals(
00431 self.client.blpop(['b', 'a'], timeout=1),
00432 (b('b'), b('d')))
00433 self.assertEquals(
00434 self.client.blpop(['b', 'a'], timeout=1),
00435 (b('a'), b('a')))
00436 self.assertEquals(
00437 self.client.blpop(['b', 'a'], timeout=1),
00438 (b('a'), b('b')))
00439 self.assertEquals(self.client.blpop(['b', 'a'], timeout=1), None)
00440 self.make_list('c', 'a')
00441 self.assertEquals(self.client.blpop('c', timeout=1), (b('c'), b('a')))
00442
00443 def test_brpop(self):
00444 self.make_list('a', 'ab')
00445 self.make_list('b', 'cd')
00446 self.assertEquals(
00447 self.client.brpop(['b', 'a'], timeout=1),
00448 (b('b'), b('d')))
00449 self.assertEquals(
00450 self.client.brpop(['b', 'a'], timeout=1),
00451 (b('b'), b('c')))
00452 self.assertEquals(
00453 self.client.brpop(['b', 'a'], timeout=1),
00454 (b('a'), b('b')))
00455 self.assertEquals(
00456 self.client.brpop(['b', 'a'], timeout=1),
00457 (b('a'), b('a')))
00458 self.assertEquals(self.client.brpop(['b', 'a'], timeout=1), None)
00459 self.make_list('c', 'a')
00460 self.assertEquals(self.client.brpop('c', timeout=1), (b('c'), b('a')))
00461
00462 def test_brpoplpush(self):
00463 self.make_list('a', '12')
00464 self.make_list('b', '34')
00465 self.assertEquals(self.client.brpoplpush('a', 'b'), b('2'))
00466 self.assertEquals(self.client.brpoplpush('a', 'b'), b('1'))
00467 self.assertEquals(self.client.brpoplpush('a', 'b', timeout=1), None)
00468 self.assertEquals(self.client.lrange('a', 0, -1), [])
00469 self.assertEquals(
00470 self.client.lrange('b', 0, -1),
00471 [b('1'), b('2'), b('3'), b('4')])
00472
00473 def test_lindex(self):
00474
00475 self.assertEquals(self.client.lindex('a', '0'), None)
00476
00477 self.client['a'] = 'b'
00478 self.assertRaises(redis.ResponseError, self.client.lindex, 'a', '0')
00479 del self.client['a']
00480
00481 self.make_list('a', 'abc')
00482 self.assertEquals(self.client.lindex('a', '0'), b('a'))
00483 self.assertEquals(self.client.lindex('a', '1'), b('b'))
00484 self.assertEquals(self.client.lindex('a', '2'), b('c'))
00485
00486 def test_linsert(self):
00487
00488 self.assertEquals(self.client.linsert('a', 'after', 'x', 'y'), 0)
00489
00490 self.client['a'] = 'b'
00491 self.assertRaises(
00492 redis.ResponseError, self.client.linsert, 'a', 'after', 'x', 'y'
00493 )
00494 del self.client['a']
00495
00496 self.make_list('a', 'abc')
00497 self.assertEquals(self.client.linsert('a', 'after', 'b', 'b1'), 4)
00498 self.assertEquals(
00499 self.client.lrange('a', 0, -1),
00500 [b('a'), b('b'), b('b1'), b('c')])
00501 self.assertEquals(self.client.linsert('a', 'before', 'b', 'a1'), 5)
00502 self.assertEquals(
00503 self.client.lrange('a', 0, -1),
00504 [b('a'), b('a1'), b('b'), b('b1'), b('c')])
00505
00506 def test_llen(self):
00507
00508 self.assertEquals(self.client.llen('a'), 0)
00509
00510 self.client['a'] = 'b'
00511 self.assertRaises(redis.ResponseError, self.client.llen, 'a')
00512 del self.client['a']
00513
00514 self.make_list('a', 'abc')
00515 self.assertEquals(self.client.llen('a'), 3)
00516
00517 def test_lpop(self):
00518
00519 self.assertEquals(self.client.lpop('a'), None)
00520
00521 self.client['a'] = 'b'
00522 self.assertRaises(redis.ResponseError, self.client.lpop, 'a')
00523 del self.client['a']
00524
00525 self.make_list('a', 'abc')
00526 self.assertEquals(self.client.lpop('a'), b('a'))
00527 self.assertEquals(self.client.lpop('a'), b('b'))
00528 self.assertEquals(self.client.lpop('a'), b('c'))
00529 self.assertEquals(self.client.lpop('a'), None)
00530
00531 def test_lpush(self):
00532
00533 self.client['a'] = 'b'
00534 self.assertRaises(redis.ResponseError, self.client.lpush, 'a', 'a')
00535 del self.client['a']
00536
00537 version = self.client.info()['redis_version']
00538 if StrictVersion(version) >= StrictVersion('1.3.4'):
00539 self.assertEqual(1, self.client.lpush('a', 'b'))
00540 self.assertEqual(2, self.client.lpush('a', 'a'))
00541 else:
00542 self.assert_(self.client.lpush('a', 'b'))
00543 self.assert_(self.client.lpush('a', 'a'))
00544 self.assertEquals(self.client.lindex('a', 0), b('a'))
00545 self.assertEquals(self.client.lindex('a', 1), b('b'))
00546
00547 def test_lpushx(self):
00548
00549 self.client['a'] = 'b'
00550 self.assertRaises(redis.ResponseError, self.client.lpushx, 'a', 'a')
00551 del self.client['a']
00552
00553 self.assertEquals(self.client.lpushx('a', 'b'), 0)
00554 self.assertEquals(self.client.lrange('a', 0, -1), [])
00555 self.make_list('a', 'abc')
00556 self.assertEquals(self.client.lpushx('a', 'd'), 4)
00557 self.assertEquals(
00558 self.client.lrange('a', 0, -1),
00559 [b('d'), b('a'), b('b'), b('c')])
00560
00561 def test_lrange(self):
00562
00563 self.assertEquals(self.client.lrange('a', 0, 1), [])
00564
00565 self.client['a'] = 'b'
00566 self.assertRaises(redis.ResponseError, self.client.lrange, 'a', 0, 1)
00567 del self.client['a']
00568
00569 self.make_list('a', 'abcde')
00570 self.assertEquals(
00571 self.client.lrange('a', 0, 2),
00572 [b('a'), b('b'), b('c')])
00573 self.assertEquals(
00574 self.client.lrange('a', 2, 10),
00575 [b('c'), b('d'), b('e')])
00576
00577 def test_lrem(self):
00578
00579 self.assertEquals(self.client.lrem('a', 'foo'), 0)
00580
00581 self.client['a'] = 'b'
00582 self.assertRaises(redis.ResponseError, self.client.lrem, 'a', 'b')
00583 del self.client['a']
00584
00585 self.make_list('a', 'aaaa')
00586 self.assertEquals(self.client.lrem('a', 'a', 1), 1)
00587 self.assertEquals(
00588 self.client.lrange('a', 0, 3),
00589 [b('a'), b('a'), b('a')])
00590 self.assertEquals(self.client.lrem('a', 'a'), 3)
00591
00592 self.assertEquals(self.client.lrange('a', 0, 1), [])
00593
00594 def test_lset(self):
00595
00596 self.assertRaises(redis.ResponseError, self.client.lset, 'a', 1, 'b')
00597
00598 self.client['a'] = 'b'
00599 self.assertRaises(redis.ResponseError, self.client.lset, 'a', 1, 'b')
00600 del self.client['a']
00601
00602 self.make_list('a', 'abc')
00603 self.assertEquals(
00604 self.client.lrange('a', 0, 2),
00605 [b('a'), b('b'), b('c')])
00606 self.assert_(self.client.lset('a', 1, 'd'))
00607 self.assertEquals(
00608 self.client.lrange('a', 0, 2),
00609 [b('a'), b('d'), b('c')])
00610
00611 def test_ltrim(self):
00612
00613 self.assert_(self.client.ltrim('a', 0, 2))
00614
00615 self.client['a'] = 'b'
00616 self.assertRaises(redis.ResponseError, self.client.ltrim, 'a', 0, 2)
00617 del self.client['a']
00618
00619 self.make_list('a', 'abc')
00620 self.assert_(self.client.ltrim('a', 0, 1))
00621 self.assertEquals(self.client.lrange('a', 0, 5), [b('a'), b('b')])
00622
00623 def test_rpop(self):
00624
00625 self.assertEquals(self.client.rpop('a'), None)
00626
00627 self.client['a'] = 'b'
00628 self.assertRaises(redis.ResponseError, self.client.rpop, 'a')
00629 del self.client['a']
00630
00631 self.make_list('a', 'abc')
00632 self.assertEquals(self.client.rpop('a'), b('c'))
00633 self.assertEquals(self.client.rpop('a'), b('b'))
00634 self.assertEquals(self.client.rpop('a'), b('a'))
00635 self.assertEquals(self.client.rpop('a'), None)
00636
00637 def test_rpoplpush(self):
00638
00639 self.make_list('b', ['b1'])
00640 self.assertEquals(self.client.rpoplpush('a', 'b'), None)
00641
00642 self.assertEquals(self.client.rpoplpush('b', 'a'), b('b1'))
00643 self.assertEquals(self.client.lindex('a', 0), b('b1'))
00644 del self.client['a']
00645 del self.client['b']
00646
00647 self.client['a'] = 'a1'
00648 self.assertRaises(redis.ResponseError, self.client.rpoplpush, 'a', 'b')
00649 del self.client['a']
00650
00651 self.make_list('a', ['a1'])
00652 self.client['b'] = 'b'
00653 self.assertRaises(redis.ResponseError, self.client.rpoplpush, 'a', 'b')
00654 del self.client['a']
00655 del self.client['b']
00656
00657 self.make_list('a', ['a1', 'a2', 'a3'])
00658 self.make_list('b', ['b1', 'b2', 'b3'])
00659 self.assertEquals(self.client.rpoplpush('a', 'b'), b('a3'))
00660 self.assertEquals(self.client.lrange('a', 0, 2), [b('a1'), b('a2')])
00661 self.assertEquals(
00662 self.client.lrange('b', 0, 4),
00663 [b('a3'), b('b1'), b('b2'), b('b3')])
00664
00665 def test_rpush(self):
00666
00667 self.client['a'] = 'b'
00668 self.assertRaises(redis.ResponseError, self.client.rpush, 'a', 'a')
00669 del self.client['a']
00670
00671 version = self.client.info()['redis_version']
00672 if StrictVersion(version) >= StrictVersion('1.3.4'):
00673 self.assertEqual(1, self.client.rpush('a', 'a'))
00674 self.assertEqual(2, self.client.rpush('a', 'b'))
00675 else:
00676 self.assert_(self.client.rpush('a', 'a'))
00677 self.assert_(self.client.rpush('a', 'b'))
00678 self.assertEquals(self.client.lindex('a', 0), b('a'))
00679 self.assertEquals(self.client.lindex('a', 1), b('b'))
00680
00681 def test_rpushx(self):
00682
00683 self.client['a'] = 'b'
00684 self.assertRaises(redis.ResponseError, self.client.rpushx, 'a', 'a')
00685 del self.client['a']
00686
00687 self.assertEquals(self.client.rpushx('a', 'b'), 0)
00688 self.assertEquals(self.client.lrange('a', 0, -1), [])
00689 self.make_list('a', 'abc')
00690 self.assertEquals(self.client.rpushx('a', 'd'), 4)
00691 self.assertEquals(
00692 self.client.lrange('a', 0, -1),
00693 [b('a'), b('b'), b('c'), b('d')])
00694
00695
00696 def make_set(self, name, l):
00697 for i in l:
00698 self.client.sadd(name, i)
00699
00700 def test_sadd(self):
00701
00702 self.client['a'] = 'a'
00703 self.assertRaises(redis.ResponseError, self.client.sadd, 'a', 'a1')
00704 del self.client['a']
00705
00706 members = set([b('a1'), b('a2'), b('a3')])
00707 self.make_set('a', members)
00708 self.assertEquals(self.client.smembers('a'), members)
00709
00710 def test_scard(self):
00711
00712 self.client['a'] = 'a'
00713 self.assertRaises(redis.ResponseError, self.client.scard, 'a')
00714 del self.client['a']
00715
00716 self.make_set('a', 'abc')
00717 self.assertEquals(self.client.scard('a'), 3)
00718
00719 def test_sdiff(self):
00720
00721 self.make_set('a', ['a1', 'a2', 'a3'])
00722 self.client['b'] = 'b'
00723 self.assertRaises(redis.ResponseError, self.client.sdiff, ['a', 'b'])
00724 del self.client['b']
00725
00726 self.make_set('b', ['b1', 'a2', 'b3'])
00727 self.assertEquals(
00728 self.client.sdiff(['a', 'b']),
00729 set([b('a1'), b('a3')]))
00730
00731 def test_sdiffstore(self):
00732
00733 self.make_set('a', ['a1', 'a2', 'a3'])
00734 self.client['b'] = 'b'
00735 self.assertRaises(
00736 redis.ResponseError, self.client.sdiffstore,
00737 'c', ['a', 'b'])
00738 del self.client['b']
00739 self.make_set('b', ['b1', 'a2', 'b3'])
00740
00741
00742
00743 self.assertEquals(self.client.sdiffstore('c', ['a', 'b']), 2)
00744 self.assertEquals(self.client.smembers('c'), set([b('a1'), b('a3')]))
00745
00746 def test_sinter(self):
00747
00748 self.make_set('a', ['a1', 'a2', 'a3'])
00749 self.client['b'] = 'b'
00750 self.assertRaises(redis.ResponseError, self.client.sinter, ['a', 'b'])
00751 del self.client['b']
00752
00753 self.make_set('b', ['a1', 'b2', 'a3'])
00754 self.assertEquals(
00755 self.client.sinter(['a', 'b']),
00756 set([b('a1'), b('a3')]))
00757
00758 def test_sinterstore(self):
00759
00760 self.make_set('a', ['a1', 'a2', 'a3'])
00761 self.client['b'] = 'b'
00762 self.assertRaises(
00763 redis.ResponseError, self.client.sinterstore,
00764 'c', ['a', 'b'])
00765 del self.client['b']
00766 self.make_set('b', ['a1', 'b2', 'a3'])
00767
00768
00769
00770 self.assertEquals(self.client.sinterstore('c', ['a', 'b']), 2)
00771 self.assertEquals(self.client.smembers('c'), set([b('a1'), b('a3')]))
00772
00773 def test_sismember(self):
00774
00775 self.client['a'] = 'a'
00776 self.assertRaises(redis.ResponseError, self.client.sismember, 'a', 'a')
00777 del self.client['a']
00778
00779 self.make_set('a', 'abc')
00780 self.assertEquals(self.client.sismember('a', 'a'), True)
00781 self.assertEquals(self.client.sismember('a', 'b'), True)
00782 self.assertEquals(self.client.sismember('a', 'c'), True)
00783 self.assertEquals(self.client.sismember('a', 'd'), False)
00784
00785 def test_smembers(self):
00786
00787 self.client['a'] = 'a'
00788 self.assertRaises(redis.ResponseError, self.client.smembers, 'a')
00789 del self.client['a']
00790
00791 self.assertEquals(self.client.smembers('a'), set())
00792
00793 self.make_set('a', 'abc')
00794 self.assertEquals(
00795 self.client.smembers('a'),
00796 set([b('a'), b('b'), b('c')]))
00797
00798 def test_smove(self):
00799
00800 self.make_set('b', ['b1', 'b2'])
00801 self.assertEquals(self.client.smove('a', 'b', 'a1'), 0)
00802
00803 self.client['a'] = 'a'
00804 self.assertRaises(
00805 redis.ResponseError, self.client.smove,
00806 'a', 'b', 'a1')
00807 del self.client['a']
00808 self.make_set('a', ['a1', 'a2'])
00809
00810 del self.client['b']
00811 self.client['b'] = 'b'
00812 self.assertRaises(
00813 redis.ResponseError, self.client.smove,
00814 'a', 'b', 'a1')
00815 del self.client['b']
00816 self.make_set('b', ['b1', 'b2'])
00817
00818 self.assert_(self.client.smove('a', 'b', 'a1'))
00819 self.assertEquals(self.client.smembers('a'), set([b('a2')]))
00820 self.assertEquals(
00821 self.client.smembers('b'),
00822 set([b('b1'), b('b2'), b('a1')]))
00823
00824 def test_spop(self):
00825
00826 self.assertEquals(self.client.spop('a'), None)
00827
00828 self.client['a'] = 'a'
00829 self.assertRaises(redis.ResponseError, self.client.spop, 'a')
00830 del self.client['a']
00831
00832 s = [b('a'), b('b'), b('c')]
00833 self.make_set('a', s)
00834 value = self.client.spop('a')
00835 self.assert_(value in s)
00836 self.assertEquals(self.client.smembers('a'), set(s) - set([value]))
00837
00838 def test_srandmember(self):
00839
00840 self.assertEquals(self.client.srandmember('a'), None)
00841
00842 self.client['a'] = 'a'
00843 self.assertRaises(redis.ResponseError, self.client.srandmember, 'a')
00844 del self.client['a']
00845
00846 self.make_set('a', 'abc')
00847 self.assert_(self.client.srandmember('a') in b('abc'))
00848
00849 def test_srem(self):
00850
00851 self.assertEquals(self.client.srem('a', 'a'), False)
00852
00853 self.client['a'] = 'a'
00854 self.assertRaises(redis.ResponseError, self.client.srem, 'a', 'a')
00855 del self.client['a']
00856
00857 self.make_set('a', 'abc')
00858 self.assertEquals(self.client.srem('a', 'd'), False)
00859 self.assertEquals(self.client.srem('a', 'b'), True)
00860 self.assertEquals(self.client.smembers('a'), set([b('a'), b('c')]))
00861
00862 def test_sunion(self):
00863
00864 self.make_set('a', ['a1', 'a2', 'a3'])
00865 self.client['b'] = 'b'
00866 self.assertRaises(redis.ResponseError, self.client.sunion, ['a', 'b'])
00867 del self.client['b']
00868
00869 self.make_set('b', ['a1', 'b2', 'a3'])
00870 self.assertEquals(
00871 self.client.sunion(['a', 'b']),
00872 set([b('a1'), b('a2'), b('a3'), b('b2')]))
00873
00874 def test_sunionstore(self):
00875
00876 self.make_set('a', ['a1', 'a2', 'a3'])
00877 self.client['b'] = 'b'
00878 self.assertRaises(
00879 redis.ResponseError, self.client.sunionstore,
00880 'c', ['a', 'b'])
00881 del self.client['b']
00882 self.make_set('b', ['a1', 'b2', 'a3'])
00883
00884
00885
00886 self.assertEquals(self.client.sunionstore('c', ['a', 'b']), 4)
00887 self.assertEquals(
00888 self.client.smembers('c'),
00889 set([b('a1'), b('a2'), b('a3'), b('b2')]))
00890
00891
00892 def make_zset(self, name, d):
00893 for k, v in d.items():
00894 self.client.zadd(name, **{k: v})
00895
00896 def test_zadd(self):
00897 self.make_zset('a', {'a1': 1, 'a2': 2, 'a3': 3})
00898 self.assertEquals(
00899 self.client.zrange('a', 0, 3),
00900 [b('a1'), b('a2'), b('a3')])
00901
00902 def test_zcard(self):
00903
00904 self.client['a'] = 'a'
00905 self.assertRaises(redis.ResponseError, self.client.zcard, 'a')
00906 del self.client['a']
00907
00908 self.make_zset('a', {'a1': 1, 'a2': 2, 'a3': 3})
00909 self.assertEquals(self.client.zcard('a'), 3)
00910
00911 def test_zcount(self):
00912
00913 self.client['a'] = 'a'
00914 self.assertRaises(redis.ResponseError, self.client.zcount, 'a', 0, 0)
00915 del self.client['a']
00916
00917 self.make_zset('a', {'a1': 1, 'a2': 2, 'a3': 3})
00918 self.assertEquals(self.client.zcount('a', '-inf', '+inf'), 3)
00919 self.assertEquals(self.client.zcount('a', 1, 2), 2)
00920 self.assertEquals(self.client.zcount('a', 10, 20), 0)
00921
00922 def test_zincrby(self):
00923
00924 self.client['a'] = 'a'
00925 self.assertRaises(redis.ResponseError, self.client.zincrby, 'a', 'a1')
00926 del self.client['a']
00927
00928 self.make_zset('a', {'a1': 1, 'a2': 2, 'a3': 3})
00929 self.assertEquals(self.client.zincrby('a', 'a2'), 3.0)
00930 self.assertEquals(self.client.zincrby('a', 'a3', amount=5), 8.0)
00931 self.assertEquals(self.client.zscore('a', 'a2'), 3.0)
00932 self.assertEquals(self.client.zscore('a', 'a3'), 8.0)
00933
00934 def test_zinterstore(self):
00935 self.make_zset('a', {'a1': 1, 'a2': 1, 'a3': 1})
00936 self.make_zset('b', {'a1': 2, 'a3': 2, 'a4': 2})
00937 self.make_zset('c', {'a1': 6, 'a3': 5, 'a4': 4})
00938
00939
00940 self.assert_(self.client.zinterstore('z', ['a', 'b', 'c']))
00941 self.assertEquals(
00942 self.client.zrange('z', 0, -1, withscores=True),
00943 [(b('a3'), 8), (b('a1'), 9)]
00944 )
00945
00946
00947 self.assert_(
00948 self.client.zinterstore('z', ['a', 'b', 'c'], aggregate='MAX')
00949 )
00950 self.assertEquals(
00951 self.client.zrange('z', 0, -1, withscores=True),
00952 [(b('a3'), 5), (b('a1'), 6)]
00953 )
00954
00955
00956 self.assert_(self.client.zinterstore('z', {'a': 1, 'b': 2, 'c': 3}))
00957 self.assertEquals(
00958 self.client.zrange('z', 0, -1, withscores=True),
00959 [(b('a3'), 20), (b('a1'), 23)]
00960 )
00961
00962 def test_zrange(self):
00963
00964 self.client['a'] = 'a'
00965 self.assertRaises(redis.ResponseError, self.client.zrange, 'a', 0, 1)
00966 del self.client['a']
00967
00968 self.make_zset('a', {'a1': 1, 'a2': 2, 'a3': 3})
00969 self.assertEquals(self.client.zrange('a', 0, 1), [b('a1'), b('a2')])
00970 self.assertEquals(self.client.zrange('a', 1, 2), [b('a2'), b('a3')])
00971 self.assertEquals(
00972 self.client.zrange('a', 0, 1, withscores=True),
00973 [(b('a1'), 1.0), (b('a2'), 2.0)])
00974 self.assertEquals(
00975 self.client.zrange('a', 1, 2, withscores=True),
00976 [(b('a2'), 2.0), (b('a3'), 3.0)])
00977
00978 self.assertEquals(
00979 self.client.zrange('a', 0, 1, withscores=True,
00980 score_cast_func=int),
00981 [(b('a1'), 1), (b('a2'), 2)])
00982
00983 self.assertEquals(self.client.zrange('b', 0, 1, withscores=True), [])
00984
00985 def test_zrangebyscore(self):
00986
00987 self.client['a'] = 'a'
00988 self.assertRaises(
00989 redis.ResponseError, self.client.zrangebyscore,
00990 'a', 0, 1)
00991 del self.client['a']
00992
00993 self.make_zset('a', {'a1': 1, 'a2': 2, 'a3': 3, 'a4': 4, 'a5': 5})
00994 self.assertEquals(
00995 self.client.zrangebyscore('a', 2, 4),
00996 [b('a2'), b('a3'), b('a4')])
00997 self.assertEquals(
00998 self.client.zrangebyscore('a', 2, 4, start=1, num=2),
00999 [b('a3'), b('a4')])
01000 self.assertEquals(
01001 self.client.zrangebyscore('a', 2, 4, withscores=True),
01002 [(b('a2'), 2.0), (b('a3'), 3.0), (b('a4'), 4.0)])
01003
01004 self.assertEquals(
01005 self.client.zrangebyscore('b', 0, 1, withscores=True), [])
01006
01007 def test_zrank(self):
01008
01009 self.client['a'] = 'a'
01010 self.assertRaises(redis.ResponseError, self.client.zrank, 'a', 'a4')
01011 del self.client['a']
01012
01013 self.make_zset('a', {'a1': 1, 'a2': 2, 'a3': 3, 'a4': 4, 'a5': 5})
01014 self.assertEquals(self.client.zrank('a', 'a1'), 0)
01015 self.assertEquals(self.client.zrank('a', 'a2'), 1)
01016 self.assertEquals(self.client.zrank('a', 'a3'), 2)
01017 self.assertEquals(self.client.zrank('a', 'a4'), 3)
01018 self.assertEquals(self.client.zrank('a', 'a5'), 4)
01019
01020 self.assertEquals(self.client.zrank('a', 'a6'), None)
01021
01022 def test_zrem(self):
01023
01024 self.client['a'] = 'a'
01025 self.assertRaises(redis.ResponseError, self.client.zrem, 'a', 'a1')
01026 del self.client['a']
01027
01028 self.make_zset('a', {'a1': 1, 'a2': 2, 'a3': 3})
01029 self.assertEquals(self.client.zrem('a', 'a2'), True)
01030 self.assertEquals(self.client.zrange('a', 0, 5), [b('a1'), b('a3')])
01031 self.assertEquals(self.client.zrem('a', 'b'), False)
01032 self.assertEquals(self.client.zrange('a', 0, 5), [b('a1'), b('a3')])
01033
01034 def test_zremrangebyrank(self):
01035
01036 self.client['a'] = 'a'
01037 self.assertRaises(
01038 redis.ResponseError, self.client.zremrangebyscore,
01039 'a', 0, 1)
01040 del self.client['a']
01041
01042 self.make_zset('a', {'a1': 1, 'a2': 2, 'a3': 3, 'a4': 4, 'a5': 5})
01043 self.assertEquals(self.client.zremrangebyrank('a', 1, 3), 3)
01044 self.assertEquals(self.client.zrange('a', 0, 5), [b('a1'), b('a5')])
01045
01046 def test_zremrangebyscore(self):
01047
01048 self.client['a'] = 'a'
01049 self.assertRaises(
01050 redis.ResponseError, self.client.zremrangebyscore,
01051 'a', 0, 1)
01052 del self.client['a']
01053
01054 self.make_zset('a', {'a1': 1, 'a2': 2, 'a3': 3, 'a4': 4, 'a5': 5})
01055 self.assertEquals(self.client.zremrangebyscore('a', 2, 4), 3)
01056 self.assertEquals(self.client.zrange('a', 0, 5), [b('a1'), b('a5')])
01057 self.assertEquals(self.client.zremrangebyscore('a', 2, 4), 0)
01058 self.assertEquals(self.client.zrange('a', 0, 5), [b('a1'), b('a5')])
01059
01060 def test_zrevrange(self):
01061
01062 self.client['a'] = 'a'
01063 self.assertRaises(
01064 redis.ResponseError, self.client.zrevrange,
01065 'a', 0, 1)
01066 del self.client['a']
01067
01068 self.make_zset('a', {'a1': 1, 'a2': 2, 'a3': 3})
01069 self.assertEquals(self.client.zrevrange('a', 0, 1), [b('a3'), b('a2')])
01070 self.assertEquals(self.client.zrevrange('a', 1, 2), [b('a2'), b('a1')])
01071 self.assertEquals(
01072 self.client.zrevrange('a', 0, 1, withscores=True),
01073 [(b('a3'), 3.0), (b('a2'), 2.0)])
01074 self.assertEquals(
01075 self.client.zrevrange('a', 1, 2, withscores=True),
01076 [(b('a2'), 2.0), (b('a1'), 1.0)])
01077
01078 self.assertEquals(self.client.zrange('b', 0, 1, withscores=True), [])
01079
01080 def test_zrevrangebyscore(self):
01081
01082 self.client['a'] = 'a'
01083 self.assertRaises(
01084 redis.ResponseError, self.client.zrevrangebyscore,
01085 'a', 0, 1)
01086 del self.client['a']
01087
01088 self.make_zset('a', {'a1': 1, 'a2': 2, 'a3': 3, 'a4': 4, 'a5': 5})
01089 self.assertEquals(
01090 self.client.zrevrangebyscore('a', 4, 2),
01091 [b('a4'), b('a3'), b('a2')])
01092 self.assertEquals(
01093 self.client.zrevrangebyscore('a', 4, 2, start=1, num=2),
01094 [b('a3'), b('a2')])
01095 self.assertEquals(
01096 self.client.zrevrangebyscore('a', 4, 2, withscores=True),
01097 [(b('a4'), 4.0), (b('a3'), 3.0), (b('a2'), 2.0)])
01098
01099 self.assertEquals(
01100 self.client.zrevrangebyscore('b', 1, 0, withscores=True),
01101 [])
01102
01103 def test_zrevrank(self):
01104
01105 self.client['a'] = 'a'
01106 self.assertRaises(redis.ResponseError, self.client.zrevrank, 'a', 'a4')
01107 del self.client['a']
01108
01109 self.make_zset('a', {'a1': 5, 'a2': 4, 'a3': 3, 'a4': 2, 'a5': 1})
01110 self.assertEquals(self.client.zrevrank('a', 'a1'), 0)
01111 self.assertEquals(self.client.zrevrank('a', 'a2'), 1)
01112 self.assertEquals(self.client.zrevrank('a', 'a3'), 2)
01113 self.assertEquals(self.client.zrevrank('a', 'a4'), 3)
01114 self.assertEquals(self.client.zrevrank('a', 'a5'), 4)
01115 self.assertEquals(self.client.zrevrank('a', 'b'), None)
01116
01117 def test_zscore(self):
01118
01119 self.client['a'] = 'a'
01120 self.assertRaises(redis.ResponseError, self.client.zscore, 'a', 'a1')
01121 del self.client['a']
01122
01123 self.make_zset('a', {'a1': 0, 'a2': 1, 'a3': 2})
01124 self.assertEquals(self.client.zscore('a', 'a1'), 0.0)
01125 self.assertEquals(self.client.zscore('a', 'a2'), 1.0)
01126
01127 self.assertEquals(self.client.zscore('a', 'a4'), None)
01128
01129 def test_zunionstore(self):
01130 self.make_zset('a', {'a1': 1, 'a2': 1, 'a3': 1})
01131 self.make_zset('b', {'a1': 2, 'a3': 2, 'a4': 2})
01132 self.make_zset('c', {'a1': 6, 'a4': 5, 'a5': 4})
01133
01134
01135 self.assert_(self.client.zunionstore('z', ['a', 'b', 'c']))
01136 self.assertEquals(
01137 self.client.zrange('z', 0, -1, withscores=True),
01138 [
01139 (b('a2'), 1),
01140 (b('a3'), 3),
01141 (b('a5'), 4),
01142 (b('a4'), 7),
01143 (b('a1'), 9)
01144 ]
01145 )
01146
01147
01148 self.assert_(
01149 self.client.zunionstore('z', ['a', 'b', 'c'], aggregate='MAX')
01150 )
01151 self.assertEquals(
01152 self.client.zrange('z', 0, -1, withscores=True),
01153 [
01154 (b('a2'), 1),
01155 (b('a3'), 2),
01156 (b('a5'), 4),
01157 (b('a4'), 5),
01158 (b('a1'), 6)
01159 ]
01160 )
01161
01162
01163 self.assert_(self.client.zunionstore('z', {'a': 1, 'b': 2, 'c': 3}))
01164 self.assertEquals(
01165 self.client.zrange('z', 0, -1, withscores=True),
01166 [
01167 (b('a2'), 1),
01168 (b('a3'), 5),
01169 (b('a5'), 12),
01170 (b('a4'), 19),
01171 (b('a1'), 23)
01172 ]
01173 )
01174
01175
01176 def make_hash(self, key, d):
01177 for k, v in iteritems(d):
01178 self.client.hset(key, k, v)
01179
01180 def test_hget_and_hset(self):
01181
01182 self.client['a'] = 'a'
01183 self.assertRaises(redis.ResponseError, self.client.hget, 'a', 'a1')
01184 del self.client['a']
01185
01186 self.assertEquals(self.client.hget('a', 'a1'), None)
01187
01188 self.make_hash('a', {'a1': 1, 'a2': 2, 'a3': 3})
01189 self.assertEquals(self.client.hget('a', 'a1'), b('1'))
01190 self.assertEquals(self.client.hget('a', 'a2'), b('2'))
01191 self.assertEquals(self.client.hget('a', 'a3'), b('3'))
01192
01193 self.assertEquals(self.client.hset('a', 'a2', 5), 0)
01194 self.assertEquals(self.client.hget('a', 'a2'), b('5'))
01195
01196 self.assertEquals(self.client.hset('a', 'a4', 4), 1)
01197 self.assertEquals(self.client.hget('a', 'a4'), b('4'))
01198
01199 self.assertEquals(self.client.hget('a', 'b'), None)
01200
01201 def test_hsetnx(self):
01202
01203 self.client.hsetnx('a', 'a1', 1)
01204 self.assertEqual(self.client.hget('a', 'a1'), b('1'))
01205
01206 self.client.hsetnx('a', 'a1', 2)
01207 self.assertEqual(self.client.hget('a', 'a1'), b('1'))
01208
01209 def test_hmset(self):
01210 d = {b('a'): b('1'), b('b'): b('2'), b('c'): b('3')}
01211 self.assert_(self.client.hmset('foo', d))
01212 self.assertEqual(self.client.hgetall('foo'), d)
01213 self.assertRaises(redis.DataError, self.client.hmset, 'foo', {})
01214
01215 def test_hmset_empty_value(self):
01216 d = {b('a'): b('1'), b('b'): b('2'), b('c'): b('')}
01217 self.assert_(self.client.hmset('foo', d))
01218 self.assertEqual(self.client.hgetall('foo'), d)
01219
01220 def test_hmget(self):
01221 d = {'a': 1, 'b': 2, 'c': 3}
01222 self.assert_(self.client.hmset('foo', d))
01223 self.assertEqual(
01224 self.client.hmget('foo', ['a', 'b', 'c']), [b('1'), b('2'), b('3')])
01225 self.assertEqual(self.client.hmget('foo', ['a', 'c']), [b('1'), b('3')])
01226
01227 self.assertEquals(self.client.hmget('foo', 'a', 'c'), [b('1'), b('3')])
01228
01229 def test_hmget_empty(self):
01230 self.assertEqual(self.client.hmget('foo', ['a', 'b']), [None, None])
01231
01232 def test_hmget_no_keys(self):
01233 self.assertRaises(redis.ResponseError, self.client.hmget, 'foo', [])
01234
01235 def test_hdel(self):
01236
01237 self.client['a'] = 'a'
01238 self.assertRaises(redis.ResponseError, self.client.hdel, 'a', 'a1')
01239 del self.client['a']
01240
01241 self.assertEquals(self.client.hdel('a', 'a1'), False)
01242
01243 self.make_hash('a', {'a1': 1, 'a2': 2, 'a3': 3})
01244 self.assertEquals(self.client.hget('a', 'a2'), b('2'))
01245 self.assert_(self.client.hdel('a', 'a2'))
01246 self.assertEquals(self.client.hget('a', 'a2'), None)
01247
01248 def test_hexists(self):
01249
01250 self.client['a'] = 'a'
01251 self.assertRaises(redis.ResponseError, self.client.hexists, 'a', 'a1')
01252 del self.client['a']
01253
01254 self.assertEquals(self.client.hexists('a', 'a1'), False)
01255
01256 self.make_hash('a', {'a1': 1, 'a2': 2, 'a3': 3})
01257 self.assertEquals(self.client.hexists('a', 'a1'), True)
01258 self.assertEquals(self.client.hexists('a', 'a4'), False)
01259 self.client.hdel('a', 'a1')
01260 self.assertEquals(self.client.hexists('a', 'a1'), False)
01261
01262 def test_hgetall(self):
01263
01264 self.client['a'] = 'a'
01265 self.assertRaises(redis.ResponseError, self.client.hgetall, 'a')
01266 del self.client['a']
01267
01268 self.assertEquals(self.client.hgetall('a'), {})
01269
01270 h = {b('a1'): b('1'), b('a2'): b('2'), b('a3'): b('3')}
01271 self.make_hash('a', h)
01272 remote_hash = self.client.hgetall('a')
01273 self.assertEquals(h, remote_hash)
01274
01275 def test_hincrby(self):
01276
01277 self.client['a'] = 'a'
01278 self.assertRaises(redis.ResponseError, self.client.hincrby, 'a', 'a1')
01279 del self.client['a']
01280
01281 self.assertEquals(self.client.hincrby('a', 'a1'), 1)
01282
01283 self.assertEquals(self.client.hincrby('a', 'a1'), 2)
01284 self.assertEquals(self.client.hincrby('a', 'a1', amount=2), 4)
01285
01286 self.assertEquals(self.client.hincrby('a', 'a1', amount=-3), 1)
01287
01288 self.assertEquals(self.client.hincrby('a', 'a2', amount=3), 3)
01289
01290 self.client.hset('a', 'a3', 'foo')
01291 self.assertRaises(redis.ResponseError, self.client.hincrby, 'a', 'a3')
01292
01293 def test_hkeys(self):
01294
01295 self.client['a'] = 'a'
01296 self.assertRaises(redis.ResponseError, self.client.hkeys, 'a')
01297 del self.client['a']
01298
01299 self.assertEquals(self.client.hkeys('a'), [])
01300
01301 h = {b('a1'): b('1'), b('a2'): b('2'), b('a3'): b('3')}
01302 self.make_hash('a', h)
01303 keys = dictkeys(h)
01304 keys.sort()
01305 remote_keys = self.client.hkeys('a')
01306 remote_keys.sort()
01307 self.assertEquals(keys, remote_keys)
01308
01309 def test_hlen(self):
01310
01311 self.client['a'] = 'a'
01312 self.assertRaises(redis.ResponseError, self.client.hlen, 'a')
01313 del self.client['a']
01314
01315 self.assertEquals(self.client.hlen('a'), 0)
01316
01317 self.make_hash('a', {'a1': 1, 'a2': 2, 'a3': 3})
01318 self.assertEquals(self.client.hlen('a'), 3)
01319 self.client.hdel('a', 'a3')
01320 self.assertEquals(self.client.hlen('a'), 2)
01321
01322 def test_hvals(self):
01323
01324 self.client['a'] = 'a'
01325 self.assertRaises(redis.ResponseError, self.client.hvals, 'a')
01326 del self.client['a']
01327
01328 self.assertEquals(self.client.hvals('a'), [])
01329
01330 h = {b('a1'): b('1'), b('a2'): b('2'), b('a3'): b('3')}
01331 self.make_hash('a', h)
01332 vals = dictvalues(h)
01333 vals.sort()
01334 remote_vals = self.client.hvals('a')
01335 remote_vals.sort()
01336 self.assertEquals(vals, remote_vals)
01337
01338
01339 def test_sort_bad_key(self):
01340
01341 self.assertEquals(self.client.sort('a'), [])
01342
01343 self.client['a'] = 'a'
01344 self.assertRaises(redis.ResponseError, self.client.sort, 'a')
01345 del self.client['a']
01346
01347 def test_sort_basic(self):
01348 self.make_list('a', '3214')
01349 self.assertEquals(
01350 self.client.sort('a'),
01351 [b('1'), b('2'), b('3'), b('4')])
01352
01353 def test_sort_limited(self):
01354 self.make_list('a', '3214')
01355 self.assertEquals(
01356 self.client.sort('a', start=1, num=2),
01357 [b('2'), b('3')])
01358
01359 def test_sort_by(self):
01360 self.client['score:1'] = 8
01361 self.client['score:2'] = 3
01362 self.client['score:3'] = 5
01363 self.make_list('a_values', '123')
01364 self.assertEquals(
01365 self.client.sort('a_values', by='score:*'),
01366 [b('2'), b('3'), b('1')])
01367
01368 def test_sort_get(self):
01369 self.client['user:1'] = 'u1'
01370 self.client['user:2'] = 'u2'
01371 self.client['user:3'] = 'u3'
01372 self.make_list('a', '231')
01373 self.assertEquals(
01374 self.client.sort('a', get='user:*'),
01375 [b('u1'), b('u2'), b('u3')])
01376
01377 def test_sort_get_multi(self):
01378 self.client['user:1'] = 'u1'
01379 self.client['user:2'] = 'u2'
01380 self.client['user:3'] = 'u3'
01381 self.make_list('a', '231')
01382 self.assertEquals(
01383 self.client.sort('a', get=('user:*', '#')),
01384 [b('u1'), b('1'), b('u2'), b('2'), b('u3'), b('3')])
01385
01386 def test_sort_desc(self):
01387 self.make_list('a', '231')
01388 self.assertEquals(
01389 self.client.sort('a', desc=True),
01390 [b('3'), b('2'), b('1')])
01391
01392 def test_sort_alpha(self):
01393 self.make_list('a', 'ecbda')
01394 self.assertEquals(
01395 self.client.sort('a', alpha=True),
01396 [b('a'), b('b'), b('c'), b('d'), b('e')])
01397
01398 def test_sort_store(self):
01399 self.make_list('a', '231')
01400 self.assertEquals(self.client.sort('a', store='sorted_values'), 3)
01401 self.assertEquals(
01402 self.client.lrange('sorted_values', 0, 5),
01403 [b('1'), b('2'), b('3')])
01404
01405 def test_sort_all_options(self):
01406 self.client['user:1:username'] = 'zeus'
01407 self.client['user:2:username'] = 'titan'
01408 self.client['user:3:username'] = 'hermes'
01409 self.client['user:4:username'] = 'hercules'
01410 self.client['user:5:username'] = 'apollo'
01411 self.client['user:6:username'] = 'athena'
01412 self.client['user:7:username'] = 'hades'
01413 self.client['user:8:username'] = 'dionysus'
01414
01415 self.client['user:1:favorite_drink'] = 'yuengling'
01416 self.client['user:2:favorite_drink'] = 'rum'
01417 self.client['user:3:favorite_drink'] = 'vodka'
01418 self.client['user:4:favorite_drink'] = 'milk'
01419 self.client['user:5:favorite_drink'] = 'pinot noir'
01420 self.client['user:6:favorite_drink'] = 'water'
01421 self.client['user:7:favorite_drink'] = 'gin'
01422 self.client['user:8:favorite_drink'] = 'apple juice'
01423
01424 self.make_list('gods', '12345678')
01425 num = self.client.sort(
01426 'gods', start=2, num=4, by='user:*:username',
01427 get='user:*:favorite_drink', desc=True, alpha=True, store='sorted')
01428 self.assertEquals(num, 4)
01429 self.assertEquals(
01430 self.client.lrange('sorted', 0, 10),
01431 [b('vodka'), b('milk'), b('gin'), b('apple juice')])
01432
01433 def test_strict_zadd(self):
01434 client = self.get_client(redis.StrictRedis)
01435 client.zadd('a', 1.0, 'a1', 2.0, 'a2', a3=3.0)
01436 self.assertEquals(client.zrange('a', 0, 3, withscores=True),
01437 [(b('a1'), 1.0), (b('a2'), 2.0), (b('a3'), 3.0)])
01438
01439 def test_strict_lrem(self):
01440 client = self.get_client(redis.StrictRedis)
01441 client.rpush('a', 'a1')
01442 client.rpush('a', 'a2')
01443 client.rpush('a', 'a3')
01444 client.rpush('a', 'a1')
01445 client.lrem('a', 0, 'a1')
01446 self.assertEquals(client.lrange('a', 0, -1), [b('a2'), b('a3')])
01447
01448 def test_strict_setex(self):
01449 "SETEX swaps the order of the value and timeout"
01450 client = self.get_client(redis.StrictRedis)
01451 self.assertEquals(client.setex('a', 60, '1'), True)
01452 self.assertEquals(client['a'], b('1'))
01453 self.assertEquals(client.ttl('a'), 60)
01454
01455 def test_strict_expire(self):
01456 "TTL is -1 by default in StrictRedis"
01457 client = self.get_client(redis.StrictRedis)
01458 self.assertEquals(client.expire('a', 10), False)
01459 self.client['a'] = 'foo'
01460 self.assertEquals(client.expire('a', 10), True)
01461 self.assertEquals(client.ttl('a'), 10)
01462 self.assertEquals(client.persist('a'), True)
01463 self.assertEquals(client.ttl('a'), -1)
01464
01465
01466
01467 def test_binary_get_set(self):
01468 self.assertTrue(self.client.set(' foo bar ', '123'))
01469 self.assertEqual(self.client.get(' foo bar '), b('123'))
01470
01471 self.assertTrue(self.client.set(' foo\r\nbar\r\n ', '456'))
01472 self.assertEqual(self.client.get(' foo\r\nbar\r\n '), b('456'))
01473
01474 self.assertTrue(self.client.set(' \r\n\t\x07\x13 ', '789'))
01475 self.assertEqual(self.client.get(' \r\n\t\x07\x13 '), b('789'))
01476
01477 self.assertEqual(
01478 sorted(self.client.keys('*')),
01479 [b(' \r\n\t\x07\x13 '), b(' foo\r\nbar\r\n '), b(' foo bar ')])
01480
01481 self.assertTrue(self.client.delete(' foo bar '))
01482 self.assertTrue(self.client.delete(' foo\r\nbar\r\n '))
01483 self.assertTrue(self.client.delete(' \r\n\t\x07\x13 '))
01484
01485 def test_binary_lists(self):
01486 mapping = {
01487 b('foo bar'): [b('1'), b('2'), b('3')],
01488 b('foo\r\nbar\r\n'): [b('4'), b('5'), b('6')],
01489 b('foo\tbar\x07'): [b('7'), b('8'), b('9')],
01490 }
01491
01492 for key, value in iteritems(mapping):
01493 for c in value:
01494 self.assertTrue(self.client.rpush(key, c))
01495
01496
01497 self.assertEqual(sorted(self.client.keys('*')),
01498 sorted(dictkeys(mapping)))
01499
01500
01501 for key in dictkeys(mapping):
01502 self.assertEqual(self.client.lrange(key, 0, -1),
01503 mapping[key])
01504
01505 def test_22_info(self):
01506 """
01507 Older Redis versions contained 'allocation_stats' in INFO that
01508 was the cause of a number of bugs when parsing.
01509 """
01510 info = "allocation_stats:6=1,7=1,8=7141,9=180,10=92,11=116,12=5330," \
01511 "13=123,14=3091,15=11048,16=225842,17=1784,18=814,19=12020," \
01512 "20=2530,21=645,22=15113,23=8695,24=142860,25=318,26=3303," \
01513 "27=20561,28=54042,29=37390,30=1884,31=18071,32=31367,33=160," \
01514 "34=169,35=201,36=10155,37=1045,38=15078,39=22985,40=12523," \
01515 "41=15588,42=265,43=1287,44=142,45=382,46=945,47=426,48=171," \
01516 "49=56,50=516,51=43,52=41,53=46,54=54,55=75,56=647,57=332," \
01517 "58=32,59=39,60=48,61=35,62=62,63=32,64=221,65=26,66=30," \
01518 "67=36,68=41,69=44,70=26,71=144,72=169,73=24,74=37,75=25," \
01519 "76=42,77=21,78=126,79=374,80=27,81=40,82=43,83=47,84=46," \
01520 "85=114,86=34,87=37,88=7240,89=34,90=38,91=18,92=99,93=20," \
01521 "94=18,95=17,96=15,97=22,98=18,99=69,100=17,101=22,102=15," \
01522 "103=29,104=39,105=30,106=70,107=22,108=21,109=26,110=52," \
01523 "111=45,112=33,113=67,114=41,115=44,116=48,117=53,118=54," \
01524 "119=51,120=75,121=44,122=57,123=44,124=66,125=56,126=52," \
01525 "127=81,128=108,129=70,130=50,131=51,132=53,133=45,134=62," \
01526 "135=12,136=13,137=7,138=15,139=21,140=11,141=20,142=6,143=7," \
01527 "144=11,145=6,146=16,147=19,148=1112,149=1,151=83,154=1," \
01528 "155=1,156=1,157=1,160=1,161=1,162=2,166=1,169=1,170=1,171=2," \
01529 "172=1,174=1,176=2,177=9,178=34,179=73,180=30,181=1,185=3," \
01530 "187=1,188=1,189=1,192=1,196=1,198=1,200=1,201=1,204=1,205=1," \
01531 "207=1,208=1,209=1,214=2,215=31,216=78,217=28,218=5,219=2," \
01532 "220=1,222=1,225=1,227=1,234=1,242=1,250=1,252=1,253=1," \
01533 ">=256=203"
01534 parsed = parse_info(info)
01535 self.assert_('allocation_stats' in parsed)
01536 self.assert_('6' in parsed['allocation_stats'])
01537 self.assert_('>=256' in parsed['allocation_stats'])
01538
01539 def test_large_responses(self):
01540 "The PythonParser has some special cases for return values > 1MB"
01541
01542 data = []
01543 for i in range(5000000 // len(ascii_letters)):
01544 data.append(ascii_letters)
01545 data = ''.join(data)
01546 self.client.set('a', data)
01547 self.assertEquals(self.client.get('a'), b(data))