Created
May 23, 2017 12:41
-
-
Save bbakerman/dbb6daf2a14b7df5e676cda01ac6fcc7 to your computer and use it in GitHub Desktop.
Example test for null values
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package graphql | |
import graphql.validation.ValidationError | |
import graphql.validation.ValidationErrorType | |
import spock.lang.Specification | |
/* | |
* Taken from http://facebook.github.io/graphql/#sec-Input-Objects | |
* | |
* | |
Test Case Original Value Variables Coerced Value | |
A { a: "abc", b: 123 } null { a: "abc", b: 123 } | |
B { a: 123, b: "123" } null { a: "123", b: 123 } | |
C { a: "abc" } null Error: Missing required field b | |
D { a: "abc", b: null } null Error: b must be non‐null. | |
E { a: null, b: 1 } null { a: null, b: 1 } | |
F { b: $var } { var: 123 } { b: 123 } | |
G { b: $var } {} Error: Missing required field b. | |
H { b: $var } { var: null } Error: b must be non‐null. | |
I { a: $var, b: 1 } { var: null } { a: null, b: 1 } | |
J { a: $var, b: 1 } {} { b: 1 } | |
*/ | |
class NullValueSupportTest extends Specification { | |
def graphqlSpecExamples = ''' | |
schema { | |
query : Query | |
mutation : Mutation | |
} | |
type Query { | |
a : String | |
b: Int! | |
} | |
type Mutation { | |
mutate(inputArg : ExampleInputObject) : Query | |
} | |
input ExampleInputObject { | |
a: String | |
b: Int! | |
} | |
''' | |
def "test graphql spec examples that output results"() { | |
def fetcher = new Fixtures.ArgCapturingDataFetcher() | |
def schema = TestUtil.schema(graphqlSpecExamples, ["Mutation": ["mutate": fetcher]]) | |
when: | |
def result = GraphQL.newGraphQL(schema).build().execute(queryStr, "mutate", "ctx", variables) | |
then: | |
assert result.errors.isEmpty(): "Validation Failure in case ${testCase} : $result.errors" | |
assert fetcher.args == expectedArgs: "Argument Failure in case ${testCase} : was ${fetcher.args}" | |
where: | |
testCase | queryStr | variables || expectedArgs | |
// ------------------------------ | |
'A' | ''' | |
mutation mutate { | |
mutate(inputArg : { a: "abc", b: 123 }) { | |
a | |
} | |
} | |
''' | [:] || [inputArg: [a: "abc", b: 123]] | |
// ------------------------------ | |
// coerced from string -> int and vice versus | |
// | |
// spec says it should work. Fails validation right now as WrongType | |
'B' | ''' | |
mutation mutate { | |
mutate(inputArg : { a: 123, b: "123" }) { | |
a | |
} | |
} | |
''' | [:] || [inputArg: [a: "123", b: 123]] | |
// ------------------------------ | |
'E' | ''' | |
mutation mutate { | |
mutate(inputArg : { a: null, b: 1 }) { | |
a | |
} | |
} | |
''' | [:] || [inputArg: [a: null, b: 1]] | |
// ------------------------------ | |
'F' | ''' | |
mutation mutate($var : Int!) { | |
mutate(inputArg : { b: $var }) { | |
a | |
} | |
} | |
''' | [var: 123] || [inputArg: [b: 123]] | |
// ------------------------------ | |
'I' | ''' | |
mutation mutate($var : String) { | |
mutate(inputArg : { a: $var, b: 1 }) { | |
a | |
} | |
} | |
''' | [var: null] || [inputArg: [a: null, b: 1]] | |
// ------------------------------ | |
'J' | ''' | |
mutation mutate($var : String) { | |
mutate(inputArg : { a: $var, b: 1 }) { | |
a | |
} | |
} | |
''' | [:] || [inputArg: [b: 1]] | |
} | |
def "test graphql spec examples that output errors"() { | |
def fetcher = new Fixtures.ArgCapturingDataFetcher() | |
def schema = TestUtil.schema(graphqlSpecExamples, ["Mutation": ["mutate": fetcher]]) | |
when: | |
def result = GraphQL.newGraphQL(schema).build().execute(queryStr, "mutate", "ctx", variables) | |
then: | |
assert !result.errors.isEmpty(): "Expected errors in ${testCase}" | |
result.errors[0] instanceof ValidationError | |
(result.errors[0] as ValidationError).validationErrorType == expectedError | |
where: | |
testCase | queryStr | variables || expectedError | |
// ------------------------------ | |
'C' | ''' | |
mutation mutate { | |
mutate(inputArg : { a: "abc"}) { | |
a | |
} | |
} | |
''' | [:] || ValidationErrorType.WrongType | |
// ------------------------------ | |
'D' | ''' | |
mutation mutate { | |
mutate(inputArg : { a: "abc", b: null }) { | |
a | |
} | |
} | |
''' | [:] || ValidationErrorType.WrongType | |
// ------------------------------ | |
'G' | ''' | |
mutation mutate($var : Int!) { | |
mutate(inputArg : { b: $var }) { | |
a | |
} | |
} | |
''' | [:] || ValidationErrorType.WrongType | |
// ------------------------------ | |
'H' | ''' | |
mutation mutate($var : Int!) { | |
mutate(inputArg : { b: $var }) { | |
a | |
} | |
} | |
''' | [var: null] || ValidationErrorType.WrongType | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment