Skip to content

Commit 31cf622

Browse files
author
Joshua
committed
Make params in HSETEX mutual exclusive
Signed-off-by: Joshua <joshua.gehlen@fkie.fraunhofer.de>
1 parent 33a8dc6 commit 31cf622

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

tests/test_commands.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3243,6 +3243,11 @@ def test_hsetex(self, r):
32433243
assert r.hsetex("a", "field1", "value2", ex=5) == 1
32443244
assert r.hget("a", "field1") == b"value2"
32453245

3246+
@skip_if_server_version_lt("9.0.0")
3247+
def test_hset_ex_invalid_params(self, r):
3248+
with pytest.raises(exceptions.DataError):
3249+
r.hsetex("a", "field1", "value1", ex=5, px=5000) # Both ex and px provided
3250+
32463251
@skip_if_server_version_lt("9.0.0")
32473252
def test_hsetex_px(self, r):
32483253
assert r.hsetex("a", "field1", "value1", px=5000) == 1

valkey/commands/core.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5066,6 +5066,7 @@ def hsetex(
50665066
px: Union[ExpiryT, None] = None,
50675067
exat: Union[AbsExpiryT, None] = None,
50685068
pxat: Union[AbsExpiryT, None] = None,
5069+
keepttl: bool = False,
50695070
nx: bool = False,
50705071
xx: bool = False,
50715072
fnx: bool = False,
@@ -5082,6 +5083,20 @@ def hsetex(
50825083

50835084
if key is None and not mapping and not items:
50845085
raise DataError("'hsetex' with no key value pairs")
5086+
5087+
def _check_for_multiple(*args):
5088+
return sum(arg is not None for arg in args)
5089+
5090+
if _check_for_multiple(ex, px, exat, pxat) > 1 or (
5091+
keepttl and _check_for_multiple(ex, px, exat, pxat) > 0
5092+
):
5093+
raise DataError(
5094+
"Only one of 'ex', 'px', 'exat', 'pxat', or 'keepttl' can be specified."
5095+
)
5096+
if nx and xx:
5097+
raise DataError("Only one of 'nx' or 'xx' can be specified.")
5098+
if fnx and fxx:
5099+
raise DataError("Only one of 'fnx' or 'fxx' can be specified.")
50855100
pieces = []
50865101
if ex is not None:
50875102
pieces.extend(["EX", ex])
@@ -5107,6 +5122,8 @@ def hsetex(
51075122
if mapping:
51085123
pieces.append(len(mapping))
51095124
for key, value in mapping.items():
5125+
if key is None or value is None:
5126+
raise DataError("'hsetex' mapping contains None key or value")
51105127
pieces.append(key)
51115128
pieces.append(value)
51125129
if items:

0 commit comments

Comments
 (0)