Skip to content

Add support for HSET items #2006

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@

* Add `items` parameter to `hset` signature
* Create codeql-analysis.yml (#1988). Thanks @chayim
* Add limited support for Lua scripting with RedisCluster
* Implement `.lock()` method on RedisCluster

* 4.1.3 (Feb 8, 2022)
* Fix flushdb and flushall (#1926)
* Add redis5 and redis4 dockers (#1871)
Expand Down
7 changes: 5 additions & 2 deletions redis/commands/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -4589,18 +4589,21 @@ def hset(
key: Optional[str] = None,
value: Optional[str] = None,
mapping: Optional[dict] = None,
items: Optional[list] = None,
) -> Union[Awaitable[int], int]:
"""
Set ``key`` to ``value`` within hash ``name``,
``mapping`` accepts a dict of key/value pairs that will be
added to hash ``name``.
``items`` accepts a list of key/value pairs that will be
added to hash ``name``.
Returns the number of fields that were added.

For more information check https://redis.io/commands/hset
"""
if key is None and not mapping:
if key is None and not mapping and not items:
raise DataError("'hset' with no key value pairs")
items = []
items = items or []
if key is not None:
items.extend((key, value))
if mapping:
Expand Down
11 changes: 11 additions & 0 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -2546,6 +2546,17 @@ def test_hset_with_multi_key_values(self, r):
assert r.hget("b", "2") == b"2"
assert r.hget("b", "foo") == b"bar"

def test_hset_with_key_values_passed_as_list(self, r):
r.hset("a", items=["1", 1, "2", 2, "3", 3])
assert r.hget("a", "1") == b"1"
assert r.hget("a", "2") == b"2"
assert r.hget("a", "3") == b"3"

r.hset("b", "foo", "bar", items=["1", 1, "2", 2])
assert r.hget("b", "1") == b"1"
assert r.hget("b", "2") == b"2"
assert r.hget("b", "foo") == b"bar"

def test_hset_without_data(self, r):
with pytest.raises(exceptions.DataError):
r.hset("x")
Expand Down
2 changes: 1 addition & 1 deletion tests/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -1497,7 +1497,7 @@ def test_profile(client):
res, det = client.ft().profile(req)
assert det["Iterators profile"]["Counter"] == 2.0
assert det["Iterators profile"]["Type"] == "WILDCARD"
assert det["Parsing time"] < 0.5
assert isinstance(det["Parsing time"], float)
assert len(res.rows) == 2 # check also the search result


Expand Down