-
Notifications
You must be signed in to change notification settings - Fork 180
/
Copy pathtest_executor_asyncio.py
141 lines (110 loc) · 3.9 KB
/
test_executor_asyncio.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
"""
isort:skip_file
"""
# type: ignore
# flake8: noqa
import pytest
asyncio = pytest.importorskip("asyncio")
from graphql.error import format_error, GraphQLError
from graphql.execution import execute
from graphql.language.parser import parse
from graphql.type import GraphQLField, GraphQLObjectType, GraphQLSchema, GraphQLString
from ..executors.asyncio import AsyncioExecutor
from .test_mutations import assert_evaluate_mutations_serially
def test_asyncio_executor():
# type: () -> None
def resolver(context, *_):
# type: (Optional[Any], *ResolveInfo) -> str
asyncio.sleep(0.001)
return "hey"
@asyncio.coroutine
def resolver_2(context, *_):
# type: (Optional[Any], *ResolveInfo) -> str
asyncio.sleep(0.003)
return "hey2"
def resolver_3(contest, *_):
# type: (Optional[Any], *ResolveInfo) -> str
return "hey3"
Type = GraphQLObjectType(
"Type",
{
"a": GraphQLField(GraphQLString, resolver=resolver),
"b": GraphQLField(GraphQLString, resolver=resolver_2),
"c": GraphQLField(GraphQLString, resolver=resolver_3),
},
)
ast = parse("{ a b c }")
result = execute(GraphQLSchema(Type), ast, executor=AsyncioExecutor())
assert not result.errors
assert result.data == {"a": "hey", "b": "hey2", "c": "hey3"}
def test_asyncio_executor_custom_loop():
# type: () -> None
loop = asyncio.get_event_loop()
def resolver(context, *_):
# type: (Optional[Any], *ResolveInfo) -> str
asyncio.sleep(0.001, loop=loop)
return "hey"
@asyncio.coroutine
def resolver_2(context, *_):
# type: (Optional[Any], *ResolveInfo) -> str
asyncio.sleep(0.003, loop=loop)
return "hey2"
def resolver_3(contest, *_):
# type: (Optional[Any], *ResolveInfo) -> str
return "hey3"
Type = GraphQLObjectType(
"Type",
{
"a": GraphQLField(GraphQLString, resolver=resolver),
"b": GraphQLField(GraphQLString, resolver=resolver_2),
"c": GraphQLField(GraphQLString, resolver=resolver_3),
},
)
ast = parse("{ a b c }")
result = execute(GraphQLSchema(Type), ast, executor=AsyncioExecutor(loop=loop))
assert not result.errors
assert result.data == {"a": "hey", "b": "hey2", "c": "hey3"}
def test_asyncio_executor_with_error():
# type: () -> None
ast = parse("query Example { a, b }")
def resolver(context, *_):
# type: (Optional[Any], *ResolveInfo) -> str
asyncio.sleep(0.001)
return "hey"
def resolver_2(context, *_):
# type: (Optional[Any], *ResolveInfo) -> NoReturn
asyncio.sleep(0.003)
raise GraphQLError("resolver_2 failed!")
Type = GraphQLObjectType(
"Type",
{
"a": GraphQLField(GraphQLString, resolver=resolver),
"b": GraphQLField(GraphQLString, resolver=resolver_2),
},
)
result = execute(GraphQLSchema(Type), ast, executor=AsyncioExecutor())
formatted_errors = list(map(format_error, result.errors))
assert formatted_errors == [
{
"locations": [{"line": 1, "column": 20}],
"path": ["b"],
"message": "resolver_2 failed!",
}
]
assert result.data == {"a": "hey", "b": None}
def test_asyncio_executor_exceptions_reraised():
# type: () -> None
ast = parse("query Example { a }")
class Error(Exception):
pass
def resolver(context, *_):
# type: (Optional[Any], *ResolveInfo) -> str
raise Error("UH OH!")
Type = GraphQLObjectType(
"Type", {"a": GraphQLField(GraphQLString, resolver=resolver)}
)
with pytest.raises(Error):
execute(GraphQLSchema(Type), ast, executor=AsyncioExecutor())
def test_evaluates_mutations_serially():
# type: () -> None
assert_evaluate_mutations_serially(executor=AsyncioExecutor())