diff options
| author | Andy McCurdy <andy@andymccurdy.com> | 2010-05-03 09:38:19 -0700 |
|---|---|---|
| committer | Andy McCurdy <andy@andymccurdy.com> | 2010-05-03 09:38:19 -0700 |
| commit | b936a60a02a5c9f1fafe301c952a8aa044ec6536 (patch) | |
| tree | 92a693ffeee6d90011f4d284e3d1a84d07b8d4e9 | |
| parent | f4c8393113e2205eb8eeddeed10d42ad70d648ef (diff) | |
| download | redis-py-b936a60a02a5c9f1fafe301c952a8aa044ec6536.tar.gz | |
values with scores of 0 were previously failing due to the way the callback converted values. changed to a separate function to get proper handling. fixes #33
| -rw-r--r-- | redis/client.py | 17 | ||||
| -rw-r--r-- | tests/server_commands.py | 7 |
2 files changed, 19 insertions, 5 deletions
diff --git a/redis/client.py b/redis/client.py index d29df67..28dbf46 100644 --- a/redis/client.py +++ b/redis/client.py @@ -178,6 +178,17 @@ def zset_score_pairs(response, **options): return response return zip(response[::2], map(float, response[1::2])) +def int_or_none(response): + if response is None: + return None + return int(response) + +def float_or_none(response): + if response is None: + return None + return float(response) + + class Redis(threading.local): """ Implementation of the Redis protocol. @@ -196,7 +207,7 @@ class Redis(threading.local): ), string_keys_to_dict( 'DECRBY HLEN INCRBY LLEN SCARD SDIFFSTORE SINTERSTORE ' - 'SUNIONSTORE ZCARD ZRANK ZREMRANGEBYSCORE ZREVRANK', + 'SUNIONSTORE ZCARD ZREMRANGEBYSCORE ZREVRANK', int ), string_keys_to_dict( @@ -204,8 +215,7 @@ class Redis(threading.local): 'LPUSH RPUSH', lambda r: isinstance(r, int) and r or r == 'OK' ), - string_keys_to_dict('ZSCORE ZINCRBY', - lambda r: r is not None and float(r) or r), + string_keys_to_dict('ZSCORE ZINCRBY', float_or_none), string_keys_to_dict( 'FLUSHALL FLUSHDB LSET LTRIM MSET RENAME ' 'SAVE SELECT SET SHUTDOWN', @@ -225,6 +235,7 @@ class Redis(threading.local): 'PING': lambda r: r == 'PONG', 'RANDOMKEY': lambda r: r and r or None, 'TTL': lambda r: r != -1 and r or None, + 'ZRANK': int_or_none, } ) diff --git a/tests/server_commands.py b/tests/server_commands.py index da20a97..b827bf3 100644 --- a/tests/server_commands.py +++ b/tests/server_commands.py @@ -684,6 +684,8 @@ class ServerCommandsTestCase(unittest.TestCase): self.assertEquals(self.client.zrank('a', 'a3'), 2) self.assertEquals(self.client.zrank('a', 'a4'), 3) self.assertEquals(self.client.zrank('a', 'a5'), 4) + # non-existent value in zset + self.assertEquals(self.client.zrank('a', 'a6'), None) def test_zrem(self): # key is not a zset @@ -746,8 +748,9 @@ class ServerCommandsTestCase(unittest.TestCase): self.assertRaises(redis.ResponseError, self.client.zscore, 'a', 'a1') del self.client['a'] # real logic - self.make_zset('a', {'a1': 1, 'a2': 2, 'a3': 3}) - self.assertEquals(self.client.zscore('a', 'a2'), 2.0) + self.make_zset('a', {'a1': 0, 'a2': 1, 'a3': 2}) + self.assertEquals(self.client.zscore('a', 'a1'), 0.0) + self.assertEquals(self.client.zscore('a', 'a2'), 1.0) # test a non-existant member self.assertEquals(self.client.zscore('a', 'a4'), None) |
