Skip to content

Commit

Permalink
Add WITHSCORES to ZREVRANK Command (#2725)
Browse files Browse the repository at this point in the history
* add withscores to zrevrank

* change 0 -> 2

* fix errors

* split test
  • Loading branch information
shacharPash authored Apr 30, 2023
1 parent 8b58ebb commit bf528fc
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
13 changes: 11 additions & 2 deletions redis/commands/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -4693,13 +4693,22 @@ def zremrangebyscore(
"""
return self.execute_command("ZREMRANGEBYSCORE", name, min, max)

def zrevrank(self, name: KeyT, value: EncodableT) -> ResponseT:
def zrevrank(
self,
name: KeyT,
value: EncodableT,
withscore: bool = False,
) -> ResponseT:
"""
Returns a 0-based value indicating the descending rank of
``value`` in sorted set ``name``
``value`` in sorted set ``name``.
The optional ``withscore`` argument supplements the command's
reply with the score of the element returned.
For more information see https://redis.io/commands/zrevrank
"""
if withscore:
return self.execute_command("ZREVRANK", name, value, "WITHSCORE")
return self.execute_command("ZREVRANK", name, value)

def zscore(self, name: KeyT, value: EncodableT) -> ResponseT:
Expand Down
9 changes: 9 additions & 0 deletions tests/test_asyncio/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -1710,6 +1710,15 @@ async def test_zrevrank(self, r: redis.Redis):
assert await r.zrevrank("a", "a2") == 3
assert await r.zrevrank("a", "a6") is None

@skip_if_server_version_lt("7.2.0")
async def test_zrevrank_withscore(self, r: redis.Redis):
await r.zadd("a", {"a1": 1, "a2": 2, "a3": 3, "a4": 4, "a5": 5})
assert await r.zrevrank("a", "a1") == 4
assert await r.zrevrank("a", "a2") == 3
assert await r.zrevrank("a", "a6") is None
assert await r.zrevrank("a", "a3", withscore=True) == [2, "3"]
assert await r.zrevrank("a", "a6", withscore=True) is None

async def test_zscore(self, r: redis.Redis):
await r.zadd("a", {"a1": 1, "a2": 2, "a3": 3})
assert await r.zscore("a", "a1") == 1.0
Expand Down
9 changes: 9 additions & 0 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -2641,6 +2641,15 @@ def test_zrevrank(self, r):
assert r.zrevrank("a", "a2") == 3
assert r.zrevrank("a", "a6") is None

@skip_if_server_version_lt("7.2.0")
def test_zrevrank_withscore(self, r):
r.zadd("a", {"a1": 1, "a2": 2, "a3": 3, "a4": 4, "a5": 5})
assert r.zrevrank("a", "a1") == 4
assert r.zrevrank("a", "a2") == 3
assert r.zrevrank("a", "a6") is None
assert r.zrevrank("a", "a3", withscore=True) == [2, "3"]
assert r.zrevrank("a", "a6", withscore=True) is None

def test_zscore(self, r):
r.zadd("a", {"a1": 1, "a2": 2, "a3": 3})
assert r.zscore("a", "a1") == 1.0
Expand Down

0 comments on commit bf528fc

Please sign in to comment.