-
Notifications
You must be signed in to change notification settings - Fork 180
/
Copy pathtest_executor_gevent.py
80 lines (62 loc) · 2.13 KB
/
test_executor_gevent.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
"""
isort:skip_file
"""
# type: ignore
# flake8: noqa
import pytest
gevent = pytest.importorskip("gevent")
from graphql.error import format_error, GraphQLError
from graphql.execution import execute
from graphql.language.location import SourceLocation
from graphql.language.parser import parse
from graphql.type import GraphQLField, GraphQLObjectType, GraphQLSchema, GraphQLString
from ..executors.gevent import GeventExecutor
from .test_mutations import assert_evaluate_mutations_serially
def test_gevent_executor():
def resolver(context, *_):
gevent.sleep(0.001)
return "hey"
def resolver_2(context, *_):
gevent.sleep(0.003)
return "hey2"
def resolver_3(contest, *_):
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=GeventExecutor())
assert not result.errors
assert result.data == {"a": "hey", "b": "hey2", "c": "hey3"}
def test_gevent_executor_with_error():
ast = parse("query Example { a, b }")
def resolver(context, *_):
gevent.sleep(0.001)
return "hey"
def resolver_2(context, *_):
gevent.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=GeventExecutor())
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_evaluates_mutations_serially():
assert_evaluate_mutations_serially(executor=GeventExecutor())