Skip to content

Commit 9786db3

Browse files
committed
Migrated some tests to kotest.
1 parent 3bb451d commit 9786db3

5 files changed

Lines changed: 186 additions & 163 deletions

File tree

graphql-java-datetime/src/test/kotlin/com/tailrocks/graphql/datetime/GraphQLDurationTest.kt

Lines changed: 73 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -19,101 +19,100 @@ import graphql.language.StringValue
1919
import graphql.schema.CoercingParseLiteralException
2020
import graphql.schema.CoercingParseValueException
2121
import graphql.schema.CoercingSerializeException
22+
import io.kotest.assertions.throwables.shouldThrow
23+
import io.kotest.core.spec.style.FreeSpec
2224
import io.kotest.core.spec.style.FunSpec
25+
import io.kotest.matchers.shouldBe
2326

2427
import java.time.Duration
2528

2629
import java.time.ZoneOffset.UTC
30+
import java.util.*
2731

2832
/**
2933
* @author Alexey Zhokhov
3034
*
3135
* Test Java 8 ISO 8601 Duration
3236
*/
33-
class GraphQLDurationTest : FunSpec({
34-
35-
/*
36-
def setup() {
37-
TimeZone.setDefault(TimeZone.getTimeZone(UTC))
37+
class GraphQLDurationTest : FreeSpec({
38+
39+
"parseLiteral -> success" - {
40+
listOf(
41+
StringValue("PT1H30M") to Duration.ofMinutes(90),
42+
StringValue("P1DT3H") to Duration.ofHours(27)
43+
).forEach { (literal, result) ->
44+
"parse literal ${literal.value} as $result" {
45+
GraphqlDurationCoercing().parseLiteral(literal) shouldBe result
46+
}
47+
}
3848
}
3949

40-
@Unroll
41-
def "Duration parse literal #literal.value as #result"() {
42-
expect:
43-
new GraphqlDurationCoercing().parseLiteral(literal) == result
44-
45-
where:
46-
literal | result
47-
new StringValue('PT1H30M') | Duration.ofMinutes(90)
48-
new StringValue('P1DT3H') | Duration.ofHours(27)
50+
"parseLiteral -> fail" - {
51+
listOf(
52+
StringValue(""),
53+
StringValue("not a duration")
54+
).forEach { literal ->
55+
"throws exception for invalid $literal" {
56+
shouldThrow<CoercingParseLiteralException> {
57+
GraphqlDurationCoercing().parseLiteral(literal)
58+
}
59+
}
60+
}
4961
}
5062

51-
@Unroll
52-
def "Duration parseLiteral throws exception for invalid #literal"() {
53-
when:
54-
new GraphqlDurationCoercing().parseLiteral(literal)
55-
56-
then:
57-
thrown(CoercingParseLiteralException)
58-
59-
where:
60-
literal | _
61-
new StringValue('') | _
62-
new StringValue('not a duration') | _
63+
"serialize -> success" - {
64+
listOf(
65+
Duration.ofHours(27) to "PT27H"
66+
).forEach { (value, result) ->
67+
"serialize $value into $result (${result::class.java})" {
68+
GraphqlDurationCoercing().serialize(value) shouldBe result
69+
}
70+
}
6371
}
6472

65-
@Unroll
66-
def "Duration serialize #value into #result (#result.class)"() {
67-
expect:
68-
new GraphqlDurationCoercing().serialize(value) == result
69-
70-
where:
71-
value | result
72-
Duration.ofHours(27) | 'PT27H'
73+
"serialize -> fail" - {
74+
listOf(
75+
"",
76+
"not a duration",
77+
"1DT3H",
78+
Object()
79+
).forEach { value ->
80+
"throws exception for invalid input: $value" {
81+
shouldThrow<CoercingSerializeException> {
82+
GraphqlDurationCoercing().serialize(value)
83+
}
84+
}
85+
}
7386
}
7487

75-
@Unroll
76-
def "serialize Duration throws exception for invalid input #value"() {
77-
when:
78-
new GraphqlDurationCoercing().serialize(value)
79-
80-
then:
81-
thrown(CoercingSerializeException)
82-
83-
where:
84-
value | _
85-
'' | _
86-
'not a duration' | _
87-
'1DT3H' | _
88-
new Object() | _
88+
"parseValue -> success" - {
89+
listOf(
90+
"PT1H30M" to Duration.ofMinutes(90),
91+
"P1DT3H" to Duration.ofHours(27)
92+
).forEach { (value, result) ->
93+
"parse $value into $result (${result::class.java})" {
94+
GraphqlDurationCoercing().parseValue(value) shouldBe result
95+
}
96+
}
8997
}
9098

91-
@Unroll
92-
def "Duration parse #value into #result (#result.class)"() {
93-
expect:
94-
new GraphqlDurationCoercing().parseValue(value) == result
95-
96-
where:
97-
value | result
98-
'PT1H30M' | Duration.ofMinutes(90)
99-
'P1DT3H' | Duration.ofHours(27)
99+
"parseValue -> fail" - {
100+
listOf(
101+
"",
102+
"not a date",
103+
"1DT3H",
104+
Object()
105+
).forEach { value ->
106+
"throws exception for invalid input: $value" {
107+
shouldThrow<CoercingParseValueException> {
108+
GraphqlDurationCoercing().parseValue(value)
109+
}
110+
}
111+
}
100112
}
101113

102-
@Unroll
103-
def "Duration parseValue throws exception for invalid input #value"() {
104-
when:
105-
new GraphqlDurationCoercing().parseValue(value)
106-
107-
then:
108-
thrown(CoercingParseValueException)
109-
110-
where:
111-
value | _
112-
'' | _
113-
'not a date' | _
114-
'1DT3H' | _
115-
new Object() | _
114+
}) {
115+
init {
116+
TimeZone.setDefault(TimeZone.getTimeZone(UTC))
116117
}
117-
*/
118-
119-
})
118+
}

graphql-java-datetime/src/test/kotlin/com/tailrocks/graphql/datetime/GraphQLLocalDateTest.kt

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import graphql.language.StringValue
1919
import graphql.schema.CoercingParseLiteralException
2020
import graphql.schema.CoercingParseValueException
2121
import graphql.schema.CoercingSerializeException
22+
import io.kotest.core.spec.style.FreeSpec
2223
import io.kotest.core.spec.style.FunSpec
2324

2425
import java.time.LocalDate
@@ -27,11 +28,12 @@ import java.time.format.DateTimeFormatter
2728

2829
import java.time.ZoneOffset.UTC
2930
import java.time.format.DateTimeFormatter.ISO_LOCAL_DATE
31+
import java.util.*
3032

3133
/**
3234
* @author Alexey Zhokhov
3335
*/
34-
class GraphQLLocalDateTest : FunSpec({
36+
class GraphQLLocalDateTest : FreeSpec({
3537

3638
/*
3739
def setup() {
@@ -45,7 +47,7 @@ class GraphQLLocalDateTest : FunSpec({
4547
4648
where:
4749
literal | result
48-
new StringValue('2017-07-09') | LocalDate.of(2017, 7, 9)
50+
new StringValue("2017-07-09") | LocalDate.of(2017, 7, 9)
4951
}
5052
5153
@Unroll
@@ -58,8 +60,8 @@ class GraphQLLocalDateTest : FunSpec({
5860
5961
where:
6062
literal | _
61-
new StringValue('') | _
62-
new StringValue('not a localdate') | _
63+
new StringValue("") | _
64+
new StringValue("not a localdate") | _
6365
}
6466
6567
@Unroll
@@ -69,7 +71,7 @@ class GraphQLLocalDateTest : FunSpec({
6971
7072
where:
7173
value | result
72-
LocalDate.of(2017, 7, 9) | '2017-07-09'
74+
LocalDate.of(2017, 7, 9) | "2017-07-09"
7375
}
7476
7577
@Unroll
@@ -81,8 +83,8 @@ class GraphQLLocalDateTest : FunSpec({
8183
8284
where:
8385
value | _
84-
'' | _
85-
'not a localdate' | _
86+
"" | _
87+
"not a localdate" | _
8688
new Object() | _
8789
}
8890
@@ -93,7 +95,7 @@ class GraphQLLocalDateTest : FunSpec({
9395
9496
where:
9597
value | result
96-
'2017-07-09' | LocalDate.of(2017, 7, 9)
98+
"2017-07-09" | LocalDate.of(2017, 7, 9)
9799
}
98100
99101
@Unroll
@@ -106,8 +108,8 @@ class GraphQLLocalDateTest : FunSpec({
106108
107109
where:
108110
value | result
109-
'2019-03-01' | LocalDate.of(2019, 3, 1)
110-
'2019-03-01T22:00:00Z' | LocalDate.of(2019, 3, 2)
111+
"2019-03-01" | LocalDate.of(2019, 3, 1)
112+
"2019-03-01T22:00:00Z" | LocalDate.of(2019, 3, 2)
111113
}
112114
113115
@Unroll
@@ -119,36 +121,40 @@ class GraphQLLocalDateTest : FunSpec({
119121
120122
where:
121123
value | _
122-
'' | _
123-
'not a date' | _
124+
"" | _
125+
"not a date" | _
124126
new Object() | _
125127
}
126128
127129
@Unroll
128130
def "LocalDate parse #value into #result (#result.class) with custom formatter"() {
129131
given:
130-
def formatter = DateTimeFormatter.ofPattern('MM/dd/yyyy')
132+
def formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy")
131133
132134
expect:
133135
new GraphqlLocalDateCoercing(false, formatter).parseValue(value) == result
134136
135137
where:
136138
value | result
137-
'02/09/1993' | LocalDate.of(1993, 2, 9)
139+
"02/09/1993" | LocalDate.of(1993, 2, 9)
138140
}
139141
140142
@Unroll
141143
def "LocalDate serialize #value into #result (#result.class) with custom formatting"() {
142144
given:
143-
def formatter = DateTimeFormatter.ofPattern('MM/dd/yyyy')
145+
def formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy")
144146
145147
expect:
146148
new GraphqlLocalDateCoercing(false, formatter).serialize(value) == result
147149
148150
where:
149151
value | result
150-
LocalDate.of(2020, 7, 6) | '07/06/2020'
152+
LocalDate.of(2020, 7, 6) | "07/06/2020"
151153
}
152154
*/
153155

154-
})
156+
}) {
157+
init {
158+
TimeZone.setDefault(TimeZone.getTimeZone(UTC))
159+
}
160+
}

0 commit comments

Comments
 (0)