Skip to content

Commit 7c5791d

Browse files
committed
Adding several missed input validations and unit test coverage for them
1 parent 83d9e93 commit 7c5791d

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

redis/commands/core.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4959,6 +4959,9 @@ def hgetdel(
49594959
Available since Redis 8.0
49604960
For more information see https://redis.io/commands/hgetdel
49614961
"""
4962+
if len(keys) == 0:
4963+
raise DataError("'hgetdel' should have at least one key provided")
4964+
49624965
return self.execute_command("HGETDEL", name, "FIELDS", len(keys), *keys)
49634966

49644967
def hgetex(
@@ -4990,6 +4993,10 @@ def hgetex(
49904993
Available since Redis 8.0
49914994
For more information see https://redis.io/commands/hgetex
49924995
"""
4996+
4997+
if len(keys) == 0:
4998+
raise DataError("'hgetex' should have at least one key provided")
4999+
49935000
opset = {ex, px, exat, pxat}
49945001
if len(opset) > 2 or len(opset) > 1 and persist:
49955002
raise DataError(
@@ -5060,8 +5067,10 @@ def hset(
50605067
50615068
For more information see https://redis.io/commands/hset
50625069
"""
5070+
50635071
if key is None and not mapping and not items:
50645072
raise DataError("'hset' with no key value pairs")
5073+
50655074
pieces = []
50665075
if items:
50675076
pieces.extend(items)
@@ -5123,6 +5132,12 @@ def hsetex(
51235132
if key is None and not mapping and not items:
51245133
raise DataError("'hsetex' with no key value pairs")
51255134

5135+
if items and len(items) % 2 != 0:
5136+
raise DataError(
5137+
"'hsetex' with odd number of items. "
5138+
"'items' must contain a list of key/value pairs."
5139+
)
5140+
51265141
opset = {ex, px, exat, pxat}
51275142
if len(opset) > 2 or len(opset) > 1 and keepttl:
51285143
raise DataError(

tests/test_asyncio/test_hash.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,9 @@ async def test_hgetdel(r):
316316
assert await r.hgetdel("test:hash", "foo", "1") == [None, None]
317317
assert await r.hget("test:hash", "2") == b"2"
318318

319+
with pytest.raises(exceptions.DataError):
320+
await r.hgetdel("test:hash")
321+
319322

320323
@skip_if_server_version_lt("7.9.0")
321324
async def test_hgetex_no_expiration(r):
@@ -396,6 +399,9 @@ async def test_hgetex_invalid_inputs(r):
396399
with pytest.raises(exceptions.DataError):
397400
await r.hgetex("b", "foo", "1", "3", ex=10, px=6000)
398401

402+
with pytest.raises(exceptions.DataError):
403+
await r.hgetex("b", ex=10)
404+
399405

400406
@skip_if_server_version_lt("7.9.0")
401407
async def test_hsetex_no_expiration(r):
@@ -545,6 +551,9 @@ async def test_hsetex_invalid_inputs(r):
545551
with pytest.raises(exceptions.DataError):
546552
await r.hsetex("b", None, None)
547553

554+
with pytest.raises(exceptions.DataError):
555+
await r.hsetex("b", "foo", "bar", items=["i1", 11, "i2"], px=6000)
556+
548557
with pytest.raises(exceptions.DataError):
549558
await r.hsetex("b", "foo", "bar", ex=10, keepttl=True)
550559

tests/test_hash.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,9 @@ def test_hgetdel(r):
383383
assert r.hgetdel("test:hash", "foo", "1") == [None, None]
384384
assert r.hget("test:hash", "2") == b"2"
385385

386+
with pytest.raises(exceptions.DataError):
387+
r.hgetdel("test:hash")
388+
386389

387390
@skip_if_server_version_lt("7.9.0")
388391
def test_hgetex_no_expiration(r):
@@ -445,6 +448,9 @@ def test_hgetex_invalid_inputs(r):
445448
with pytest.raises(exceptions.DataError):
446449
r.hgetex("b", "foo", "1", "3", ex=10, px=6000)
447450

451+
with pytest.raises(exceptions.DataError):
452+
r.hgetex("b", ex=10)
453+
448454

449455
@skip_if_server_version_lt("7.9.0")
450456
def test_hsetex_no_expiration(r):
@@ -454,7 +460,6 @@ def test_hsetex_no_expiration(r):
454460
assert r.hsetex("test:hash", None, None, mapping={"1": 1, "4": b"four"}) == 1
455461
assert r.httl("test:hash", "foo", "1", "4") == [-2, -1, -1]
456462
assert r.hgetex("test:hash", "foo", "1") == [None, b"1"]
457-
pass
458463

459464

460465
@skip_if_server_version_lt("7.9.0")
@@ -584,6 +589,9 @@ def test_hsetex_invalid_inputs(r):
584589
with pytest.raises(exceptions.DataError):
585590
r.hsetex("b", None, None)
586591

592+
with pytest.raises(exceptions.DataError):
593+
r.hsetex("b", "foo", "bar", items=["i1", 11, "i2"], px=6000)
594+
587595
with pytest.raises(exceptions.DataError):
588596
r.hsetex("b", "foo", "bar", ex=10, keepttl=True)
589597

0 commit comments

Comments
 (0)