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: adds HSTRLEN command #1564

Merged
merged 2 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
* Node: Added XLEN command ([#1555](https:/aws/glide-for-redis/pull/1555))
* Node: Added ZINTERCARD command ([#1553](https:/aws/glide-for-redis/pull/1553))
* Python: Added LMPOP and BLMPOP commands ([#1547](https:/aws/glide-for-redis/pull/1547))
* Python: Added HSTRLEN command ([#1564](https:/aws/glide-for-redis/pull/1564))

### Breaking Changes
* Node: Update XREAD to return a Map of Map ([#1494](https:/aws/glide-for-redis/pull/1494))
Expand Down
25 changes: 24 additions & 1 deletion python/python/glide/async_commands/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -838,7 +838,7 @@ async def hget(self, key: str, field: str) -> Optional[str]:
Returns None if `field` is not presented in the hash or `key` does not exist.

Examples:
>>> await client.hset("my_hash", "field")
>>> await client.hset("my_hash", "field", "value")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️

>>> await client.hget("my_hash", "field")
"value"
>>> await client.hget("my_hash", "nonexistent_field")
Expand Down Expand Up @@ -1152,6 +1152,29 @@ async def hrandfield_withvalues(self, key: str, count: int) -> List[List[str]]:
),
)

async def hstrlen(self, key: str, field: str) -> int:
"""
Returns the string length of the value associated with `field` in the hash stored at `key`.

See https://valkey.io/commands/hstrlen/ for more details.

Args:
key (str): The key of the hash.
field (str): The field in the hash.

Returns:
int: The string length or 0 if `field` or `key` does not exist.

Examples:
>>> await client.hset("my_hash", "field", "value")
>>> await client.hstrlen("my_hash", "my_field")
5
"""
return cast(
int,
await self._execute_command(RequestType.HStrlen, [key, field]),
)

async def lpush(self, key: str, elements: List[str]) -> int:
"""
Insert all the specified values at the head of the list stored at `key`.
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 @@ -718,6 +718,21 @@ def hrandfield_withvalues(self: TTransaction, key: str, count: int) -> TTransact
RequestType.HRandField, [key, str(count), "WITHVALUES"]
)

def hstrlen(self: TTransaction, key: str, field: str) -> TTransaction:
"""
Returns the string length of the value associated with `field` in the hash stored at `key`.

See https://valkey.io/commands/hstrlen/ for more details.

Args:
key (str): The key of the hash.
field (str): The field in the hash.

Commands response:
int: The string length or 0 if `field` or `key` does not exist.
"""
return self.append_command(RequestType.HStrlen, [key, field])

def lpush(self: TTransaction, key: str, elements: List[str]) -> TTransaction:
"""
Insert all the specified values at the head of the list stored at `key`.
Expand Down
15 changes: 15 additions & 0 deletions python/python/tests/test_async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,21 @@ async def test_hrandfield_withvalues(self, redis_client: TRedisClient):
with pytest.raises(RequestError):
await redis_client.hrandfield_withvalues(key2, 5)

@pytest.mark.parametrize("cluster_mode", [True, False])
@pytest.mark.parametrize("protocol", [ProtocolVersion.RESP2, ProtocolVersion.RESP3])
async def test_hstrlen(self, redis_client: TRedisClient):
key = get_random_string(10)

assert await redis_client.hstrlen(key, "field") == 0
assert await redis_client.hset(key, {"field": "value"}) == 1
assert await redis_client.hstrlen(key, "field") == 5

assert await redis_client.hstrlen(key, "field2") == 0
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure why you have this test, since you're already testing the non-existing field case on line 873

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line 873 is for non-existing key, I think it's nice to have both


await redis_client.set(key, "value")
with pytest.raises(RequestError):
await redis_client.hstrlen(key, "field")

@pytest.mark.parametrize("cluster_mode", [True, False])
@pytest.mark.parametrize("protocol", [ProtocolVersion.RESP2, ProtocolVersion.RESP3])
async def test_lpush_lpop_lrange(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 @@ -164,6 +164,8 @@ async def transaction_test(
args.append([key3])
transaction.hrandfield_withvalues(key4, 1)
args.append([[key3, "10.5"]])
transaction.hstrlen(key4, key3)
args.append(4)

transaction.client_getname()
args.append(None)
Expand Down
Loading