Skip to content

Commit 4b594b1

Browse files
mervyn-mccreightstuebingerb
authored andcommitted
test: use runTest of coroutines.test
1 parent 0c50dc7 commit 4b594b1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+381
-329
lines changed

kgraphql-ktor/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ dependencies {
1111
implementation(libs.ktor.server.core)
1212

1313
testImplementation(libs.junit.jupiter.core)
14+
testImplementation(libs.kotlinx.coroutines.test)
1415
testImplementation(libs.kotest)
1516
testImplementation(libs.ktor.server.test.host)
1617
testImplementation(libs.ktor.server.auth)

kgraphql-ktor/src/test/kotlin/com/apurebase/kgraphql/KtorFeatureTest.kt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import io.ktor.http.ContentType
66
import io.ktor.http.HttpStatusCode
77
import io.ktor.http.contentType
88
import io.ktor.server.application.ApplicationCall
9+
import kotlinx.coroutines.test.runTest
910
import kotlinx.serialization.json.add
1011
import kotlinx.serialization.json.put
1112
import kotlinx.serialization.json.putJsonArray
@@ -17,7 +18,7 @@ class KtorFeatureTest : KtorTest() {
1718
data class User(val id: Int = -1, val name: String = "")
1819

1920
@Test
20-
suspend fun `Simple query test`() {
21+
fun `Simple query test`() = runTest {
2122
val server = withServer {
2223
query("hello") {
2324
resolver { -> "World!" }
@@ -33,7 +34,7 @@ class KtorFeatureTest : KtorTest() {
3334
}
3435

3536
@Test
36-
suspend fun `Simple mutation test`() {
37+
fun `Simple mutation test`() = runTest {
3738
val server = withServer {
3839
query("dummy") {
3940
resolver { -> "dummy" }
@@ -55,7 +56,7 @@ class KtorFeatureTest : KtorTest() {
5556
data class UserData(val username: String, val stuff: String)
5657

5758
@Test
58-
suspend fun `Simple context test`() {
59+
fun `Simple context test`() = runTest {
5960
val georgeName = "George"
6061
val contextSetup: ContextBuilder.(ApplicationCall) -> Unit = { _ ->
6162
+UserData(georgeName, "STUFF")
@@ -106,7 +107,7 @@ class KtorFeatureTest : KtorTest() {
106107
data class InputTwo(val one: InputOne, val quantity: Int, val tokens: List<String>)
107108

108109
@Test
109-
suspend fun `Simple variables test`() {
110+
fun `Simple variables test`() = runTest {
110111
val server = withServer {
111112
inputType<InputTwo>()
112113
query("test") { resolver { input: InputTwo -> "success: $input" } }
@@ -134,7 +135,7 @@ class KtorFeatureTest : KtorTest() {
134135
}
135136

136137
@Test
137-
suspend fun `Error response test`() {
138+
fun `Error response test`() = runTest {
138139
val server = withServer {
139140
query("actor") {
140141
resolver { -> Actor("George", 23) }

kgraphql/src/test/kotlin/com/apurebase/kgraphql/access/AccessRulesTest.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import com.apurebase.kgraphql.deserialize
77
import com.apurebase.kgraphql.expect
88
import com.apurebase.kgraphql.extract
99
import io.kotest.matchers.shouldBe
10+
import kotlinx.coroutines.test.runTest
1011
import org.junit.jupiter.api.Test
1112

1213
class AccessRulesTest {
@@ -38,7 +39,7 @@ class AccessRulesTest {
3839
}
3940

4041
@Test
41-
suspend fun `allow when matching`() {
42+
fun `allow when matching`() = runTest {
4243
val kobe = deserialize(
4344
schema.execute("{black_mamba{name}}", context = context { +"LAKERS" })
4445
).extract<String>("data/black_mamba/name")
@@ -47,7 +48,7 @@ class AccessRulesTest {
4748
}
4849

4950
@Test
50-
suspend fun `reject when not matching`() {
51+
fun `reject when not matching`() = runTest {
5152
expect<IllegalAccessException>("ILLEGAL ACCESS") {
5253
deserialize(
5354
schema.execute("{ black_mamba {id} }", context = context { +"LAKERS" })
@@ -56,12 +57,12 @@ class AccessRulesTest {
5657
}
5758

5859
@Test
59-
suspend fun `allow property resolver access rule`() {
60+
fun `allow property resolver access rule`() = runTest {
6061
deserialize(schema.execute("{white_mamba {item}}")).extract<String>("data/white_mamba/item") shouldBe "item"
6162
}
6263

6364
@Test
64-
suspend fun `reject property resolver access rule`() {
65+
fun `reject property resolver access rule`() = runTest {
6566
expect<IllegalAccessException>("ILLEGAL ACCESS") {
6667
schema.execute("{black_mamba {item}}", context = context { +"LAKERS" }).also(::println)
6768
}

kgraphql/src/test/kotlin/com/apurebase/kgraphql/configuration/SchemaConfigurationTest.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ import com.apurebase.kgraphql.ValidationException
55
import com.apurebase.kgraphql.defaultSchema
66
import com.apurebase.kgraphql.expect
77
import io.kotest.matchers.shouldBe
8+
import kotlinx.coroutines.test.runTest
89
import org.junit.jupiter.params.ParameterizedTest
910
import org.junit.jupiter.params.provider.ValueSource
1011

1112
class SchemaConfigurationTest {
1213
@ParameterizedTest
1314
@ValueSource(booleans = [true, false])
14-
suspend fun `execution result should be the same with and without caching`(withCaching: Boolean) {
15+
fun `execution result should be the same with and without caching`(withCaching: Boolean) = runTest {
1516
val schema = schema {
1617
configure {
1718
useCachingDocumentParser = withCaching
@@ -28,7 +29,7 @@ class SchemaConfigurationTest {
2829

2930
@ParameterizedTest
3031
@ValueSource(booleans = [true, false])
31-
suspend fun `execution result should use pretty printing if configured`(withPrettyPrinter: Boolean) {
32+
fun `execution result should use pretty printing if configured`(withPrettyPrinter: Boolean) = runTest {
3233
val schema = schema {
3334
configure {
3435
useDefaultPrettyPrinter = withPrettyPrinter
@@ -57,7 +58,7 @@ class SchemaConfigurationTest {
5758

5859
@ParameterizedTest
5960
@ValueSource(booleans = [true, false])
60-
suspend fun `introspections should be allowed depending on configuration`(introspectionAllowed: Boolean) {
61+
fun `introspections should be allowed depending on configuration`(introspectionAllowed: Boolean) = runTest {
6162
val schema = defaultSchema {
6263
configure {
6364
introspection = introspectionAllowed

kgraphql/src/test/kotlin/com/apurebase/kgraphql/integration/DataLoaderExecutionTest.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import com.apurebase.kgraphql.extract
66
import io.kotest.matchers.collections.shouldHaveSize
77
import kotlinx.coroutines.coroutineScope
88
import kotlinx.coroutines.delay
9+
import kotlinx.coroutines.test.runTest
910
import nidomiro.kdataloader.ExecutionResult
1011
import org.junit.jupiter.api.Test
1112
import kotlin.random.Random
@@ -69,7 +70,7 @@ class DataLoaderExecutionTest {
6970
}
7071

7172
@Test
72-
suspend fun `stress test with dataloaders and custom supervisor jobs`() {
73+
fun `stress test with dataloaders and custom supervisor jobs`() = runTest {
7374
val result = deserialize(
7475
schema.execute(
7576
"""

kgraphql/src/test/kotlin/com/apurebase/kgraphql/integration/EnumTest.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,28 @@ import com.apurebase.kgraphql.defaultSchema
66
import com.apurebase.kgraphql.deserialize
77
import com.apurebase.kgraphql.extract
88
import io.kotest.matchers.shouldBe
9+
import kotlinx.coroutines.test.runTest
910
import org.junit.jupiter.api.Test
1011

1112
class EnumTest : BaseSchemaTest() {
1213

1314
@Test
14-
suspend fun `query with enum field`() {
15+
fun `query with enum field`() = runTest {
1516
val map = execute("{film{type}}")
1617
assertNoErrors(map)
1718
map.extract<String>("data/film/type") shouldBe "FULL_LENGTH"
1819
}
1920

2021
@Test
21-
suspend fun `query with enum argument`() {
22+
fun `query with enum argument`() = runTest {
2223
val map = execute("{ films: filmsByType(type: FULL_LENGTH){title, type}}")
2324
assertNoErrors(map)
2425
map.extract<String>("data/films[0]/type") shouldBe "FULL_LENGTH"
2526
map.extract<String>("data/films[1]/type") shouldBe "FULL_LENGTH"
2627
}
2728

2829
@Test
29-
suspend fun `query with enum array variables`() {
30+
fun `query with enum array variables`() = runTest {
3031
val schema = defaultSchema {
3132
configure {
3233
wrapErrors = false

kgraphql/src/test/kotlin/com/apurebase/kgraphql/integration/FakeComplicatedDataLoad.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,4 @@ class FakeComplicatedDataLoad : CoroutineScope {
2828
"${cache1.get(delay to returnValue)}:${cache2.get(delay to returnValue)}"
2929
}.await()
3030
}
31-
3231
}

kgraphql/src/test/kotlin/com/apurebase/kgraphql/integration/InputObjectTest.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ import com.apurebase.kgraphql.KGraphQL
44
import com.apurebase.kgraphql.expect
55
import com.apurebase.kgraphql.schema.SchemaException
66
import io.kotest.matchers.shouldBe
7+
import kotlinx.coroutines.test.runTest
78
import org.junit.jupiter.api.Test
89

910
class InputObjectTest {
1011
data class Person(val name: String, val age: Int)
1112

1213
@Test
13-
suspend fun `property name should default to Kotlin name`() {
14+
fun `property name should default to Kotlin name`() = runTest {
1415
val schema = KGraphQL.schema {
1516
query("getPerson") {
1617
resolver { name: String -> Person(name = name, age = 42) }
@@ -79,7 +80,7 @@ class InputObjectTest {
7980
}
8081

8182
@Test
82-
suspend fun `property name should be configurable`() {
83+
fun `property name should be configurable`() = runTest {
8384
val schema = KGraphQL.schema {
8485
inputType<Person> {
8586
name = "PersonInput"

kgraphql/src/test/kotlin/com/apurebase/kgraphql/integration/LongScalarTest.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ import com.apurebase.kgraphql.defaultSchema
55
import com.apurebase.kgraphql.deserialize
66
import com.apurebase.kgraphql.extract
77
import io.kotest.matchers.shouldBe
8+
import kotlinx.coroutines.test.runTest
89
import org.junit.jupiter.api.Test
910

1011
class LongScalarTest {
1112

1213
@Test
13-
suspend fun testLongField() {
14+
fun testLongField() = runTest {
1415
val schema = defaultSchema {
1516
extendedScalars()
1617
query("long") {
@@ -24,7 +25,7 @@ class LongScalarTest {
2425
}
2526

2627
@Test
27-
suspend fun testLongArgument() {
28+
fun testLongArgument() = runTest {
2829
val schema = defaultSchema {
2930
extendedScalars()
3031
query("isLong") {
@@ -47,7 +48,7 @@ class LongScalarTest {
4748
data class VeryLong(val long: Long)
4849

4950
@Test
50-
suspend fun `Schema may declare custom long scalar type`() {
51+
fun `Schema may declare custom long scalar type`() = runTest {
5152
val schema = KGraphQL.schema {
5253
longScalar<VeryLong> {
5354
deserialize = ::VeryLong

kgraphql/src/test/kotlin/com/apurebase/kgraphql/integration/MutationTest.kt

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ import com.apurebase.kgraphql.assertNoErrors
77
import com.apurebase.kgraphql.expect
88
import com.apurebase.kgraphql.extract
99
import io.kotest.matchers.shouldBe
10+
import kotlinx.coroutines.test.runTest
1011
import org.junit.jupiter.api.Test
1112

1213
class MutationTest : BaseSchemaTest() {
1314

1415
private val testActor = Actor("Michael Caine", 72)
1516

1617
@Test
17-
suspend fun `simple mutation multiple fields`() {
18+
fun `simple mutation multiple fields`() = runTest {
1819
val map = execute("mutation {createActor(name: \"${testActor.name}\", age: ${testActor.age}){name, age}}")
1920
assertNoErrors(map)
2021
map.extract<Map<String, Any>>("data/createActor") shouldBe mapOf(
@@ -24,63 +25,63 @@ class MutationTest : BaseSchemaTest() {
2425
}
2526

2627
@Test
27-
suspend fun `simple mutation single field`() {
28+
fun `simple mutation single field`() = runTest {
2829
val map = execute("mutation {createActor(name: \"${testActor.name}\", age: ${testActor.age}){name}}")
2930
assertNoErrors(map)
3031
map.extract<Map<String, Any>>("data/createActor") shouldBe mapOf<String, Any>("name" to testActor.name)
3132
}
3233

3334
@Test
34-
suspend fun `simple mutation single field 2`() {
35+
fun `simple mutation single field 2`() = runTest {
3536
val map = execute("mutation {createActor(name: \"${testActor.name}\", age: ${testActor.age}){age}}")
3637
assertNoErrors(map)
3738
map.extract<Map<String, Any>>("data/createActor") shouldBe mapOf<String, Any>("age" to testActor.age)
3839
}
3940

4041
@Test
41-
suspend fun `invalid mutation name`() {
42+
fun `invalid mutation name`() = runTest {
4243
expect<ValidationException>("Property createBanana on Mutation does not exist") {
4344
execute("mutation {createBanana(name: \"${testActor.name}\", age: ${testActor.age}){age}}")
4445
}
4546
}
4647

4748
@Test
48-
suspend fun `invalid argument type`() {
49+
fun `invalid argument type`() = runTest {
4950
expect<InvalidInputValueException>("Cannot coerce \"fwfwf\" to numeric constant") {
5051
execute("mutation {createActor(name: \"${testActor.name}\", age: \"fwfwf\"){age}}")
5152
}
5253
}
5354

5455
@Test
55-
suspend fun `invalid arguments number`() {
56+
fun `invalid arguments number`() = runTest {
5657
expect<ValidationException>("createActor does support arguments [name, age]. Found arguments [name, age, bananan]") {
5758
execute("mutation {createActor(name: \"${testActor.name}\", age: ${testActor.age}, bananan: \"fwfwf\"){age}}")
5859
}
5960
}
6061

6162
@Test
62-
suspend fun `invalid arguments number with NotIntrospected class`() {
63+
fun `invalid arguments number with NotIntrospected class`() = runTest {
6364
expect<ValidationException>("createActorWithContext does support arguments [name, age]. Found arguments [name, age, bananan]") {
6465
execute("mutation {createActorWithContext(name: \"${testActor.name}\", age: ${testActor.age}, bananan: \"fwfwf\"){age}}")
6566
}
6667
}
6768

6869
@Test
69-
suspend fun `mutation with alias`() {
70+
fun `mutation with alias`() = runTest {
7071
val map = execute("mutation {caine : createActor(name: \"${testActor.name}\", age: ${testActor.age}){age}}")
7172
assertNoErrors(map)
7273
map.extract<Map<String, Any>>("data/caine") shouldBe mapOf<String, Any>("age" to testActor.age)
7374
}
7475

7576
@Test
76-
suspend fun `mutation with field alias`() {
77+
fun `mutation with field alias`() = runTest {
7778
val map = execute("mutation {createActor(name: \"${testActor.name}\", age: ${testActor.age}){howOld: age}}")
7879
assertNoErrors(map)
7980
map.extract<Map<String, Any>>("data/createActor") shouldBe mapOf<String, Any>("howOld" to testActor.age)
8081
}
8182

8283
@Test
83-
suspend fun `simple mutation with aliased input type`() {
84+
fun `simple mutation with aliased input type`() = runTest {
8485
val map = execute(
8586
"mutation(\$newActor: ActorInput!) { createActorWithAliasedInputType(newActor: \$newActor) {name}}",
8687
variables = "{\"newActor\": {\"name\": \"${testActor.name}\", \"age\": ${testActor.age}}}"

0 commit comments

Comments
 (0)