From 445076fa7704055ecef7c142cbb316dbb2b9a238 Mon Sep 17 00:00:00 2001 From: Shoham Elias Date: Thu, 18 Jan 2024 11:35:22 +0000 Subject: [PATCH] Python: adds ZREM command --- python/python/glide/async_commands/core.py | 31 ++++++++++++++++++++++ python/python/tests/test_async_client.py | 11 ++++++++ 2 files changed, 42 insertions(+) diff --git a/python/python/glide/async_commands/core.py b/python/python/glide/async_commands/core.py index c231f7a4b9..ff1f0a5fa5 100644 --- a/python/python/glide/async_commands/core.py +++ b/python/python/glide/async_commands/core.py @@ -1185,3 +1185,34 @@ async def zadd_incr( Optional[float], await self._execute_command(RequestType.Zadd, args), ) + + async def zrem( + self, + key: str, + members: List[str], + ) -> int: + """ + Removes the specified members from the sorted set stored at `key`. + Specified members that are not a member of this set are ignored. + + See https://redis.io/commands/zrem/ for more details. + + Args: + key (str): The key of the sorted set. + members (List[str]): A list of members to remove from the sorted set. + + Returns: + int: The number of members that were removed from the sorted set, not including non-existing members. + If `key` does not exist, it is treated as an empty sorted set, and this command returns 0. + If `key` holds a value that is not a sorted set, an error is returned. + + Examples: + >>> await zrem("my_sorted_set", ["member1", "member2"]) + 2 # Indicates that two members have been removed from the sorted set "my_sorted_set." + >>> await zrem("non_existing_sorted_set", ["member1", "member2"]) + 0 # Indicates that no members were removed as the sorted set "non_existing_sorted_set" does not exist. + """ + return cast( + int, + await self._execute_command(RequestType.Zrem, [key] + members), + ) diff --git a/python/python/tests/test_async_client.py b/python/python/tests/test_async_client.py index 83f54870d4..23052d7b1d 100644 --- a/python/python/tests/test_async_client.py +++ b/python/python/tests/test_async_client.py @@ -1044,6 +1044,17 @@ async def test_zadd_gt_lt(self, redis_client: TRedisClient): == None ) + @pytest.mark.parametrize("cluster_mode", [True, False]) + async def test_zrem(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.zrem(key, ["one"]) == 1 + assert await redis_client.zrem(key, ["one", "two", "three"]) == 2 + + assert await redis_client.zrem("non_existing_set", ["member"]) == 0 + class TestCommandsUnitTests: def test_expiry_cmd_args(self):