Skip to content

Commit e27fbbd

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

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-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: 12 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,15 @@ 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+
if int(keepttl) + sum(arg is not None for arg in [ex, px, exat, pxat]) > 1:
5088+
raise DataError(
5089+
"Only one of 'ex', 'px', 'exat', 'pxat', or 'keepttl' can be specified."
5090+
)
5091+
if nx and xx:
5092+
raise DataError("Only one of 'nx' or 'xx' can be specified.")
5093+
if fnx and fxx:
5094+
raise DataError("Only one of 'fnx' or 'fxx' can be specified.")
50855095
pieces = []
50865096
if ex is not None:
50875097
pieces.extend(["EX", ex])
@@ -5107,6 +5117,8 @@ def hsetex(
51075117
if mapping:
51085118
pieces.append(len(mapping))
51095119
for key, value in mapping.items():
5120+
if key is None or value is None:
5121+
raise DataError("'hsetex' mapping contains None key or value")
51105122
pieces.append(key)
51115123
pieces.append(value)
51125124
if items:

0 commit comments

Comments
 (0)