From d5d3271321a5ece2f026252727a9fc0771fff2a9 Mon Sep 17 00:00:00 2001 From: Shachar Pashchur Date: Sun, 18 Dec 2022 12:24:11 +0200 Subject: [PATCH 1/9] add timeout to query class --- redis/commands/search/query.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/redis/commands/search/query.py b/redis/commands/search/query.py index e51918f500..74e3f315aa 100644 --- a/redis/commands/search/query.py +++ b/redis/commands/search/query.py @@ -28,6 +28,7 @@ def __init__(self, query_string): self._filters = list() self._ids = None self._slop = -1 + self._timeout = -1 self._in_order = False self._sortby = None self._return_fields = [] @@ -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 @@ -188,6 +194,8 @@ def _get_args_tags(self): args += self._ids if self._slop >= 0: args += ["SLOP", self._slop] + if self._timeout >= 0: + args += ["TIMEOUT", self._timeout] if self._in_order: args.append("INORDER") if self._return_fields: From 023b5333a8a36e76a429c6c131ff90ebf8cf9f4a Mon Sep 17 00:00:00 2001 From: Shachar Pashchur Date: Sun, 18 Dec 2022 16:40:04 +0200 Subject: [PATCH 2/9] Add test_timeout --- tests/test_search.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/test_search.py b/tests/test_search.py index 08c76f7e08..5e23693d55 100644 --- a/tests/test_search.py +++ b/tests/test_search.py @@ -1615,3 +1615,8 @@ 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(modclient: redis.Redis): + q1 = Query("foo").timeout(5000) + assert q1.get_args() == ['foo', 'TIMEOUT', 5000, 'LIMIT', 0, 10] \ No newline at end of file From a8f444313b4bebfd02e0a297b1def33af3024008 Mon Sep 17 00:00:00 2001 From: Shachar Pashchur Date: Sun, 18 Dec 2022 16:43:10 +0200 Subject: [PATCH 3/9] fix lines --- tests/test_search.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test_search.py b/tests/test_search.py index 5e23693d55..decfde3088 100644 --- a/tests/test_search.py +++ b/tests/test_search.py @@ -1616,7 +1616,8 @@ def test_withsuffixtrie(modclient: redis.Redis): info = modclient.ft().info() assert "WITHSUFFIXTRIE" in info["attributes"][0] + @pytest.mark.redismod def test_timeout(modclient: redis.Redis): q1 = Query("foo").timeout(5000) - assert q1.get_args() == ['foo', 'TIMEOUT', 5000, 'LIMIT', 0, 10] \ No newline at end of file + assert q1.get_args() == ['foo', 'TIMEOUT', 5000, 'LIMIT', 0, 10] From 1e276319bfa691f70ce4c27707696228ca9b32f8 Mon Sep 17 00:00:00 2001 From: Shachar Pashchur Date: Sun, 18 Dec 2022 17:16:16 +0200 Subject: [PATCH 4/9] fix format --- tests/test_search.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_search.py b/tests/test_search.py index decfde3088..206d41adcc 100644 --- a/tests/test_search.py +++ b/tests/test_search.py @@ -1620,4 +1620,4 @@ def test_withsuffixtrie(modclient: redis.Redis): @pytest.mark.redismod def test_timeout(modclient: redis.Redis): q1 = Query("foo").timeout(5000) - assert q1.get_args() == ['foo', 'TIMEOUT', 5000, 'LIMIT', 0, 10] + assert q1.get_args() == ["foo", "TIMEOUT", 5000, "LIMIT", 0, 10] From 3dc11af34209e29aa675433b52bda935977a73d5 Mon Sep 17 00:00:00 2001 From: Shachar Pashchur Date: Mon, 19 Dec 2022 14:08:34 +0200 Subject: [PATCH 5/9] add test & fixes --- redis/commands/search/query.py | 4 ++-- tests/test_asyncio/test_search.py | 13 +++++++++++++ tests/test_search.py | 9 ++++++++- 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/redis/commands/search/query.py b/redis/commands/search/query.py index 74e3f315aa..5071cfabf2 100644 --- a/redis/commands/search/query.py +++ b/redis/commands/search/query.py @@ -28,7 +28,7 @@ def __init__(self, query_string): self._filters = list() self._ids = None self._slop = -1 - self._timeout = -1 + self._timeout = None self._in_order = False self._sortby = None self._return_fields = [] @@ -194,7 +194,7 @@ def _get_args_tags(self): args += self._ids if self._slop >= 0: args += ["SLOP", self._slop] - if self._timeout >= 0: + if self._timeout: args += ["TIMEOUT", self._timeout] if self._in_order: args.append("INORDER") diff --git a/tests/test_asyncio/test_search.py b/tests/test_asyncio/test_search.py index 88c80d301b..ec1dbb59cf 100644 --- a/tests/test_asyncio/test_search.py +++ b/tests/test_asyncio/test_search.py @@ -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(): + 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) diff --git a/tests/test_search.py b/tests/test_search.py index 206d41adcc..2c08390b42 100644 --- a/tests/test_search.py +++ b/tests/test_search.py @@ -1618,6 +1618,13 @@ def test_withsuffixtrie(modclient: redis.Redis): @pytest.mark.redismod -def test_timeout(modclient: redis.Redis): +def test_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): + modclient.ft().search(q1) From d0c963594ebe12117fcdf90c51f02ed4ca52004f Mon Sep 17 00:00:00 2001 From: Shachar Pashchur Date: Mon, 19 Dec 2022 14:48:09 +0200 Subject: [PATCH 6/9] merge tests --- tests/test_asyncio/test_search.py | 10 +++------- tests/test_search.py | 10 +++------- 2 files changed, 6 insertions(+), 14 deletions(-) diff --git a/tests/test_asyncio/test_search.py b/tests/test_asyncio/test_search.py index ec1dbb59cf..21f2110064 100644 --- a/tests/test_asyncio/test_search.py +++ b/tests/test_asyncio/test_search.py @@ -1004,13 +1004,9 @@ async def test_search_commands_in_pipeline(modclient: redis.Redis): @pytest.mark.redismod -def test_timeout(): +def test_query_timeout(modclient: redis.Redis): 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): + q2 = Query("foo").timeout("500") + with pytest.raises(redis.exceptions.ResponseError): modclient.ft().search(q1) diff --git a/tests/test_search.py b/tests/test_search.py index 2c08390b42..488503e396 100644 --- a/tests/test_search.py +++ b/tests/test_search.py @@ -1618,13 +1618,9 @@ def test_withsuffixtrie(modclient: redis.Redis): @pytest.mark.redismod -def test_timeout(): +def test_query_timeout(modclient: redis.Redis): 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): + q2 = Query("foo").timeout("500") + with pytest.raises(redis.exceptions.ResponseError): modclient.ft().search(q1) From 5f0086920e9c455ba7d3720b8eb81b8e7bd8202b Mon Sep 17 00:00:00 2001 From: Shachar Pashchur Date: Mon, 19 Dec 2022 14:49:48 +0200 Subject: [PATCH 7/9] change timeout to not_a_number --- tests/test_asyncio/test_search.py | 2 +- tests/test_search.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_asyncio/test_search.py b/tests/test_asyncio/test_search.py index 21f2110064..1f6256a462 100644 --- a/tests/test_asyncio/test_search.py +++ b/tests/test_asyncio/test_search.py @@ -1007,6 +1007,6 @@ async def test_search_commands_in_pipeline(modclient: redis.Redis): def test_query_timeout(modclient: redis.Redis): q1 = Query("foo").timeout(5000) assert q1.get_args() == ["foo", "TIMEOUT", 5000, "LIMIT", 0, 10] - q2 = Query("foo").timeout("500") + q2 = Query("foo").timeout("not_a_number") with pytest.raises(redis.exceptions.ResponseError): modclient.ft().search(q1) diff --git a/tests/test_search.py b/tests/test_search.py index 488503e396..f268679e29 100644 --- a/tests/test_search.py +++ b/tests/test_search.py @@ -1621,6 +1621,6 @@ def test_withsuffixtrie(modclient: redis.Redis): def test_query_timeout(modclient: redis.Redis): q1 = Query("foo").timeout(5000) assert q1.get_args() == ["foo", "TIMEOUT", 5000, "LIMIT", 0, 10] - q2 = Query("foo").timeout("500") + q2 = Query("foo").timeout("not_a_number") with pytest.raises(redis.exceptions.ResponseError): modclient.ft().search(q1) From 8b7181f257e68b1ebf849c0400e14eb89c6515ef Mon Sep 17 00:00:00 2001 From: Shachar Pashchur Date: Mon, 19 Dec 2022 14:52:07 +0200 Subject: [PATCH 8/9] change q1 to q2 --- tests/test_asyncio/test_search.py | 2 +- tests/test_search.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_asyncio/test_search.py b/tests/test_asyncio/test_search.py index 1f6256a462..224b690582 100644 --- a/tests/test_asyncio/test_search.py +++ b/tests/test_asyncio/test_search.py @@ -1009,4 +1009,4 @@ def test_query_timeout(modclient: redis.Redis): assert q1.get_args() == ["foo", "TIMEOUT", 5000, "LIMIT", 0, 10] q2 = Query("foo").timeout("not_a_number") with pytest.raises(redis.exceptions.ResponseError): - modclient.ft().search(q1) + modclient.ft().search(q2) diff --git a/tests/test_search.py b/tests/test_search.py index f268679e29..82fb2f2dae 100644 --- a/tests/test_search.py +++ b/tests/test_search.py @@ -1623,4 +1623,4 @@ def test_query_timeout(modclient: redis.Redis): assert q1.get_args() == ["foo", "TIMEOUT", 5000, "LIMIT", 0, 10] q2 = Query("foo").timeout("not_a_number") with pytest.raises(redis.exceptions.ResponseError): - modclient.ft().search(q1) + modclient.ft().search(q2) From 55b19552b43eadd280c48ab79926923c951e7ac8 Mon Sep 17 00:00:00 2001 From: Shachar Pashchur Date: Tue, 20 Dec 2022 10:54:04 +0200 Subject: [PATCH 9/9] Fix async method --- tests/test_asyncio/test_search.py | 6 +++--- tests/test_search.py | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/test_asyncio/test_search.py b/tests/test_asyncio/test_search.py index 224b690582..12313b65e3 100644 --- a/tests/test_asyncio/test_search.py +++ b/tests/test_asyncio/test_search.py @@ -1004,9 +1004,9 @@ async def test_search_commands_in_pipeline(modclient: redis.Redis): @pytest.mark.redismod -def test_query_timeout(modclient: redis.Redis): +async def test_query_timeout(modclient: redis.Redis): q1 = Query("foo").timeout(5000) assert q1.get_args() == ["foo", "TIMEOUT", 5000, "LIMIT", 0, 10] q2 = Query("foo").timeout("not_a_number") - with pytest.raises(redis.exceptions.ResponseError): - modclient.ft().search(q2) + with pytest.raises(redis.ResponseError): + await modclient.ft().search(q2) diff --git a/tests/test_search.py b/tests/test_search.py index 82fb2f2dae..12876f6262 100644 --- a/tests/test_search.py +++ b/tests/test_search.py @@ -1622,5 +1622,5 @@ def test_query_timeout(modclient: redis.Redis): q1 = Query("foo").timeout(5000) assert q1.get_args() == ["foo", "TIMEOUT", 5000, "LIMIT", 0, 10] q2 = Query("foo").timeout("not_a_number") - with pytest.raises(redis.exceptions.ResponseError): + with pytest.raises(redis.ResponseError): modclient.ft().search(q2)