Permalink
Browse files

add more test cases and take care of more tagged keys

  • Loading branch information...
1 parent 27c1844 commit acf577e6b867d19f2d42377e7be37a2e5434da78 @salimane committed Sep 14, 2012
Showing with 162 additions and 4 deletions.
  1. +34 −3 rediscluster/client.py
  2. +128 −1 tests/server_commands.py
View
@@ -34,7 +34,8 @@ class StrictRedis:
'zcard' : 'zcard', 'zcount' : 'zcount', 'zrange' : 'zrange', 'zrangebyscore' : 'zrangebyscore',
'zrank' : 'zrank', 'zrevrange' : 'zrevrange', 'zrevrangebyscore' : 'zrevrangebyscore',
'zrevrank' : 'zrevrank', 'zscore' : 'zscore',
- 'mget' : 'mget', 'bitcount' : 'bitcount', 'echo' : 'echo', 'debug_object' : 'debug_object'
+ 'mget' : 'mget', 'bitcount' : 'bitcount', 'echo' : 'echo', 'debug_object' : 'debug_object',
+ 'substr' : 'substr'
}
write_keys = {
@@ -72,7 +73,7 @@ class StrictRedis:
'sinter' : 'sinter', 'sinterstore' : 'sinterstore',
'sunion' : 'sunion', 'sunionstore' : 'sunionstore',
'smove' : 'smove', 'zinterstore' : 'zinterstore',
- 'zunionstore' : 'zunionstore',
+ 'zunionstore' : 'zunionstore', 'sort' : 'sort'
}
loop_keys = {
@@ -279,4 +280,34 @@ def rc_sunionstore(self, dst, src, *args):
if result is not set([]):
return self.sadd(dst, *list(result))
return 0
-
+
+ def rc_mset(self, mapping):
+ "Sets each key in the ``mapping`` dict to its corresponding value"
+ result = True
+ for k, v in iteritems(mapping):
+ result = True and self.set(k, v)
+ return result
+
+ def rc_msetnx(self, mapping):
+ """
+ Sets each key in the ``mapping`` dict to its corresponding value if
+ none of the keys are already set
+ """
+ for k, v in iteritems(mapping):
+ if self.exists(k): return False
+ result = True
+ for k, v in iteritems(mapping):
+ result = result and self.set(k, v)
+ return result
+
+ def rc_mget(self, *args):
+ """
+ Returns a list of values ordered identically to ``*args``
+ """
+ result = []
+ for key in args:
+ result.append(self.get(key))
+ return result
+
+
+
View
@@ -21,6 +21,21 @@ def setUp(self):
def tearDown(self):
self.client.flushdb()
#self.client.connection_pool.disconnect()
+
+ def test_response_callbacks(self):
+ try:
+ raise unittest.SkipTest()
+ except AttributeError:
+ return
+ self.assertEquals(
+ self.client.response_callbacks,
+ redis.Redis.RESPONSE_CALLBACKS)
+ self.assertNotEquals(
+ id(self.client.response_callbacks),
+ id(redis.Redis.RESPONSE_CALLBACKS))
+ self.client.set_response_callback('GET', lambda x: 'static')
+ self.client.set('a', 'foo')
+ self.assertEquals(self.client.get('a'), 'static')
# GENERAL SERVER COMMANDS
def test_dbsize(self):
@@ -30,7 +45,7 @@ def test_dbsize(self):
for size in dictvalues(self.client.dbsize()):
sizeno += size
self.assertEquals(sizeno, 2*self.client.no_servers)
-
+
def test_get_and_set(self):
# get and set can't be tested independently of each other
client = self.get_client()
@@ -322,6 +337,79 @@ def test_incr(self):
self.assertEquals(self.client.get('a'), b('2'))
self.assertEquals(self.client.incr('a', amount=5), 7)
self.assertEquals(self.client.get('a'), b('7'))
+
+ def test_keys(self):
+ try:
+ raise unittest.SkipTest()
+ except AttributeError:
+ return
+ self.assertEquals(self.client.keys(), [])
+ keys = set([b('test_a'), b('test_b'), b('testc')])
+ for key in keys:
+ self.client.set(key, 1)
+ self.assertEquals(
+ set(self.client.keys(pattern='test_*')),
+ keys - set([b('testc')]))
+ self.assertEquals(set(self.client.keys(pattern='test*')), keys)
+
+ def test_mget(self):
+ self.assertEquals(self.client.mget('a', 'b'), [None, None])
+ self.client.set('a', '1')
+ self.client.set('b', '2')
+ self.client.set('c', '3')
+ self.assertEquals(
+ self.client.mget('a', 'other', 'b', 'c'),
+ [b('1'), None, b('2'), b('3')])
+
+ def test_mset(self):
+ d = {'a': '1', 'b': '2', 'c': '3'}
+ self.assert_(self.client.mset(d))
+ for k, v in iteritems(d):
+ self.assertEquals(self.client.get(k), b(v))
+
+ def test_msetnx(self):
+ d = {'a': '1', 'b': '2', 'c': '3'}
+ self.assert_(self.client.msetnx(d))
+ d2 = {'a': 'x', 'd': '4'}
+ self.assert_(not self.client.msetnx(d2))
+ for k, v in iteritems(d):
+ self.assertEquals(self.client.get(k), b(v))
+ self.assertEquals(self.client.get('d'), None)
+
+ def test_randomkey(self):
+ #CLUSTER
+ try:
+ raise unittest.SkipTest()
+ except AttributeError:
+ return
+ self.assertEquals(self.client.randomkey(), None)
+ self.client.set('a', '1')
+ self.client.set('b', '2')
+ self.client.set('c', '3')
+ self.assert_(self.client.randomkey() in (b('a'), b('b'), b('c')))
+
+ def test_rename(self):
+ #CLUSTER
+ try:
+ raise unittest.SkipTest()
+ except AttributeError:
+ return
+ self.client.set('a', '1')
+ self.assert_(self.client.rename('a', 'b'))
+ self.assertEquals(self.client.get('a'), None)
+ self.assertEquals(self.client.get('b'), b('1'))
+
+ def test_renamenx(self):
+ #CLUSTER
+ try:
+ raise unittest.SkipTest()
+ except AttributeError:
+ return
+ self.client.set('a', '1')
+ self.client.set('b', '2')
+ self.assert_(not self.client.renamenx('a', 'b'))
+ self.assertEquals(self.client.get('a'), b('1'))
+ self.assertEquals(self.client.get('b'), b('2'))
def test_setex(self):
self.assertEquals(self.client.setex('a', 60, '1'), True)
@@ -344,6 +432,20 @@ def test_setrange(self):
def test_strlen(self):
self.client.set('a', 'abcdef')
self.assertEquals(self.client.strlen('a'), 6)
+
+ def test_substr(self):
+ # invalid key type
+ self.client.rpush('a', 'a1')
+ self.assertRaises(rediscluster.ResponseError, self.client.substr, 'a', 0)
+ self.client.delete('a')
+ # real logic
+ self.client.set('a', 'abcdefghi')
+ self.assertEquals(self.client.substr('a', 0), b('abcdefghi'))
+ self.assertEquals(self.client.substr('a', 2), b('cdefghi'))
+ self.assertEquals(self.client.substr('a', 3, 5), b('def'))
+ self.assertEquals(self.client.substr('a', 3, -2), b('defgh'))
+ self.client.set('a', 123456) # does substr work with ints?
+ self.assertEquals(self.client.substr('a', 2, -2), b('345'))
def test_type(self):
self.assertEquals(self.client.type('a'), b('none'))
@@ -1292,6 +1394,11 @@ def test_hvals(self):
# SORT
def test_sort_bad_key(self):
+ #CLUSTER
+ try:
+ raise unittest.SkipTest()
+ except AttributeError:
+ return
# key is not set
self.assertEquals(self.client.sort('a'), [])
# key is a string value
@@ -1300,12 +1407,22 @@ def test_sort_bad_key(self):
self.client.delete('a')
def test_sort_basic(self):
+ #CLUSTER
+ try:
+ raise unittest.SkipTest()
+ except AttributeError:
+ return
self.make_list('a', '3214')
self.assertEquals(
self.client.sort('a'),
[b('1'), b('2'), b('3'), b('4')])
def test_sort_limited(self):
+ #CLUSTER
+ try:
+ raise unittest.SkipTest()
+ except AttributeError:
+ return
self.make_list('a', '3214')
self.assertEquals(
self.client.sort('a', start=1, num=2),
@@ -1354,12 +1471,22 @@ def test_sort_get_multi(self):
[b('u1'), b('1'), b('u2'), b('2'), b('u3'), b('3')])
def test_sort_desc(self):
+ #CLUSTER
+ try:
+ raise unittest.SkipTest()
+ except AttributeError:
+ return
self.make_list('a', '231')
self.assertEquals(
self.client.sort('a', desc=True),
[b('3'), b('2'), b('1')])
def test_sort_alpha(self):
+ #CLUSTER
+ try:
+ raise unittest.SkipTest()
+ except AttributeError:
+ return
self.make_list('a', 'ecbda')
self.assertEquals(
self.client.sort('a', alpha=True),

0 comments on commit acf577e

Please sign in to comment.