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 WITHSCORES to ZREVRANK Command #2725

Merged
merged 4 commits into from
Apr 30, 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 @@ -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