-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathDateTimeScalarTest.groovy
More file actions
139 lines (120 loc) · 6.66 KB
/
DateTimeScalarTest.groovy
File metadata and controls
139 lines (120 loc) · 6.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package graphql.scalars.datetime
import graphql.language.StringValue
import graphql.scalars.ExtendedScalars
import graphql.scalars.util.AbstractScalarTest
import graphql.schema.CoercingParseLiteralException
import graphql.schema.CoercingParseValueException
import graphql.schema.CoercingSerializeException
import spock.lang.Unroll
import static graphql.scalars.util.TestKit.mkIntValue
import static graphql.scalars.util.TestKit.mkLocalDT
import static graphql.scalars.util.TestKit.mkOffsetDT
import static graphql.scalars.util.TestKit.mkStringValue
import static graphql.scalars.util.TestKit.mkZonedDT
class DateTimeScalarTest extends AbstractScalarTest {
def coercing = ExtendedScalars.DateTime.getCoercing()
@Unroll
def "datetime parseValue"() {
when:
def result = coercing.parseValue(input, graphQLContext, locale)
then:
result == expectedValue
where:
input | expectedValue
"1985-04-12T23:20:50.52Z" | mkOffsetDT("1985-04-12T23:20:50.52Z")
"1996-12-19T16:39:57-08:00" | mkOffsetDT("1996-12-19T16:39:57-08:00")
"1937-01-01T12:00:27.87+00:20" | mkOffsetDT("1937-01-01T12:00:27.87+00:20")
"2022-11-24T01:00:01.02+00:00" | mkOffsetDT("2022-11-24T01:00:01.02+00:00")
mkOffsetDT(year: 1980, hour: 3) | mkOffsetDT("1980-08-08T03:10:09+10:00")
mkZonedDT(year: 1980, hour: 3) | mkOffsetDT("1980-08-08T03:10:09+10:00")
}
@Unroll
def "datetime valueToLiteral"() {
when:
def result = coercing.valueToLiteral(input, graphQLContext, locale)
then:
result.isEqualTo(expectedValue)
where:
input | expectedValue
"1985-04-12T23:20:50.52Z" | mkStringValue("1985-04-12T23:20:50.520Z")
"1996-12-19T16:39:57-08:00" | mkStringValue("1996-12-19T16:39:57.000-08:00")
"1937-01-01T12:00:27.87+00:20" | mkStringValue("1937-01-01T12:00:27.870+00:20")
"1937-01-01T12:00+00:20" | mkStringValue("1937-01-01T12:00:00.000+00:20")
"2022-11-24T01:00:01.02+00:00" | mkStringValue("2022-11-24T01:00:01.020Z")
mkOffsetDT(year: 1980, hour: 3) | mkStringValue("1980-08-08T03:10:09.000+10:00")
mkZonedDT(year: 1980, hour: 3) | mkStringValue("1980-08-08T03:10:09.000+10:00")
}
@Unroll
def "datetime parseValue bad inputs"() {
when:
coercing.parseValue(input, graphQLContext, locale)
then:
thrown(expectedValue)
where:
input | expectedValue
"1985-04-12" | CoercingParseValueException // No time provided
"2022-11-24T01:00:01.02-00:00" | CoercingParseValueException // -00:00 is not a valid offset in specification
mkLocalDT(year: 1980, hour: 3) | CoercingParseValueException // LocalDateTime has no time zone
666 | CoercingParseValueException // A random number
"2011-08-30T13:22:53.108" | CoercingParseValueException // No offset provided
"2011-08-30T24:22:53.108Z" | CoercingParseValueException // 24 is not allowed as hour of the time
"2010-02-30T21:22:53.108Z" | CoercingParseValueException // 30th of February is not a valid date
"2010-02-11T21:22:53.108+25:11" | CoercingParseValueException // 25 is not a valid hour for offset
}
def "datetime AST literal"() {
when:
def result = coercing.parseLiteral(input, variables, graphQLContext, locale)
then:
result == expectedValue
where:
input | expectedValue
new StringValue("1985-04-12T23:20:50.52Z") | mkOffsetDT("1985-04-12T23:20:50.52Z")
}
def "datetime serialisation"() {
when:
def result = coercing.serialize(input, graphQLContext, locale)
then:
result == expectedValue
where:
input | expectedValue
"1985-04-12T23:20:50.52Z" | "1985-04-12T23:20:50.520Z"
"1996-12-19T16:39:57-08:00" | "1996-12-19T16:39:57.000-08:00"
"1937-01-01T12:00:27.87+00:20" | "1937-01-01T12:00:27.870+00:20"
"2022-11-24T01:00:01.02+00:00" | "2022-11-24T01:00:01.020Z"
mkOffsetDT(year: 1980, hour: 3) | "1980-08-08T03:10:09.000+10:00"
mkZonedDT(year: 1980, hour: 3) | "1980-08-08T03:10:09.000+10:00"
}
def "datetime serialisation bad inputs"() {
when:
coercing.serialize(input, graphQLContext, locale)
then:
thrown(expectedValue)
where:
input | expectedValue
"1985-04-12" | CoercingSerializeException // No time provided
"2022-11-24T01:00:01.02-00:00" | CoercingSerializeException // -00:00 is not a valid offset in specification
mkLocalDT(year: 1980, hour: 3) | CoercingSerializeException // LocalDateTime has no time zone
666 | CoercingSerializeException // A random number
"2011-08-30T13:22:53.108" | CoercingSerializeException // No offset provided
"2011-08-30T24:22:53.108Z" | CoercingSerializeException // 24 is not allowed as hour of the time
"2010-02-30T21:22:53.108Z" | CoercingSerializeException // 30th of February is not a valid date
"2010-02-11T21:22:53.108+25:11" | CoercingSerializeException // 25 is not a valid hour for offset
}
@Unroll
def "datetime parseLiteral bad inputs"() {
when:
coercing.parseLiteral(input, variables, graphQLContext, locale)
then:
thrown(expectedValue)
where:
input | expectedValue
mkStringValue("2022-11-24T01:00:01.02-00:00") | CoercingParseLiteralException // -00:00 is not a valid offset in specification
mkStringValue("1985-04-12") | CoercingParseLiteralException // No time provided
mkStringValue("2022-11-24T01:00:01.02-00:00") | CoercingParseLiteralException // -00:00 is not a valid offset in specification
mkIntValue(666) | CoercingParseLiteralException // A random number
mkStringValue("2011-08-30T13:22:53.108") | CoercingParseLiteralException // No offset provided
mkStringValue("2011-08-30T24:22:53.108Z") | CoercingParseLiteralException // 24 is not allowed as hour of the time
mkStringValue("2010-02-30T21:22:53.108Z") | CoercingParseLiteralException // 30th of February is not a valid date
mkStringValue("2010-02-11T21:22:53.108+25:11") | CoercingParseLiteralException // 25 is not a valid hour for offset
}
}