Skip to content
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

Add TIMEOUT to query class #2519

Merged
merged 9 commits into from
Dec 21, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions redis/commands/search/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def __init__(self, query_string):
self._filters = list()
self._ids = None
self._slop = -1
self._timeout = None
self._in_order = False
self._sortby = None
self._return_fields = []
Expand Down Expand Up @@ -131,6 +132,11 @@ def slop(self, slop):
self._slop = slop
return self

def timeout(self, timeout):
"""overrides the timeout parameter of the module"""
self._timeout = timeout
return self

def in_order(self):
"""
Match only documents where the query terms appear in
Expand Down Expand Up @@ -188,6 +194,8 @@ def _get_args_tags(self):
args += self._ids
if self._slop >= 0:
args += ["SLOP", self._slop]
if self._timeout:
args += ["TIMEOUT", self._timeout]
if self._in_order:
args.append("INORDER")
if self._return_fields:
Expand Down
13 changes: 13 additions & 0 deletions tests/test_asyncio/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -1001,3 +1001,16 @@ async def test_search_commands_in_pipeline(modclient: redis.Redis):
assert "doc2" == res[3][4]
assert res[3][5] is None
assert res[3][3] == res[3][6] == ["txt", "foo bar"]


@pytest.mark.redismod
def test_timeout():
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shacharPash Can you combine the 2 tests into one and rename it to test_query_timeout?

q1 = Query("foo").timeout(5000)
assert q1.get_args() == ["foo", "TIMEOUT", 5000, "LIMIT", 0, 10]


@pytest.mark.redismod
def test_not_number_timeout(modclient: redis.Redis):
q1 = Query("foo").timeout("not a number")
with pytest.raises(Exception):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to check for a more specific error

modclient.ft().search(q1)
13 changes: 13 additions & 0 deletions tests/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -1615,3 +1615,16 @@ def test_withsuffixtrie(modclient: redis.Redis):
waitForIndex(modclient, getattr(modclient.ft(), "index_name", "idx"))
info = modclient.ft().info()
assert "WITHSUFFIXTRIE" in info["attributes"][0]


@pytest.mark.redismod
def test_timeout():
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as the async tests...

q1 = Query("foo").timeout(5000)
assert q1.get_args() == ["foo", "TIMEOUT", 5000, "LIMIT", 0, 10]


@pytest.mark.redismod
def test_not_number_timeout(modclient: redis.Redis):
q1 = Query("foo").timeout("not a number")
with pytest.raises(Exception):
modclient.ft().search(q1)