Skip to content

Commit 50a2a13

Browse files
committed
Refactor of tests to use TestUtil more consistently where I could
1 parent ac30ed1 commit 50a2a13

File tree

6 files changed

+48
-116
lines changed

6 files changed

+48
-116
lines changed

src/test/groovy/graphql/TestUtil.groovy

Lines changed: 30 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,12 @@ import graphql.schema.GraphQLInputType
1313
import graphql.schema.GraphQLObjectType
1414
import graphql.schema.GraphQLScalarType
1515
import graphql.schema.GraphQLSchema
16-
import graphql.schema.PropertyDataFetcher
1716
import graphql.schema.TypeResolver
18-
import graphql.schema.idl.FieldWiringEnvironment
19-
import graphql.schema.idl.InterfaceWiringEnvironment
17+
import graphql.schema.idl.MockedWiringFactory
2018
import graphql.schema.idl.RuntimeWiring
2119
import graphql.schema.idl.SchemaGenerator
2220
import graphql.schema.idl.SchemaParser
2321
import graphql.schema.idl.TypeRuntimeWiring
24-
import graphql.schema.idl.UnionWiringEnvironment
2522
import graphql.schema.idl.WiringFactory
2623
import graphql.schema.idl.errors.SchemaProblem
2724

@@ -61,13 +58,24 @@ class TestUtil {
6158
}
6259

6360
static GraphQLSchema schema(String spec) {
64-
schema(spec, RuntimeWiring.newRuntimeWiring().wiringFactory(mockWiringFactory).build())
61+
schema(spec, mockRuntimeWiring)
62+
}
63+
64+
static GraphQLSchema schema(Reader specReader) {
65+
schema(specReader, mockRuntimeWiring)
6566
}
6667

6768
static GraphQLSchema schemaFile(String fileName) {
68-
return schemaFile(fileName, RuntimeWiring.newRuntimeWiring().wiringFactory(mockWiringFactory).build())
69+
return schemaFile(fileName, mockRuntimeWiring)
70+
}
71+
72+
73+
static GraphQLSchema schemaFromResource(String resourceFileName, RuntimeWiring wiring) {
74+
def stream = TestUtil.class.getClassLoader().getResourceAsStream(resourceFileName)
75+
return schema(stream, wiring)
6976
}
7077

78+
7179
static GraphQLSchema schemaFile(String fileName, RuntimeWiring wiring) {
7280
def stream = TestUtil.class.getClassLoader().getResourceAsStream(fileName)
7381

@@ -78,14 +86,22 @@ class TestUtil {
7886
}
7987

8088

81-
@SuppressWarnings("GroovyMissingReturnStatement")
8289
static GraphQLSchema schema(String spec, RuntimeWiring runtimeWiring) {
90+
schema(new StringReader(spec), runtimeWiring)
91+
}
92+
93+
static GraphQLSchema schema(InputStream specStream, RuntimeWiring runtimeWiring) {
94+
schema(new InputStreamReader(specStream), runtimeWiring)
95+
}
96+
97+
static GraphQLSchema schema(Reader specReader, RuntimeWiring runtimeWiring) {
8398
try {
84-
def registry = new SchemaParser().parse(spec)
99+
def registry = new SchemaParser().parse(specReader)
85100
def options = SchemaGenerator.Options.defaultOptions().enforceSchemaDirectives(false)
86101
return new SchemaGenerator().makeExecutableSchema(options, registry, runtimeWiring)
87102
} catch (SchemaProblem e) {
88103
assert false: "The schema could not be compiled : ${e}"
104+
return null
89105
}
90106
}
91107

@@ -95,56 +111,16 @@ class TestUtil {
95111
return new SchemaGenerator().makeExecutableSchema(options, registry, runtimeWiring)
96112
} catch (SchemaProblem e) {
97113
assert false: "The schema could not be compiled : ${e}"
114+
return null
98115
}
99116
}
100117

101-
static WiringFactory mockWiringFactory = new WiringFactory() {
102-
103-
@Override
104-
boolean providesTypeResolver(InterfaceWiringEnvironment environment) {
105-
return true
106-
}
107-
108-
@Override
109-
TypeResolver getTypeResolver(InterfaceWiringEnvironment environment) {
110-
new TypeResolver() {
111-
@Override
112-
GraphQLObjectType getType(TypeResolutionEnvironment env) {
113-
throw new UnsupportedOperationException("Not implemented")
114-
}
115-
}
116-
}
117-
118-
@Override
119-
boolean providesTypeResolver(UnionWiringEnvironment environment) {
120-
return true
121-
}
122-
123-
@Override
124-
TypeResolver getTypeResolver(UnionWiringEnvironment environment) {
125-
new TypeResolver() {
126-
@Override
127-
GraphQLObjectType getType(TypeResolutionEnvironment env) {
128-
throw new UnsupportedOperationException("Not implemented")
129-
}
130-
}
131-
}
132-
133-
@Override
134-
boolean providesDataFetcher(FieldWiringEnvironment environment) {
135-
return true
136-
}
137-
138-
@Override
139-
DataFetcher getDataFetcher(FieldWiringEnvironment environment) {
140-
return new PropertyDataFetcher(environment.getFieldDefinition().getName())
141-
}
142-
}
118+
static WiringFactory mockWiringFactory = new MockedWiringFactory()
143119

144120
static RuntimeWiring mockRuntimeWiring = RuntimeWiring.newRuntimeWiring().wiringFactory(mockWiringFactory).build()
145121

146122
static GraphQLScalarType mockScalar(String name) {
147-
new GraphQLScalarType(name, name, mockCoercing());
123+
new GraphQLScalarType(name, name, mockCoercing())
148124
}
149125

150126
private static Coercing mockCoercing() {
@@ -169,10 +145,10 @@ class TestUtil {
169145
static GraphQLScalarType mockScalar(ScalarTypeDefinition definition) {
170146
new GraphQLScalarType(
171147
definition.getName(),
172-
definition.getDescription(),
148+
definition.getDescription() == null ? null : definition.getDescription().getContent(),
173149
mockCoercing(),
174150
definition.getDirectives().stream().map({ mockDirective(it.getName()) }).collect(Collectors.toList()),
175-
definition);
151+
definition)
176152
}
177153

178154
static GraphQLDirective mockDirective(String name) {
@@ -189,7 +165,7 @@ class TestUtil {
189165
}
190166
})
191167
}
192-
return builder.build();
168+
return builder.build()
193169
}
194170

195171

src/test/groovy/graphql/execution/FieldCollectorTest.groovy

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,22 @@
11
package graphql.execution
22

3+
import graphql.TestUtil
34
import graphql.language.Document
45
import graphql.language.Field
56
import graphql.language.InlineFragment
67
import graphql.language.OperationDefinition
78
import graphql.parser.Parser
89
import graphql.schema.GraphQLObjectType
9-
import graphql.schema.GraphQLSchema
10-
import graphql.schema.idl.MockedWiringFactory
11-
import graphql.schema.idl.RuntimeWiring
12-
import graphql.schema.idl.SchemaGenerator
13-
import graphql.schema.idl.SchemaParser
1410
import spock.lang.Specification
1511

1612
import static graphql.execution.FieldCollectorParameters.newParameters
1713

1814
class FieldCollectorTest extends Specification {
1915

2016

21-
GraphQLSchema createSchema(String schema) {
22-
def registry = new SchemaParser().parse(schema)
23-
return new SchemaGenerator().makeExecutableSchema(registry,
24-
RuntimeWiring.newRuntimeWiring().wiringFactory(new MockedWiringFactory()).build())
25-
}
26-
2717
def "collect fields"() {
2818
given:
29-
def schema = createSchema("""
19+
def schema = TestUtil.schema("""
3020
type Query {
3121
bar1: String
3222
bar2: String
@@ -53,7 +43,7 @@ class FieldCollectorTest extends Specification {
5343
}
5444

5545
def "collect fields on inline fragments"() {
56-
def schema = createSchema("""
46+
def schema = TestUtil.schema("""
5747
type Query{
5848
bar1: String
5949
bar2: Test

src/test/groovy/graphql/introspection/IntrospectionResultToSchemaTest.groovy

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ import graphql.language.ObjectTypeDefinition
1313
import graphql.language.UnionTypeDefinition
1414
import graphql.schema.GraphQLObjectType
1515
import graphql.schema.GraphQLSchema
16-
import graphql.schema.idl.RuntimeWiring
17-
import graphql.schema.idl.SchemaGenerator
18-
import graphql.schema.idl.SchemaParser
1916
import graphql.schema.idl.SchemaPrinter
2017
import groovy.json.JsonSlurper
2118
import spock.lang.Specification
@@ -556,9 +553,8 @@ input CharacterInput {
556553

557554
when:
558555
def printedSchema = new SchemaPrinter().print(graphQLSchema)
559-
def typeRegistry = new SchemaParser().parse(printedSchema)
560-
RuntimeWiring runtimeWiring = RuntimeWiring.newRuntimeWiring().build()
561-
GraphQLSchema schema = new SchemaGenerator().makeExecutableSchema(typeRegistry, runtimeWiring)
556+
557+
GraphQLSchema schema = TestUtil.schema(printedSchema)
562558

563559
def introspectionResult = GraphQL.newGraphQL(schema).build().execute(ExecutionInput.newExecutionInput().query(INTROSPECTION_QUERY).build())
564560
Document schemaDefinitionDocument = introspectionResultToSchema.createSchemaDefinition(introspectionResult.data as Map)

src/test/groovy/graphql/schema/idl/ExecutableStarWarsSchemaTest.groovy

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package graphql.schema.idl
22

33
import graphql.GraphQL
44
import graphql.StarWarsData
5-
import graphql.schema.GraphQLSchema
5+
import graphql.TestUtil
66
import graphql.schema.StaticDataFetcher
77
import spock.lang.Specification
88

@@ -14,15 +14,6 @@ import static TypeRuntimeWiring.newTypeWiring
1414
*/
1515
class ExecutableStarWarsSchemaTest extends Specification {
1616

17-
GraphQLSchema load(String fileName, RuntimeWiring wiring) {
18-
def stream = getClass().getClassLoader().getResourceAsStream(fileName)
19-
20-
def typeRegistry = new SchemaParser().parse(new InputStreamReader(stream))
21-
def schema = new SchemaGenerator().makeExecutableSchema(typeRegistry, wiring)
22-
schema
23-
}
24-
25-
2617
RuntimeWiring wiring = RuntimeWiring.newRuntimeWiring()
2718
.type(newTypeWiring("QueryType")
2819
.dataFetchers(
@@ -44,7 +35,7 @@ class ExecutableStarWarsSchemaTest extends Specification {
4435
)
4536
.build()
4637

47-
def executableStarWarsSchema = load("starWarsSchema.graphqls", wiring)
38+
def executableStarWarsSchema = TestUtil.schemaFromResource("starWarsSchema.graphqls", wiring)
4839

4940

5041
def 'Correctly identifies R2-D2 as the hero of the Star Wars Saga'() {

src/test/groovy/graphql/schema/idl/SchemaPrinterTest.groovy

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class SchemaPrinterTest extends Specification {
4747
.type("Node", { type -> type.typeResolver(resolver) } as UnaryOperator<TypeRuntimeWiring.Builder>)
4848
.scalar(ASTEROID)
4949
.build()
50-
GraphQLSchema schema = load("starWarsSchemaExtended.graphqls", wiring)
50+
GraphQLSchema schema = TestUtil.schemaFromResource("starWarsSchemaExtended.graphqls", wiring)
5151
schema
5252
}
5353

@@ -77,20 +77,6 @@ class SchemaPrinterTest extends Specification {
7777
}
7878
}
7979

80-
GraphQLSchema load(String fileName, RuntimeWiring wiring) {
81-
def stream = getClass().getClassLoader().getResourceAsStream(fileName)
82-
83-
def typeRegistry = new SchemaParser().parse(new InputStreamReader(stream))
84-
def schema = new SchemaGenerator().makeExecutableSchema(typeRegistry, wiring)
85-
schema
86-
}
87-
88-
GraphQLSchema generate(String spec) {
89-
def typeRegistry = new SchemaParser().parse(spec)
90-
def schema = new SchemaGenerator().makeExecutableSchema(typeRegistry, newRuntimeWiring().build())
91-
schema
92-
}
93-
9480
static class MyGraphQLObjectType extends GraphQLObjectType {
9581

9682
MyGraphQLObjectType(String name, String description, List<GraphQLFieldDefinition> fieldDefinitions) {
@@ -178,7 +164,7 @@ class SchemaPrinterTest extends Specification {
178164
}
179165

180166
def "default root names are handled"() {
181-
def schema = generate("""
167+
def schema = TestUtil.schema("""
182168
type Query {
183169
field: String
184170
}
@@ -211,7 +197,7 @@ type Subscription {
211197
}
212198

213199
def "schema prints if forced with default root names"() {
214-
def schema = generate("""
200+
def schema = TestUtil.schema("""
215201
type Query {
216202
field: String
217203
}
@@ -253,7 +239,7 @@ type Subscription {
253239

254240

255241
def "schema is printed if default root names are not ALL present"() {
256-
def schema = generate("""
242+
def schema = TestUtil.schema("""
257243
type Query {
258244
field: String
259245
}
@@ -623,7 +609,7 @@ type Query {
623609

624610

625611
def "schema will be sorted"() {
626-
def schema = generate("""
612+
def schema = TestUtil.schema("""
627613
type Query {
628614
fieldB(argZ : String, argY : Int, argX : String) : String
629615
fieldA(argZ : String, argY : Int, argX : String) : String
@@ -738,7 +724,7 @@ enum Episode {
738724

739725

740726
def "AST doc string entries are printed if present"() {
741-
def schema = generate('''
727+
def schema = TestUtil.schema('''
742728
# comments up here
743729
"""docstring"""
744730
# and comments as well down here

src/test/groovy/graphql/schema/idl/WiringFactoryTest.groovy

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package graphql.schema.idl
22

3+
import graphql.TestUtil
34
import graphql.TypeResolutionEnvironment
45
import graphql.schema.Coercing
56
import graphql.schema.DataFetcher
@@ -9,7 +10,6 @@ import graphql.schema.DataFetchingEnvironment
910
import graphql.schema.GraphQLInterfaceType
1011
import graphql.schema.GraphQLObjectType
1112
import graphql.schema.GraphQLScalarType
12-
import graphql.schema.GraphQLSchema
1313
import graphql.schema.GraphQLUnionType
1414
import graphql.schema.PropertyDataFetcher
1515
import graphql.schema.TypeResolver
@@ -125,13 +125,6 @@ class WiringFactoryTest extends Specification {
125125
}
126126

127127

128-
GraphQLSchema generateSchema(String schemaSpec, RuntimeWiring wiring) {
129-
def typeRegistry = new SchemaParser().parse(schemaSpec)
130-
def result = new SchemaGenerator().makeExecutableSchema(typeRegistry, wiring)
131-
result
132-
}
133-
134-
135128
def "ensure that wiring factory is called to resolve and create data fetchers"() {
136129

137130
def spec = """
@@ -183,7 +176,7 @@ class WiringFactoryTest extends Specification {
183176
.wiringFactory(combinedWiringFactory)
184177
.build()
185178

186-
def schema = generateSchema(spec, wiring)
179+
def schema = TestUtil.schema(spec, wiring)
187180

188181
expect:
189182

@@ -246,7 +239,7 @@ class WiringFactoryTest extends Specification {
246239
.wiringFactory(wiringFactory)
247240
.build()
248241

249-
generateSchema(spec, wiring)
242+
TestUtil.schema(spec, wiring)
250243

251244
expect:
252245

@@ -292,7 +285,7 @@ class WiringFactoryTest extends Specification {
292285
.wiringFactory(wiringFactory)
293286
.build()
294287

295-
generateSchema(spec, wiring)
288+
TestUtil.schema(spec, wiring)
296289

297290
expect:
298291

@@ -316,7 +309,7 @@ class WiringFactoryTest extends Specification {
316309
.wiringFactory(wiringFactory)
317310
.build()
318311

319-
def schema = generateSchema(spec, wiring)
312+
def schema = TestUtil.schema(spec, wiring)
320313

321314
GraphQLObjectType type = schema.getType("Query") as GraphQLObjectType
322315

0 commit comments

Comments
 (0)