From fb491ee13050f6bf18a81a9e2c4bc2cd4c422e9d Mon Sep 17 00:00:00 2001 From: Alibi Date: Wed, 10 May 2023 14:35:11 +0600 Subject: [PATCH 1/2] add withscore to zrank with tests --- redis/commands/core.py | 13 +++++++++++-- tests/test_asyncio/test_commands.py | 9 +++++++++ tests/test_commands.py | 9 +++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/redis/commands/core.py b/redis/commands/core.py index f2d7bf2eb4..af5384e9ef 100644 --- a/redis/commands/core.py +++ b/redis/commands/core.py @@ -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: diff --git a/tests/test_asyncio/test_commands.py b/tests/test_asyncio/test_commands.py index 955b9d42bc..5219291942 100644 --- a/tests/test_asyncio/test_commands.py +++ b/tests/test_asyncio/test_commands.py @@ -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 diff --git a/tests/test_commands.py b/tests/test_commands.py index c71e347fab..dcdb28a601 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -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") + async 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 From a0a678dabb2d0af686b9172f2789cfb7c19b4553 Mon Sep 17 00:00:00 2001 From: Alibi Date: Wed, 10 May 2023 15:00:08 +0600 Subject: [PATCH 2/2] fix test --- tests/test_commands.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_commands.py b/tests/test_commands.py index dcdb28a601..4e2ac5b041 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -2582,7 +2582,7 @@ def test_zrank(self, r): assert r.zrank("a", "a6") is None @skip_if_server_version_lt("7.2.0") - async def test_zrank_withscore(self, r: redis.Redis): + 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