Skip to content

Commit f2ceabc

Browse files
Adding Tests for java scalar types
1 parent cbc58e0 commit f2ceabc

File tree

6 files changed

+621
-0
lines changed

6 files changed

+621
-0
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package graphql.scalars
2+
3+
import graphql.Scalars
4+
import graphql.language.BooleanValue
5+
import graphql.language.FloatValue
6+
import graphql.language.IntValue
7+
import graphql.language.StringValue
8+
import graphql.schema.CoercingParseLiteralException
9+
import graphql.schema.CoercingParseValueException
10+
import graphql.schema.CoercingSerializeException
11+
import spock.lang.Specification
12+
import spock.lang.Unroll
13+
14+
import java.util.concurrent.atomic.AtomicInteger
15+
16+
class ScalarsBigDecimalTest extends Specification {
17+
18+
@Unroll
19+
def "BigDecimal parse literal #literal.value as #result"() {
20+
expect:
21+
Scalars.GraphQLBigDecimal.getCoercing().parseLiteral(literal) == result
22+
23+
where:
24+
literal | result
25+
new IntValue(12345678910 as BigInteger) | new BigDecimal("12345678910")
26+
new StringValue("12345678911.12") | new BigDecimal("12345678911.12")
27+
new FloatValue(new BigDecimal("42.42")) | new BigDecimal("42.42")
28+
29+
}
30+
31+
@Unroll
32+
def "BigDecimal returns null for invalid #literal"() {
33+
when:
34+
Scalars.GraphQLBigDecimal.getCoercing().parseLiteral(literal)
35+
then:
36+
thrown(CoercingParseLiteralException)
37+
38+
where:
39+
literal | _
40+
new BooleanValue(true) | _
41+
new StringValue("not a number") | _
42+
}
43+
44+
@Unroll
45+
def "BigDecimal serialize #value into #result (#result.class)"() {
46+
expect:
47+
Scalars.GraphQLBigDecimal.getCoercing().serialize(value) == result
48+
Scalars.GraphQLBigDecimal.getCoercing().parseValue(value) == result
49+
50+
where:
51+
value | result
52+
"42" | new BigDecimal("42")
53+
"42.123" | new BigDecimal("42.123")
54+
42.0000d | new BigDecimal("42.000")
55+
new Integer(42) | new BigDecimal("42")
56+
"-1" | new BigDecimal("-1")
57+
new BigInteger(42) | new BigDecimal("42")
58+
new BigDecimal("42") | new BigDecimal("42")
59+
42.3f | new BigDecimal("42.3")
60+
42.0d | new BigDecimal("42")
61+
new Byte("42") | new BigDecimal("42")
62+
new Short("42") | new BigDecimal("42")
63+
1234567l | new BigDecimal("1234567")
64+
new AtomicInteger(42) | new BigDecimal("42")
65+
}
66+
67+
@Unroll
68+
def "serialize throws exception for invalid input #value"() {
69+
when:
70+
Scalars.GraphQLBigDecimal.getCoercing().serialize(value)
71+
then:
72+
thrown(CoercingSerializeException)
73+
74+
where:
75+
value | _
76+
"" | _
77+
"not a number " | _
78+
new Object() | _
79+
}
80+
81+
@Unroll
82+
def "parseValue throws exception for invalid input #value"() {
83+
when:
84+
Scalars.GraphQLBigDecimal.getCoercing().parseValue(value)
85+
then:
86+
thrown(CoercingParseValueException)
87+
88+
where:
89+
value | _
90+
"" | _
91+
"not a number " | _
92+
new Object() | _
93+
}
94+
95+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package graphql.scalars
2+
3+
import graphql.Scalars
4+
import graphql.language.BooleanValue
5+
import graphql.language.FloatValue
6+
import graphql.language.IntValue
7+
import graphql.language.StringValue
8+
import graphql.schema.CoercingParseLiteralException
9+
import graphql.schema.CoercingParseValueException
10+
import graphql.schema.CoercingSerializeException
11+
import spock.lang.Specification
12+
import spock.lang.Unroll
13+
14+
import java.util.concurrent.atomic.AtomicInteger
15+
16+
class ScalarsBigIntegerTest extends Specification {
17+
18+
@Unroll
19+
def "BigInteger parse literal #literal.value as #result"() {
20+
expect:
21+
Scalars.GraphQLBigInteger.getCoercing().parseLiteral(literal) == result
22+
23+
where:
24+
literal | result
25+
new IntValue(12345678910 as BigInteger) | new BigInteger("12345678910")
26+
new StringValue("12345678911") | new BigInteger("12345678911")
27+
new FloatValue(new BigDecimal("42")) | new BigInteger("42")
28+
}
29+
30+
@Unroll
31+
def "BigInteger returns null for invalid #literal"() {
32+
when:
33+
Scalars.GraphQLBigInteger.getCoercing().parseLiteral(literal)
34+
then:
35+
thrown(CoercingParseLiteralException)
36+
37+
where:
38+
literal | _
39+
new BooleanValue(true) | _
40+
new StringValue("42.3") | _
41+
new FloatValue(new BigDecimal("12.12")) | _
42+
new StringValue("not a number") | _
43+
}
44+
45+
@Unroll
46+
def "BigInteger serialize #value into #result (#result.class)"() {
47+
expect:
48+
Scalars.GraphQLBigInteger.getCoercing().serialize(value) == result
49+
Scalars.GraphQLBigInteger.getCoercing().parseValue(value) == result
50+
51+
where:
52+
value | result
53+
"42" | new BigInteger("42")
54+
new Integer(42) | new BigInteger("42")
55+
"-1" | new BigInteger("-1")
56+
new BigInteger("42") | new BigInteger("42")
57+
42.0d | new BigInteger("42")
58+
new Byte("42") | new BigInteger("42")
59+
new Short("42") | new BigInteger("42")
60+
1234567l | new BigInteger("1234567")
61+
new AtomicInteger(42) | new BigInteger("42")
62+
}
63+
64+
@Unroll
65+
def "serialize throws exception for invalid input #value"() {
66+
when:
67+
Scalars.GraphQLBigInteger.getCoercing().serialize(value)
68+
then:
69+
thrown(CoercingSerializeException)
70+
71+
where:
72+
value | _
73+
"" | _
74+
"not a number " | _
75+
new BigDecimal("12.12") | _
76+
"12.12" | _
77+
new Object() | _
78+
}
79+
80+
@Unroll
81+
def "parseValue throws exception for invalid input #value"() {
82+
when:
83+
Scalars.GraphQLBigInteger.getCoercing().parseValue(value)
84+
then:
85+
thrown(CoercingParseValueException)
86+
87+
where:
88+
value | _
89+
"" | _
90+
"not a number " | _
91+
new BigDecimal("12.12") | _
92+
"12.12" | _
93+
new Object() | _
94+
}
95+
96+
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package graphql.scalars
2+
3+
import graphql.Scalars
4+
import graphql.language.FloatValue
5+
import graphql.language.IntValue
6+
import graphql.language.StringValue
7+
import graphql.schema.CoercingParseLiteralException
8+
import graphql.schema.CoercingParseValueException
9+
import graphql.schema.CoercingSerializeException
10+
import spock.lang.Specification
11+
import spock.lang.Unroll
12+
13+
import java.util.concurrent.atomic.AtomicInteger
14+
15+
class ScalarsByteTest extends Specification {
16+
17+
@Unroll
18+
def "Byte parse literal #literal.value as #result"() {
19+
expect:
20+
Scalars.GraphQLByte.getCoercing().parseLiteral(literal) == result
21+
22+
where:
23+
literal | result
24+
new IntValue(42 as BigInteger) | 42
25+
new IntValue(Byte.MAX_VALUE as BigInteger) | Byte.MAX_VALUE
26+
new IntValue(Byte.MIN_VALUE as BigInteger) | Byte.MIN_VALUE
27+
28+
}
29+
30+
@Unroll
31+
def "Byte returns null for invalid #literal"() {
32+
when:
33+
Scalars.GraphQLByte.getCoercing().parseLiteral(literal)
34+
then:
35+
thrown(CoercingParseLiteralException)
36+
37+
where:
38+
literal | _
39+
new IntValue(12345678910 as BigInteger) | _
40+
new StringValue("-1") | _
41+
new FloatValue(42.3) | _
42+
new IntValue(Byte.MAX_VALUE + 1l as BigInteger) | _
43+
new IntValue(Byte.MIN_VALUE - 1l as BigInteger) | _
44+
new StringValue("-1") | _
45+
new FloatValue(42.3) | _
46+
47+
}
48+
49+
@Unroll
50+
def "Byte serialize #value into #result (#result.class)"() {
51+
expect:
52+
Scalars.GraphQLByte.getCoercing().serialize(value) == result
53+
Scalars.GraphQLByte.getCoercing().parseValue(value) == result
54+
55+
where:
56+
value | result
57+
"42" | 42
58+
"42.0000" | 42
59+
42.0000d | 42
60+
new Integer(42) | 42
61+
"-1" | -1
62+
new BigInteger(42) | 42
63+
new BigDecimal("42") | 42
64+
42.0f | 42
65+
42.0d | 42
66+
new Byte("42") | 42
67+
new Short("42") | 42
68+
123l | 123
69+
new AtomicInteger(42) | 42
70+
Byte.MAX_VALUE | Byte.MAX_VALUE
71+
Byte.MIN_VALUE | Byte.MIN_VALUE
72+
}
73+
74+
@Unroll
75+
def "serialize throws exception for invalid input #value"() {
76+
when:
77+
Scalars.GraphQLByte.getCoercing().serialize(value)
78+
then:
79+
thrown(CoercingSerializeException)
80+
81+
where:
82+
value | _
83+
"" | _
84+
"not a number " | _
85+
"42.3" | _
86+
new Long(42345784398534785l) | _
87+
new Double(42.3) | _
88+
new Float(42.3) | _
89+
Byte.MAX_VALUE + 1l | _
90+
Byte.MIN_VALUE - 1l | _
91+
new Object() | _
92+
93+
}
94+
95+
@Unroll
96+
def "parseValue throws exception for invalid input #value"() {
97+
when:
98+
Scalars.GraphQLByte.getCoercing().parseValue(value)
99+
then:
100+
thrown(CoercingParseValueException)
101+
102+
where:
103+
value | _
104+
"" | _
105+
"not a number " | _
106+
"42.3" | _
107+
new Long(42345784398534785l) | _
108+
new Double(42.3) | _
109+
new Float(42.3) | _
110+
Byte.MAX_VALUE + 1l | _
111+
Byte.MIN_VALUE - 1l | _
112+
new Object() | _
113+
114+
}
115+
116+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package graphql.scalars
2+
3+
import graphql.Scalars
4+
import graphql.language.IntValue
5+
import graphql.language.StringValue
6+
import graphql.schema.CoercingParseLiteralException
7+
import graphql.schema.CoercingParseValueException
8+
import graphql.schema.CoercingSerializeException
9+
import spock.lang.Specification
10+
import spock.lang.Unroll
11+
12+
class ScalarsCharTest extends Specification {
13+
14+
@Unroll
15+
def "Char parse literal #literal.value as #result"() {
16+
expect:
17+
Scalars.GraphQLChar.getCoercing().parseLiteral(literal) == result
18+
19+
where:
20+
literal | result
21+
new StringValue("a") | 'a'
22+
new StringValue("b") | 'b'
23+
24+
}
25+
26+
@Unroll
27+
def "Short returns null for invalid #literal"() {
28+
when:
29+
Scalars.GraphQLChar.getCoercing().parseLiteral(literal)
30+
then:
31+
thrown(CoercingParseLiteralException)
32+
33+
where:
34+
literal | _
35+
new StringValue("aa") | _
36+
new IntValue(12 as BigInteger) | _
37+
}
38+
39+
@Unroll
40+
def "Short serialize #value into #result (#result.class)"() {
41+
expect:
42+
Scalars.GraphQLChar.getCoercing().serialize(value) == result
43+
Scalars.GraphQLChar.getCoercing().parseValue(value) == result
44+
45+
where:
46+
value | result
47+
"a" | 'a'
48+
'z' | 'z'
49+
}
50+
51+
@Unroll
52+
def "serialize throws exception for invalid input #value"() {
53+
when:
54+
Scalars.GraphQLChar.getCoercing().serialize(value)
55+
then:
56+
thrown(CoercingSerializeException)
57+
58+
where:
59+
value | _
60+
"" | _
61+
"aa" | _
62+
new Object() | _
63+
64+
}
65+
66+
@Unroll
67+
def "parseValue throws exception for invalid input #value"() {
68+
when:
69+
Scalars.GraphQLChar.getCoercing().parseValue(value)
70+
then:
71+
thrown(CoercingParseValueException)
72+
73+
where:
74+
value | _
75+
"" | _
76+
"aa" | _
77+
new Object() | _
78+
79+
}
80+
81+
}

0 commit comments

Comments
 (0)