Skip to content

Commit

Permalink
Removed IFEQ from enum, rewrote tests in accordance
Browse files Browse the repository at this point in the history
Signed-off-by: Angraybill <[email protected]>
  • Loading branch information
Angraybill committed Jan 17, 2025
1 parent e6480e5 commit 3be8dc3
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 34 deletions.
42 changes: 25 additions & 17 deletions python/python/glide/async_commands/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,20 @@ class ConditionalChange(Enum):
A condition to the `SET`, `ZADD` and `GEOADD` commands.
- ONLY_IF_EXISTS - Only update key / elements that already exist. Equivalent to `XX` in the Valkey API.
- ONLY_IF_DOES_NOT_EXIST - Only set key / add elements that does not already exist. Equivalent to `NX` in the Valkey API.
- ONLY_IF_EQUAL - Only update key if the provided value is equal to the old value. Equivalent to `IFEQ` in the Valkey API.
"""

ONLY_IF_EXISTS = "XX"
ONLY_IF_DOES_NOT_EXIST = "NX"
ONLY_IF_EQUAL = "IFEQ"

@dataclass
class OnlyIfEqual():
"""
Condition to the `SET` command
- comparison_value - value to compare to the current value of a key.
If comparison_value is equal to the key, it will overwrite the value of key to the new provided value
Equivalent to the IFEQ comparison-value in the Valkey API
"""
comparison_value: TEncodable


class ExpiryType(Enum):
Expand Down Expand Up @@ -437,8 +445,7 @@ async def set(
self,
key: TEncodable,
value: TEncodable,
conditional_set: Optional[ConditionalChange] = None,
comparison_value: str = None,
conditional_set: Optional[Union[ConditionalChange, OnlyIfEqual]] = None,
expiry: Optional[ExpirySet] = None,
return_old_value: bool = False,
) -> Optional[bytes]:
Expand Down Expand Up @@ -480,26 +487,27 @@ async def set(
# ONLY_IF_EQUAL -> Only set key if provided value is equal to current value of the key
>>> awaut client.set("key", "value")
'OK' # "key" is reset to "value"
>>> await client.set("key", "new_value", conditional_set=ConditionalChange.ONLY_IF_EQUAL, comparison_value="different_value")
>>> await client.set("key", "value")
'OK' # Reset "key" to "value"
>>> await client.set("key", "new_value", conditional_set=OnlyIfEqual("different_value")
'None' # Did not rewrite value of "key" because provided value was not equal to the previous value of "key"
>>> await client.set("key", "new_value", conditional_set.ConditionalChange.ONLY_IF_EQUAL, comparison_value="value")
>>> await client.get("key")
b'value' # Still the original value because nothing got rewritten in the last call
>>> await client.set("key", "new_value", conditional_set=OnlyIfEqual("value")
'OK'
>>> await client.get("key")
b'newest_value" # Set "key" to "new_value" because the provided value was equal to the previous value of "key"
"""
args = [key, value]

if conditional_set:
if isinstance(conditional_set, ConditionalChange):
args.append(conditional_set.value)
if conditional_set.value == ConditionalChange.ONLY_IF_EQUAL.value:
if comparison_value:
args.append(comparison_value)
else:
raise ValueError(
"The 'comparison_value' option must be set when using 'ONLY_IF_EQUAL'"
)

elif isinstance(conditional_set, OnlyIfEqual):
args.append("IFEQ")
if conditional_set.comparison_value:
args.append(conditional_set.comparison_value)
else:
raise ValueError("'comparison_value' must be set when using 'OnlyIfEqual'")

if return_old_value:
args.append("GET")
Expand Down
36 changes: 19 additions & 17 deletions python/python/tests/test_async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from glide.async_commands.command_args import Limit, ListDirection, OrderBy
from glide.async_commands.core import (
ConditionalChange,
OnlyIfEqual,
ExpireOptions,
ExpiryGetEx,
ExpirySet,
Expand Down Expand Up @@ -471,26 +472,27 @@ async def test_conditional_set(self, glide_client: TGlideClient):

@pytest.mark.parametrize("cluster_mode", [True, False])
@pytest.mark.parametrize("protocol", [ProtocolVersion.RESP2, ProtocolVersion.RESP3])
@pytest.mark.skip_if_version_below("8.1.0")
async def test_set_only_if_equal(self, glide_client: TGlideClient):
if await check_if_server_version_lt(glide_client, "8.1.0"):
key = get_random_string(10)
value = get_random_string(10)
value2 = get_random_string(10)
key = get_random_string(10)
value = get_random_string(10)
value2 = get_random_string(10)
wrong_comparison_value = get_random_string(10)
while wrong_comparison_value == value:
wrong_comparison_value = get_random_string(10)
while wrong_comparison_value == value:
wrong_comparison_value = get_random_string(10)


res = await glide_client.set(
key, "foobar", conditional_set=ConditionalChange.ONLY_IF_EQUAL, comparison_value=wrong_comparison_value,
)
assert res is None
assert await glide_client.get(key) == value.encode()
res = await glide_client.set(
key, value2, conditional_set=ConditionalChange.ONLY_IF_EQUAL, comparison_value=value,
)
assert res == OK
assert await glide_client.get(key) == value2.encode()
await glide_client.set(key, value)

res = await glide_client.set(
key, "foobar", conditional_set=OnlyIfEqual(wrong_comparison_value),
)
assert res is None
assert await glide_client.get(key) == value.encode()
res = await glide_client.set(
key, value2, conditional_set=OnlyIfEqual(value),
)
assert res == OK
assert await glide_client.get(key) == value2.encode()

@pytest.mark.parametrize("cluster_mode", [True, False])
@pytest.mark.parametrize("protocol", [ProtocolVersion.RESP2, ProtocolVersion.RESP3])
Expand Down

0 comments on commit 3be8dc3

Please sign in to comment.