Skip to content

Commit 6c798df

Browse files
mdczaplickichayimdvora-h
authored
Add support for HSET items (#2006)
* Add `items` parameter to `hset` * Add test for `hset` with `items` * Update CHANGES * fix test_profile Co-authored-by: Chayim <[email protected]> Co-authored-by: dvora-h <[email protected]>
1 parent 98fd06e commit 6c798df

File tree

4 files changed

+20
-3
lines changed

4 files changed

+20
-3
lines changed

CHANGES

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
2+
* Add `items` parameter to `hset` signature
13
* Create codeql-analysis.yml (#1988). Thanks @chayim
24
* Add limited support for Lua scripting with RedisCluster
35
* Implement `.lock()` method on RedisCluster
6+
47
* 4.1.3 (Feb 8, 2022)
58
* Fix flushdb and flushall (#1926)
69
* Add redis5 and redis4 dockers (#1871)

redis/commands/core.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4589,18 +4589,21 @@ def hset(
45894589
key: Optional[str] = None,
45904590
value: Optional[str] = None,
45914591
mapping: Optional[dict] = None,
4592+
items: Optional[list] = None,
45924593
) -> Union[Awaitable[int], int]:
45934594
"""
45944595
Set ``key`` to ``value`` within hash ``name``,
45954596
``mapping`` accepts a dict of key/value pairs that will be
45964597
added to hash ``name``.
4598+
``items`` accepts a list of key/value pairs that will be
4599+
added to hash ``name``.
45974600
Returns the number of fields that were added.
45984601
45994602
For more information check https://redis.io/commands/hset
46004603
"""
4601-
if key is None and not mapping:
4604+
if key is None and not mapping and not items:
46024605
raise DataError("'hset' with no key value pairs")
4603-
items = []
4606+
items = items or []
46044607
if key is not None:
46054608
items.extend((key, value))
46064609
if mapping:

tests/test_commands.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2546,6 +2546,17 @@ def test_hset_with_multi_key_values(self, r):
25462546
assert r.hget("b", "2") == b"2"
25472547
assert r.hget("b", "foo") == b"bar"
25482548

2549+
def test_hset_with_key_values_passed_as_list(self, r):
2550+
r.hset("a", items=["1", 1, "2", 2, "3", 3])
2551+
assert r.hget("a", "1") == b"1"
2552+
assert r.hget("a", "2") == b"2"
2553+
assert r.hget("a", "3") == b"3"
2554+
2555+
r.hset("b", "foo", "bar", items=["1", 1, "2", 2])
2556+
assert r.hget("b", "1") == b"1"
2557+
assert r.hget("b", "2") == b"2"
2558+
assert r.hget("b", "foo") == b"bar"
2559+
25492560
def test_hset_without_data(self, r):
25502561
with pytest.raises(exceptions.DataError):
25512562
r.hset("x")

tests/test_search.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1500,7 +1500,7 @@ def test_profile(client):
15001500
res, det = client.ft().profile(req)
15011501
assert det["Iterators profile"]["Counter"] == 2.0
15021502
assert det["Iterators profile"]["Type"] == "WILDCARD"
1503-
assert det["Parsing time"] < 0.5
1503+
assert isinstance(det["Parsing time"], float)
15041504
assert len(res.rows) == 2 # check also the search result
15051505

15061506

0 commit comments

Comments
 (0)