Skip to content

Commit

Permalink
search unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg committed Mar 13, 2024
1 parent 5d201a0 commit e89e2a3
Show file tree
Hide file tree
Showing 12 changed files with 818 additions and 21 deletions.
1 change: 0 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ jobs:
fail-fast: false
matrix:
python-version: [
"3.7",
"3.8",
"3.9",
"3.10",
Expand Down
18 changes: 16 additions & 2 deletions elasticsearch_dsl/_async/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,26 @@
from ..utils import AttrDict


class Search(SearchBase):
class AsyncSearch(SearchBase):
def __aiter__(self):
"""
Iterate over the hits.
"""
return aiter(await self.execute())

class ResultsIterator:
def __init__(self, search):
self.search = search
self.iterator = None

async def __anext__(self):
if self.iterator is None:
self.iterator = iter(await self.search.execute())
try:
return next(self.iterator)
except StopIteration:
raise StopAsyncIteration()

return ResultsIterator(self)

async def count(self):
"""
Expand Down
16 changes: 15 additions & 1 deletion elasticsearch_dsl/_sync/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,21 @@ def __iter__(self):
"""
Iterate over the hits.
"""
return iter(self.execute())

class ResultsIterator:
def __init__(self, search):
self.search = search
self.iterator = None

def __next__(self):
if self.iterator is None:
self.iterator = iter(self.search.execute())
try:
return next(self.iterator)
except StopIteration:
raise StopIteration()

return ResultsIterator(self)

def count(self):
"""
Expand Down
6 changes: 0 additions & 6 deletions elasticsearch_dsl/search_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,12 +333,6 @@ def filter(self, *args, **kwargs):
def exclude(self, *args, **kwargs):
return self.query(Bool(filter=[~Q(*args, **kwargs)]))

def __iter__(self):
"""
Iterate over the hits.
"""
return iter(self.execute())

def __getitem__(self, n):
"""
Support slicing the `Search` instance for pagination.
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ filterwarnings =
error
ignore:Legacy index templates are deprecated in favor of composable templates.:elasticsearch.exceptions.ElasticsearchWarning
ignore:datetime.datetime.utcfromtimestamp\(\) is deprecated and scheduled for removal in a future version..*:DeprecationWarning
asyncio_mode = auto
2 changes: 2 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@
"pytest",
"pytest-cov",
"pytest-mock",
"pytest-asyncio",
"pytz",
"coverage",
"mock", # needed to have AsyncMock in Python <= 3.7
# Override Read the Docs default (sphinx<2 and sphinx-rtd-theme<0.5)
"sphinx>2",
"sphinx-rtd-theme>0.5",
Expand Down
16 changes: 16 additions & 0 deletions tests/_async/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Licensed to Elasticsearch B.V. under one or more contributor
# license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright
# ownership. Elasticsearch B.V. licenses this file to you under
# the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
Loading

0 comments on commit e89e2a3

Please sign in to comment.