| 1 |
import asyncio |
| 2 |
import sys |
| 3 |
from inspect import isawaitable |
| 4 |
|
| 5 |
from graphql.pyutils import is_awaitable |
| 6 |
from pytest import mark |
| 7 |
|
| 8 |
|
| 9 |
def describe_is_awaitable(): |
| 10 |
def declines_the_none_value(): |
| 11 |
assert not isawaitable(None) |
| 12 |
assert not is_awaitable(None) |
| 13 |
|
| 14 |
def declines_a_boolean_value(): |
| 15 |
assert not isawaitable(True) |
| 16 |
assert not is_awaitable(True) |
| 17 |
|
| 18 |
def declines_an_int_value(): |
| 19 |
assert not is_awaitable(42) |
| 20 |
|
| 21 |
def declines_a_string_value(): |
| 22 |
assert not isawaitable("some_string") |
| 23 |
assert not is_awaitable("some_string") |
| 24 |
|
| 25 |
def declines_a_dict_value(): |
| 26 |
assert not isawaitable({}) |
| 27 |
assert not is_awaitable({}) |
| 28 |
|
| 29 |
def declines_an_object_instance(): |
| 30 |
assert not isawaitable(object()) |
| 31 |
assert not is_awaitable(object()) |
| 32 |
|
| 33 |
def declines_the_type_class(): |
| 34 |
assert not isawaitable(type) |
| 35 |
assert not is_awaitable(type) |
| 36 |
|
| 37 |
def declines_a_lambda_function(): |
| 38 |
assert not isawaitable(lambda: True) # pragma: no cover |
| 39 |
assert not is_awaitable(lambda: True) # pragma: no cover |
| 40 |
|
| 41 |
def declines_a_normal_function(): |
| 42 |
def some_function(): |
| 43 |
return True |
| 44 |
|
| 45 |
assert not isawaitable(some_function()) |
| 46 |
assert not is_awaitable(some_function) |
| 47 |
|
| 48 |
def declines_a_normal_generator_function(): |
| 49 |
def some_generator(): |
| 50 |
yield True # pragma: no cover |
| 51 |
|
| 52 |
assert not isawaitable(some_generator) |
| 53 |
assert not is_awaitable(some_generator) |
| 54 |
|
| 55 |
def declines_a_normal_generator_object(): |
| 56 |
def some_generator(): |
| 57 |
yield True # pragma: no cover |
| 58 |
|
| 59 |
assert not isawaitable(some_generator()) |
| 60 |
assert not is_awaitable(some_generator()) |
| 61 |
|
| 62 |
def declines_a_coroutine_function(): |
| 63 |
async def some_coroutine(): |
| 64 |
return True # pragma: no cover |
| 65 |
|
| 66 |
assert not isawaitable(some_coroutine) |
| 67 |
assert not is_awaitable(some_coroutine) |
| 68 |
|
| 69 |
@mark.asyncio |
| 70 |
@mark.filterwarnings("ignore:.* was never awaited:RuntimeWarning") |
| 71 |
async def recognizes_a_coroutine_object(): |
| 72 |
async def some_coroutine(): |
| 73 |
return False # pragma: no cover |
| 74 |
|
| 75 |
assert isawaitable(some_coroutine()) |
| 76 |
assert is_awaitable(some_coroutine()) |
| 77 |
|
| 78 |
@mark.filterwarnings("ignore::Warning") # Deprecation and Runtime |
| 79 |
@mark.skipif( |
| 80 |
sys.version_info >= (3, 11), |
| 81 |
reason="Generator-based coroutines not supported after Python 3.11", |
| 82 |
) |
| 83 |
def recognizes_an_old_style_coroutine(): # pragma: no cover |
| 84 |
@asyncio.coroutine |
| 85 |
def some_old_style_coroutine(): |
| 86 |
yield False # pragma: no cover |
| 87 |
|
| 88 |
assert is_awaitable(some_old_style_coroutine()) |
| 89 |
assert is_awaitable(some_old_style_coroutine()) |
| 90 |
|
| 91 |
@mark.asyncio |
| 92 |
@mark.filterwarnings("ignore:.* was never awaited:RuntimeWarning") |
| 93 |
async def recognizes_a_future_object(): |
| 94 |
async def some_coroutine(): |
| 95 |
return False # pragma: no cover |
| 96 |
|
| 97 |
some_future = asyncio.ensure_future(some_coroutine()) |
| 98 |
|
| 99 |
assert is_awaitable(some_future) |
| 100 |
assert is_awaitable(some_future) |
| 101 |
|
| 102 |
@mark.filterwarnings("ignore:.* was never awaited:RuntimeWarning") |
| 103 |
def declines_an_async_generator(): |
| 104 |
async def some_async_generator(): |
| 105 |
yield True # pragma: no cover |
| 106 |
|
| 107 |
assert not isawaitable(some_async_generator()) |
| 108 |
assert not is_awaitable(some_async_generator()) |