1from graphql.type import (
2    GraphQLField,
3    GraphQLFloat,
4    GraphQLInt,
5    GraphQLInterfaceType,
6    GraphQLList,
7    GraphQLNonNull,
8    GraphQLObjectType,
9    GraphQLOutputType,
10    GraphQLSchema,
11    GraphQLString,
12    GraphQLUnionType,
13)
14from graphql.utilities import is_equal_type, is_type_sub_type_of
15
16
17def describe_type_comparators():
18    def describe_is_equal_type():
19        def same_references_are_equal():
20            assert is_equal_type(GraphQLString, GraphQLString) is True
21
22        def int_and_float_are_not_equal():
23            assert is_equal_type(GraphQLInt, GraphQLFloat) is False
24
25        def lists_of_same_type_are_equal():
26            assert (
27                is_equal_type(GraphQLList(GraphQLInt), GraphQLList(GraphQLInt)) is True
28            )
29
30        def lists_is_not_equal_to_item():
31            assert is_equal_type(GraphQLList(GraphQLInt), GraphQLInt) is False
32
33        def nonnull_of_same_type_are_equal():
34            assert (
35                is_equal_type(GraphQLNonNull(GraphQLInt), GraphQLNonNull(GraphQLInt))
36                is True
37            )
38
39        def nonnull_is_not_equal_to_nullable():
40            assert is_equal_type(GraphQLNonNull(GraphQLInt), GraphQLInt) is False
41
42    def describe_is_type_sub_type_of():
43        def _test_schema(field_type: GraphQLOutputType = GraphQLString):
44            return GraphQLSchema(
45                query=GraphQLObjectType("Query", {"field": GraphQLField(field_type)})
46            )
47
48        def same_reference_is_subtype():
49            assert (
50                is_type_sub_type_of(_test_schema(), GraphQLString, GraphQLString)
51                is True
52            )
53
54        def int_is_not_subtype_of_float():
55            assert (
56                is_type_sub_type_of(_test_schema(), GraphQLInt, GraphQLFloat) is False
57            )
58
59        def non_null_is_subtype_of_nullable():
60            assert (
61                is_type_sub_type_of(
62                    _test_schema(), GraphQLNonNull(GraphQLInt), GraphQLInt
63                )
64                is True
65            )
66
67        def nullable_is_not_subtype_of_non_null():
68            assert (
69                is_type_sub_type_of(
70                    _test_schema(), GraphQLInt, GraphQLNonNull(GraphQLInt)
71                )
72                is False
73            )
74
75        def item_is_not_subtype_of_list():
76            assert not is_type_sub_type_of(
77                _test_schema(), GraphQLInt, GraphQLList(GraphQLInt)
78            )
79
80        def list_is_not_subtype_of_item():
81            assert not is_type_sub_type_of(
82                _test_schema(), GraphQLList(GraphQLInt), GraphQLInt
83            )
84
85        def member_is_subtype_of_union():
86            member = GraphQLObjectType("Object", {"field": GraphQLField(GraphQLString)})
87            union = GraphQLUnionType("Union", [member])
88            schema = _test_schema(union)
89            assert is_type_sub_type_of(schema, member, union)
90
91        def implementing_object_is_subtype_of_interface():
92            iface = GraphQLInterfaceType(
93                "Interface", {"field": GraphQLField(GraphQLString)}
94            )
95            impl = GraphQLObjectType(
96                "Object",
97                {"field": GraphQLField(GraphQLString)},
98                [iface],
99            )
100            schema = _test_schema(impl)
101            assert is_type_sub_type_of(schema, impl, iface)
102
103        def implementing_interface_is_subtype_of_interface():
104            iface = GraphQLInterfaceType(
105                "Interface", {"field": GraphQLField(GraphQLString)}
106            )
107            iface2 = GraphQLInterfaceType(
108                "Interface2", {"field": GraphQLField(GraphQLString)}, [iface]
109            )
110            impl = GraphQLObjectType(
111                "Object",
112                {"field": GraphQLField(GraphQLString)},
113                [iface2, iface],
114            )
115            schema = _test_schema(impl)
116            assert is_type_sub_type_of(schema, iface2, iface)
117