Skip to content

Commit

Permalink
Switch to pytest-asyncio's strict mode for async tests (#1782) (#1811)
Browse files Browse the repository at this point in the history
* Switch to pytest-asyncio's strict mode for async tests

* add one missing test

* use 'sync' as sync I/O marker

(cherry picked from commit 81181ca)
  • Loading branch information
miguelgrinberg committed Apr 29, 2024
1 parent ed58aee commit 9ba89eb
Show file tree
Hide file tree
Showing 36 changed files with 234 additions and 2 deletions.
3 changes: 2 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ 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
markers =
sync: mark a test as performing I/O without asyncio.
4 changes: 4 additions & 0 deletions tests/_async/test_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from datetime import datetime
from hashlib import md5

import pytest
from pytest import raises

from elasticsearch_dsl import (
Expand Down Expand Up @@ -570,18 +571,21 @@ def test_meta_fields_can_be_set_directly_in_init():
assert md.meta.id is p


@pytest.mark.asyncio
async def test_save_no_index(async_mock_client):
md = MyDoc()
with raises(ValidationException):
await md.save(using="mock")


@pytest.mark.asyncio
async def test_delete_no_index(async_mock_client):
md = MyDoc()
with raises(ValidationException):
await md.delete(using="mock")


@pytest.mark.asyncio
async def test_update_no_fields():
md = MyDoc()
with raises(IllegalOperation):
Expand Down
2 changes: 2 additions & 0 deletions tests/_async/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import string
from random import choice

import pytest
from pytest import raises

from elasticsearch_dsl import (
Expand Down Expand Up @@ -188,6 +189,7 @@ def test_index_template_can_have_order():
assert {"index_patterns": ["i-*"], "order": 2} == it.to_dict()


@pytest.mark.asyncio
async def test_index_template_save_result(async_mock_client):
it = AsyncIndexTemplate("test-template", "test-*")

Expand Down
7 changes: 7 additions & 0 deletions tests/_async/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

from copy import deepcopy

import pytest
from pytest import raises

from elasticsearch_dsl import A, AsyncEmptySearch, AsyncSearch, Document, Q, query
Expand All @@ -29,6 +30,7 @@ def test_expand__to_dot_is_respected():
assert {"query": {"match": {"a__b": 42}}} == s.to_dict()


@pytest.mark.asyncio
async def test_execute_uses_cache():
s = AsyncSearch()
r = object()
Expand All @@ -37,6 +39,7 @@ async def test_execute_uses_cache():
assert r is await s.execute()


@pytest.mark.asyncio
async def test_cache_can_be_ignored(async_mock_client):
s = AsyncSearch(using="mock")
r = object()
Expand All @@ -46,6 +49,7 @@ async def test_cache_can_be_ignored(async_mock_client):
async_mock_client.search.assert_awaited_once_with(index=None, body={})


@pytest.mark.asyncio
async def test_iter_iterates_over_hits():
s = AsyncSearch()
s._response = [1, 2, 3]
Expand Down Expand Up @@ -508,6 +512,7 @@ def test_from_dict_doesnt_need_query():
assert {"size": 5} == s.to_dict()


@pytest.mark.asyncio
async def test_params_being_passed_to_search(async_mock_client):
s = AsyncSearch(using="mock")
s = s.params(routing="42")
Expand Down Expand Up @@ -599,6 +604,7 @@ def test_exclude():
} == s.to_dict()


@pytest.mark.asyncio
async def test_delete_by_query(async_mock_client):
s = AsyncSearch(using="mock").query("match", lang="java")
await s.delete()
Expand Down Expand Up @@ -683,6 +689,7 @@ def test_rescore_query_to_dict():
}


@pytest.mark.asyncio
async def test_empty_search():
s = AsyncEmptySearch(index="index-name")
s = s.query("match", lang="java")
Expand Down
3 changes: 3 additions & 0 deletions tests/_async/test_update_by_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

from copy import deepcopy

import pytest

from elasticsearch_dsl import AsyncUpdateByQuery, Q
from elasticsearch_dsl.response import UpdateByQueryResponse

Expand Down Expand Up @@ -136,6 +138,7 @@ def test_from_dict_doesnt_need_query():
assert {"script": {"source": "test"}} == ubq.to_dict()


@pytest.mark.asyncio
async def test_params_being_passed_to_search(async_mock_client):
ubq = AsyncUpdateByQuery(using="mock")
ubq = ubq.params(routing="42")
Expand Down
4 changes: 4 additions & 0 deletions tests/_sync/test_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from datetime import datetime
from hashlib import md5

import pytest
from pytest import raises

from elasticsearch_dsl import (
Expand Down Expand Up @@ -570,18 +571,21 @@ def test_meta_fields_can_be_set_directly_in_init():
assert md.meta.id is p


@pytest.mark.sync
def test_save_no_index(mock_client):
md = MyDoc()
with raises(ValidationException):
md.save(using="mock")


@pytest.mark.sync
def test_delete_no_index(mock_client):
md = MyDoc()
with raises(ValidationException):
md.delete(using="mock")


@pytest.mark.sync
def test_update_no_fields():
md = MyDoc()
with raises(IllegalOperation):
Expand Down
2 changes: 2 additions & 0 deletions tests/_sync/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import string
from random import choice

import pytest
from pytest import raises

from elasticsearch_dsl import Date, Document, Index, IndexTemplate, Text, analyzer
Expand Down Expand Up @@ -181,6 +182,7 @@ def test_index_template_can_have_order():
assert {"index_patterns": ["i-*"], "order": 2} == it.to_dict()


@pytest.mark.sync
def test_index_template_save_result(mock_client):
it = IndexTemplate("test-template", "test-*")

Expand Down
7 changes: 7 additions & 0 deletions tests/_sync/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

from copy import deepcopy

import pytest
from pytest import raises

from elasticsearch_dsl import A, Document, EmptySearch, Q, Search, query
Expand All @@ -29,6 +30,7 @@ def test_expand__to_dot_is_respected():
assert {"query": {"match": {"a__b": 42}}} == s.to_dict()


@pytest.mark.sync
def test_execute_uses_cache():
s = Search()
r = object()
Expand All @@ -37,6 +39,7 @@ def test_execute_uses_cache():
assert r is s.execute()


@pytest.mark.sync
def test_cache_can_be_ignored(mock_client):
s = Search(using="mock")
r = object()
Expand All @@ -46,6 +49,7 @@ def test_cache_can_be_ignored(mock_client):
mock_client.search.assert_called_once_with(index=None, body={})


@pytest.mark.sync
def test_iter_iterates_over_hits():
s = Search()
s._response = [1, 2, 3]
Expand Down Expand Up @@ -508,6 +512,7 @@ def test_from_dict_doesnt_need_query():
assert {"size": 5} == s.to_dict()


@pytest.mark.sync
def test_params_being_passed_to_search(mock_client):
s = Search(using="mock")
s = s.params(routing="42")
Expand Down Expand Up @@ -597,6 +602,7 @@ def test_exclude():
} == s.to_dict()


@pytest.mark.sync
def test_delete_by_query(mock_client):
s = Search(using="mock").query("match", lang="java")
s.delete()
Expand Down Expand Up @@ -681,6 +687,7 @@ def test_rescore_query_to_dict():
}


@pytest.mark.sync
def test_empty_search():
s = EmptySearch(index="index-name")
s = s.query("match", lang="java")
Expand Down
3 changes: 3 additions & 0 deletions tests/_sync/test_update_by_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

from copy import deepcopy

import pytest

from elasticsearch_dsl import Q, UpdateByQuery
from elasticsearch_dsl.response import UpdateByQueryResponse

Expand Down Expand Up @@ -136,6 +138,7 @@ def test_from_dict_doesnt_need_query():
assert {"script": {"source": "test"}} == ubq.to_dict()


@pytest.mark.sync
def test_params_being_passed_to_search(mock_client):
ubq = UpdateByQuery(using="mock")
ubq = ubq.params(routing="42")
Expand Down
5 changes: 5 additions & 0 deletions tests/test_integration/_async/test_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
# specific language governing permissions and limitations
# under the License.

import pytest

from elasticsearch_dsl import analyzer, token_filter, tokenizer


@pytest.mark.asyncio
async def test_simulate_with_just__builtin_tokenizer(async_client):
a = analyzer("my-analyzer", tokenizer="keyword")
tokens = (await a.async_simulate("Hello World!", using=async_client)).tokens
Expand All @@ -26,6 +29,7 @@ async def test_simulate_with_just__builtin_tokenizer(async_client):
assert tokens[0].token == "Hello World!"


@pytest.mark.asyncio
async def test_simulate_complex(async_client):
a = analyzer(
"my-analyzer",
Expand All @@ -39,6 +43,7 @@ async def test_simulate_complex(async_client):
assert ["this", "works"] == [t.token for t in tokens]


@pytest.mark.asyncio
async def test_simulate_builtin(async_client):
a = analyzer("my-analyzer", "english")
tokens = (await a.async_simulate("fixes running")).tokens
Expand Down

0 comments on commit 9ba89eb

Please sign in to comment.