Skip to content

Commit

Permalink
Python: adds ZSCORE command
Browse files Browse the repository at this point in the history
  • Loading branch information
shohamazon committed Feb 1, 2024
1 parent 2ea5d6c commit 495c8f1
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 3 deletions.
1 change: 1 addition & 0 deletions glide-core/src/client/value_conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ pub(crate) fn expected_type_for_cmd(cmd: &Cmd) -> Option<ExpectedReturnType> {
Some(ExpectedReturnType::Boolean)
}
b"SMEMBERS" => Some(ExpectedReturnType::Set),
b"ZSCORE" => Some(ExpectedReturnType::DoubleOrNull),
_ => None,
}
}
Expand Down
1 change: 1 addition & 0 deletions glide-core/src/protobuf/redis_request.proto
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ enum RequestType {
Zcard = 64;
Zcount = 65;
ZIncrBy = 66;
ZScore = 67;
}

message Command {
Expand Down
1 change: 1 addition & 0 deletions glide-core/src/socket_listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ fn get_command(request: &Command) -> Option<Cmd> {
RequestType::Zcard => Some(cmd("ZCARD")),
RequestType::Zcount => Some(cmd("ZCOUNT")),
RequestType::ZIncrBy => Some(cmd("ZINCRBY")),
RequestType::ZScore => Some(cmd("ZSCORE")),
}
}

Expand Down
37 changes: 34 additions & 3 deletions python/python/glide/async_commands/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,21 +182,24 @@ async def _execute_command(
request_type: RequestType.ValueType,
args: List[str],
route: Optional[Route] = ...,
) -> TResult: ...
) -> TResult:
...

async def _execute_transaction(
self,
commands: List[Tuple[RequestType.ValueType, List[str]]],
route: Optional[Route] = None,
) -> List[TResult]: ...
) -> List[TResult]:
...

async def _execute_script(
self,
hash: str,
keys: Optional[List[str]] = None,
args: Optional[List[str]] = None,
route: Optional[Route] = None,
) -> TResult: ...
) -> TResult:
...

async def set(
self,
Expand Down Expand Up @@ -1245,6 +1248,34 @@ async def zrem(
await self._execute_command(RequestType.Zrem, [key] + members),
)

async def zscore(self, key: str, member: str) -> Optional[float]:
"""
Returns the score of `member` in the sorted set stored at `key`.
See https://redis.io/commands/zscore/ for more details.
Args:
key (str): The key of the sorted set.
member (str): The member whose score is to be retrieved.
Returns:
Optional[float]: The score of the member.
If `member` does not exist in the sorted set, None is returned.
If `key` does not exist, None is returned.
If `key` holds a value that is not a sorted set, an error is returned.
Examples:
>>> await zscore("my_sorted_set", "member")
10.5 # Indicates that the score of "member" in the sorted set "my_sorted_set" is 10.5.
>>> await zscore("my_sorted_set", "non_existing_member")
None
"""
return cast(
Optional[float],
await self._execute_command(RequestType.ZScore, [key, member]),
)

async def invoke_script(
self,
script: Script,
Expand Down
18 changes: 18 additions & 0 deletions python/python/glide/async_commands/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,24 @@ def zrem(
"""
self.append_command(RequestType.Zrem, [key] + members)

def zscore(self, key: str, member: str):
"""
Returns the score of `member` in the sorted set stored at `key`.
See https://redis.io/commands/zscore/ for more details.
Args:
key (str): The key of the sorted set.
member (str): The member whose score is to be retrieved.
Commands response:
Optional[float]: The score of the member.
If `member` does not exist in the sorted set, None is returned.
If `key` does not exist, None is returned.
If `key` holds a value that is not a sorted set, the transaction fails with an error
"""
self.append_command(RequestType.ZScore, [key, member])


class Transaction(BaseTransaction):
"""
Expand Down
12 changes: 12 additions & 0 deletions python/python/tests/test_async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1069,6 +1069,18 @@ async def test_zcard(self, redis_client: TRedisClient):
assert await redis_client.zrem(key, ["one"]) == 1
assert await redis_client.zcard(key) == 2

@pytest.mark.parametrize("cluster_mode", [True, False])
async def test_zscore(self, redis_client: TRedisClient):
key = get_random_string(10)
members_scores = {"one": 1, "two": 2, "three": 3}
assert await redis_client.zadd(key, members_scores=members_scores) == 3
assert await redis_client.zscore(key, "one") == 1.0

assert await redis_client.zscore(key, "non_existing_member") == None
assert (
await redis_client.zscore("non_existing_key", "non_existing_member") == None
)


class TestCommandsUnitTests:
def test_expiry_cmd_args(self):
Expand Down
2 changes: 2 additions & 0 deletions python/python/tests/test_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ def transaction_test(
transaction.zadd_incr(key8, "one", 3)
transaction.zrem(key8, ["one"])
transaction.zcard(key8)
transaction.zscore(key8, "two")
return [
OK,
value,
Expand Down Expand Up @@ -132,6 +133,7 @@ def transaction_test(
4,
1,
2,
2.0,
]


Expand Down

0 comments on commit 495c8f1

Please sign in to comment.