Skip to content

Commit

Permalink
Python: adds TYPE command (#945)
Browse files Browse the repository at this point in the history
  • Loading branch information
shohamazon authored Feb 13, 2024
1 parent 341eaaf commit 0a233c1
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* Python, Node: Added RPOPCOUNT and LPOPCOUNT to transaction ([#874](https://github.com/aws/glide-for-redis/pull/874))
* Standalone client: Improve connection errors. ([#854](https://github.com/aws/glide-for-redis/pull/854))
* Python, Node: When recieving LPOP/RPOP with count, convert result to Array. ([#811](https://github.com/aws/glide-for-redis/pull/811))
* Python: Added TYPE command ([#945](https://github.com/aws/glide-for-redis/pull/945))

#### 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
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 @@ -103,6 +103,7 @@ enum RequestType {
Zcount = 65;
ZIncrBy = 66;
ZScore = 67;
Type = 68;
}

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 @@ -346,6 +346,7 @@ fn get_command(request: &Command) -> Option<Cmd> {
RequestType::Zcount => Some(cmd("ZCOUNT")),
RequestType::ZIncrBy => Some(cmd("ZINCRBY")),
RequestType::ZScore => Some(cmd("ZSCORE")),
RequestType::Type => Some(cmd("TYPE")),
}
}

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

async def type(self, key: str) -> str:
"""
Returns the string representation of the type of the value stored at `key`.
See https://redis.io/commands/type/ for more details.
Args:
key (str): The key to check its data type.
Returns:
str: If the key exists, the type of the stored value is returned.
Otherwise, a "none" string is returned.
Examples:
>>> await client.set("key", "value")
>>> await client.type("key")
'string'
"""
return cast(str, await self._execute_command(RequestType.Type, [key]))

async def zadd(
self,
key: str,
Expand Down
15 changes: 15 additions & 0 deletions python/python/glide/async_commands/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -803,6 +803,21 @@ def ttl(self, key: str):
"""
self.append_command(RequestType.TTL, [key])

def type(self, key: str):
"""
Returns the string representation of the type of the value stored at `key`.
See https://redis.io/commands/type/ for more details.
Args:
key (str): The key to check its data type.
Commands response:
str: If the key exists, the type of the stored value is returned.
Otherwise, a "none" string is returned.
"""
self.append_command(RequestType.Type, [key])

def zadd(
self,
key: str,
Expand Down
30 changes: 30 additions & 0 deletions python/python/tests/test_async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1174,6 +1174,36 @@ async def test_zscore(self, redis_client: TRedisClient):
await redis_client.zscore("non_existing_key", "non_existing_member") == None
)

@pytest.mark.parametrize("cluster_mode", [True, False])
@pytest.mark.parametrize("protocol", [ProtocolVersion.RESP2, ProtocolVersion.RESP3])
async def test_type(self, redis_client: TRedisClient):
key = get_random_string(10)
assert await redis_client.set(key, "value") == OK
assert (await redis_client.type(key)).lower() == "string"
assert await redis_client.delete([key]) == 1

assert await redis_client.lpush(key, ["value"]) == 1
assert (await redis_client.type(key)).lower() == "list"
assert await redis_client.delete([key]) == 1

assert await redis_client.sadd(key, ["value"]) == 1
assert (await redis_client.type(key)).lower() == "set"
assert await redis_client.delete([key]) == 1

assert await redis_client.zadd(key, {"member": 1.0}) == 1
assert (await redis_client.type(key)).lower() == "zset"
assert await redis_client.delete([key]) == 1

assert await redis_client.hset(key, {"field": "value"}) == 1
assert (await redis_client.type(key)).lower() == "hash"
assert await redis_client.delete([key]) == 1

await redis_client.custom_command(["XADD", key, "*", "field", "value"])
assert await redis_client.type(key) == "stream"
assert await redis_client.delete([key]) == 1

assert (await redis_client.type(key)).lower() == "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 @@ -35,6 +35,7 @@ def transaction_test(

transaction.set(key, value)
transaction.get(key)
transaction.type(key)

transaction.exists([key])

Expand Down Expand Up @@ -98,6 +99,7 @@ def transaction_test(
return [
OK,
value,
"string",
1,
1,
None,
Expand Down

0 comments on commit 0a233c1

Please sign in to comment.