Skip to content

Commit e89e2a3

Browse files
search unit tests
1 parent 5d201a0 commit e89e2a3

File tree

12 files changed

+818
-21
lines changed

12 files changed

+818
-21
lines changed

Diff for: .github/workflows/ci.yml

-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ jobs:
6161
fail-fast: false
6262
matrix:
6363
python-version: [
64-
"3.7",
6564
"3.8",
6665
"3.9",
6766
"3.10",

Diff for: elasticsearch_dsl/_async/search.py

+16-2
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,26 @@
2424
from ..utils import AttrDict
2525

2626

27-
class Search(SearchBase):
27+
class AsyncSearch(SearchBase):
2828
def __aiter__(self):
2929
"""
3030
Iterate over the hits.
3131
"""
32-
return aiter(await self.execute())
32+
33+
class ResultsIterator:
34+
def __init__(self, search):
35+
self.search = search
36+
self.iterator = None
37+
38+
async def __anext__(self):
39+
if self.iterator is None:
40+
self.iterator = iter(await self.search.execute())
41+
try:
42+
return next(self.iterator)
43+
except StopIteration:
44+
raise StopAsyncIteration()
45+
46+
return ResultsIterator(self)
3347

3448
async def count(self):
3549
"""

Diff for: elasticsearch_dsl/_sync/search.py

+15-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,21 @@ def __iter__(self):
2929
"""
3030
Iterate over the hits.
3131
"""
32-
return iter(self.execute())
32+
33+
class ResultsIterator:
34+
def __init__(self, search):
35+
self.search = search
36+
self.iterator = None
37+
38+
def __next__(self):
39+
if self.iterator is None:
40+
self.iterator = iter(self.search.execute())
41+
try:
42+
return next(self.iterator)
43+
except StopIteration:
44+
raise StopIteration()
45+
46+
return ResultsIterator(self)
3347

3448
def count(self):
3549
"""

Diff for: elasticsearch_dsl/search_base.py

-6
Original file line numberDiff line numberDiff line change
@@ -333,12 +333,6 @@ def filter(self, *args, **kwargs):
333333
def exclude(self, *args, **kwargs):
334334
return self.query(Bool(filter=[~Q(*args, **kwargs)]))
335335

336-
def __iter__(self):
337-
"""
338-
Iterate over the hits.
339-
"""
340-
return iter(self.execute())
341-
342336
def __getitem__(self, n):
343337
"""
344338
Support slicing the `Search` instance for pagination.

Diff for: setup.cfg

+1
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ filterwarnings =
1111
error
1212
ignore:Legacy index templates are deprecated in favor of composable templates.:elasticsearch.exceptions.ElasticsearchWarning
1313
ignore:datetime.datetime.utcfromtimestamp\(\) is deprecated and scheduled for removal in a future version..*:DeprecationWarning
14+
asyncio_mode = auto

Diff for: setup.py

+2
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,10 @@
3737
"pytest",
3838
"pytest-cov",
3939
"pytest-mock",
40+
"pytest-asyncio",
4041
"pytz",
4142
"coverage",
43+
"mock", # needed to have AsyncMock in Python <= 3.7
4244
# Override Read the Docs default (sphinx<2 and sphinx-rtd-theme<0.5)
4345
"sphinx>2",
4446
"sphinx-rtd-theme>0.5",

Diff for: tests/_async/__init__.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Licensed to Elasticsearch B.V. under one or more contributor
2+
# license agreements. See the NOTICE file distributed with
3+
# this work for additional information regarding copyright
4+
# ownership. Elasticsearch B.V. licenses this file to you under
5+
# the Apache License, Version 2.0 (the "License"); you may
6+
# not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.

0 commit comments

Comments
 (0)