Skip to content

Commit

Permalink
Add WITHSCORE to ZRANK (#2758)
Browse files Browse the repository at this point in the history
* add withscore to zrank with tests

* fix test
  • Loading branch information
bodevone authored May 28, 2023
1 parent db7b9dd commit d95d8a2
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 @@ -4654,13 +4654,22 @@ def zrevrangebyscore(
options = {"withscores": withscores, "score_cast_func": score_cast_func}
return self.execute_command(*pieces, **options)

def zrank(self, name: KeyT, value: EncodableT) -> ResponseT:
def zrank(
self,
name: KeyT,
value: EncodableT,
withscore: bool = False,
) -> ResponseT:
"""
Returns a 0-based value indicating the rank of ``value`` in sorted set
``name``
``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/zrank
"""
if withscore:
return self.execute_command("ZRANK", name, value, "WITHSCORE")
return self.execute_command("ZRANK", name, value)

def zrem(self, name: KeyT, *values: FieldT) -> 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 @@ -1645,6 +1645,15 @@ async def test_zrank(self, r: redis.Redis):
assert await r.zrank("a", "a2") == 1
assert await r.zrank("a", "a6") is None

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

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

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

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

0 comments on commit d95d8a2

Please sign in to comment.