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

Add WITHSCORE to ZRANK #2758

Merged
merged 2 commits into from
May 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
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