Skip to content

Commit

Permalink
values with scores of 0 were previously failing due to the way the ca…
Browse files Browse the repository at this point in the history
…llback converted values. changed to a separate function to get proper handling. fixes redis#33
  • Loading branch information
andymccurdy committed May 3, 2010
1 parent f4c8393 commit b936a60
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
17 changes: 14 additions & 3 deletions redis/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -196,16 +207,15 @@ 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(
# these return OK, or int if redis-server is >=1.3.4
'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',
Expand All @@ -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,
}
)

Expand Down
7 changes: 5 additions & 2 deletions tests/server_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,8 @@ def test_zrank(self):
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
Expand Down Expand Up @@ -746,8 +748,9 @@ def test_zscore(self):
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)

Expand Down

0 comments on commit b936a60

Please sign in to comment.