|
15 | 15 | */ |
16 | 16 | package com.tailrocks.graphql.datetime |
17 | 17 |
|
| 18 | +import com.tailrocks.graphql.datetime.DateTimeHelper.createDate |
18 | 19 | import graphql.language.StringValue |
19 | | -import graphql.schema.CoercingParseLiteralException |
20 | | -import graphql.schema.CoercingParseValueException |
21 | | -import graphql.schema.CoercingSerializeException |
22 | | -import spock.lang.Specification |
23 | | -import spock.lang.Unroll |
24 | | - |
25 | | -import static com.tailrocks.graphql.datetime.DateTimeHelper.createDate |
26 | | -import static java.time.ZoneOffset.UTC |
| 20 | +import io.kotest.core.spec.style.FunSpec |
| 21 | +import io.kotest.datatest.withData |
| 22 | +import io.kotest.matchers.shouldBe |
| 23 | +import java.time.ZoneOffset.UTC |
| 24 | +import java.util.* |
27 | 25 |
|
28 | 26 | /** |
29 | 27 | * @author Alexey Zhokhov |
30 | 28 | */ |
31 | | -class GraphQLDateTest extends Specification { |
32 | | - |
33 | | - def setup() { |
34 | | - TimeZone.setDefault(TimeZone.getTimeZone(UTC)) |
| 29 | +class GraphQLDateTest : FunSpec({ |
| 30 | + |
| 31 | + context("parse literal") { |
| 32 | + data class Item(val literal: StringValue, val result: Date) |
| 33 | + |
| 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 |
| 56 | + } |
35 | 57 | } |
36 | 58 |
|
37 | | - @Unroll |
38 | | - def "Date parse literal #literal.value as #result"() { |
39 | | - expect: |
40 | | - new GraphqlDateCoercing().parseLiteral(literal) == result |
41 | | - |
42 | | - where: |
43 | | - literal | result |
44 | | - new StringValue('2017-07-09T11:54:42.277Z') | createDate(2017, 7, 9, 11, 54, 42, 277) |
45 | | - new StringValue('2017-07-09T13:14:45.947Z') | createDate(2017, 7, 9, 13, 14, 45, 947) |
46 | | - new StringValue('2017-07-09T11:54:42Z') | createDate(2017, 7, 9, 11, 54, 42) |
47 | | - new StringValue('2017-07-09') | createDate(2017, 7, 9) |
48 | | - } |
| 59 | + /* |
| 60 | +@Unroll |
| 61 | +def "Date parseLiteral throws exception for invalid #literal"() { |
| 62 | + when: |
| 63 | + new GraphqlDateCoercing().parseLiteral(literal) |
49 | 64 |
|
50 | | - @Unroll |
51 | | - def "Date parseLiteral throws exception for invalid #literal"() { |
52 | | - when: |
53 | | - new GraphqlDateCoercing().parseLiteral(literal) |
| 65 | + then: |
| 66 | + thrown(CoercingParseLiteralException) |
54 | 67 |
|
55 | | - then: |
56 | | - thrown(CoercingParseLiteralException) |
| 68 | + where: |
| 69 | + literal | _ |
| 70 | + new StringValue('') | _ |
| 71 | + new StringValue('not a date') | _ |
| 72 | +} |
57 | 73 |
|
58 | | - where: |
59 | | - literal | _ |
60 | | - new StringValue('') | _ |
61 | | - new StringValue('not a date') | _ |
62 | | - } |
| 74 | +@Unroll |
| 75 | +def "Date serialize #value into #result (#result.class)"() { |
| 76 | + expect: |
| 77 | + new GraphqlDateCoercing().serialize(value) == result |
| 78 | +
|
| 79 | + where: |
| 80 | + value | result |
| 81 | + createDate(2017, 7, 9, 11, 54, 42, 277) | '2017-07-09T11:54:42.277Z' |
| 82 | + createDate(2017, 7, 9, 13, 14, 45, 947) | '2017-07-09T13:14:45.947Z' |
| 83 | + createDate(2017, 7, 9, 11, 54, 42) | '2017-07-09T11:54:42Z' |
| 84 | + createDate(2017, 7, 9) | '2017-07-09T00:00:00Z' |
| 85 | +} |
63 | 86 |
|
64 | | - @Unroll |
65 | | - def "Date serialize #value into #result (#result.class)"() { |
66 | | - expect: |
67 | | - new GraphqlDateCoercing().serialize(value) == result |
68 | | - |
69 | | - where: |
70 | | - value | result |
71 | | - createDate(2017, 7, 9, 11, 54, 42, 277) | '2017-07-09T11:54:42.277Z' |
72 | | - createDate(2017, 7, 9, 13, 14, 45, 947) | '2017-07-09T13:14:45.947Z' |
73 | | - createDate(2017, 7, 9, 11, 54, 42) | '2017-07-09T11:54:42Z' |
74 | | - createDate(2017, 7, 9) | '2017-07-09T00:00:00Z' |
75 | | - } |
| 87 | +@Unroll |
| 88 | +def "serialize throws exception for invalid input #value"() { |
| 89 | + when: |
| 90 | + new GraphqlDateCoercing().serialize(value) |
| 91 | + then: |
| 92 | + thrown(CoercingSerializeException) |
| 93 | +
|
| 94 | + where: |
| 95 | + value | _ |
| 96 | + '' | _ |
| 97 | + 'not a date' | _ |
| 98 | + new Object() | _ |
| 99 | +} |
76 | 100 |
|
77 | | - @Unroll |
78 | | - def "serialize throws exception for invalid input #value"() { |
79 | | - when: |
80 | | - new GraphqlDateCoercing().serialize(value) |
81 | | - then: |
82 | | - thrown(CoercingSerializeException) |
83 | | - |
84 | | - where: |
85 | | - value | _ |
86 | | - '' | _ |
87 | | - 'not a date' | _ |
88 | | - new Object() | _ |
89 | | - } |
| 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 | +} |
90 | 113 |
|
91 | | - @Unroll |
92 | | - def "Date parse #value into #result (#result.class)"() { |
93 | | - expect: |
94 | | - new GraphqlDateCoercing().parseValue(value) == result |
95 | | - |
96 | | - where: |
97 | | - value | result |
98 | | - '2017-07-09T11:54:42.277Z' | createDate(2017, 7, 9, 11, 54, 42, 277) |
99 | | - '2017-07-09T13:14:45.947Z' | createDate(2017, 7, 9, 13, 14, 45, 947) |
100 | | - '2017-07-09T11:54:42Z' | createDate(2017, 7, 9, 11, 54, 42) |
101 | | - '2017-07-09' | createDate(2017, 7, 9) |
102 | | - } |
| 114 | +@Unroll |
| 115 | +def "parseValue throws exception for invalid input #value"() { |
| 116 | + when: |
| 117 | + new GraphqlDateCoercing().parseValue(value) |
| 118 | + then: |
| 119 | + thrown(CoercingParseValueException) |
| 120 | +
|
| 121 | + where: |
| 122 | + value | _ |
| 123 | + '' | _ |
| 124 | + 'not a date' | _ |
| 125 | + new Object() | _ |
| 126 | +} |
| 127 | + */ |
103 | 128 |
|
104 | | - @Unroll |
105 | | - def "parseValue throws exception for invalid input #value"() { |
106 | | - when: |
107 | | - new GraphqlDateCoercing().parseValue(value) |
108 | | - then: |
109 | | - thrown(CoercingParseValueException) |
110 | | - |
111 | | - where: |
112 | | - value | _ |
113 | | - '' | _ |
114 | | - 'not a date' | _ |
115 | | - new Object() | _ |
| 129 | +}) { |
| 130 | + |
| 131 | + init { |
| 132 | + TimeZone.setDefault(TimeZone.getTimeZone(UTC)) |
116 | 133 | } |
117 | 134 |
|
118 | 135 | } |
0 commit comments