File: test_definition.py

package info (click to toggle)
python-graphene 3.4.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,120 kB
  • sloc: python: 8,935; makefile: 214; sh: 18
file content (330 lines) | stat: -rw-r--r-- 9,009 bytes parent folder | download
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import copy

from ..argument import Argument
from ..definitions import GrapheneGraphQLType
from ..enum import Enum
from ..field import Field
from ..inputfield import InputField
from ..inputobjecttype import InputObjectType
from ..interface import Interface
from ..objecttype import ObjectType
from ..scalars import Boolean, Int, String
from ..schema import Schema
from ..structures import List, NonNull
from ..union import Union


class Image(ObjectType):
    url = String()
    width = Int()
    height = Int()


class Author(ObjectType):
    id = String()
    name = String()
    pic = Field(Image, width=Int(), height=Int())
    recent_article = Field(lambda: Article)


class Article(ObjectType):
    id = String()
    is_published = Boolean()
    author = Field(Author)
    title = String()
    body = String()


class Query(ObjectType):
    article = Field(Article, id=String())
    feed = List(Article)


class Mutation(ObjectType):
    write_article = Field(Article)


class Subscription(ObjectType):
    article_subscribe = Field(Article, id=String())


class MyObjectType(ObjectType):
    pass


class MyInterface(Interface):
    pass


class MyUnion(Union):
    class Meta:
        types = (Article,)


class MyEnum(Enum):
    foo = "foo"


class MyInputObjectType(InputObjectType):
    pass


def test_defines_a_query_only_schema():
    blog_schema = Schema(Query)

    assert blog_schema.query == Query
    assert blog_schema.graphql_schema.query_type.graphene_type == Query

    article_field = Query._meta.fields["article"]
    assert article_field.type == Article
    assert article_field.type._meta.name == "Article"

    article_field_type = article_field.type
    assert issubclass(article_field_type, ObjectType)

    title_field = article_field_type._meta.fields["title"]
    assert title_field.type == String

    author_field = article_field_type._meta.fields["author"]
    author_field_type = author_field.type
    assert issubclass(author_field_type, ObjectType)
    recent_article_field = author_field_type._meta.fields["recent_article"]

    assert recent_article_field.type == Article

    feed_field = Query._meta.fields["feed"]
    assert feed_field.type.of_type == Article


def test_defines_a_mutation_schema():
    blog_schema = Schema(Query, mutation=Mutation)

    assert blog_schema.mutation == Mutation
    assert blog_schema.graphql_schema.mutation_type.graphene_type == Mutation

    write_mutation = Mutation._meta.fields["write_article"]
    assert write_mutation.type == Article
    assert write_mutation.type._meta.name == "Article"


def test_defines_a_subscription_schema():
    blog_schema = Schema(Query, subscription=Subscription)

    assert blog_schema.subscription == Subscription
    assert blog_schema.graphql_schema.subscription_type.graphene_type == Subscription

    subscription = Subscription._meta.fields["article_subscribe"]
    assert subscription.type == Article
    assert subscription.type._meta.name == "Article"


def test_includes_nested_input_objects_in_the_map():
    class NestedInputObject(InputObjectType):
        value = String()

    class SomeInputObject(InputObjectType):
        nested = InputField(NestedInputObject)

    class SomeMutation(Mutation):
        mutate_something = Field(Article, input=Argument(SomeInputObject))

    class SomeSubscription(Mutation):
        subscribe_to_something = Field(Article, input=Argument(SomeInputObject))

    schema = Schema(query=Query, mutation=SomeMutation, subscription=SomeSubscription)
    type_map = schema.graphql_schema.type_map

    assert type_map["NestedInputObject"].graphene_type is NestedInputObject


def test_includes_interfaces_thunk_subtypes_in_the_type_map():
    class SomeInterface(Interface):
        f = Int()

    class SomeSubtype(ObjectType):
        class Meta:
            interfaces = (SomeInterface,)

    class Query(ObjectType):
        iface = Field(lambda: SomeInterface)

    schema = Schema(query=Query, types=[SomeSubtype])
    type_map = schema.graphql_schema.type_map

    assert type_map["SomeSubtype"].graphene_type is SomeSubtype


def test_includes_types_in_union():
    class SomeType(ObjectType):
        a = String()

    class OtherType(ObjectType):
        b = String()

    class MyUnion(Union):
        class Meta:
            types = (SomeType, OtherType)

    class Query(ObjectType):
        union = Field(MyUnion)

    schema = Schema(query=Query)
    type_map = schema.graphql_schema.type_map

    assert type_map["OtherType"].graphene_type is OtherType
    assert type_map["SomeType"].graphene_type is SomeType


def test_maps_enum():
    class SomeType(ObjectType):
        a = String()

    class OtherType(ObjectType):
        b = String()

    class MyUnion(Union):
        class Meta:
            types = (SomeType, OtherType)

    class Query(ObjectType):
        union = Field(MyUnion)

    schema = Schema(query=Query)
    type_map = schema.graphql_schema.type_map

    assert type_map["OtherType"].graphene_type is OtherType
    assert type_map["SomeType"].graphene_type is SomeType


def test_includes_interfaces_subtypes_in_the_type_map():
    class SomeInterface(Interface):
        f = Int()

    class SomeSubtype(ObjectType):
        class Meta:
            interfaces = (SomeInterface,)

    class Query(ObjectType):
        iface = Field(SomeInterface)

    schema = Schema(query=Query, types=[SomeSubtype])
    type_map = schema.graphql_schema.type_map

    assert type_map["SomeSubtype"].graphene_type is SomeSubtype


def test_stringifies_simple_types():
    assert str(Int) == "Int"
    assert str(Article) == "Article"
    assert str(MyInterface) == "MyInterface"
    assert str(MyUnion) == "MyUnion"
    assert str(MyEnum) == "MyEnum"
    assert str(MyInputObjectType) == "MyInputObjectType"
    assert str(NonNull(Int)) == "Int!"
    assert str(List(Int)) == "[Int]"
    assert str(NonNull(List(Int))) == "[Int]!"
    assert str(List(NonNull(Int))) == "[Int!]"
    assert str(List(List(Int))) == "[[Int]]"


# def test_identifies_input_types():
#     expected = (
#         (GraphQLInt, True),
#         (ObjectType, False),
#         (InterfaceType, False),
#         (UnionType, False),
#         (EnumType, True),
#         (InputObjectType, True)
#     )

#     for type_, answer in expected:
#         assert is_input_type(type_) == answer
#         assert is_input_type(GraphQLList(type_)) == answer
#         assert is_input_type(GraphQLNonNull(type_)) == answer


# def test_identifies_output_types():
#     expected = (
#         (GraphQLInt, True),
#         (ObjectType, True),
#         (InterfaceType, True),
#         (UnionType, True),
#         (EnumType, True),
#         (InputObjectType, False)
#     )

#     for type, answer in expected:
#         assert is_output_type(type) == answer
#         assert is_output_type(GraphQLList(type)) == answer
#         assert is_output_type(GraphQLNonNull(type)) == answer


# def test_prohibits_nesting_nonnull_inside_nonnull():
#     with raises(Exception) as excinfo:
#         GraphQLNonNull(GraphQLNonNull(GraphQLInt))

#     assert 'Can only create NonNull of a Nullable GraphQLType but got: Int!.' in str(excinfo.value)


# def test_prohibits_putting_non_object_types_in_unions():
#     bad_union_types = [
#         GraphQLInt,
#         GraphQLNonNull(GraphQLInt),
#         GraphQLList(GraphQLInt),
#         InterfaceType,
#         UnionType,
#         EnumType,
#         InputObjectType
#     ]
#     for x in bad_union_types:
#         with raises(Exception) as excinfo:
#             GraphQLSchema(
#                 GraphQLObjectType(
#                     'Root',
#                     fields={
#                         'union': GraphQLField(GraphQLUnionType('BadUnion', [x]))
#                     }
#                 )
#             )

#         assert 'BadUnion may only contain Object types, it cannot contain: ' + str(x) + '.' \
#                == str(excinfo.value)


def test_does_not_mutate_passed_field_definitions():
    class CommonFields:
        field1 = String()
        field2 = String(id=String())

    class TestObject1(CommonFields, ObjectType):
        pass

    class TestObject2(CommonFields, ObjectType):
        pass

    assert TestObject1._meta.fields == TestObject2._meta.fields

    class CommonFields:
        field1 = String()
        field2 = String()

    class TestInputObject1(CommonFields, InputObjectType):
        pass

    class TestInputObject2(CommonFields, InputObjectType):
        pass

    assert TestInputObject1._meta.fields == TestInputObject2._meta.fields


def test_graphene_graphql_type_can_be_copied():
    class Query(ObjectType):
        field = String()

        def resolve_field(self, info):
            return ""

    schema = Schema(query=Query)
    query_type_copy = copy.copy(schema.graphql_schema.query_type)
    assert query_type_copy.__dict__ == schema.graphql_schema.query_type.__dict__
    assert isinstance(schema.graphql_schema.query_type, GrapheneGraphQLType)