@@ -19,8 +19,10 @@ import graphql.language.StringValue
1919import graphql.schema.CoercingParseLiteralException
2020import graphql.schema.CoercingParseValueException
2121import graphql.schema.CoercingSerializeException
22+ import io.kotest.assertions.throwables.shouldThrow
2223import io.kotest.core.spec.style.FreeSpec
2324import io.kotest.core.spec.style.FunSpec
25+ import io.kotest.matchers.shouldBe
2426
2527import java.time.LocalDate
2628import java.time.LocalDateTime
@@ -36,156 +38,171 @@ import java.util.concurrent.TimeUnit.MILLISECONDS
3638/* *
3739 * @author Alexey Zhokhov
3840 */
39- class GraphQLLocalDateTimeTest : FreeSpec ({
40-
41- /*
42- def setup() {
43- TimeZone.setDefault(TimeZone.getTimeZone(UTC))
44- }
45-
46- @Unroll
47- def "LocalDateTime parse literal #literal.value as #result"() {
48- expect:
49- new GraphqlLocalDateTimeCoercing(false, ISO_INSTANT).parseLiteral(literal) == result
50-
51- where:
52- literal | result
53- new StringValue("2017-07-09T11:54:42.277Z") | LocalDateTime.of(2017, 7, 9, 11, 54, 42, (int) MILLISECONDS.toNanos(277))
54- new StringValue("2017-07-09T13:14:45.947Z") | LocalDateTime.of(2017, 7, 9, 13, 14, 45, (int) MILLISECONDS.toNanos(947))
55- new StringValue("2017-07-09T11:54:42Z") | LocalDateTime.of(2017, 7, 9, 11, 54, 42)
56- new StringValue("2017-07-09T13:14:45.947") | LocalDateTime.of(2017, 7, 9, 13, 14, 45, (int) MILLISECONDS.toNanos(947))
57- new StringValue("2017-07-09T11:54:42") | LocalDateTime.of(2017, 7, 9, 11, 54, 42)
58- new StringValue("2017-07-09") | LocalDateTime.of(LocalDate.of(2017, 7, 9), LocalTime.MIDNIGHT)
59- }
60-
61- @Unroll
62- def "LocalDateTime parseLiteral throws exception for invalid #literal"() {
63- when:
64- new GraphqlLocalDateTimeCoercing(false, ISO_INSTANT).parseLiteral(literal)
65-
66- then:
67- thrown(CoercingParseLiteralException)
68-
69- where:
70- literal | _
71- new StringValue("") | _
72- new StringValue("not a localdatetime") | _
73- }
74-
75- @Unroll
76- def "LocalDateTime serialize #value into #result (#result.class)"() {
77- expect:
78- new GraphqlLocalDateTimeCoercing(false, ISO_INSTANT).serialize(value) == result
79-
80- where:
81- value | result
82- LocalDateTime.of(2017, 7, 9, 11, 54, 42, (int) MILLISECONDS.toNanos(277)) | "2017-07-09T11:54:42.277Z"
83- LocalDateTime.of(2017, 7, 9, 13, 14, 45, (int) MILLISECONDS.toNanos(947)) | "2017-07-09T13:14:45.947Z"
84- LocalDateTime.of(2017, 7, 9, 11, 54, 42) | "2017-07-09T11:54:42Z"
85- LocalDateTime.of(LocalDate.of(2017, 7, 9), LocalTime.MIDNIGHT) | "2017-07-09T00:00:00Z"
41+ class GraphQLLocalDateTimeTest : FreeSpec ({
42+
43+ " parseLiteral -> success" - {
44+ listOf(
45+ StringValue ("2017-07-09T11:54:42.277Z")
46+ to LocalDateTime .of(2017, 7, 9, 11, 54, 42, MILLISECONDS .toNanos(277).toInt()),
47+ StringValue ("2017-07-09T13:14:45.947Z")
48+ to LocalDateTime .of(2017, 7, 9, 13, 14, 45, MILLISECONDS .toNanos(947).toInt()),
49+ StringValue ("2017-07-09T11:54:42Z")
50+ to LocalDateTime .of(2017, 7, 9, 11, 54, 42),
51+ StringValue ("2017-07-09T13:14:45.947")
52+ to LocalDateTime .of(2017, 7, 9, 13, 14, 45, MILLISECONDS .toNanos(947).toInt()),
53+ StringValue ("2017-07-09T11:54:42")
54+ to LocalDateTime .of(2017, 7, 9, 11, 54, 42),
55+ StringValue ("2017-07-09")
56+ to LocalDateTime .of(LocalDate .of(2017, 7, 9), LocalTime .MIDNIGHT )
57+ ).forEach { (literal, result) ->
58+ " parse literal ${literal.value} as $result " {
59+ GraphqlLocalDateTimeCoercing (false, ISO_INSTANT ).parseLiteral(literal) shouldBe result
60+ }
61+ }
8662 }
8763
88- @Unroll
89- def "serialize throws exception for invalid input #value"() {
90- when:
91- new GraphqlLocalDateTimeCoercing(false, ISO_INSTANT).serialize(value)
92- then:
93- thrown(CoercingSerializeException)
94-
95- where:
96- value | _
97- "" | _
98- "not a localdatetime" | _
99- new Object() | _
64+ " parseLiteral -> fail" - {
65+ listOf(
66+ StringValue (""),
67+ StringValue ("not a localdatetime")
68+ ).forEach { literal ->
69+ " throws exception for invalid $literal " {
70+ shouldThrow<CoercingParseLiteralException > {
71+ GraphqlLocalDateTimeCoercing (false, ISO_INSTANT ).parseLiteral(literal)
72+ }
73+ }
74+ }
10075 }
10176
102- @Unroll
103- def "LocalDateTime parse #value into #result (#result.class)"() {
104- expect:
105- new GraphqlLocalDateTimeCoercing(false, ISO_INSTANT).parseValue(value) == result
106-
107- where:
108- value | result
109- "2017-07-09T11:54:42.277Z" | LocalDateTime.of(2017, 7, 9, 11, 54, 42, (int) MILLISECONDS.toNanos(277))
110- "2017-07-09T13:14:45.947Z" | LocalDateTime.of(2017, 7, 9, 13, 14, 45, (int) MILLISECONDS.toNanos(947))
111- "2017-07-09T11:54:42Z" | LocalDateTime.of(2017, 7, 9, 11, 54, 42)
112- "2017-07-09T13:14:45.947" | LocalDateTime.of(2017, 7, 9, 13, 14, 45, (int) MILLISECONDS.toNanos(947))
113- "2017-07-09T11:54:42" | LocalDateTime.of(2017, 7, 9, 11, 54, 42)
114- "2017-07-09" | LocalDateTime.of(LocalDate.of(2017, 7, 9), LocalTime.MIDNIGHT)
77+ " serialize -> success" - {
78+ listOf(
79+ LocalDateTime .of(2017, 7, 9, 11, 54, 42, MILLISECONDS .toNanos(277).toInt())
80+ to "2017-07-09T11:54:42.277Z",
81+ LocalDateTime .of(2017, 7, 9, 13, 14, 45, MILLISECONDS .toNanos(947).toInt())
82+ to "2017-07-09T13:14:45.947Z",
83+ LocalDateTime .of(2017, 7, 9, 11, 54, 42)
84+ to "2017-07-09T11:54:42Z",
85+ LocalDateTime .of(LocalDate .of(2017, 7, 9), LocalTime .MIDNIGHT )
86+ to "2017-07-09T00:00:00Z"
87+ ).forEach { (value, result) ->
88+ " serialize $value into $result (${result::class .java} )" {
89+ GraphqlLocalDateTimeCoercing (false, ISO_INSTANT ).serialize(value) shouldBe result
90+ }
91+ }
11592 }
11693
117- @Unroll
118- def "parseValue throws exception for invalid input #value"() {
119- when:
120- new GraphqlLocalDateTimeCoercing(false, ISO_INSTANT).parseValue(value)
121- then:
122- thrown(CoercingParseValueException)
123-
124- where:
125- value | _
126- "" | _
127- "not a localdatetime" | _
128- new Object() | _
94+ " serialize -> fail " - {
95+ listOf(
96+ "",
97+ "not a localdatetime",
98+ Object ()
99+ ).forEach { value ->
100+ " throws exception for invalid input $value " {
101+ shouldThrow< CoercingSerializeException > {
102+ GraphqlLocalDateTimeCoercing (false, ISO_INSTANT ).serialize(value)
103+ }
104+ }
105+ }
129106 }
130107
131- @Unroll
132- def "serialize #value into #result (#result.class) using zone conversion"() {
133- when:
134- TimeZone.setDefault(TimeZone.getTimeZone(ZoneOffset.ofHours(1)))
135-
136- then:
137- new GraphqlLocalDateTimeCoercing(true, ISO_INSTANT).serialize(value) == result
138-
139- where:
140- value | result
141- LocalDateTime.of(2017, 7, 9, 11, 54, 42, (int) MILLISECONDS.toNanos(277)) | "2017-07-09T10:54:42.277Z"
142- LocalDateTime.of(2017, 7, 9, 13, 14, 45, (int) MILLISECONDS.toNanos(947)) | "2017-07-09T12:14:45.947Z"
143- LocalDateTime.of(2017, 7, 9, 11, 54, 42) | "2017-07-09T10:54:42Z"
144- LocalDateTime.of(LocalDate.of(2017, 7, 9), LocalTime.MIDNIGHT.plusHours(1)) | "2017-07-09T00:00:00Z"
108+ " parseValue -> success" - {
109+ listOf(
110+ "2017-07-09T11:54:42.277Z"
111+ to LocalDateTime .of(2017, 7, 9, 11, 54, 42, MILLISECONDS .toNanos(277).toInt()),
112+ "2017-07-09T13:14:45.947Z"
113+ to LocalDateTime .of(2017, 7, 9, 13, 14, 45, MILLISECONDS .toNanos(947).toInt()),
114+ "2017-07-09T11:54:42Z"
115+ to LocalDateTime .of(2017, 7, 9, 11, 54, 42),
116+ "2017-07-09T13:14:45.947"
117+ to LocalDateTime .of(2017, 7, 9, 13, 14, 45, MILLISECONDS .toNanos(947).toInt()),
118+ "2017-07-09T11:54:42"
119+ to LocalDateTime .of(2017, 7, 9, 11, 54, 42),
120+ "2017-07-09"
121+ to LocalDateTime .of(LocalDate .of(2017, 7, 9), LocalTime .MIDNIGHT )
122+ ).forEach { (value, result) ->
123+ " parse $value into $result (${result::class .java} )" {
124+ GraphqlLocalDateTimeCoercing (false, ISO_INSTANT ).parseValue(value) shouldBe result
125+ }
126+ }
145127 }
146128
147- @Unroll
148- def "parse #value into #result (#result.class) using zone conversion"() {
149- when:
150- TimeZone.setDefault(TimeZone.getTimeZone(ZoneOffset.ofHours(1)))
151-
152- then:
153- new GraphqlLocalDateTimeCoercing(true, ISO_INSTANT).parseValue(value) == result
154-
155- where:
156- value | result
157- "2017-07-09T10:54:42.277Z" | LocalDateTime.of(2017, 7, 9, 11, 54, 42, (int) MILLISECONDS.toNanos(277))
158- "2017-07-09T12:14:45.947Z" | LocalDateTime.of(2017, 7, 9, 13, 14, 45, (int) MILLISECONDS.toNanos(947))
159- "2017-07-09T10:54:42Z" | LocalDateTime.of(2017, 7, 9, 11, 54, 42)
160- "2017-07-09" | LocalDateTime.of(LocalDate.of(2017, 7, 9), LocalTime.MIDNIGHT)
129+ " parseValue -> fail" - {
130+ listOf(
131+ "",
132+ "not a localdatetime",
133+ Object ()
134+ ).forEach { value ->
135+ " throws exception for invalid $value " {
136+ shouldThrow<CoercingParseValueException > {
137+ GraphqlLocalDateTimeCoercing (false, ISO_INSTANT ).parseValue(value)
138+ }
139+ }
140+ }
161141 }
162142
163- @Unroll
164- def "LocalDateTime parse #value into #result (#result.class) with custom formatter"() {
165- given:
166- def formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd\"T\"HH:mm:ss")
167-
168- expect:
169- new GraphqlLocalDateTimeCoercing(false, formatter).parseValue(value) == result
170-
171- where:
172- value | result
173- "1993-02-09T13:15:59" | LocalDateTime.of(1993, 2, 9, 13, 15, 59)
143+ " zone conversion" - {
144+ TimeZone .setDefault(TimeZone .getTimeZone(ZoneOffset .ofHours(1)))
145+
146+ " serialize -> success" - {
147+ listOf(
148+ LocalDateTime .of(2017, 7, 9, 11, 54, 42, MILLISECONDS .toNanos(277).toInt())
149+ to "2017-07-09T10:54:42.277Z",
150+ LocalDateTime .of(2017, 7, 9, 13, 14, 45, MILLISECONDS .toNanos(947).toInt())
151+ to "2017-07-09T12:14:45.947Z",
152+ LocalDateTime .of(2017, 7, 9, 11, 54, 42)
153+ to "2017-07-09T10:54:42Z",
154+ LocalDateTime .of(LocalDate .of(2017, 7, 9), LocalTime .MIDNIGHT .plusHours(1))
155+ to "2017-07-09T00:00:00Z"
156+ ).forEach { (value, result) ->
157+ " serialize $value into $result (${result::class .java} ) using zone conversion" {
158+ GraphqlLocalDateTimeCoercing (true, ISO_INSTANT ).serialize(value) shouldBe result
159+ }
160+ }
161+ }
162+
163+ " parseValue -> success" - {
164+ listOf(
165+ "2017-07-09T10:54:42.277Z"
166+ to LocalDateTime .of(2017, 7, 9, 11, 54, 42, MILLISECONDS .toNanos(277).toInt()),
167+ "2017-07-09T12:14:45.947Z"
168+ to LocalDateTime .of(2017, 7, 9, 13, 14, 45, MILLISECONDS .toNanos(947).toInt()),
169+ "2017-07-09T10:54:42Z"
170+ to LocalDateTime .of(2017, 7, 9, 11, 54, 42),
171+ "2017-07-09"
172+ to LocalDateTime .of(LocalDate .of(2017, 7, 9), LocalTime .MIDNIGHT )
173+ ).forEach { (value, result) ->
174+ " parse $value into $result (${result::class .java} ) using zone conversion" {
175+ GraphqlLocalDateTimeCoercing (true, ISO_INSTANT ).parseValue(value) shouldBe result
176+ }
177+ }
178+ }
174179 }
175180
176- @Unroll
177- def "Date serialize #value into #result (#result.class) with custom formatting"() {
178- given:
179- def formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd\"T\"HH:mm:ss")
180-
181- expect:
182- new GraphqlLocalDateTimeCoercing(false, formatter).serialize(value) == result
183-
184- where:
185- value | result
186- LocalDateTime.of(1993, 2, 9, 13, 15, 59) | "1993-02-09T13:15:59"
181+ " custom formatter" - {
182+ val formatter = DateTimeFormatter .ofPattern("yyyy-MM -dd'T 'HH :mm:ss")
183+
184+ " parseValue -> success" - {
185+ listOf(
186+ "1993-02-09T13:15:59"
187+ to LocalDateTime .of(1993, 2, 9, 13, 15, 59)
188+ ).forEach { (value, result) ->
189+ " parse $value into $result (${result::class .java} ) with custom formatter" {
190+ GraphqlLocalDateTimeCoercing (false, formatter).parseValue(value) shouldBe result
191+ }
192+ }
193+ }
194+
195+ " serialize -> success" - {
196+ listOf(
197+ LocalDateTime .of(1993, 2, 9, 13, 15, 59)
198+ to "1993-02-09T13:15:59"
199+ ).forEach { (value, result) ->
200+ " serialize $value into $result (${result::class .java} ) with custom formatting" {
201+ GraphqlLocalDateTimeCoercing (false, formatter).serialize(value) shouldBe result
202+ }
203+ }
204+ }
187205 }
188- */
189206
190207}) {
191208 init {
0 commit comments