Skip to content

Commit 2e86cfc

Browse files
committed
Adding async tests; Add docstrings info for the new arg; removed unused option
1 parent 09f0a83 commit 2e86cfc

File tree

2 files changed

+43
-9
lines changed

2 files changed

+43
-9
lines changed

redis/commands/core.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4786,14 +4786,15 @@ def zrank(
47864786
The optional WITHSCORE argument supplements the command's
47874787
reply with the score of the element returned.
47884788
4789+
``score_cast_func`` a callable used to cast the score return value
4790+
47894791
For more information, see https://redis.io/commands/zrank
47904792
"""
47914793
pieces = ["ZRANK", name, value]
47924794
if withscore:
47934795
pieces.append("WITHSCORE")
47944796

47954797
options = {"withscore": withscore, "score_cast_func": score_cast_func}
4796-
options["keys"] = [name]
47974798

47984799
return self.execute_command(*pieces, **options)
47994800

@@ -4851,14 +4852,15 @@ def zrevrank(
48514852
The optional ``withscore`` argument supplements the command's
48524853
reply with the score of the element returned.
48534854
4855+
``score_cast_func`` a callable used to cast the score return value
4856+
48544857
For more information, see https://redis.io/commands/zrevrank
48554858
"""
48564859
pieces = ["ZREVRANK", name, value]
48574860
if withscore:
48584861
pieces.append("WITHSCORE")
48594862

48604863
options = {"withscore": withscore, "score_cast_func": score_cast_func}
4861-
options["keys"] = name
48624864

48634865
return self.execute_command(*pieces, **options)
48644866

@@ -4883,6 +4885,8 @@ def zunion(
48834885
Scores will be aggregated based on the ``aggregate``, or SUM if
48844886
none is provided.
48854887
4888+
``score_cast_func`` a callable used to cast the score return value
4889+
48864890
For more information, see https://redis.io/commands/zunion
48874891
"""
48884892
return self._zaggregate(

tests/test_asyncio/test_commands.py

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from redis.commands.json.path import Path
2424
from redis.commands.search.field import TextField
2525
from redis.commands.search.query import Query
26+
from redis.utils import safe_str
2627
from tests.conftest import (
2728
assert_resp_response,
2829
assert_resp_response_in,
@@ -2071,11 +2072,14 @@ async def test_zrange(self, r: redis.Redis):
20712072
r, response, [(b"a2", 2.0), (b"a3", 3.0)], [[b"a2", 2.0], [b"a3", 3.0]]
20722073
)
20732074

2074-
# custom score function
2075-
# assert await r.zrange("a", 0, 1, withscores=True, score_cast_func=int) == [
2076-
# (b"a1", 1),
2077-
# (b"a2", 2),
2078-
# ]
2075+
# custom score cast function
2076+
response = await r.zrange("a", 0, 1, withscores=True, score_cast_func=safe_str)
2077+
assert_resp_response(
2078+
r,
2079+
response,
2080+
[(b"a1", "1"), (b"a2", "2")],
2081+
[[b"a1", "1.0"], [b"a2", "2.0"]],
2082+
)
20792083

20802084
@skip_if_server_version_lt("2.8.9")
20812085
async def test_zrangebylex(self, r: redis.Redis):
@@ -2127,6 +2131,15 @@ async def test_zrangebyscore(self, r: redis.Redis):
21272131
[(b"a2", 2), (b"a3", 3), (b"a4", 4)],
21282132
[[b"a2", 2], [b"a3", 3], [b"a4", 4]],
21292133
)
2134+
response = await r.zrangebyscore(
2135+
"a", 2, 4, withscores=True, score_cast_func=safe_str
2136+
)
2137+
assert_resp_response(
2138+
r,
2139+
response,
2140+
[(b"a2", "2"), (b"a3", "3"), (b"a4", "4")],
2141+
[[b"a2", "2.0"], [b"a3", "3.0"], [b"a4", "4.0"]],
2142+
)
21302143

21312144
async def test_zrank(self, r: redis.Redis):
21322145
await r.zadd("a", {"a1": 1, "a2": 2, "a3": 3, "a4": 4, "a5": 5})
@@ -2141,10 +2154,14 @@ async def test_zrank_withscore(self, r: redis.Redis):
21412154
assert await r.zrank("a", "a2") == 1
21422155
assert await r.zrank("a", "a6") is None
21432156
assert_resp_response(
2144-
r, await r.zrank("a", "a3", withscore=True), [2, b"3"], [2, 3.0]
2157+
r, await r.zrank("a", "a3", withscore=True), [2, 3.0], [2, 3.0]
21452158
)
21462159
assert await r.zrank("a", "a6", withscore=True) is None
21472160

2161+
# custom score cast function
2162+
response = await r.zrank("a", "a3", withscore=True, score_cast_func=safe_str)
2163+
assert_resp_response(r, response, [2, "3"], [2, "3.0"])
2164+
21482165
async def test_zrem(self, r: redis.Redis):
21492166
await r.zadd("a", {"a1": 1, "a2": 2, "a3": 3})
21502167
assert await r.zrem("a", "a2") == 1
@@ -2200,6 +2217,19 @@ async def test_zrevrange(self, r: redis.Redis):
22002217
r, response, [(b"a3", 3), (b"a2", 2)], [[b"a3", 3], [b"a2", 2]]
22012218
)
22022219

2220+
# custom score cast function
2221+
# should be applied to resp2 and resp3
2222+
# responses
2223+
response = await r.zrevrange(
2224+
"a", 0, 1, withscores=True, score_cast_func=safe_str
2225+
)
2226+
assert_resp_response(
2227+
r,
2228+
response,
2229+
[(b"a3", "3"), (b"a2", "2")],
2230+
[[b"a3", "3.0"], [b"a2", "2.0"]],
2231+
)
2232+
22032233
async def test_zrevrangebyscore(self, r: redis.Redis):
22042234
await r.zadd("a", {"a1": 1, "a2": 2, "a3": 3, "a4": 4, "a5": 5})
22052235
assert await r.zrevrangebyscore("a", 4, 2) == [b"a4", b"a3", b"a2"]
@@ -2240,7 +2270,7 @@ async def test_zrevrank_withscore(self, r: redis.Redis):
22402270
assert await r.zrevrank("a", "a2") == 3
22412271
assert await r.zrevrank("a", "a6") is None
22422272
assert_resp_response(
2243-
r, await r.zrevrank("a", "a3", withscore=True), [2, b"3"], [2, 3.0]
2273+
r, await r.zrevrank("a", "a3", withscore=True), [2, 3.0], [2, 3.0]
22442274
)
22452275
assert await r.zrevrank("a", "a6", withscore=True) is None
22462276

0 commit comments

Comments
 (0)