Skip to content

Commit

Permalink
Python: adds SISMEMBER command
Browse files Browse the repository at this point in the history
  • Loading branch information
shohamazon committed Feb 20, 2024
1 parent 5d0a2e7 commit b4d8c1b
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 2 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
* Python, Node: When recieving LPOP/RPOP with count, convert result to Array. ([#811](https://github.com/aws/glide-for-redis/pull/811))
* Python, Node: Added TYPE command ([#945](https://github.com/aws/glide-for-redis/pull/945), [#980](https://github.com/aws/glide-for-redis/pull/980))
* Python: Added HLEN command ([#944](https://github.com/aws/glide-for-redis/pull/944))
* Node: Added ZCOUNT command ([#909](https://github.com/aws/glide-for-redis/pull/909))
* Python, Node: Added ZCOUNT command ([#878](https://github.com/aws/glide-for-redis/pull/878)) ([#909](https://github.com/aws/glide-for-redis/pull/909))
* Python: Added ECHO command ([#953](https://github.com/aws/glide-for-redis/pull/953))
* Python: Added ZPOPMIN command ([#975](https://github.com/aws/glide-for-redis/pull/975))
* Python: Added SISMEMBER command ([#971](https://github.com/aws/glide-for-redis/pull/971))

#### Features
* Python, Node: Added support in Lua Scripts ([#775](https://github.com/aws/glide-for-redis/pull/775), [#860](https://github.com/aws/glide-for-redis/pull/860))
Expand Down
2 changes: 1 addition & 1 deletion glide-core/src/client/value_conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ pub(crate) fn expected_type_for_cmd(cmd: &Cmd) -> Option<ExpectedReturnType> {
Some(ExpectedReturnType::Map)
}
b"INCRBYFLOAT" | b"HINCRBYFLOAT" => Some(ExpectedReturnType::Double),
b"HEXISTS" | b"EXPIRE" | b"EXPIREAT" | b"PEXPIRE" | b"PEXPIREAT" => {
b"HEXISTS" | b"EXPIRE" | b"SISMEMBER" | b"EXPIREAT" | b"PEXPIRE" | b"PEXPIREAT" => {
Some(ExpectedReturnType::Boolean)
}
b"SMEMBERS" => Some(ExpectedReturnType::Set),
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 @@ -107,6 +107,7 @@ enum RequestType {
HLen = 69;
Echo = 70;
ZPopMin = 71;
SIsMember = 72;
}

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 @@ -350,6 +350,7 @@ fn get_command(request: &Command) -> Option<Cmd> {
RequestType::HLen => Some(cmd("HLEN")),
RequestType::Echo => Some(cmd("ECHO")),
RequestType::ZPopMin => Some(cmd("ZPOPMIN")),
RequestType::SIsMember => Some(cmd("SISMEMBER")),
}
}

Expand Down
29 changes: 29 additions & 0 deletions python/python/glide/async_commands/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,35 @@ async def scard(self, key: str) -> int:
"""
return cast(int, await self._execute_command(RequestType.SCard, [key]))

async def sismember(
self,
key: str,
member: str,
) -> bool:
"""
Returns if `member` is a member of the set stored at `key`.
See https://redis.io/commands/sismember/ for more details.
Args:
key (str): The key of the set.
member (str): The member to check for existence in the set.
Returns:
bool: True if the member exists in the set, False otherwise.
If `key` doesn't exist, it is treated as an empty set and the command returns False.
Examples:
>>> await client.sismember("my_set", "member1")
True # Indicates that "member1" exists in the set "my_set".
>>> await client.sismember("my_set", "non_existing_member")
False # Indicates that "non_existing_member" does not exist in the set "my_set".
"""
return cast(
bool,
await self._execute_command(RequestType.SIsMember, [key, member]),
)

async def ltrim(self, key: str, start: int, end: int) -> TOK:
"""
Trim an existing list so that it will contain only the specified range of elements specified.
Expand Down
20 changes: 20 additions & 0 deletions python/python/glide/async_commands/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,26 @@ def scard(self: TTransaction, key: str) -> TTransaction:
"""
return self.append_command(RequestType.SCard, [key])

def sismember(
self: TTransaction,
key: str,
member: str,
) -> TTransaction:
"""
Returns if `member` is a member of the set stored at `key`.
See https://redis.io/commands/sismember/ for more details.
Args:
key (str): The key of the set.
member (str): The member to check for existence in the set.
Commands response:
bool: True if the member exists in the set, False otherwise.
If `key` doesn't exist, it is treated as an empty set and the command returns False.
"""
return self.append_command(RequestType.SIsMember, [key, member])

def ltrim(self: TTransaction, key: str, start: int, end: int) -> TTransaction:
"""
Trim an existing list so that it will contain only the specified range of elements specified.
Expand Down
11 changes: 11 additions & 0 deletions python/python/tests/test_async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,17 @@ async def test_sadd_srem_smembers_scard_wrong_type_raise_error(
await redis_client.smembers(key)
assert "Operation against a key holding the wrong kind of value" in str(e)

@pytest.mark.parametrize("cluster_mode", [True, False])
@pytest.mark.parametrize("protocol", [ProtocolVersion.RESP2, ProtocolVersion.RESP3])
async def test_sismember(self, redis_client: TRedisClient):
key = get_random_string(10)
member = get_random_string(5)
assert await redis_client.sadd(key, [member]) == 1
assert await redis_client.sismember(key, member) == True

assert await redis_client.sismember(key, get_random_string(5)) == False
assert await redis_client.sismember("non_existing_key", member) == False

@pytest.mark.parametrize("cluster_mode", [True, False])
@pytest.mark.parametrize("protocol", [ProtocolVersion.RESP2, ProtocolVersion.RESP3])
async def test_ltrim(self, redis_client: TRedisClient):
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 @@ -134,6 +134,8 @@ def transaction_test(
args.append({"bar"})
transaction.scard(key7)
args.append(1)
transaction.sismember(key7, "bar")
args.append(True)

transaction.zadd(key8, {"one": 1, "two": 2, "three": 3})
args.append(3)
Expand Down

0 comments on commit b4d8c1b

Please sign in to comment.