Skip to content

Commit b4da0b2

Browse files
committed
Move schema related test to test_schema + cleanup
Replicates graphql/graphql-js@0f3c177
1 parent efaad2e commit b4da0b2

File tree

2 files changed

+159
-170
lines changed

2 files changed

+159
-170
lines changed

tests/type/test_definition.py

Lines changed: 2 additions & 165 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from graphql.error import INVALID
66
from graphql.type import (
77
GraphQLArgument,
8-
GraphQLBoolean,
98
GraphQLField,
109
GraphQLFieldResolver,
1110
GraphQLInt,
@@ -22,72 +21,6 @@
2221
GraphQLOutputType,
2322
GraphQLInputField,
2423
GraphQLNonNull,
25-
is_object_type,
26-
)
27-
28-
29-
BlogImage = GraphQLObjectType(
30-
"Image",
31-
{
32-
"url": GraphQLField(GraphQLString),
33-
"width": GraphQLField(GraphQLInt),
34-
"height": GraphQLField(GraphQLInt),
35-
},
36-
)
37-
38-
39-
BlogAuthor = GraphQLObjectType(
40-
"Author",
41-
lambda: {
42-
"id": GraphQLField(GraphQLString),
43-
"name": GraphQLField(GraphQLString),
44-
"pic": GraphQLField(
45-
BlogImage,
46-
args={
47-
"width": GraphQLArgument(GraphQLInt),
48-
"height": GraphQLArgument(GraphQLInt),
49-
},
50-
),
51-
"recentArticle": GraphQLField(BlogArticle),
52-
},
53-
)
54-
55-
56-
BlogArticle = GraphQLObjectType(
57-
"Article",
58-
lambda: {
59-
"id": GraphQLField(GraphQLString),
60-
"isPublished": GraphQLField(GraphQLBoolean),
61-
"author": GraphQLField(BlogAuthor),
62-
"title": GraphQLField(GraphQLString),
63-
"body": GraphQLField(GraphQLString),
64-
},
65-
)
66-
67-
68-
BlogQuery = GraphQLObjectType(
69-
"Query",
70-
{
71-
"article": GraphQLField(
72-
BlogArticle, args={"id": GraphQLArgument(GraphQLString)}
73-
),
74-
"feed": GraphQLField(GraphQLList(BlogArticle)),
75-
},
76-
)
77-
78-
79-
BlogMutation = GraphQLObjectType(
80-
"Mutation", {"writeArticle": GraphQLField(BlogArticle)}
81-
)
82-
83-
84-
BlogSubscription = GraphQLObjectType(
85-
"Subscription",
86-
{
87-
"articleSubscribe": GraphQLField(
88-
args={"id": GraphQLArgument(GraphQLString)}, type_=BlogArticle
89-
)
90-
},
9124
)
9225

9326
ObjectType = GraphQLObjectType("Object", {})
@@ -105,47 +38,6 @@ def schema_with_field_type(type_: GraphQLOutputType) -> GraphQLSchema:
10538

10639

10740
def describe_type_system_example():
108-
def defines_a_query_only_schema():
109-
BlogSchema = GraphQLSchema(BlogQuery)
110-
111-
assert BlogSchema.query_type == BlogQuery
112-
assert is_object_type(BlogQuery)
113-
114-
article_field = BlogQuery.fields["article"]
115-
assert article_field.type == BlogArticle
116-
117-
assert is_object_type(BlogArticle)
118-
blog_article_fields = BlogArticle.fields
119-
title_field = blog_article_fields["title"]
120-
assert title_field.type == GraphQLString
121-
author_field = blog_article_fields["author"]
122-
assert author_field.type == BlogAuthor
123-
124-
assert is_object_type(BlogAuthor)
125-
recent_article_field = BlogAuthor.fields["recentArticle"]
126-
assert recent_article_field.type == BlogArticle
127-
128-
feed_field = BlogQuery.fields["feed"]
129-
assert feed_field.type.of_type == BlogArticle
130-
131-
def defines_a_mutation_schema():
132-
BlogSchema = GraphQLSchema(query=BlogQuery, mutation=BlogMutation)
133-
134-
assert BlogSchema.mutation_type == BlogMutation
135-
136-
assert is_object_type(BlogMutation)
137-
write_mutation = BlogMutation.fields["writeArticle"]
138-
assert write_mutation.type == BlogArticle
139-
140-
def defines_a_subscription_schema():
141-
BlogSchema = GraphQLSchema(query=BlogQuery, subscription=BlogSubscription)
142-
143-
assert BlogSchema.subscription_type == BlogSubscription
144-
145-
subscription = BlogSubscription.fields["articleSubscribe"]
146-
assert subscription.type == BlogArticle
147-
assert subscription.type.name == "Article"
148-
14941
def defines_an_enum_type_with_deprecated_value():
15042
EnumTypeWithDeprecatedValue = GraphQLEnumType(
15143
name="EnumWithDeprecatedValue",
@@ -200,65 +92,10 @@ def defines_an_object_type_with_deprecated_field():
20092
assert deprecated_field.type is GraphQLString
20193
assert deprecated_field.args == {}
20294

203-
def includes_nested_input_objects_in_the_map():
204-
NestedInputObject = GraphQLInputObjectType(
205-
"NestedInputObject", {"value": GraphQLInputField(GraphQLString)}
206-
)
207-
SomeInputObject = GraphQLInputObjectType(
208-
"SomeInputObject", {"nested": GraphQLInputField(NestedInputObject)}
209-
)
210-
SomeMutation = GraphQLObjectType(
211-
"SomeMutation",
212-
{
213-
"mutateSomething": GraphQLField(
214-
BlogArticle, {"input": GraphQLArgument(SomeInputObject)}
215-
)
216-
},
217-
)
218-
SomeSubscription = GraphQLObjectType(
219-
"SomeSubscription",
220-
{
221-
"subscribeToSomething": GraphQLField(
222-
BlogArticle, {"input": GraphQLArgument(SomeInputObject)}
223-
)
224-
},
225-
)
226-
schema = GraphQLSchema(
227-
query=BlogQuery, mutation=SomeMutation, subscription=SomeSubscription
228-
)
229-
assert schema.type_map["NestedInputObject"] is NestedInputObject
230-
231-
def includes_interface_possible_types_in_the_type_map():
232-
SomeInterface = GraphQLInterfaceType(
233-
"SomeInterface", {"f": GraphQLField(GraphQLInt)}
234-
)
235-
SomeSubtype = GraphQLObjectType(
236-
"SomeSubtype", {"f": GraphQLField(GraphQLInt)}, interfaces=[SomeInterface]
237-
)
238-
schema = GraphQLSchema(
239-
query=GraphQLObjectType("Query", {"iface": GraphQLField(SomeInterface)}),
240-
types=[SomeSubtype],
241-
)
242-
assert schema.type_map["SomeSubtype"] is SomeSubtype
243-
244-
def includes_interfaces_thunk_subtypes_in_the_type_map():
245-
SomeInterface = GraphQLInterfaceType(
246-
"SomeInterface", {"f": GraphQLField(GraphQLInt)}
247-
)
248-
SomeSubtype = GraphQLObjectType(
249-
"SomeSubtype",
250-
{"f": GraphQLField(GraphQLInt)},
251-
interfaces=lambda: [SomeInterface],
252-
)
253-
schema = GraphQLSchema(
254-
query=GraphQLObjectType("Query", {"iface": GraphQLField(SomeInterface)}),
255-
types=[SomeSubtype],
256-
)
257-
assert schema.type_map["SomeSubtype"] is SomeSubtype
258-
25995
def stringifies_simple_types():
26096
assert str(GraphQLInt) == "Int"
261-
assert str(BlogArticle) == "Article"
97+
assert str(ScalarType) == "Scalar"
98+
assert str(ObjectType) == "Object"
26299
assert str(InterfaceType) == "Interface"
263100
assert str(UnionType) == "Union"
264101
assert str(EnumType) == "Enum"

tests/type/test_schema.py

Lines changed: 157 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,174 @@
11
from pytest import raises
22

33
from graphql.language import DirectiveLocation
4+
from graphql.pyutils import dedent
45
from graphql.type import (
6+
GraphQLArgument,
7+
GraphQLBoolean,
8+
GraphQLDirective,
59
GraphQLField,
10+
GraphQLInputObjectType,
11+
GraphQLInputField,
12+
GraphQLInt,
13+
GraphQLInterfaceType,
14+
GraphQLList,
615
GraphQLObjectType,
716
GraphQLScalarType,
817
GraphQLSchema,
918
GraphQLString,
10-
GraphQLInputObjectType,
11-
GraphQLInputField,
12-
GraphQLDirective,
13-
GraphQLArgument,
14-
GraphQLList,
1519
)
20+
from graphql.utilities import print_schema
1621

1722

1823
def describe_type_system_schema():
24+
def define_sample_schema():
25+
BlogImage = GraphQLObjectType(
26+
"Image",
27+
{
28+
"url": GraphQLField(GraphQLString),
29+
"width": GraphQLField(GraphQLInt),
30+
"height": GraphQLField(GraphQLInt),
31+
},
32+
)
33+
34+
BlogAuthor = GraphQLObjectType(
35+
"Author",
36+
lambda: {
37+
"id": GraphQLField(GraphQLString),
38+
"name": GraphQLField(GraphQLString),
39+
"pic": GraphQLField(
40+
BlogImage,
41+
args={
42+
"width": GraphQLArgument(GraphQLInt),
43+
"height": GraphQLArgument(GraphQLInt),
44+
},
45+
),
46+
"recentArticle": GraphQLField(BlogArticle),
47+
},
48+
)
49+
50+
BlogArticle = GraphQLObjectType(
51+
"Article",
52+
lambda: {
53+
"id": GraphQLField(GraphQLString),
54+
"isPublished": GraphQLField(GraphQLBoolean),
55+
"author": GraphQLField(BlogAuthor),
56+
"title": GraphQLField(GraphQLString),
57+
"body": GraphQLField(GraphQLString),
58+
},
59+
)
60+
61+
BlogQuery = GraphQLObjectType(
62+
"Query",
63+
{
64+
"article": GraphQLField(
65+
BlogArticle, args={"id": GraphQLArgument(GraphQLString)}
66+
),
67+
"feed": GraphQLField(GraphQLList(BlogArticle)),
68+
},
69+
)
70+
71+
BlogMutation = GraphQLObjectType(
72+
"Mutation", {"writeArticle": GraphQLField(BlogArticle)}
73+
)
74+
75+
BlogSubscription = GraphQLObjectType(
76+
"Subscription",
77+
{
78+
"articleSubscribe": GraphQLField(
79+
args={"id": GraphQLArgument(GraphQLString)}, type_=BlogArticle
80+
)
81+
},
82+
)
83+
84+
schema = GraphQLSchema(BlogQuery, BlogMutation, BlogSubscription)
85+
86+
assert print_schema(schema) == dedent(
87+
"""
88+
type Article {
89+
id: String
90+
isPublished: Boolean
91+
author: Author
92+
title: String
93+
body: String
94+
}
95+
96+
type Author {
97+
id: String
98+
name: String
99+
pic(width: Int, height: Int): Image
100+
recentArticle: Article
101+
}
102+
103+
type Image {
104+
url: String
105+
width: Int
106+
height: Int
107+
}
108+
109+
type Mutation {
110+
writeArticle: Article
111+
}
112+
113+
type Query {
114+
article(id: String): Article
115+
feed: [Article]
116+
}
117+
118+
type Subscription {
119+
articleSubscribe(id: String): Article
120+
}
121+
"""
122+
)
123+
19124
def describe_type_map():
125+
def includes_interface_possible_types_in_the_type_map():
126+
SomeInterface = GraphQLInterfaceType("SomeInterface", {})
127+
SomeSubtype = GraphQLObjectType(
128+
"SomeSubtype", {}, interfaces=[SomeInterface]
129+
)
130+
schema = GraphQLSchema(
131+
query=GraphQLObjectType(
132+
"Query", {"iface": GraphQLField(SomeInterface)}
133+
),
134+
types=[SomeSubtype],
135+
)
136+
assert schema.type_map["SomeInterface"] is SomeInterface
137+
assert schema.type_map["SomeSubtype"] is SomeSubtype
138+
139+
def includes_interfaces_thunk_subtypes_in_the_type_map():
140+
SomeInterface = GraphQLInterfaceType("SomeInterface", {})
141+
SomeSubtype = GraphQLObjectType(
142+
"SomeSubtype", {}, interfaces=lambda: [SomeInterface]
143+
)
144+
schema = GraphQLSchema(
145+
query=GraphQLObjectType(
146+
"Query", {"iface": GraphQLField(SomeInterface)}
147+
),
148+
types=[SomeSubtype],
149+
)
150+
assert schema.type_map["SomeInterface"] is SomeInterface
151+
assert schema.type_map["SomeSubtype"] is SomeSubtype
152+
153+
def includes_nested_input_objects_in_the_map():
154+
NestedInputObject = GraphQLInputObjectType("NestedInputObject", {})
155+
SomeInputObject = GraphQLInputObjectType(
156+
"SomeInputObject", {"nested": GraphQLInputField(NestedInputObject)}
157+
)
158+
159+
schema = GraphQLSchema(
160+
GraphQLObjectType(
161+
"Query",
162+
{
163+
"something": GraphQLField(
164+
GraphQLString, {"input": GraphQLArgument(SomeInputObject)}
165+
)
166+
},
167+
)
168+
)
169+
assert schema.type_map["SomeInputObject"] is SomeInputObject
170+
assert schema.type_map["NestedInputObject"] is NestedInputObject
171+
20172
def includes_input_types_only_used_in_directives():
21173
directive = GraphQLDirective(
22174
name="dir",

0 commit comments

Comments
 (0)