Skip to content

Commit

Permalink
Support JSON.MSET Command (#2766)
Browse files Browse the repository at this point in the history
* support JSON.MERGE Command

* linters

* try with abc instead person

* change @skip_ifmodversion_lt to latest ReJSON 2.4.7

* change version

* fix test

* linters

* add async test

* Support JSON.MSET command

* trying to run CI

* linters

* add async test

* reminder do delete the integration changes

* delete the line from integration

* fix the interface

* change docstring

---------

Co-authored-by: Chayim <chayim@users.noreply.github.com>
Co-authored-by: dvora-h <dvora.heller@redis.com>
  • Loading branch information
3 people committed Jun 25, 2023
1 parent 04aadd7 commit ab617a1
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 1 deletion.
1 change: 1 addition & 0 deletions redis/commands/json/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def __init__(
"JSON.GET": self._decode,
"JSON.MGET": bulk_of_jsons(self._decode),
"JSON.SET": lambda r: r and nativestr(r) == "OK",
"JSON.MSET": lambda r: r and nativestr(r) == "OK",
"JSON.MERGE": lambda r: r and nativestr(r) == "OK",
"JSON.NUMINCRBY": self._decode,
"JSON.NUMMULTBY": self._decode,
Expand Down
19 changes: 18 additions & 1 deletion redis/commands/json/commands.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os
from json import JSONDecodeError, loads
from typing import Dict, List, Optional, Union
from typing import Dict, List, Optional, Tuple, Union

from redis.exceptions import DataError
from redis.utils import deprecated_function
Expand Down Expand Up @@ -253,6 +253,23 @@ def set(
pieces.append("XX")
return self.execute_command("JSON.SET", *pieces)

def mset(self, triplets: List[Tuple[str, str, JsonType]]) -> Optional[str]:
"""
Set the JSON value at key ``name`` under the ``path`` to ``obj``
for one or more keys.
``triplets`` is a list of one or more triplets of key, path, value.
For the purpose of using this within a pipeline, this command is also
aliased to JSON.MSET.
For more information see `JSON.MSET <https://redis.io/commands/json.mset>`_.
"""
pieces = []
for triplet in triplets:
pieces.extend([triplet[0], str(triplet[1]), self._encode(triplet[2])])
return self.execute_command("JSON.MSET", *pieces)

def merge(
self,
name: str,
Expand Down
11 changes: 11 additions & 0 deletions tests/test_asyncio/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,17 @@ async def test_mgetshouldsucceed(modclient: redis.Redis):
assert await modclient.json().mget([1, 2], Path.root_path()) == [1, 2]


@pytest.mark.redismod
@skip_ifmodversion_lt("2.6.0", "ReJSON") # todo: update after the release
async def test_mset(modclient: redis.Redis):
await modclient.json().mset(
[("1", Path.root_path(), 1), ("2", Path.root_path(), 2)]
)

assert await modclient.json().mget(["1"], Path.root_path()) == [1]
assert await modclient.json().mget(["1", "2"], Path.root_path()) == [1, 2]


@pytest.mark.redismod
@skip_ifmodversion_lt("99.99.99", "ReJSON") # todo: update after the release
async def test_clear(modclient: redis.Redis):
Expand Down
9 changes: 9 additions & 0 deletions tests/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,15 @@ def test_mgetshouldsucceed(client):
assert client.json().mget([1, 2], Path.root_path()) == [1, 2]


@pytest.mark.redismod
@skip_ifmodversion_lt("2.6.0", "ReJSON") # todo: update after the release
def test_mset(client):
client.json().mset([("1", Path.root_path(), 1), ("2", Path.root_path(), 2)])

assert client.json().mget(["1"], Path.root_path()) == [1]
assert client.json().mget(["1", "2"], Path.root_path()) == [1, 2]


@pytest.mark.redismod
@skip_ifmodversion_lt("99.99.99", "ReJSON") # todo: update after the release
def test_clear(client):
Expand Down

0 comments on commit ab617a1

Please sign in to comment.