Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python: add ZCARD command #871

Merged
merged 1 commit into from
Jan 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions python/python/glide/async_commands/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1184,6 +1184,24 @@ async def zadd_incr(
await self._execute_command(RequestType.Zadd, args),
)

async def zcard(self, key: str) -> int:
"""
Returns the cardinality (number of elements) of the sorted set stored at `key`.

See https://redis.io/commands/zcard/ for more details.

Args:
key (str): The key of the sorted set.

Returns:
int: The number of elements in the sorted set.

Examples:
>>> await zcard("my_sorted_set")
3 # Indicates that there are 3 elements in the sorted set "my_sorted_set".
"""
return cast(int, await self._execute_command(RequestType.Zcard, [key]))

async def zrem(
self,
key: str,
Expand Down
14 changes: 14 additions & 0 deletions python/python/glide/async_commands/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -877,6 +877,20 @@ def zadd_incr(
args += [str(increment), member]
self.append_command(RequestType.Zadd, args)

def zcard(self, key: str):
"""
Returns the cardinality (number of elements) of the sorted set stored at `key`.

See https://redis.io/commands/zcard/ for more details.

Args:
key (str): The key of the sorted set.

Commands response:
int: The number of elements in the sorted set.
"""
self.append_command(RequestType.Zcard, [key])

def zrem(
self,
key: str,
Expand Down
10 changes: 10 additions & 0 deletions python/python/tests/test_async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1055,6 +1055,16 @@ async def test_zrem(self, redis_client: TRedisClient):

assert await redis_client.zrem("non_existing_set", ["member"]) == 0

@pytest.mark.parametrize("cluster_mode", [True, False])
async def test_zcard(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.zcard(key) == 3

assert await redis_client.zrem(key, ["one"]) == 1
assert await redis_client.zcard(key) == 2


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 @@ -87,6 +87,7 @@ def transaction_test(
transaction.zadd(key8, {"one": 1, "two": 2, "three": 3})
transaction.zadd_incr(key8, "one", 3)
transaction.zrem(key8, ["one"])
transaction.zcard(key8)
return [
OK,
value,
Expand Down Expand Up @@ -128,6 +129,7 @@ def transaction_test(
3,
4,
1,
2,
]


Expand Down
Loading