2323from redis .commands .json .path import Path
2424from redis .commands .search .field import TextField
2525from redis .commands .search .query import Query
26+ from redis .utils import safe_str
2627from 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