Skip to content

Commit a91c06f

Browse files
committed
Add tests for type comparators
Related GraphQL-js commit: graphql/graphql-js@3201ebb
1 parent c4c618c commit a91c06f

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
from collections import OrderedDict
2+
3+
from graphql.type import (GraphQLField, GraphQLFloat, GraphQLInt,
4+
GraphQLInterfaceType, GraphQLList, GraphQLNonNull,
5+
GraphQLObjectType, GraphQLSchema, GraphQLString,
6+
GraphQLUnionType)
7+
8+
from ..type_comparators import is_equal_type, is_type_sub_type_of
9+
10+
11+
def _test_schema(field_type):
12+
return GraphQLSchema(
13+
query=GraphQLObjectType(
14+
name='Query',
15+
fields=OrderedDict([
16+
('field', GraphQLField(field_type)),
17+
])
18+
)
19+
)
20+
21+
22+
def test_is_equal_type_same_reference_are_equal():
23+
assert is_equal_type(GraphQLString, GraphQLString)
24+
25+
26+
def test_is_equal_type_int_and_float_are_not_equal():
27+
assert not is_equal_type(GraphQLInt, GraphQLFloat)
28+
29+
30+
def test_is_equal_type_lists_of_same_type_are_equal():
31+
assert is_equal_type(
32+
GraphQLList(GraphQLInt),
33+
GraphQLList(GraphQLInt)
34+
)
35+
36+
37+
def test_is_equal_type_lists_is_not_equal_to_item():
38+
assert not is_equal_type(GraphQLList(GraphQLInt), GraphQLInt)
39+
40+
41+
def test_is_equal_type_nonnull_of_same_type_are_equal():
42+
assert is_equal_type(
43+
GraphQLNonNull(GraphQLInt),
44+
GraphQLNonNull(GraphQLInt)
45+
)
46+
47+
48+
def test_is_equal_type_nonnull_is_not_equal_to_nullable():
49+
assert not is_equal_type(GraphQLNonNull(GraphQLInt), GraphQLInt)
50+
51+
52+
def test_is_equal_type_nonnull_is_not_equal_to_nullable():
53+
assert not is_equal_type(GraphQLNonNull(GraphQLInt), GraphQLInt)
54+
55+
56+
def test_is_type_sub_type_of_same_reference_is_subtype():
57+
schema = _test_schema(GraphQLString)
58+
assert is_type_sub_type_of(schema, GraphQLString, GraphQLString)
59+
60+
61+
def test_is_type_sub_type_of_int_is_not_subtype_of_float():
62+
schema = _test_schema(GraphQLString)
63+
assert not is_type_sub_type_of(schema, GraphQLInt, GraphQLFloat)
64+
65+
66+
def test_is_type_sub_type_of_non_null_is_subtype_of_nullable():
67+
schema = _test_schema(GraphQLString)
68+
assert is_type_sub_type_of(schema, GraphQLNonNull(GraphQLInt), GraphQLInt)
69+
70+
71+
def test_is_type_sub_type_of_nullable_is_not_subtype_of_non_null():
72+
schema = _test_schema(GraphQLString)
73+
assert not is_type_sub_type_of(schema, GraphQLInt, GraphQLNonNull(GraphQLInt))
74+
75+
76+
def test_is_type_sub_type_of_item_is_not_subtype_of_list():
77+
schema = _test_schema(GraphQLString)
78+
assert not is_type_sub_type_of(schema, GraphQLInt, GraphQLList(GraphQLInt))
79+
80+
81+
def test_is_type_sub_type_of_list_is_not_subtype_of_item():
82+
schema = _test_schema(GraphQLString)
83+
assert not is_type_sub_type_of(schema, GraphQLList(GraphQLInt), GraphQLInt)
84+
85+
86+
def test_is_type_sub_type_of_member_is_subtype_of_union():
87+
member = GraphQLObjectType(
88+
name='Object',
89+
is_type_of=lambda *_: True,
90+
fields={
91+
'field': GraphQLField(GraphQLString)
92+
}
93+
)
94+
union = GraphQLUnionType(name='Union', types=[member])
95+
schema = _test_schema(union)
96+
assert is_type_sub_type_of(schema, member, union)
97+
98+
99+
def test_is_type_sub_type_of_implementation_is_subtype_of_interface():
100+
iface = GraphQLInterfaceType(
101+
name='Interface',
102+
fields={
103+
'field': GraphQLField(GraphQLString)
104+
}
105+
)
106+
impl = GraphQLObjectType(
107+
name='Object',
108+
is_type_of=lambda *_: True,
109+
interfaces=[iface],
110+
fields={
111+
'field': GraphQLField(GraphQLString)
112+
}
113+
)
114+
schema = _test_schema(impl)
115+
assert is_type_sub_type_of(schema, impl, iface)

0 commit comments

Comments
 (0)