@@ -17,59 +17,50 @@ package com.tailrocks.graphql.datetime
1717
1818import com.tailrocks.graphql.datetime.DateTimeHelper.createDate
1919import graphql.language.StringValue
20- import io.kotest.core.spec.style.FunSpec
21- import io.kotest.datatest.withData
20+ import graphql.schema.CoercingParseLiteralException
21+ import graphql.schema.CoercingParseValueException
22+ import io.kotest.assertions.throwables.shouldThrow
23+ import io.kotest.core.spec.style.FreeSpec
2224import io.kotest.matchers.shouldBe
2325import java.time.ZoneOffset.UTC
2426import java.util.*
2527
2628/* *
2729 * @author Alexey Zhokhov
2830 */
29- class GraphQLDateTest : FunSpec ({
30-
31- context("parse literal") {
32- data class Item (val literal: StringValue , val result: Date )
31+ class GraphQLDateTest : FreeSpec ({
32+
33+ " parseLiteral -> success" - {
34+ listOf(
35+ StringValue ("2017-07-09T11:54:42.277Z")
36+ to createDate(2017, 7, 9, 11, 54, 42, 277),
37+ StringValue ("2017-07-09T13:14:45.947Z")
38+ to createDate(2017, 7, 9, 13, 14, 45, 947),
39+ StringValue ("2017-07-09T11:54:42Z")
40+ to createDate(2017, 7, 9, 11, 54, 42),
41+ StringValue ("2017-07-09")
42+ to createDate(2017, 7, 9)
43+ ).forEach { (literal, result) ->
44+ " parse literal ${literal.value} as $result " {
45+ GraphqlDateCoercing ().parseLiteral(literal) shouldBe result
46+ }
47+ }
48+ }
3349
34- withData(
35- nameFn = { "${it.literal} -> ${it.result}" },
36- listOf(
37- Item (
38- StringValue ("2017-07-09T11:54:42.277Z"),
39- createDate(2017, 7, 9, 11, 54, 42, 277)
40- ),
41- Item (
42- StringValue ("2017-07-09T13:14:45.947Z"),
43- createDate(2017, 7, 9, 13, 14, 45, 947),
44- ),
45- Item (
46- StringValue ("2017-07-09T11:54:42Z"),
47- createDate(2017, 7, 9, 11, 54, 42),
48- ),
49- Item (
50- StringValue ("2017-07-09"),
51- createDate(2017, 7, 9)
52- )
53- )
54- ) { (literal: StringValue , result: Date ) ->
55- GraphqlDateCoercing ().parseLiteral(literal) shouldBe result
50+ " parseLiteral -> fail" - {
51+ listOf(
52+ StringValue (""),
53+ StringValue ("not a date")
54+ ).forEach { literal ->
55+ " throws exception for input: $literal " {
56+ shouldThrow<CoercingParseLiteralException > {
57+ GraphqlDateCoercing ().parseLiteral(literal)
58+ }
59+ }
5660 }
5761 }
5862
5963 /*
60- @Unroll
61- def "Date parseLiteral throws exception for invalid #literal"() {
62- when:
63- new GraphqlDateCoercing().parseLiteral(literal)
64-
65- then:
66- thrown(CoercingParseLiteralException)
67-
68- where:
69- literal | _
70- new StringValue('') | _
71- new StringValue('not a date') | _
72- }
7364
7465@Unroll
7566def "Date serialize #value into #result (#result.class)"() {
@@ -98,38 +89,41 @@ def "serialize throws exception for invalid input #value"() {
9889 new Object() | _
9990}
10091
101- @Unroll
102- def "Date parse #value into #result (#result.class)"() {
103- expect:
104- new GraphqlDateCoercing().parseValue(value) == result
105-
106- where:
107- value | result
108- '2017-07-09T11:54:42.277Z' | createDate(2017, 7, 9, 11, 54, 42, 277)
109- '2017-07-09T13:14:45.947Z' | createDate(2017, 7, 9, 13, 14, 45, 947)
110- '2017-07-09T11:54:42Z' | createDate(2017, 7, 9, 11, 54, 42)
111- '2017-07-09' | createDate(2017, 7, 9)
112- }
92+ */
11393
114- @Unroll
115- def "parseValue throws exception for invalid input #value"() {
116- when:
117- new GraphqlDateCoercing().parseValue(value)
118- then:
119- thrown(CoercingParseValueException)
94+ " parseValue -> success" - {
95+ listOf(
96+ "2017-07-09T11:54:42.277Z"
97+ to createDate(2017, 7, 9, 11, 54, 42, 277),
98+ "2017-07-09T13:14:45.947Z"
99+ to createDate(2017, 7, 9, 13, 14, 45, 947),
100+ "2017-07-09T11:54:42Z"
101+ to createDate(2017, 7, 9, 11, 54, 42),
102+ "2017-07-09"
103+ to createDate(2017, 7, 9)
104+ ).forEach { (value, result) ->
105+ " parse $value into $result ($result .class)" {
106+ GraphqlDateCoercing ().parseValue(value) shouldBe result
107+ }
108+ }
109+ }
120110
121- where:
122- value | _
123- '' | _
124- 'not a date' | _
125- new Object() | _
126- }
127- */
111+ " parseValue -> fail" - {
112+ listOf(
113+ "",
114+ "not a date",
115+ Object ()
116+ ).forEach { value ->
117+ " throws exception for invalid input: $value " {
118+ shouldThrow<CoercingParseValueException > {
119+ GraphqlDateCoercing ().parseValue(value)
120+ }
121+ }
122+ }
123+ }
128124
129125}) {
130-
131126 init {
132127 TimeZone .setDefault(TimeZone .getTimeZone(UTC ))
133128 }
134-
135129}
0 commit comments