|
| 1 | +import pytest |
| 2 | + |
| 3 | +asyncio = pytest.importorskip("asyncio") |
| 4 | + |
| 5 | +from graphql.execution.executors.asyncio import AsyncioExecutor |
| 6 | +from graphql.type.definition import ( |
| 7 | + GraphQLField, |
| 8 | + GraphQLNonNull, |
| 9 | + GraphQLObjectType, |
| 10 | +) |
| 11 | +from graphql.type.scalars import GraphQLString |
| 12 | +from graphql.type.schema import GraphQLSchema |
| 13 | +from graphql_server import RequestParams, run_http_query |
| 14 | +from promise import Promise |
| 15 | + |
| 16 | +from .utils import as_dicts |
| 17 | + |
| 18 | + |
| 19 | +def resolve_error_sync(_obj, _info): |
| 20 | + raise ValueError("error sync") |
| 21 | + |
| 22 | + |
| 23 | +@asyncio.coroutine |
| 24 | +def resolve_error_async(_obj, _info): |
| 25 | + yield from asyncio.sleep(0.001) |
| 26 | + raise ValueError("error async") |
| 27 | + |
| 28 | + |
| 29 | +def resolve_field_sync(_obj, _info): |
| 30 | + return "sync" |
| 31 | + |
| 32 | + |
| 33 | +@asyncio.coroutine |
| 34 | +def resolve_field_async(_obj, info): |
| 35 | + yield from asyncio.sleep(0.001) |
| 36 | + return "async" |
| 37 | + |
| 38 | + |
| 39 | +NonNullString = GraphQLNonNull(GraphQLString) |
| 40 | + |
| 41 | +QueryRootType = GraphQLObjectType( |
| 42 | + name="QueryRoot", |
| 43 | + fields={ |
| 44 | + "errorSync": GraphQLField(NonNullString, resolver=resolve_error_sync), |
| 45 | + "errorAsync": GraphQLField(NonNullString, resolver=resolve_error_async), |
| 46 | + "fieldSync": GraphQLField(NonNullString, resolver=resolve_field_sync), |
| 47 | + "fieldAsync": GraphQLField(NonNullString, resolver=resolve_field_async), |
| 48 | + }, |
| 49 | +) |
| 50 | + |
| 51 | +schema = GraphQLSchema(QueryRootType) |
| 52 | + |
| 53 | + |
| 54 | +def test_get_reponses_using_asyncioexecutor(): |
| 55 | + class TestExecutor(AsyncioExecutor): |
| 56 | + called = False |
| 57 | + waited = False |
| 58 | + cleaned = False |
| 59 | + |
| 60 | + def wait_until_finished(self): |
| 61 | + TestExecutor.waited = True |
| 62 | + super().wait_until_finished() |
| 63 | + |
| 64 | + def clean(self): |
| 65 | + TestExecutor.cleaned = True |
| 66 | + super().clean() |
| 67 | + |
| 68 | + def execute(self, fn, *args, **kwargs): |
| 69 | + TestExecutor.called = True |
| 70 | + return super().execute(fn, *args, **kwargs) |
| 71 | + |
| 72 | + query = "{fieldSync fieldAsync}" |
| 73 | + |
| 74 | + loop = asyncio.get_event_loop() |
| 75 | + |
| 76 | + @asyncio.coroutine |
| 77 | + def get_results(): |
| 78 | + result_promises, params = run_http_query( |
| 79 | + schema, |
| 80 | + "get", |
| 81 | + {}, |
| 82 | + dict(query=query), |
| 83 | + executor=TestExecutor(loop=loop), |
| 84 | + return_promise=True, |
| 85 | + ) |
| 86 | + results = yield from Promise.all(result_promises) |
| 87 | + return results, params |
| 88 | + |
| 89 | + results, params = loop.run_until_complete(get_results()) |
| 90 | + |
| 91 | + expected_results = [{"data": {"fieldSync": "sync", "fieldAsync": "async"}}] |
| 92 | + |
| 93 | + assert as_dicts(results) == expected_results |
| 94 | + assert params == [RequestParams(query=query, variables=None, operation_name=None)] |
| 95 | + assert TestExecutor.called |
| 96 | + assert not TestExecutor.waited |
| 97 | + assert TestExecutor.cleaned |
0 commit comments