Permalink
Browse files

add test cases for pexpire, pexpireat, incrbyfloat, hincrbyfloat, mul…

…ti lpush/rpush
  • Loading branch information...
salimane committed Oct 8, 2012
1 parent 3d87be8 commit 8008b3833d184f183adaf65f0aae2887cb66f005
Showing with 125 additions and 5 deletions.
  1. +4 −4 rediscluster/cluster_client.py
  2. +121 −1 tests/cluster_commands.py
@@ -33,9 +33,9 @@ class StrictRedisCluster:
_write_keys = {
'append': 'append', 'blpop': 'blpop', 'brpop': 'brpop', 'brpoplpush': 'brpoplpush',
'decr': 'decr', 'decrby': 'decrby', 'del': 'del', 'exists': 'exists', 'hexists': 'hexists',
- 'expire': 'expire', 'expireat': 'expireat', 'getset': 'getset', 'hdel': 'hdel',
- 'hincrby': 'hincrby', 'hset': 'hset', 'hsetnx': 'hsetnx', 'hmset': 'hmset',
- 'incr': 'incr', 'incrby': 'incrby', 'linsert': 'linsert', 'lpop': 'lpop',
+ 'expire': 'expire', 'expireat': 'expireat', 'pexpire': 'pexpire', 'pexpireat': 'pexpireat', 'getset': 'getset', 'hdel': 'hdel',
+ 'hincrby': 'hincrby', 'hincrbyfloat': 'hincrbyfloat', 'hset': 'hset', 'hsetnx': 'hsetnx', 'hmset': 'hmset',
+ 'incr': 'incr', 'incrby': 'incrby', 'incrbyfloat': 'incrbyfloat', 'linsert': 'linsert', 'lpop': 'lpop',
'lpush': 'lpush', 'lpushx': 'lpushx', 'lrem': 'lrem', 'lset': 'lset',
'ltrim': 'ltrim', 'move': 'move',
'persist': 'persist', 'publish': 'publish', 'psubscribe': 'psubscribe', 'punsubscribe': 'punsubscribe',
@@ -77,7 +77,7 @@ class StrictRedisCluster:
'flushall': 'flushall', 'flushdb': 'flushdb',
'randomkey': 'randomkey', 'sync': 'sync',
'config_set': 'config_set', 'config_get': 'config_get',
- 'time': 'time'
+ 'time': 'time', 'client_list': 'client_list'
}
def __init__(self, cluster={}, db=0):
View
@@ -99,6 +99,11 @@ def test_delitem(self):
del self.client['a']
self.assertEquals(self.client.get('a'), None)
+ def test_client_list(self):
+ for clients in dictvalues(self.client.client_list()):
+ self.assert_(isinstance(clients[0], dict))
+ self.assert_('addr' in clients[0])
+
def test_config_get(self):
for data in dictvalues(self.client.config_get()):
self.assert_('maxmemory' in data)
@@ -225,7 +230,44 @@ def test_expireat(self):
# expire at given a datetime object
self.client['b'] = 'bar'
self.assertEquals(self.client.expireat('b', expire_at), True)
- self.assertAlmostEquals(self.client.ttl('b'), 60, delta=2)
+ self.assertEquals(self.client.ttl('b'), 60)
+
+ def test_pexpire(self):
+ for info in dictvalues(self.client.info()):
+ version = info['redis_version']
+ if StrictVersion(version) < StrictVersion('2.5.0'):
+ try:
+ raise unittest.SkipTest()
+ except AttributeError:
+ return
+
+ self.assertEquals(self.client.pexpire('a', 10000), False)
+ self.client['a'] = 'foo'
+ self.assertEquals(self.client.pexpire('a', 10000), True)
+ self.assert_(self.client.pttl('a') <= 10000)
+ self.assertEquals(self.client.persist('a'), True)
+ self.assertEquals(self.client.pttl('a'), -1)
+
+ def test_pexpireat(self):
+ for info in dictvalues(self.client.info()):
+ version = info['redis_version']
+ if StrictVersion(version) < StrictVersion('2.5.0'):
+ try:
+ raise unittest.SkipTest()
+ except AttributeError:
+ return
+
+ expire_at = datetime.datetime.now() + datetime.timedelta(minutes=1)
+ self.assertEquals(self.client.pexpireat('a', expire_at), False)
+ self.client['a'] = 'foo'
+ # expire at in unix time (milliseconds)
+ expire_at_seconds = int(time.mktime(expire_at.timetuple())) * 1000
+ self.assertEquals(self.client.pexpireat('a', expire_at_seconds), True)
+ self.assert_(self.client.ttl('a') <= 60)
+ # expire at given a datetime object
+ self.client['b'] = 'bar'
+ self.assertEquals(self.client.pexpireat('b', expire_at), True)
+ self.assert_(self.client.ttl('b') <= 60)
def test_get_set_bit(self):
self.assertEquals(self.client.getbit('a', 5), False)
@@ -364,6 +406,20 @@ def test_incr(self):
self.assertEquals(self.client.incr('a', amount=5), 7)
self.assertEquals(self.client['a'], b('7'))
+ def test_incrbyfloat(self):
+ for info in dictvalues(self.client.info()):
+ version = info['redis_version']
+ if StrictVersion(version) < StrictVersion('2.5.0'):
+ try:
+ raise unittest.SkipTest()
+ except AttributeError:
+ return
+
+ self.assertEquals(self.client.incrbyfloat('a'), 1.0)
+ self.assertEquals(self.client['a'], b('1'))
+ self.assertEquals(self.client.incrbyfloat('a', 1.1), 2.1)
+ self.assertEquals(float(self.client['a']), float(2.1))
+
def test_keys(self):
try:
raise unittest.SkipTest()
@@ -613,6 +669,11 @@ def test_lpush(self):
# real logic
self.assertEqual(1, self.client.lpush('a', 'b'))
self.assertEqual(2, self.client.lpush('a', 'a'))
+ for info in dictvalues(self.client.info()):
+ version = info['redis_version']
+ if StrictVersion(version) >= StrictVersion('2.4.0'):
+ self.assertEqual(4, self.client.lpush('a', 'b', 'a'))
+ break
self.assertEquals(self.client.lindex('a', 0), b('a'))
self.assertEquals(self.client.lindex('a', 1), b('b'))
@@ -752,6 +813,11 @@ def test_rpush(self):
# real logic
self.assertEqual(1, self.client.rpush('a', 'a'))
self.assertEqual(2, self.client.rpush('a', 'b'))
+ for info in dictvalues(self.client.info()):
+ version = info['redis_version']
+ if StrictVersion(version) >= StrictVersion('2.4.0'):
+ self.assertEqual(4, self.client.rpush('a', 'a', 'b'))
+ break
self.assertEquals(self.client.lindex('a', 0), b('a'))
self.assertEquals(self.client.lindex('a', 1), b('b'))
@@ -936,6 +1002,15 @@ def test_srandmember(self):
self.make_set('a', 'abc')
self.assert_(self.client.srandmember('a') in b('abc'))
+ for info in dictvalues(self.client.info()):
+ version = info['redis_version']
+ if StrictVersion(version) >= StrictVersion('2.5.0'):
+ randoms = self.client.srandmember('a', number=2)
+ self.assertEquals(len(randoms), 2)
+ for r in randoms:
+ self.assert_(r in b('abc'))
+ break
+
def test_srem(self):
# key is not set
self.assertEquals(self.client.srem('a', 'a'), False)
@@ -1406,6 +1481,25 @@ def test_hincrby(self):
self.assertRaises(
rediscluster.ResponseError, self.client.hincrby, 'a', 'a3')
+ def test_hincrbyfloat(self):
+ for info in dictvalues(self.client.info()):
+ version = info['redis_version']
+ if StrictVersion(version) < StrictVersion('2.5.0'):
+ try:
+ raise unittest.SkipTest()
+ except AttributeError:
+ return
+
+ # key is not a hash
+ self.client['a'] = 'a'
+ self.assertRaises(rediscluster.ResponseError,
+ self.client.hincrbyfloat, 'a', 'a1')
+ del self.client['a']
+ # no key should create the hash and incr the key's value to 1
+ self.assertEquals(self.client.hincrbyfloat('a', 'a1'), 1.0)
+ self.assertEquals(self.client.hincrbyfloat('a', 'a1'), 2.0)
+ self.assertEquals(self.client.hincrbyfloat('a', 'a1', 1.2), 3.2)
+
def test_hkeys(self):
# key is not a hash
self.client['a'] = 'a'
@@ -1628,6 +1722,23 @@ def test_strict_expire(self):
self.assertEquals(client.persist('a'), True)
self.assertEquals(client.ttl('a'), -1)
+ def test_strict_pexpire(self):
+ client = self.get_client(rediscluster.StrictRedisCluster)
+ for info in dictvalues(self.client.info()):
+ version = info['redis_version']
+ if StrictVersion(version) < StrictVersion('2.5.0'):
+ try:
+ raise unittest.SkipTest()
+ except AttributeError:
+ return
+
+ self.assertEquals(client.pexpire('a', 10000), False)
+ self.client['a'] = 'foo'
+ self.assertEquals(client.pexpire('a', 10000), True)
+ self.assert_(client.pttl('a') <= 10000)
+ self.assertEquals(client.persist('a'), True)
+ self.assertEquals(client.pttl('a'), -1)
+
## BINARY SAFE
# TODO add more tests
def test_binary_get_set(self):
@@ -1706,3 +1817,12 @@ def test_22_info(self):
data = ''.join(data)
self.client.set('a', data)
self.assertEquals(self.client.get('a'), b(data))"""
+
+ def test_floating_point_encoding(self):
+ """
+ High precision floating point values sent to the server should keep
+ precision.
+ """
+ timestamp = 1349673917.939762
+ self.client.zadd('a', timestamp, 'aaa')
+ self.assertEquals(self.client.zscore('a', 'aaa'), timestamp)

0 comments on commit 8008b38

Please sign in to comment.