Skip to content

Commit 24f79fe

Browse files
committed
#23 Implement remaining unit core_type/test_definition unit tests.
Implement `__eq__` operator on `GraphQLField`, `GraphQLArgument` and `GraphQLInputObjectField`.
1 parent 4790874 commit 24f79fe

File tree

2 files changed

+152
-2
lines changed

2 files changed

+152
-2
lines changed

graphql/core/type/definition.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,19 @@ def __init__(self, type, args=None, resolver=None, deprecation_reason=None, desc
298298
self.deprecation_reason = deprecation_reason
299299
self.description = description
300300

301+
def __eq__(self, other):
302+
return (
303+
self is other or (
304+
isinstance(other, GraphQLField) and
305+
self.name == other.name and
306+
self.type == other.type and
307+
self.args == other.args and
308+
self.resolver == other.resolver and
309+
self.deprecation_reason == other.deprecation_reason and
310+
self.description == other.description
311+
)
312+
)
313+
301314

302315
class GraphQLArgument(object):
303316
__slots__ = 'name', 'type', 'default_value', 'description'
@@ -308,6 +321,17 @@ def __init__(self, type, default_value=None, description=None):
308321
self.default_value = default_value
309322
self.description = description
310323

324+
def __eq__(self, other):
325+
return (
326+
self is other or (
327+
isinstance(other, GraphQLArgument) and
328+
self.name == other.name and
329+
self.type == other.type and
330+
self.default_value == other.default_value and
331+
self.description == other.description
332+
)
333+
)
334+
311335

312336
class GraphQLInterfaceType(GraphQLType):
313337
"""Interface Type Definition
@@ -614,6 +638,16 @@ def __init__(self, type, default_value=None, description=None):
614638
self.default_value = default_value
615639
self.description = description
616640

641+
def __eq__(self, other):
642+
return (
643+
self is other or (
644+
isinstance(other, GraphQLInputObjectField) and
645+
self.name == other.name and
646+
self.type == other.type and
647+
self.description == other.description
648+
)
649+
)
650+
617651

618652
class GraphQLList(GraphQLType):
619653
"""List Modifier

tests/core_type/test_definition.py

Lines changed: 118 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
GraphQLSchema,
44
GraphQLEnumType,
55
GraphQLEnumValue,
6+
GraphQLInputObjectField,
67
GraphQLInputObjectType,
78
GraphQLInterfaceType,
89
GraphQLObjectType,
@@ -15,6 +16,7 @@
1516
GraphQLField,
1617
GraphQLArgument,
1718
)
19+
from graphql.core.type.definition import is_input_type, is_output_type
1820

1921
BlogImage = GraphQLObjectType('Image', {
2022
'url': GraphQLField(GraphQLString),
@@ -103,6 +105,64 @@ def test_defines_a_mutation_schema():
103105
assert write_mutation.name == 'writeArticle'
104106

105107

108+
def test_includes_nested_input_objects_in_the_map():
109+
NestedInputObject = GraphQLInputObjectType(
110+
name='NestedInputObject',
111+
fields={'value': GraphQLInputObjectField(GraphQLString)}
112+
)
113+
114+
SomeInputObject = GraphQLInputObjectType(
115+
name='SomeInputObject',
116+
fields={'nested': GraphQLInputObjectField(NestedInputObject)}
117+
)
118+
119+
SomeMutation = GraphQLObjectType(
120+
name='SomeMutation',
121+
fields={
122+
'mutateSomething': GraphQLField(
123+
type=BlogArticle,
124+
args={
125+
'input': GraphQLArgument(SomeInputObject)
126+
}
127+
)
128+
}
129+
)
130+
131+
schema = GraphQLSchema(
132+
query=BlogQuery,
133+
mutation=SomeMutation
134+
)
135+
136+
assert schema.get_type_map()['NestedInputObject'] is NestedInputObject
137+
138+
139+
def test_includes_interfaces_thunk_subtypes_in_the_type_map():
140+
SomeInterface = GraphQLInterfaceType(
141+
name='SomeInterface',
142+
fields={
143+
'f': GraphQLField(GraphQLInt)
144+
}
145+
)
146+
147+
SomeSubtype = GraphQLObjectType(
148+
name='SomeSubtype',
149+
fields={
150+
'f': GraphQLField(GraphQLInt)
151+
},
152+
interfaces=lambda: [SomeInterface],
153+
is_type_of=lambda: True
154+
)
155+
156+
schema = GraphQLSchema(query=GraphQLObjectType(
157+
name='Query',
158+
fields={
159+
'iface': GraphQLField(SomeInterface)
160+
}
161+
))
162+
163+
assert schema.get_type_map()['SomeSubtype'] is SomeSubtype
164+
165+
106166
def test_includes_interfaces_subtypes_in_the_type_map():
107167
SomeInterface = GraphQLInterfaceType('SomeInterface', fields={'f': GraphQLField(GraphQLInt)})
108168
SomeSubtype = GraphQLObjectType(
@@ -130,16 +190,41 @@ def test_stringifies_simple_types():
130190

131191

132192
def test_identifies_input_types():
133-
pass # TODO
193+
expected = (
194+
(GraphQLInt, True),
195+
(ObjectType, False),
196+
(InterfaceType, False),
197+
(UnionType, False),
198+
(EnumType, True),
199+
(InputObjectType, True)
200+
)
201+
202+
for type, answer in expected:
203+
assert is_input_type(type) == answer
204+
assert is_input_type(GraphQLList(type)) == answer
205+
assert is_input_type(GraphQLNonNull(type)) == answer
134206

135207

136208
def test_identifies_output_types():
137-
pass # TODO
209+
expected = (
210+
(GraphQLInt, True),
211+
(ObjectType, True),
212+
(InterfaceType, True),
213+
(UnionType, True),
214+
(EnumType, True),
215+
(InputObjectType, False)
216+
)
217+
218+
for type, answer in expected:
219+
assert is_output_type(type) == answer
220+
assert is_output_type(GraphQLList(type)) == answer
221+
assert is_output_type(GraphQLNonNull(type)) == answer
138222

139223

140224
def test_prohibits_nesting_nonnull_inside_nonnull():
141225
with raises(Exception) as excinfo:
142226
GraphQLNonNull(GraphQLNonNull(GraphQLInt))
227+
143228
assert 'Can only create NonNull of a Nullable GraphQLType but got: Int!.' in str(excinfo.value)
144229

145230

@@ -159,3 +244,34 @@ def test_prohibits_putting_non_object_types_in_unions():
159244

160245
assert 'BadUnion may only contain Object types, it cannot contain: ' + str(x) + '.' \
161246
== str(excinfo.value)
247+
248+
249+
def test_does_not_mutate_passed_field_definitions():
250+
fields = {
251+
'field1': GraphQLField(GraphQLString),
252+
'field2': GraphQLField(GraphQLString, args={'id': GraphQLArgument(GraphQLString)}),
253+
}
254+
255+
TestObject1 = GraphQLObjectType(name='Test1', fields=fields)
256+
TestObject2 = GraphQLObjectType(name='Test1', fields=fields)
257+
258+
assert TestObject1.get_fields() == TestObject2.get_fields()
259+
assert fields == {
260+
'field1': GraphQLField(GraphQLString),
261+
'field2': GraphQLField(GraphQLString, args={'id': GraphQLArgument(GraphQLString)}),
262+
}
263+
264+
input_fields = {
265+
'field1': GraphQLInputObjectField(GraphQLString),
266+
'field2': GraphQLInputObjectField(GraphQLString),
267+
}
268+
269+
TestInputObject1 = GraphQLInputObjectType(name='Test1', fields=input_fields)
270+
TestInputObject2 = GraphQLInputObjectType(name='Test2', fields=input_fields)
271+
272+
assert TestInputObject1.get_fields() == TestInputObject2.get_fields()
273+
274+
assert input_fields == {
275+
'field1': GraphQLInputObjectField(GraphQLString),
276+
'field2': GraphQLInputObjectField(GraphQLString),
277+
}

0 commit comments

Comments
 (0)