Skip to content

Commit

Permalink
update json().arrindex() default values (redis#2611)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
2 people authored and aksinha334 committed Mar 15, 2023
1 parent 39bfa18 commit 1a693b2
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
14 changes: 9 additions & 5 deletions redis/commands/json/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -43,9 +43,13 @@ def arrindex(
For more information see `JSON.ARRINDEX <https://redis.io/commands/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]
Expand Down
12 changes: 9 additions & 3 deletions tests/test_asyncio/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions tests/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1a693b2

Please sign in to comment.