Skip to content

Commit 0aaa3fc

Browse files
committed
add TestUnforgivingExecutionContext Tests without UnforgivingExecutionContext
1 parent f039af2 commit 0aaa3fc

File tree

1 file changed

+109
-2
lines changed

1 file changed

+109
-2
lines changed

graphene/types/tests/test_schema.py

Lines changed: 109 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from graphql.type import GraphQLObjectType, GraphQLSchema
2-
from pytest import raises
3-
2+
from graphql import GraphQLError
3+
from pytest import fixture, mark, raises
44
from graphene.tests.utils import dedent
55

66
from ..field import Field
@@ -68,3 +68,110 @@ def test_schema_requires_query_type():
6868
assert len(result.errors) == 1
6969
error = result.errors[0]
7070
assert error.message == "Query root type must be provided."
71+
72+
73+
class TestUnforgivingExecutionContext:
74+
@fixture
75+
def schema(self):
76+
class ErrorFieldsMixin:
77+
sanity_field = String()
78+
expected_error_field = String()
79+
unexpected_value_error_field = String()
80+
unexpected_type_error_field = String()
81+
unexpected_attribute_error_field = String()
82+
unexpected_key_error_field = String()
83+
84+
@staticmethod
85+
def resolve_sanity_field(obj, info):
86+
return "not an error"
87+
88+
@staticmethod
89+
def resolve_expected_error_field(obj, info):
90+
raise GraphQLError("expected error")
91+
92+
@staticmethod
93+
def resolve_unexpected_value_error_field(obj, info):
94+
raise ValueError("unexpected error")
95+
96+
@staticmethod
97+
def resolve_unexpected_type_error_field(obj, info):
98+
raise TypeError("unexpected error")
99+
100+
@staticmethod
101+
def resolve_unexpected_attribute_error_field(obj, info):
102+
raise AttributeError("unexpected error")
103+
104+
@staticmethod
105+
def resolve_unexpected_key_error_field(obj, info):
106+
return {}["fails"]
107+
108+
class NestedObject(ErrorFieldsMixin, ObjectType):
109+
pass
110+
111+
class MyQuery(ErrorFieldsMixin, ObjectType):
112+
nested_object = Field(NestedObject)
113+
nested_object_error = Field(NestedObject)
114+
115+
@staticmethod
116+
def resolve_nested_object(obj, info):
117+
return object()
118+
119+
@staticmethod
120+
def resolve_nested_object_error(obj, info):
121+
raise TypeError()
122+
123+
schema = Schema(query=MyQuery)
124+
return schema
125+
126+
def test_sanity_check(self, schema):
127+
# this should pass with no errors (sanity check)
128+
result = schema.execute(
129+
"query { sanityField }",
130+
)
131+
assert not result.errors
132+
assert result.data == {"sanityField": "not an error"}
133+
134+
def test_nested_sanity_check(self, schema):
135+
# this should pass with no errors (sanity check)
136+
result = schema.execute(
137+
r"query { nestedObject { sanityField } }",
138+
)
139+
assert not result.errors
140+
assert result.data == {"nestedObject": {"sanityField": "not an error"}}
141+
142+
def test_graphql_error(self, schema):
143+
result = schema.execute(
144+
"query { expectedErrorField }",
145+
)
146+
assert len(result.errors) == 1
147+
assert result.errors[0].message == "expected error"
148+
assert result.data == {"expectedErrorField": None}
149+
150+
def test_nested_graphql_error(self, schema):
151+
result = schema.execute(
152+
r"query { nestedObject { expectedErrorField } }",
153+
)
154+
assert len(result.errors) == 1
155+
assert result.errors[0].message == "expected error"
156+
assert result.data == {"nestedObject": {"expectedErrorField": None}}
157+
158+
@mark.parametrize(
159+
"field,exception",
160+
[
161+
("unexpectedValueErrorField", ValueError),
162+
("unexpectedTypeErrorField", TypeError),
163+
("unexpectedAttributeErrorField", AttributeError),
164+
("unexpectedKeyErrorField", KeyError),
165+
("nestedObject { unexpectedValueErrorField }", ValueError),
166+
("nestedObject { unexpectedTypeErrorField }", TypeError),
167+
("nestedObject { unexpectedAttributeErrorField }", AttributeError),
168+
("nestedObject { unexpectedKeyErrorField }", KeyError),
169+
("nestedObjectError { __typename }", TypeError),
170+
],
171+
)
172+
def test_unexpected_error(self, field, exception, schema):
173+
with raises(exception):
174+
# no result, but the exception should be propagated
175+
schema.execute(
176+
f"query {{ {field} }}",
177+
)

0 commit comments

Comments
 (0)