Skip to content

Commit

Permalink
PR suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
aaron-congo committed Apr 29, 2024
1 parent db8173f commit b6a89b7
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 38 deletions.
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 @@ -363,7 +363,7 @@ fn convert_flat_array_to_key_value_pairs(array: Vec<Value>) -> RedisResult<Value
.into());
}

let mut result = Vec::new();
let mut result = Vec::with_capacity(array.len() / 2);
for i in (0..array.len()).step_by(2) {
let pair = vec![array[i].clone(), array[i + 1].clone()];
result.push(Value::Array(pair));
Expand Down
16 changes: 7 additions & 9 deletions python/python/glide/async_commands/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -835,7 +835,7 @@ async def hrandfield(self, key: str) -> Optional[str]:
"""
Returns a random field name from the hash value stored at `key`.
See https://redis.io/commands/hrandfield/ for more details.
See https://valkey.io/commands/hrandfield for more details.
Args:
key (str): The key of the hash.
Expand All @@ -854,9 +854,9 @@ async def hrandfield(self, key: str) -> Optional[str]:

async def hrandfield_count(self, key: str, count: int) -> List[str]:
"""
Retrieves random field names from the hash value stored at `key`.
Retrieves up to `count` random field names from the hash value stored at `key`.
See https://redis.io/commands/hrandfield/ for more details.
See https://valkey.io/commands/hrandfield for more details.
Args:
key (str): The key of the hash.
Expand All @@ -881,13 +881,11 @@ async def hrandfield_count(self, key: str, count: int) -> List[str]:

WITH_VALUES: str = "WITHVALUES"

async def hrandfield_count_withvalues(
self, key: str, count: int
) -> List[List[str]]:
async def hrandfield_withvalues(self, key: str, count: int) -> List[List[str]]:
"""
Retrieves random field names along with their values from the hash value stored at `key`.
Retrieves up to `count` random field names along with their values from the hash value stored at `key`.
See https://redis.io/commands/hrandfield/ for more details.
See https://valkey.io/commands/hrandfield for more details.
Args:
key (str): The key of the hash.
Expand All @@ -901,7 +899,7 @@ async def hrandfield_count_withvalues(
If the hash does not exist or is empty, the response will be an empty list.
Examples:
>>> await client.hrandfield_count_withvalues("my_hash", -3)
>>> await client.hrandfield_withvalues("my_hash", -3)
[["field1", "value1"], ["field1", "value1"], ["field2", "value2"]]
"""
return cast(
Expand Down
14 changes: 6 additions & 8 deletions python/python/glide/async_commands/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,7 @@ def hrandfield(self: TTransaction, key: str) -> TTransaction:
"""
Returns a random field name from the hash value stored at `key`.
See https://redis.io/commands/hrandfield/ for more details.
See https://valkey.io/commands/hrandfield for more details.
Args:
key (str): The key of the hash.
Expand All @@ -626,9 +626,9 @@ def hrandfield(self: TTransaction, key: str) -> TTransaction:

def hrandfield_count(self: TTransaction, key: str, count: int) -> TTransaction:
"""
Retrieves random field names from the hash value stored at `key`.
Retrieves up to `count` random field names from the hash value stored at `key`.
See https://redis.io/commands/hrandfield/ for more details.
See https://valkey.io/commands/hrandfield for more details.
Args:
key (str): The key of the hash.
Expand All @@ -642,13 +642,11 @@ def hrandfield_count(self: TTransaction, key: str, count: int) -> TTransaction:
"""
return self.append_command(RequestType.HRandField, [key, str(count)])

def hrandfield_count_withvalues(
self: TTransaction, key: str, count: int
) -> TTransaction:
def hrandfield_withvalues(self: TTransaction, key: str, count: int) -> TTransaction:
"""
Retrieves random field names along with their values from the hash value stored at `key`.
Retrieves up to `count` random field names along with their values from the hash value stored at `key`.
See https://redis.io/commands/hrandfield/ for more details.
See https://valkey.io/commands/hrandfield for more details.
Args:
key (str): The key of the hash.
Expand Down
16 changes: 6 additions & 10 deletions python/python/tests/test_async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -846,7 +846,7 @@ async def test_hrandfield_count(self, redis_client: TRedisClient):

@pytest.mark.parametrize("cluster_mode", [True, False])
@pytest.mark.parametrize("protocol", [ProtocolVersion.RESP2, ProtocolVersion.RESP3])
async def test_hrandfield_count_withvalues(self, redis_client: TRedisClient):
async def test_hrandfield_withvalues(self, redis_client: TRedisClient):
key = get_random_string(10)
key2 = get_random_string(5)
field = get_random_string(5)
Expand All @@ -855,27 +855,23 @@ async def test_hrandfield_count_withvalues(self, redis_client: TRedisClient):

assert await redis_client.hset(key, field_value_map) == 2
# Unique values are expected as count is positive
rand_fields_with_values = await redis_client.hrandfield_count_withvalues(key, 4)
rand_fields_with_values = await redis_client.hrandfield_withvalues(key, 4)
assert len(rand_fields_with_values) == 2
for field_with_value in rand_fields_with_values:
assert field_with_value in [[field, "value"], [field2, "value2"]]

# Duplicate values are expected as count is negative
rand_fields_with_values = await redis_client.hrandfield_count_withvalues(
key, -4
)
rand_fields_with_values = await redis_client.hrandfield_withvalues(key, -4)
assert len(rand_fields_with_values) == 4
for field_with_value in rand_fields_with_values:
assert field_with_value in [[field, "value"], [field2, "value2"]]

assert await redis_client.hrandfield_count_withvalues(key, 0) == []
assert (
await redis_client.hrandfield_count_withvalues("non_existing_key", 4) == []
)
assert await redis_client.hrandfield_withvalues(key, 0) == []
assert await redis_client.hrandfield_withvalues("non_existing_key", 4) == []

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

@pytest.mark.parametrize("cluster_mode", [True, False])
@pytest.mark.parametrize("protocol", [ProtocolVersion.RESP2, ProtocolVersion.RESP3])
Expand Down
16 changes: 6 additions & 10 deletions python/python/tests/test_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ async def transaction_test(
key8 = "{{{}}}:{}".format(keyslot, get_random_string(3))
key9 = "{{{}}}:{}".format(keyslot, get_random_string(3))
key10 = "{{{}}}:{}".format(keyslot, get_random_string(3)) # hyper log log
key11 = "{{{}}}:{}".format(keyslot, get_random_string(3)) # hash

value = datetime.now(timezone.utc).strftime("%m/%d/%Y, %H:%M:%S")
value2 = get_random_string(5)
Expand Down Expand Up @@ -129,15 +128,12 @@ async def transaction_test(
args.append({key: value, key2: value2, key3: "10.5"})
transaction.hdel(key4, [key, key2])
args.append(2)

transaction.hset(key11, {key: value})
args.append(1)
transaction.hrandfield(key11)
args.append(key)
transaction.hrandfield_count(key11, 1)
args.append([key])
transaction.hrandfield_count_withvalues(key11, 1)
args.append([[key, value]])
transaction.hrandfield(key4)
args.append(key3)
transaction.hrandfield_count(key4, 1)
args.append([key3])
transaction.hrandfield_withvalues(key4, 1)
args.append([[key3, "10.5"]])

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

0 comments on commit b6a89b7

Please sign in to comment.