From bcc0fc276c9c1b73c91a849b914557f5b1aa2de4 Mon Sep 17 00:00:00 2001 From: Alex Schmitz Date: Mon, 13 Mar 2023 17:03:07 -0500 Subject: [PATCH 1/6] update json().arrindex() default values --- redis/commands/json/commands.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/redis/commands/json/commands.py b/redis/commands/json/commands.py index 7fd4039203..fb4d68e14a 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: + pieces.append(start) + if stop: + pieces.append(stop) + + return self.execute_command("JSON.ARRINDEX", *pieces) def arrinsert( self, name: str, path: str, index: int, *args: List[JsonType] From 7ca1d378d6f5691902ade1260b88ae3ae5ffbf5b Mon Sep 17 00:00:00 2001 From: Alex Schmitz Date: Mon, 13 Mar 2023 17:23:12 -0500 Subject: [PATCH 2/6] add unit test --- tests/test_json.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/test_json.py b/tests/test_json.py index a776e9e736..780faffe85 100644 --- a/tests/test_json.py +++ b/tests/test_json.py @@ -166,6 +166,8 @@ 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 -1 == client.json().arrindex("arr", Path.root_path(), 4, start=0, stop=-1) @pytest.mark.redismod From 5ff8fcf41cfee517a61881540e7d9a5007ca775d Mon Sep 17 00:00:00 2001 From: Alex Schmitz Date: Tue, 14 Mar 2023 09:44:34 -0500 Subject: [PATCH 3/6] fix falsy checks --- redis/commands/json/commands.py | 4 ++-- tests/test_json.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/redis/commands/json/commands.py b/redis/commands/json/commands.py index fb4d68e14a..c02c47ad86 100644 --- a/redis/commands/json/commands.py +++ b/redis/commands/json/commands.py @@ -44,9 +44,9 @@ def arrindex( For more information see `JSON.ARRINDEX `_. """ # noqa pieces = [name, str(path), self._encode(scalar)] - if start: + if start is not None: pieces.append(start) - if stop: + if stop is not None: pieces.append(stop) return self.execute_command("JSON.ARRINDEX", *pieces) diff --git a/tests/test_json.py b/tests/test_json.py index 780faffe85..0f82868b2b 100644 --- a/tests/test_json.py +++ b/tests/test_json.py @@ -167,7 +167,7 @@ def test_arrindex(client): 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 -1 == client.json().arrindex("arr", Path.root_path(), 4, start=0, stop=-1) + assert -1 == client.json().arrindex("arr", Path.root_path(), 4, start=0, stop=-1) @pytest.mark.redismod From fd615c6355c86d608fc38ee857c714e0c6b7f6b1 Mon Sep 17 00:00:00 2001 From: Alex Schmitz Date: Tue, 14 Mar 2023 09:51:10 -0500 Subject: [PATCH 4/6] more unit tests --- tests/test_json.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/test_json.py b/tests/test_json.py index 0f82868b2b..8e8da05609 100644 --- a/tests/test_json.py +++ b/tests/test_json.py @@ -167,7 +167,10 @@ def test_arrindex(client): 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 From d76bbed2d3e17a94a75d9ec74b79f5a371139fe8 Mon Sep 17 00:00:00 2001 From: Alex Schmitz Date: Tue, 14 Mar 2023 10:10:16 -0500 Subject: [PATCH 5/6] add asyncio tests --- tests/test_asyncio/test_json.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/test_asyncio/test_json.py b/tests/test_asyncio/test_json.py index b8854d20cd..236358e952 100644 --- a/tests/test_asyncio/test_json.py +++ b/tests/test_asyncio/test_json.py @@ -148,6 +148,11 @@ 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) + assert 4 == await modclient.json().arrindex("arr", Path.root_path(), 4) + assert 4 == await modclient.json().arrindex("arr", Path.root_path(), 4, start=0) + assert 4 == await modclient.json().arrindex("arr", Path.root_path(), 4, start=0, stop=5000) + assert -1 == await modclient.json().arrindex("arr", Path.root_path(), 4, start=0, stop=-1) + assert -1 == await modclient.json().arrindex("arr", Path.root_path(), 4, start=1, stop=3) @pytest.mark.redismod From 69ea3da2e9d58750a5621e5a75416ee9d2ab00da Mon Sep 17 00:00:00 2001 From: Alex Schmitz Date: Tue, 14 Mar 2023 10:16:46 -0500 Subject: [PATCH 6/6] fix lint line length --- tests/test_asyncio/test_json.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/tests/test_asyncio/test_json.py b/tests/test_asyncio/test_json.py index 236358e952..fc530c63c1 100644 --- a/tests/test_asyncio/test_json.py +++ b/tests/test_asyncio/test_json.py @@ -145,14 +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) - assert 4 == await modclient.json().arrindex("arr", Path.root_path(), 4) - assert 4 == await modclient.json().arrindex("arr", Path.root_path(), 4, start=0) - assert 4 == await modclient.json().arrindex("arr", Path.root_path(), 4, start=0, stop=5000) - assert -1 == await modclient.json().arrindex("arr", Path.root_path(), 4, start=0, stop=-1) - assert -1 == await modclient.json().arrindex("arr", Path.root_path(), 4, start=1, stop=3) + 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