From 1a693b2056181e898749844e05d7a45ecc84d083 Mon Sep 17 00:00:00 2001 From: Alex Schmitz Date: Wed, 15 Mar 2023 04:33:38 -0500 Subject: [PATCH] update json().arrindex() default values (#2611) * update json().arrindex() default values * add unit test * fix falsy checks * more unit tests * add asyncio tests * fix lint line length --------- Co-authored-by: Alex Schmitz --- redis/commands/json/commands.py | 14 +++++++++----- tests/test_asyncio/test_json.py | 12 +++++++++--- tests/test_json.py | 5 +++++ 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/redis/commands/json/commands.py b/redis/commands/json/commands.py index 7fd4039203..c02c47ad86 100644 --- a/redis/commands/json/commands.py +++ b/redis/commands/json/commands.py @@ -31,8 +31,8 @@ def arrindex( name: str, path: str, scalar: int, - start: Optional[int] = 0, - stop: Optional[int] = -1, + start: Optional[int] = None, + stop: Optional[int] = None, ) -> List[Union[int, None]]: """ Return the index of ``scalar`` in the JSON array under ``path`` at key @@ -43,9 +43,13 @@ def arrindex( For more information see `JSON.ARRINDEX `_. """ # noqa - return self.execute_command( - "JSON.ARRINDEX", name, str(path), self._encode(scalar), start, stop - ) + pieces = [name, str(path), self._encode(scalar)] + if start is not None: + pieces.append(start) + if stop is not None: + pieces.append(stop) + + return self.execute_command("JSON.ARRINDEX", *pieces) def arrinsert( self, name: str, path: str, index: int, *args: List[JsonType] diff --git a/tests/test_asyncio/test_json.py b/tests/test_asyncio/test_json.py index b8854d20cd..fc530c63c1 100644 --- a/tests/test_asyncio/test_json.py +++ b/tests/test_asyncio/test_json.py @@ -145,9 +145,15 @@ async def test_arrappend(modclient: redis.Redis): @pytest.mark.redismod async def test_arrindex(modclient: redis.Redis): - await modclient.json().set("arr", Path.root_path(), [0, 1, 2, 3, 4]) - assert 1 == await modclient.json().arrindex("arr", Path.root_path(), 1) - assert -1 == await modclient.json().arrindex("arr", Path.root_path(), 1, 2) + r_path = Path.root_path() + await modclient.json().set("arr", r_path, [0, 1, 2, 3, 4]) + assert 1 == await modclient.json().arrindex("arr", r_path, 1) + assert -1 == await modclient.json().arrindex("arr", r_path, 1, 2) + assert 4 == await modclient.json().arrindex("arr", r_path, 4) + assert 4 == await modclient.json().arrindex("arr", r_path, 4, start=0) + assert 4 == await modclient.json().arrindex("arr", r_path, 4, start=0, stop=5000) + assert -1 == await modclient.json().arrindex("arr", r_path, 4, start=0, stop=-1) + assert -1 == await modclient.json().arrindex("arr", r_path, 4, start=1, stop=3) @pytest.mark.redismod diff --git a/tests/test_json.py b/tests/test_json.py index a776e9e736..8e8da05609 100644 --- a/tests/test_json.py +++ b/tests/test_json.py @@ -166,6 +166,11 @@ def test_arrindex(client): client.json().set("arr", Path.root_path(), [0, 1, 2, 3, 4]) assert 1 == client.json().arrindex("arr", Path.root_path(), 1) assert -1 == client.json().arrindex("arr", Path.root_path(), 1, 2) + assert 4 == client.json().arrindex("arr", Path.root_path(), 4) + assert 4 == client.json().arrindex("arr", Path.root_path(), 4, start=0) + assert 4 == client.json().arrindex("arr", Path.root_path(), 4, start=0, stop=5000) + assert -1 == client.json().arrindex("arr", Path.root_path(), 4, start=0, stop=-1) + assert -1 == client.json().arrindex("arr", Path.root_path(), 4, start=1, stop=3) @pytest.mark.redismod