Skip to content

Commit 81181ca

Browse files
Switch to pytest-asyncio's strict mode for async tests (#1782)
* Switch to pytest-asyncio's strict mode for async tests * add one missing test * use 'sync' as sync I/O marker
1 parent 9a53a5b commit 81181ca

36 files changed

+234
-2
lines changed

setup.cfg

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ 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
14+
markers =
15+
sync: mark a test as performing I/O without asyncio.

tests/_async/test_document.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from datetime import datetime
2222
from hashlib import md5
2323

24+
import pytest
2425
from pytest import raises
2526

2627
from elasticsearch_dsl import (
@@ -570,18 +571,21 @@ def test_meta_fields_can_be_set_directly_in_init():
570571
assert md.meta.id is p
571572

572573

574+
@pytest.mark.asyncio
573575
async def test_save_no_index(async_mock_client):
574576
md = MyDoc()
575577
with raises(ValidationException):
576578
await md.save(using="mock")
577579

578580

581+
@pytest.mark.asyncio
579582
async def test_delete_no_index(async_mock_client):
580583
md = MyDoc()
581584
with raises(ValidationException):
582585
await md.delete(using="mock")
583586

584587

588+
@pytest.mark.asyncio
585589
async def test_update_no_fields():
586590
md = MyDoc()
587591
with raises(IllegalOperation):

tests/_async/test_index.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import string
1919
from random import choice
2020

21+
import pytest
2122
from pytest import raises
2223

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

190191

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

tests/_async/test_search.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
from copy import deepcopy
1919

20+
import pytest
2021
from pytest import raises, warns
2122

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

3132

33+
@pytest.mark.asyncio
3234
async def test_execute_uses_cache():
3335
s = AsyncSearch()
3436
r = object()
@@ -37,6 +39,7 @@ async def test_execute_uses_cache():
3739
assert r is await s.execute()
3840

3941

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

4851

52+
@pytest.mark.asyncio
4953
async def test_iter_iterates_over_hits():
5054
s = AsyncSearch()
5155
s._response = [1, 2, 3]
@@ -514,6 +518,7 @@ def test_from_dict_doesnt_need_query():
514518
assert {"size": 5} == s.to_dict()
515519

516520

521+
@pytest.mark.asyncio
517522
async def test_params_being_passed_to_search(async_mock_client):
518523
s = AsyncSearch(using="mock")
519524
s = s.params(routing="42")
@@ -605,6 +610,7 @@ def test_exclude():
605610
} == s.to_dict()
606611

607612

613+
@pytest.mark.asyncio
608614
async def test_delete_by_query(async_mock_client):
609615
s = AsyncSearch(using="mock").query("match", lang="java")
610616
await s.delete()
@@ -689,6 +695,7 @@ def test_rescore_query_to_dict():
689695
}
690696

691697

698+
@pytest.mark.asyncio
692699
async def test_empty_search():
693700
s = AsyncEmptySearch(index="index-name")
694701
s = s.query("match", lang="java")

tests/_async/test_update_by_query.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
from copy import deepcopy
1919

20+
import pytest
21+
2022
from elasticsearch_dsl import AsyncUpdateByQuery, Q
2123
from elasticsearch_dsl.response import UpdateByQueryResponse
2224

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

138140

141+
@pytest.mark.asyncio
139142
async def test_params_being_passed_to_search(async_mock_client):
140143
ubq = AsyncUpdateByQuery(using="mock")
141144
ubq = ubq.params(routing="42")

tests/_sync/test_document.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from datetime import datetime
2222
from hashlib import md5
2323

24+
import pytest
2425
from pytest import raises
2526

2627
from elasticsearch_dsl import (
@@ -570,18 +571,21 @@ def test_meta_fields_can_be_set_directly_in_init():
570571
assert md.meta.id is p
571572

572573

574+
@pytest.mark.sync
573575
def test_save_no_index(mock_client):
574576
md = MyDoc()
575577
with raises(ValidationException):
576578
md.save(using="mock")
577579

578580

581+
@pytest.mark.sync
579582
def test_delete_no_index(mock_client):
580583
md = MyDoc()
581584
with raises(ValidationException):
582585
md.delete(using="mock")
583586

584587

588+
@pytest.mark.sync
585589
def test_update_no_fields():
586590
md = MyDoc()
587591
with raises(IllegalOperation):

tests/_sync/test_index.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import string
1919
from random import choice
2020

21+
import pytest
2122
from pytest import raises
2223

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

183184

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

tests/_sync/test_search.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
from copy import deepcopy
1919

20+
import pytest
2021
from pytest import raises, warns
2122

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

3132

33+
@pytest.mark.sync
3234
def test_execute_uses_cache():
3335
s = Search()
3436
r = object()
@@ -37,6 +39,7 @@ def test_execute_uses_cache():
3739
assert r is s.execute()
3840

3941

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

4851

52+
@pytest.mark.sync
4953
def test_iter_iterates_over_hits():
5054
s = Search()
5155
s._response = [1, 2, 3]
@@ -514,6 +518,7 @@ def test_from_dict_doesnt_need_query():
514518
assert {"size": 5} == s.to_dict()
515519

516520

521+
@pytest.mark.sync
517522
def test_params_being_passed_to_search(mock_client):
518523
s = Search(using="mock")
519524
s = s.params(routing="42")
@@ -603,6 +608,7 @@ def test_exclude():
603608
} == s.to_dict()
604609

605610

611+
@pytest.mark.sync
606612
def test_delete_by_query(mock_client):
607613
s = Search(using="mock").query("match", lang="java")
608614
s.delete()
@@ -687,6 +693,7 @@ def test_rescore_query_to_dict():
687693
}
688694

689695

696+
@pytest.mark.sync
690697
def test_empty_search():
691698
s = EmptySearch(index="index-name")
692699
s = s.query("match", lang="java")

tests/_sync/test_update_by_query.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
from copy import deepcopy
1919

20+
import pytest
21+
2022
from elasticsearch_dsl import Q, UpdateByQuery
2123
from elasticsearch_dsl.response import UpdateByQueryResponse
2224

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

138140

141+
@pytest.mark.sync
139142
def test_params_being_passed_to_search(mock_client):
140143
ubq = UpdateByQuery(using="mock")
141144
ubq = ubq.params(routing="42")

tests/test_integration/_async/test_analysis.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@
1515
# specific language governing permissions and limitations
1616
# under the License.
1717

18+
import pytest
19+
1820
from elasticsearch_dsl import analyzer, token_filter, tokenizer
1921

2022

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

2831

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

4145

46+
@pytest.mark.asyncio
4247
async def test_simulate_builtin(async_client):
4348
a = analyzer("my-analyzer", "english")
4449
tokens = (await a.async_simulate("fixes running")).tokens

0 commit comments

Comments
 (0)