summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorshacharPash <93581407+shacharPash@users.noreply.github.com>2023-04-30 14:57:27 +0300
committerGitHub <noreply@github.com>2023-04-30 14:57:27 +0300
commitbf528fc7f776ce8e926b2e9abfa4e2460d73baa4 (patch)
treedf3c61e3fcc8d31d4a1d5ea598ad4d01e67dd7e5
parent8b58ebb73e03970fade4d3f9e2c961831713c228 (diff)
downloadredis-py-bf528fc7f776ce8e926b2e9abfa4e2460d73baa4.tar.gz
Add WITHSCORES to ZREVRANK Command (#2725)
* add withscores to zrevrank * change 0 -> 2 * fix errors * split test
-rw-r--r--redis/commands/core.py13
-rw-r--r--tests/test_asyncio/test_commands.py9
-rw-r--r--tests/test_commands.py9
3 files changed, 29 insertions, 2 deletions
diff --git a/redis/commands/core.py b/redis/commands/core.py
index e2cabb8..d67291b 100644
--- a/redis/commands/core.py
+++ b/redis/commands/core.py
@@ -4693,13 +4693,22 @@ class SortedSetCommands(CommandsProtocol):
"""
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:
diff --git a/tests/test_asyncio/test_commands.py b/tests/test_asyncio/test_commands.py
index 7c6fd45..409934c 100644
--- a/tests/test_asyncio/test_commands.py
+++ b/tests/test_asyncio/test_commands.py
@@ -1710,6 +1710,15 @@ class TestRedisCommands:
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
diff --git a/tests/test_commands.py b/tests/test_commands.py
index a44dac4..2b769be 100644
--- a/tests/test_commands.py
+++ b/tests/test_commands.py
@@ -2641,6 +2641,15 @@ class TestRedisCommands:
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