Skip to content

Commit

Permalink
zrevrange results should be sorted by score and keys in reverse order
Browse files Browse the repository at this point in the history
ZREVRANGE sorts tied scores in reverse alphabetical order by key
  • Loading branch information
mbox committed Mar 27, 2014
1 parent 82b0702 commit cdaae6d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion fakeredis.py
Original file line number Diff line number Diff line change
Expand Up @@ -857,7 +857,7 @@ def zrange(self, name, start, end, desc=False, withscores=False, score_cast_func
return [(k, score_cast_func(all_items[k])) for k in items]

def _get_zelements_in_order(self, all_items, reverse=False):
by_keyname = sorted(all_items.items(), key=lambda x: x[0])
by_keyname = sorted(all_items.items(), key=lambda x: x[0], reverse=reverse)
in_order = sorted(by_keyname, key=lambda x: x[1], reverse=reverse)
return [el[0] for el in in_order]

Expand Down
10 changes: 10 additions & 0 deletions test_fakeredis.py
Original file line number Diff line number Diff line change
Expand Up @@ -936,6 +936,16 @@ def test_zrevrange(self):
self.assertEqual(self.redis.zrevrange('foo', 0, -1),
['three', 'two', 'one'])

def test_zrevrange_sorted_keys(self):
self.redis.zadd('foo', one=1)
self.redis.zadd('foo', two=2)
self.redis.zadd('foo', 2, 'two_b')
self.redis.zadd('foo', three=3)
self.assertEqual(self.redis.zrevrange('foo', 0, 2), ['three', 'two_b', 'two'])
self.assertEqual(self.redis.zrevrange('foo', 0, -1),
['three', 'two_b', 'two', 'one'])


def test_zrangebyscore(self):
self.redis.zadd('foo', zero=0)
self.redis.zadd('foo', two=2)
Expand Down

0 comments on commit cdaae6d

Please sign in to comment.