From 5e8c72dc214422b1f907c0691b68485c0122bb80 Mon Sep 17 00:00:00 2001 From: shacharPash Date: Thu, 27 Apr 2023 16:38:43 +0300 Subject: [PATCH 1/4] add withscores to zrevrank --- redis/commands/core.py | 8 +++++--- tests/test_asyncio/test_commands.py | 2 ++ tests/test_commands.py | 2 ++ 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/redis/commands/core.py b/redis/commands/core.py index e2cabb85fa..89b8809bd4 100644 --- a/redis/commands/core.py +++ b/redis/commands/core.py @@ -4693,14 +4693,16 @@ 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 """ - return self.execute_command("ZREVRANK", name, value) + return self.execute_command("ZREVRANK", name, value, withscore) 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 7c6fd45ab9..871e6a53f3 100644 --- a/tests/test_asyncio/test_commands.py +++ b/tests/test_asyncio/test_commands.py @@ -1709,6 +1709,8 @@ async def test_zrevrank(self, r: redis.Redis): 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", withscores=True) == [0, "3"] + assert await r.zrevrank("a", "a6", withscores=True) is None async def test_zscore(self, r: redis.Redis): await r.zadd("a", {"a1": 1, "a2": 2, "a3": 3}) diff --git a/tests/test_commands.py b/tests/test_commands.py index 94249e9419..3b099d5948 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -2640,6 +2640,8 @@ def test_zrevrank(self, r): assert r.zrevrank("a", "a1") == 4 assert r.zrevrank("a", "a2") == 3 assert r.zrevrank("a", "a6") is None + assert r.zrevrank("a", "a3", withscores=True) == [0, "3"] + assert r.zrevrank("a", "a6", withscores=True) is None def test_zscore(self, r): r.zadd("a", {"a1": 1, "a2": 2, "a3": 3}) From cb7e4577e54aba90ab2852320421d33b23d52df5 Mon Sep 17 00:00:00 2001 From: shacharPash Date: Thu, 27 Apr 2023 16:49:44 +0300 Subject: [PATCH 2/4] change 0 -> 2 --- tests/test_asyncio/test_commands.py | 4 ++-- tests/test_commands.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/test_asyncio/test_commands.py b/tests/test_asyncio/test_commands.py index 871e6a53f3..d4e95b53ef 100644 --- a/tests/test_asyncio/test_commands.py +++ b/tests/test_asyncio/test_commands.py @@ -1709,8 +1709,8 @@ async def test_zrevrank(self, r: redis.Redis): 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", withscores=True) == [0, "3"] - assert await r.zrevrank("a", "a6", withscores=True) 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}) diff --git a/tests/test_commands.py b/tests/test_commands.py index 3b099d5948..47dabda7ae 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -2640,8 +2640,8 @@ def test_zrevrank(self, r): assert r.zrevrank("a", "a1") == 4 assert r.zrevrank("a", "a2") == 3 assert r.zrevrank("a", "a6") is None - assert r.zrevrank("a", "a3", withscores=True) == [0, "3"] - assert r.zrevrank("a", "a6", withscores=True) 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}) From c41cef93460f2647e66bb2f1ab10c7d59778677f Mon Sep 17 00:00:00 2001 From: shacharPash Date: Thu, 27 Apr 2023 17:03:56 +0300 Subject: [PATCH 3/4] fix errors --- redis/commands/core.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/redis/commands/core.py b/redis/commands/core.py index 89b8809bd4..d67291b314 100644 --- a/redis/commands/core.py +++ b/redis/commands/core.py @@ -4693,7 +4693,12 @@ def zremrangebyscore( """ return self.execute_command("ZREMRANGEBYSCORE", name, min, max) - def zrevrank(self, name: KeyT, value: EncodableT, withscore: bool = False) -> 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``. @@ -4702,7 +4707,9 @@ def zrevrank(self, name: KeyT, value: EncodableT, withscore: bool = False) -> Re For more information see https://redis.io/commands/zrevrank """ - return self.execute_command("ZREVRANK", name, value, withscore) + 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: """ From aa694f836be68e8fbc24252d1710e1a2bccff6be Mon Sep 17 00:00:00 2001 From: shacharPash Date: Thu, 27 Apr 2023 17:16:32 +0300 Subject: [PATCH 4/4] split test --- tests/test_asyncio/test_commands.py | 7 +++++++ tests/test_commands.py | 7 +++++++ 2 files changed, 14 insertions(+) diff --git a/tests/test_asyncio/test_commands.py b/tests/test_asyncio/test_commands.py index d4e95b53ef..409934c9a3 100644 --- a/tests/test_asyncio/test_commands.py +++ b/tests/test_asyncio/test_commands.py @@ -1709,6 +1709,13 @@ async def test_zrevrank(self, r: redis.Redis): assert await r.zrevrank("a", "a1") == 4 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 diff --git a/tests/test_commands.py b/tests/test_commands.py index 47dabda7ae..f631092df7 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -2640,6 +2640,13 @@ def test_zrevrank(self, r): assert r.zrevrank("a", "a1") == 4 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