Skip to content

Commit

Permalink
Python: adds HVALS command
Browse files Browse the repository at this point in the history
  • Loading branch information
shohamazon committed Mar 17, 2024
1 parent 03a562a commit fd1d660
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* Python, Node: Added ZPOPMAX command ([#996](https://github.com/aws/glide-for-redis/pull/996), [#1009](https://github.com/aws/glide-for-redis/pull/1009))
* Python: Added ZRANGE command ([#906](https://github.com/aws/glide-for-redis/pull/906))
* Python, Node: Added PTTL command ([#1036](https://github.com/aws/glide-for-redis/pull/1036), [#1082](https://github.com/aws/glide-for-redis/pull/1082))
* Node: Added HVAL command ([#1022](https://github.com/aws/glide-for-redis/pull/1022))
* Python, Node: Added HVAL command ([#1130](https://github.com/aws/glide-for-redis/pull/1130)), ([#1022](https://github.com/aws/glide-for-redis/pull/1022))
* Node: Added PERSIST command ([#1023](https://github.com/aws/glide-for-redis/pull/1023))
* Node: Added ZREMRANGEBYSCORE command ([#926](https://github.com/aws/glide-for-redis/pull/926))
* Node: Added ZREMRANGEBYRANK command ([#924](https://github.com/aws/glide-for-redis/pull/924))
Expand Down
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 @@ -634,6 +634,24 @@ async def hlen(self, key: str) -> int:
"""
return cast(int, await self._execute_command(RequestType.HLen, [key]))

async def hvals(self, key: str) -> List[str]:
"""
Returns all values in the hash stored at `key`.
See https://redis.io/commands/hvals/ for more details.
Args:
key (str): The key of the hash.
Returns:
List[str]: A list of values in the hash, or an empty list when the key does not exist.
Examples:
>>> await client.hvals("my_hash")
["value1", "value2", "value3"] # Returns all the values stored in the hash "my_hash".
"""
return cast(List[str], await self._execute_command(RequestType.Hvals, [key]))

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
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 @@ -529,6 +529,20 @@ def hdel(self: TTransaction, key: str, fields: List[str]) -> TTransaction:
"""
return self.append_command(RequestType.HashDel, [key] + fields)

def hvals(self: TTransaction, key: str) -> TTransaction:
"""
Returns all values in the hash stored at `key`.
See https://redis.io/commands/hvals/ for more details.
Args:
key (str): The key of the hash.
Command response:
List[str]: A list of values in the hash, or an empty list when the key does not exist.
"""
return self.append_command(RequestType.Hvals, [key])

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
19 changes: 19 additions & 0 deletions python/python/tests/test_async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,25 @@ async def test_hlen(self, redis_client: TRedisClient):
with pytest.raises(RequestError):
await redis_client.hlen(key2)

@pytest.mark.parametrize("cluster_mode", [True, False])
@pytest.mark.parametrize("protocol", [ProtocolVersion.RESP2, ProtocolVersion.RESP3])
async def test_hvals(self, redis_client: TRedisClient):
key = get_random_string(10)
key2 = get_random_string(5)
field = get_random_string(5)
field2 = get_random_string(5)
field_value_map = {field: "value", field2: "value2"}

assert await redis_client.hset(key, field_value_map) == 2
assert await redis_client.hvals(key) == ["value", "value2"]
assert await redis_client.hdel(key, [field]) == 1
assert await redis_client.hvals(key) == ["value2"]
assert await redis_client.hvals("non_existing_key") == []

assert await redis_client.set(key2, "value") == OK
with pytest.raises(RequestError):
await redis_client.hvals(key2)

@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 @@ -86,6 +86,8 @@ def transaction_test(
args.append(value2)
transaction.hlen(key4)
args.append(2)
transaction.hvals(key4)
args.append([value, value2])
transaction.hsetnx(key4, key, value)
args.append(False)
transaction.hincrby(key4, key3, 5)
Expand Down

0 comments on commit fd1d660

Please sign in to comment.