summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndy McCurdy <andy@andymccurdy.com>2010-03-10 13:57:26 -0800
committerAndy McCurdy <andy@andymccurdy.com>2010-03-10 13:57:26 -0800
commit708c458adb8d90a00041977df4880d28a11e2cb9 (patch)
tree921896391cce28894e22b7c8685f81bef816fc18
parent263581708f5397804e2d42fc50b4c4b9f717f224 (diff)
downloadredis-py-708c458adb8d90a00041977df4880d28a11e2cb9.tar.gz
HSET now returns 1 if it added a new field to the hash, otherwise 0.
There was no test for RPUSH, now there is :)
-rw-r--r--redis/client.py6
-rw-r--r--tests/server_commands.py20
2 files changed, 23 insertions, 3 deletions
diff --git a/redis/client.py b/redis/client.py
index 908c933..d0f7516 100644
--- a/redis/client.py
+++ b/redis/client.py
@@ -181,7 +181,6 @@ class Redis(threading.local):
string_keys_to_dict('ZRANGE ZRANGEBYSCORE ZREVRANGE', zset_score_pairs),
{
'BGSAVE': lambda r: r == 'Background saving started',
- 'HSET': lambda r: True,
'INFO': parse_info,
'LASTSAVE': timestamp_to_datetime,
'PING': lambda r: r == 'PONG',
@@ -874,7 +873,10 @@ class Redis(threading.local):
return self.format_bulk('HGET', name, key)
def hset(self, name, key, value):
- "Set ``key`` to ``value`` within hash ``name``"
+ """
+ Set ``key`` to ``value`` within hash ``name``
+ Returns 1 if HSET created a new field, otherwise 0
+ """
return self.format_multi_bulk('HSET', name, key, value)
diff --git a/tests/server_commands.py b/tests/server_commands.py
index 8582285..1ac47cb 100644
--- a/tests/server_commands.py
+++ b/tests/server_commands.py
@@ -353,6 +353,22 @@ class ServerCommandsTestCase(unittest.TestCase):
self.assertEquals(self.client.lrange('a', 0, 2), ['a1', 'a2'])
self.assertEquals(self.client.lrange('b', 0, 4),
['a3', 'b1', 'b2', 'b3'])
+
+ def test_rpush(self):
+ # key is not a list
+ self.client['a'] = 'b'
+ self.assertRaises(redis.ResponseError, self.client.rpush, 'a', 'a')
+ del self.client['a']
+ # real logic
+ version = self.client.info()['redis_version']
+ if StrictVersion(version) >= StrictVersion('1.3.4'):
+ self.assertEqual(1, self.client.rpush('a', 'a'))
+ self.assertEqual(2, self.client.rpush('a', 'b'))
+ else:
+ self.assert_(self.client.rpush('a', 'a'))
+ self.assert_(self.client.rpush('a', 'b'))
+ self.assertEquals(self.client.lindex('a', 0), 'a')
+ self.assertEquals(self.client.lindex('a', 1), 'b')
# Set commands
def make_set(self, name, l):
@@ -666,8 +682,10 @@ class ServerCommandsTestCase(unittest.TestCase):
self.assertEquals(self.client.hget('a', 'a1'), '1')
self.assertEquals(self.client.hget('a', 'a2'), '2')
self.assertEquals(self.client.hget('a', 'a3'), '3')
- self.assertEquals(self.client.hset('a', 'a2', 5), True)
+ self.assertEquals(self.client.hset('a', 'a2', 5), 0)
self.assertEquals(self.client.hget('a', 'a2'), '5')
+ self.assertEquals(self.client.hset('a', 'a4', 4), 1)
+ self.assertEquals(self.client.hget('a', 'a4'), '4')
# SORT
def test_sort_bad_key(self):