Skip to content

Commit a46cc84

Browse files
committed
Remove expected warnings from tests more reliably
1 parent 56ea5dd commit a46cc84

File tree

5 files changed

+66
-30
lines changed

5 files changed

+66
-30
lines changed

tests/execution/test_subscribe.py

+4
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
GraphQLString,
3434
)
3535

36+
from ..fixtures import cleanup
3637
from ..utils.assert_equal_awaitables_or_values import assert_equal_awaitables_or_values
3738

3839

@@ -405,6 +406,9 @@ async def async_fn(obj, info):
405406
assert is_awaitable(result)
406407
assert await result == expected_result
407408

409+
del result
410+
cleanup()
411+
408412
@mark.asyncio
409413
async def resolves_to_an_error_for_subscription_resolver_errors():
410414
expected_result = (

tests/execution/test_sync.py

+12-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from gc import collect
21
from inspect import isawaitable
32

43
from pytest import mark, raises
@@ -9,6 +8,8 @@
98
from graphql.type import GraphQLField, GraphQLObjectType, GraphQLSchema, GraphQLString
109
from graphql.validation import validate
1110

11+
from ..fixtures import cleanup
12+
1213

1314
def describe_execute_synchronously_when_possible():
1415
def _resolve_sync(root_value, _info):
@@ -91,6 +92,8 @@ async def throws_if_encountering_async_execution_with_check_sync():
9192
)
9293
msg = str(exc_info.value)
9394
assert msg == "GraphQL execution failed to complete synchronously."
95+
del exc_info
96+
cleanup()
9497

9598
@mark.asyncio
9699
@mark.filterwarnings("ignore:.* was never awaited:RuntimeWarning")
@@ -108,9 +111,8 @@ async def throws_if_encountering_async_operation_without_check_sync():
108111
}
109112
],
110113
)
111-
# garbage collect coroutine in order to not postpone the warning
112114
del result
113-
collect()
115+
cleanup()
114116

115117
@mark.asyncio
116118
@mark.filterwarnings("ignore:.* was never awaited:RuntimeWarning")
@@ -129,6 +131,8 @@ async def throws_if_encountering_async_iterable_execution_with_check_sync():
129131
)
130132
msg = str(exc_info.value)
131133
assert msg == "GraphQL execution failed to complete synchronously."
134+
del exc_info
135+
cleanup()
132136

133137
@mark.asyncio
134138
@mark.filterwarnings("ignore:.* was never awaited:RuntimeWarning")
@@ -145,6 +149,8 @@ async def throws_if_encountering_async_iterable_execution_without_check_sync():
145149
execute_sync(schema, document=parse(doc), root_value="rootValue")
146150
msg = str(exc_info.value)
147151
assert msg == "GraphQL execution failed to complete synchronously."
152+
del exc_info
153+
cleanup()
148154

149155
def describe_graphql_sync():
150156
def reports_errors_raised_during_schema_validation():
@@ -192,6 +198,8 @@ async def throws_if_encountering_async_operation_with_check_sync():
192198
graphql_sync(schema, doc, "rootValue", check_sync=True)
193199
msg = str(exc_info.value)
194200
assert msg == "GraphQL execution failed to complete synchronously."
201+
del exc_info
202+
cleanup()
195203

196204
@mark.asyncio
197205
@mark.filterwarnings("ignore:.* was never awaited:RuntimeWarning")
@@ -209,6 +217,5 @@ async def throws_if_encountering_async_operation_without_check_sync():
209217
}
210218
],
211219
)
212-
# garbage collect coroutine in order to not postpone the warning
213220
del result
214-
collect()
221+
cleanup()

tests/fixtures/__init__.py

+11
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,29 @@
11
"""Fixtures for graphql tests"""
22
import json
3+
from gc import collect
34
from os.path import dirname, join
45

56
from pytest import fixture
67

78

89
__all__ = [
10+
"cleanup",
911
"kitchen_sink_query",
1012
"kitchen_sink_sdl",
1113
"big_schema_sdl",
1214
"big_schema_introspection_result",
1315
]
1416

1517

18+
def cleanup(rounds=5):
19+
"""Run garbage collector.
20+
21+
This can be used to remove coroutines that were not awaited after running tests.
22+
"""
23+
for _generation in range(rounds):
24+
collect()
25+
26+
1627
def read_graphql(name):
1728
path = join(dirname(__file__), name + ".graphql")
1829
return open(path, encoding="utf-8").read()

tests/pyutils/test_is_awaitable.py

+33-23
Original file line numberDiff line numberDiff line change
@@ -61,49 +61,59 @@ def some_generator():
6161
assert not is_awaitable(some_generator())
6262

6363
def declines_a_coroutine_function():
64-
async def some_coroutine():
64+
async def some_async_function():
6565
return True # pragma: no cover
6666

67-
assert not isawaitable(some_coroutine)
68-
assert not is_awaitable(some_coroutine)
67+
assert not isawaitable(some_async_function)
68+
assert not is_awaitable(some_async_function)
6969

7070
@mark.asyncio
71-
@mark.filterwarnings("ignore:.* was never awaited:RuntimeWarning")
7271
async def recognizes_a_coroutine_object():
73-
async def some_coroutine():
74-
return False # pragma: no cover
72+
async def some_async_function():
73+
return True
74+
75+
some_coroutine = some_async_function()
76+
77+
assert isawaitable(some_coroutine)
78+
assert is_awaitable(some_coroutine)
7579

76-
assert isawaitable(some_coroutine())
77-
assert is_awaitable(some_coroutine())
80+
assert await some_coroutine is True
7881

7982
@mark.filterwarnings("ignore::Warning") # Deprecation and Runtime warnings
8083
@mark.skipif(
8184
python_version >= (3, 11),
8285
reason="Generator-based coroutines not supported any more since Python 3.11",
8386
)
84-
def recognizes_an_old_style_coroutine(): # pragma: no cover
87+
async def recognizes_an_old_style_coroutine(): # pragma: no cover
8588
@asyncio.coroutine # type: ignore
86-
def some_old_style_coroutine():
87-
yield False # pragma: no cover
89+
def some_function():
90+
yield True
8891

89-
assert is_awaitable(some_old_style_coroutine())
90-
assert is_awaitable(some_old_style_coroutine())
92+
some_old_style_coroutine = some_function()
93+
assert is_awaitable(some_old_style_coroutine)
94+
assert is_awaitable(some_old_style_coroutine)
9195

9296
@mark.asyncio
93-
@mark.filterwarnings("ignore:.* was never awaited:RuntimeWarning")
9497
async def recognizes_a_future_object():
95-
async def some_coroutine():
96-
return False # pragma: no cover
98+
async def some_async_function():
99+
return True
97100

98-
some_future = asyncio.ensure_future(some_coroutine())
101+
some_coroutine = some_async_function()
102+
some_future = asyncio.ensure_future(some_coroutine)
99103

100104
assert is_awaitable(some_future)
101105
assert is_awaitable(some_future)
102106

103-
@mark.filterwarnings("ignore:.* was never awaited:RuntimeWarning")
104-
def declines_an_async_generator():
105-
async def some_async_generator():
106-
yield True # pragma: no cover
107+
assert await some_future is True
108+
109+
@mark.asyncio
110+
async def declines_an_async_generator():
111+
async def some_async_generator_function():
112+
yield True
113+
114+
some_async_generator = some_async_generator_function()
115+
116+
assert not isawaitable(some_async_generator)
117+
assert not is_awaitable(some_async_generator)
107118

108-
assert not isawaitable(some_async_generator())
109-
assert not is_awaitable(some_async_generator())
119+
assert await some_async_generator.__anext__() is True

tests/utils/test_assert_equal_awaitables_or_values.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,17 @@ async def test_value(value):
3838
)
3939

4040
@mark.asyncio
41-
@mark.filterwarnings("ignore:.* was never awaited:RuntimeWarning")
4241
async def throws_when_given_mixture_of_equal_values_and_awaitables():
4342
async def test_value():
4443
return {"test": "test"}
4544

45+
value1 = await test_value()
46+
value2 = test_value()
47+
4648
with raises(
4749
AssertionError,
4850
match=r"Received an invalid mixture of promises and values\.",
4951
):
50-
await assert_equal_awaitables_or_values(await test_value(), test_value())
52+
await assert_equal_awaitables_or_values(value1, value2)
53+
54+
assert await value2 == value1

0 commit comments

Comments
 (0)