Skip to content

Commit d3a871b

Browse files
author
Winsor, Daniel
committed
Adding SpecValidation tests, sections 5.1 and 5.2.1
1 parent c3fbc2d commit d3a871b

5 files changed

Lines changed: 523 additions & 0 deletions

File tree

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package graphql.validation
2+
3+
import graphql.validation.SpecValidationSchema
4+
import graphql.parser.Parser
5+
import graphql.validation.ValidationError
6+
import graphql.validation.Validator
7+
import spock.lang.Requires
8+
import spock.lang.Specification
9+
10+
/**
11+
* validation examples used in the spec in section 5.1
12+
* http://facebook.github.io/graphql/#sec-Validation
13+
* @author dwinsor
14+
*
15+
*/
16+
class SpecValidation51Test extends SpecValidationBase {
17+
18+
def '5.1.1.1 Operation Name Uniqueness Valid'() {
19+
def query = """
20+
query getDogName {
21+
dog {
22+
name
23+
}
24+
}
25+
26+
query getOwnerName {
27+
dog {
28+
owner {
29+
name
30+
}
31+
}
32+
}
33+
"""
34+
when:
35+
def validationErrors = validate(query)
36+
37+
then:
38+
validationErrors.empty
39+
}
40+
41+
@Requires({SpecValidationBase.enableStrictValidation})
42+
def '5.1.1.1 Operation Name Uniqueness Not Valid'() {
43+
def query = """
44+
query getName {
45+
dog {
46+
name
47+
}
48+
}
49+
50+
query getName {
51+
dog {
52+
owner {
53+
name
54+
}
55+
}
56+
}
57+
"""
58+
when:
59+
def validationErrors = validate(query)
60+
//def result = new GraphQL(SpecValidationSchema.specValidationSchema).execute(query)
61+
62+
then:
63+
!validationErrors.empty
64+
}
65+
66+
@Requires({SpecValidationBase.enableStrictValidation})
67+
def '5.1.1.1 Operation Name Uniqueness Not Valid Different Operations'() {
68+
def query = """
69+
query dogOperation {
70+
dog {
71+
name
72+
}
73+
}
74+
75+
mutation dogOperation {
76+
mutateDog {
77+
id
78+
}
79+
}
80+
"""
81+
when:
82+
def validationErrors = validate(query)
83+
84+
then:
85+
!validationErrors.empty
86+
}
87+
88+
89+
def '5.1.2.1 Lone Anonymous Operation Valid'() {
90+
def query = """
91+
{
92+
dog {
93+
name
94+
}
95+
}
96+
"""
97+
when:
98+
def validationErrors = validate(query)
99+
100+
then:
101+
validationErrors.empty
102+
}
103+
104+
105+
@Requires({SpecValidationBase.enableStrictValidation})
106+
def '5.1.2.1 Lone Anonymous Operation Not Valid'() {
107+
def query = """
108+
{
109+
dog {
110+
name
111+
}
112+
}
113+
114+
query getName {
115+
dog {
116+
owner {
117+
name
118+
}
119+
}
120+
}
121+
"""
122+
when:
123+
def validationErrors = validate(query)
124+
125+
then:
126+
!validationErrors.empty
127+
}
128+
}
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
package graphql.validation
2+
3+
import graphql.validation.SpecValidationSchema
4+
import graphql.parser.Parser
5+
import graphql.validation.ValidationError
6+
import graphql.validation.Validator
7+
import spock.lang.Specification
8+
9+
/**
10+
* validation examples used in the spec in section 5.2.1
11+
* http://facebook.github.io/graphql/#sec-Validation
12+
* @author dwinsor
13+
*
14+
*/
15+
class SpecValidationTest extends SpecValidationBase {
16+
17+
def '5.2.1 Field Selections on ... fieldNotDefined'() {
18+
def query = """
19+
{
20+
dog {
21+
... fieldNotDefined
22+
}
23+
}
24+
fragment fieldNotDefined on Dog {
25+
meowVolume
26+
}
27+
"""
28+
when:
29+
def validationErrors = validate(query)
30+
31+
then:
32+
!validationErrors.empty
33+
validationErrors.size() == 1
34+
validationErrors.get(0).getValidationErrorType() == ValidationErrorType.FieldUndefined
35+
}
36+
37+
def '5.2.1 Field Selections on ... aliasedLyingFieldTargetNotDefined'() {
38+
def query = """
39+
{
40+
dog {
41+
... aliasedLyingFieldTargetNotDefined
42+
}
43+
}
44+
fragment aliasedLyingFieldTargetNotDefined on Dog {
45+
barkVolume: kawVolume
46+
}
47+
"""
48+
when:
49+
def validationErrors = validate(query)
50+
51+
then:
52+
!validationErrors.empty
53+
validationErrors.size() == 1
54+
validationErrors.get(0).getValidationErrorType() == ValidationErrorType.FieldUndefined
55+
}
56+
57+
def '5.2.1 Field Selections on ... interfaceFieldSelection'() {
58+
def query = """
59+
{
60+
dog {
61+
... interfaceFieldSelection
62+
}
63+
}
64+
fragment interfaceFieldSelection on Pet {
65+
name
66+
}
67+
"""
68+
when:
69+
def validationErrors = validate(query)
70+
71+
then:
72+
validationErrors.empty
73+
}
74+
75+
def '5.2.1 Field Selections on ... definedOnImplementorsButNotInterface'() {
76+
def query = """
77+
{
78+
dog {
79+
... definedOnImplementorsButNotInterface
80+
}
81+
}
82+
fragment definedOnImplementorsButNotInterface on Pet {
83+
nickname
84+
}
85+
"""
86+
when:
87+
def validationErrors = validate(query)
88+
89+
then:
90+
!validationErrors.empty
91+
validationErrors.size() == 1
92+
validationErrors.get(0).getValidationErrorType() == ValidationErrorType.FieldUndefined
93+
}
94+
95+
def '5.2.1 Field Selections on ... inDirectFieldSelectionOnUnion'() {
96+
def query = """
97+
{
98+
dog {
99+
... inDirectFieldSelectionOnUnion
100+
}
101+
}
102+
fragment inDirectFieldSelectionOnUnion on CatOrDog {
103+
__typename
104+
... on Pet {
105+
name
106+
}
107+
... on Dog {
108+
barkVolume
109+
}
110+
}
111+
"""
112+
when:
113+
def validationErrors = validate(query)
114+
115+
then:
116+
validationErrors.empty
117+
}
118+
119+
def '5.2.1 Field Selections on ... directFieldSelectionOnUnion'() {
120+
def query = """
121+
{
122+
dog {
123+
... directFieldSelectionOnUnion
124+
}
125+
}
126+
fragment directFieldSelectionOnUnion on CatOrDog {
127+
name
128+
barkVolume
129+
}
130+
"""
131+
when:
132+
def validationErrors = validate(query)
133+
134+
then:
135+
!validationErrors.empty
136+
validationErrors.size() == 2
137+
validationErrors.get(0).getValidationErrorType() == ValidationErrorType.FieldUndefined
138+
validationErrors.get(1).getValidationErrorType() == ValidationErrorType.FieldUndefined
139+
}
140+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package graphql.validation
2+
3+
import graphql.validation.SpecValidationSchema
4+
import graphql.parser.Parser
5+
import graphql.validation.ValidationError
6+
import graphql.validation.Validator
7+
import spock.lang.Specification
8+
9+
/**
10+
* validation examples used in the spec
11+
* http://facebook.github.io/graphql/#sec-Validation
12+
* @author dwinsor
13+
*
14+
*/
15+
class SpecValidationBase extends Specification {
16+
17+
public static final boolean enableStrictValidation = false;
18+
19+
List<ValidationError> validate(String query) {
20+
def document = new Parser().parseDocument(query)
21+
return new Validator().validateDocument(SpecValidationSchema.specValidationSchema, document)
22+
}
23+
}

0 commit comments

Comments
 (0)