Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Supports argument score_cast_func in zrangebyscore() #194

Merged
merged 4 commits into from
Jun 15, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions fakeredis.py
Original file line number Diff line number Diff line change
Expand Up @@ -1627,8 +1627,8 @@ def _get_zelements_in_order(self, all_items, reverse=False):
in_order = sorted(by_keyname, key=lambda x: x[1], reverse=reverse)
return [el[0] for el in in_order]

def zrangebyscore(self, name, min, max,
start=None, num=None, withscores=False):
def zrangebyscore(self, name, min, max, start=None, num=None,
withscores=False, score_cast_func=float):
"""
Return a range of values from the sorted set ``name`` with scores
between ``min`` and ``max``.
Expand All @@ -1638,11 +1638,13 @@ def zrangebyscore(self, name, min, max,

``withscores`` indicates to return the scores along with the values.
The return type is a list of (value, score) pairs

`score_cast_func`` a callable used to cast the score return value
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing a leading backtick (looks like that's inherited from redis-py, but might as well fix it). Same issue on zrevrangebyscore.

"""
return self._zrangebyscore(name, min, max, start, num, withscores,
return self._zrangebyscore(name, min, max, start, num, withscores, score_cast_func,
reverse=False)

def _zrangebyscore(self, name, min, max, start, num, withscores, reverse):
def _zrangebyscore(self, name, min, max, start, num, withscores, score_cast_func, reverse):
if (start is not None and num is None) or \
(num is not None and start is None):
raise redis.RedisError("``start`` and ``num`` must both "
Expand All @@ -1657,7 +1659,7 @@ def _zrangebyscore(self, name, min, max, start, num, withscores, reverse):
if start is not None:
matches = matches[start:start + num]
if withscores:
return [(k, all_items[k]) for k in matches]
return [(k, score_cast_func(all_items[k])) for k in matches]
return matches

def zrangebylex(self, name, min, max,
Expand Down Expand Up @@ -1807,8 +1809,8 @@ def zrevrange(self, name, start, num, withscores=False):
"""
return self.zrange(name, start, num, True, withscores)

def zrevrangebyscore(self, name, max, min,
start=None, num=None, withscores=False):
def zrevrangebyscore(self, name, max, min, start=None, num=None,
withscores=False, score_cast_func=float):
"""
Return a range of values from the sorted set ``name`` with scores
between ``min`` and ``max`` in descending order.
Expand All @@ -1818,8 +1820,10 @@ def zrevrangebyscore(self, name, max, min,

``withscores`` indicates to return the scores along with the values.
The return type is a list of (value, score) pairs

`score_cast_func`` a callable used to cast the score return value
"""
return self._zrangebyscore(name, min, max, start, num, withscores,
return self._zrangebyscore(name, min, max, start, num, withscores, score_cast_func,
reverse=True)

def zrevrangebylex(self, name, max, min,
Expand Down
18 changes: 18 additions & 0 deletions test_fakeredis.py
Original file line number Diff line number Diff line change
Expand Up @@ -1879,6 +1879,24 @@ def test_zrangebyscore_withscores(self):
self.assertEqual(self.redis.zrangebyscore('foo', 1, 3, 0, 2, True),
[(b'one', 1), (b'two', 2)])

def test_zrangebyscore_cast_scores(self):
self.redis.zadd('foo', two=2)
self.redis.zadd('foo', two_a_also=2.2)

def round_str(x):
return round(float(x))

expected_without_cast_round = [(b'two', 2.0), (b'two_a_also', 2.2)]
expected_with_cast_round = [(b'two', 2.0), (b'two_a_also', 2.0)]
self.assertItemsEqual(
self.redis.zrangebyscore('foo', 2, 3, withscores=True),
expected_without_cast_round
)
self.assertItemsEqual(
self.redis.zrangebyscore('foo', 2, 3, withscores=True, score_cast_func=round_str),
expected_with_cast_round
)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test looks fine, but can you add a similar test for zrevrangebyscore for completeness.

def test_zrevrangebyscore(self):
self.redis.zadd('foo', one=1)
self.redis.zadd('foo', two=2)
Expand Down