Skip to content

Commit 739dc33

Browse files
committed
chore: add codegen tests
1 parent 2ab7b55 commit 739dc33

File tree

8 files changed

+264
-69
lines changed

8 files changed

+264
-69
lines changed

graphql-kotlin-toolkit-codegen/build.gradle.kts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ dependencies {
1414

1515
implementation(project(":graphql-kotlin-toolkit-common"))
1616

17-
testImplementation("org.jetbrains.kotlin:kotlin-test")
18-
testImplementation("org.jetbrains.kotlin:kotlin-test-junit")
19-
testImplementation("io.kotlintest:kotlintest-runner-junit5:3.3.2")
17+
testImplementation("com.github.tschuchortdev:kotlin-compile-testing:1.2.8")
18+
testImplementation("org.junit.jupiter:junit-jupiter-engine:5.6.2")
19+
testImplementation("org.junit.jupiter:junit-jupiter-params:5.6.2")
20+
testImplementation("org.jetbrains.kotlin:kotlin-reflect")
2021
}
2122

2223
tasks.dokka {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.auritylab.graphql.kotlin.toolkit.codegen._test
2+
3+
import com.auritylab.graphql.kotlin.toolkit.codegen.generator.FileGenerator
4+
import com.tschuchort.compiletesting.KotlinCompilation
5+
import com.tschuchort.compiletesting.SourceFile
6+
import kotlin.reflect.KClass
7+
8+
abstract class AbstractCompilationTest {
9+
10+
protected fun compile(generator: FileGenerator): KClass<*> {
11+
// Generate the code of the generator.
12+
val fileSpec = generator.generate()
13+
14+
// Create the source files
15+
val source = SourceFile.kotlin("Generated.kt", fileSpec.toString())
16+
17+
val compilation = KotlinCompilation()
18+
19+
compilation.sources = listOf(source)
20+
compilation.inheritClassPath = false
21+
22+
val compileResult = compilation.compile()
23+
24+
return compileResult.classLoader.loadClass(fileSpec.packageName + "." + fileSpec.name).kotlin
25+
}
26+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.auritylab.graphql.kotlin.toolkit.codegen._test
2+
3+
import com.auritylab.graphql.kotlin.toolkit.codegen.CodegenOptions
4+
import com.auritylab.graphql.kotlin.toolkit.codegen.codeblock.ArgumentCodeBlockGenerator
5+
import com.auritylab.graphql.kotlin.toolkit.codegen.mapper.GeneratedMapper
6+
import com.auritylab.graphql.kotlin.toolkit.codegen.mapper.ImplementerMapper
7+
import com.auritylab.graphql.kotlin.toolkit.codegen.mapper.KotlinTypeMapper
8+
import graphql.schema.GraphQLObjectType
9+
import graphql.schema.GraphQLSchema
10+
import java.nio.file.Path
11+
12+
internal object TestObject {
13+
val options = CodegenOptions(hashSetOf(), Path.of(""))
14+
val generatedMapper = GeneratedMapper(options)
15+
val kotlinTypeMapper = KotlinTypeMapper(options, generatedMapper)
16+
val argumentCodeBlockGenerator = ArgumentCodeBlockGenerator(kotlinTypeMapper, generatedMapper)
17+
val implementerMapper = ImplementerMapper(options, schema)
18+
19+
val schema: GraphQLSchema
20+
get() = GraphQLSchema.newSchema()
21+
.query(GraphQLObjectType.newObject().name("Query").build())
22+
.mutation(GraphQLObjectType.newObject().name("Mutation").build())
23+
.build()
24+
}
25+
26+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.auritylab.graphql.kotlin.toolkit.codegen.generator
2+
3+
import com.auritylab.graphql.kotlin.toolkit.codegen._test.AbstractCompilationTest
4+
import com.auritylab.graphql.kotlin.toolkit.codegen._test.TestObject
5+
import graphql.schema.GraphQLEnumType
6+
import org.junit.jupiter.api.Assertions
7+
import org.junit.jupiter.api.Test
8+
import kotlin.reflect.full.isSubclassOf
9+
10+
internal class EnumGeneratorTest : AbstractCompilationTest() {
11+
@Test
12+
fun shouldGenerateCompilableCode() {
13+
val generator =
14+
EnumGenerator(testEnum, TestObject.options, TestObject.kotlinTypeMapper, TestObject.generatedMapper)
15+
16+
// Compile the generator output.
17+
val generatedClass = compile(generator)
18+
19+
// Assert that the generated class is of type enum.
20+
Assertions.assertTrue(generatedClass.isSubclassOf(Enum::class))
21+
22+
val enumConstants = generatedClass.java.enumConstants as Array<Enum<*>>
23+
24+
// Assert the size of the enum constants.
25+
Assertions.assertEquals(4, enumConstants.size)
26+
27+
// Assert against the defined values.
28+
testEnum.values.forEachIndexed { i, definition ->
29+
Assertions.assertEquals(definition.name, enumConstants[i].name)
30+
}
31+
}
32+
}
33+
34+
private val testEnum = GraphQLEnumType.newEnum()
35+
.name("ETestEnum")
36+
.value("first")
37+
.value("second")
38+
.value("third")
39+
.value("fourth")
40+
.build()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package com.auritylab.graphql.kotlin.toolkit.codegen.generator
2+
3+
import com.auritylab.graphql.kotlin.toolkit.codegen._test.AbstractCompilationTest
4+
import com.auritylab.graphql.kotlin.toolkit.codegen._test.TestObject
5+
import graphql.Scalars
6+
import graphql.schema.GraphQLInputObjectField
7+
import graphql.schema.GraphQLInputObjectType
8+
import graphql.schema.GraphQLNonNull
9+
import org.junit.jupiter.api.Assertions
10+
import org.junit.jupiter.api.BeforeAll
11+
import org.junit.jupiter.api.Test
12+
import org.junit.jupiter.api.TestInstance
13+
import kotlin.reflect.KClass
14+
import kotlin.reflect.full.companionObject
15+
import kotlin.reflect.full.memberFunctions
16+
import kotlin.reflect.full.memberProperties
17+
import kotlin.reflect.full.starProjectedType
18+
19+
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
20+
internal class InputObjectGeneratorTest : AbstractCompilationTest() {
21+
val generator = InputObjectGenerator(
22+
testInputObject,
23+
TestObject.argumentCodeBlockGenerator,
24+
TestObject.options,
25+
TestObject.kotlinTypeMapper,
26+
TestObject.generatedMapper
27+
)
28+
29+
lateinit var generatedClass: KClass<*>
30+
31+
@BeforeAll
32+
fun compileCode() {
33+
// Compile the code of the generator
34+
generatedClass = compile(generator)
35+
}
36+
37+
@Test
38+
fun `should generate properties correctly`() {
39+
// Assert against the member properties.
40+
val memberProperties = generatedClass.memberProperties.toList()
41+
Assertions.assertEquals(2, memberProperties.size)
42+
Assertions.assertEquals("name", memberProperties[0].name)
43+
Assertions.assertEquals("number", memberProperties[1].name)
44+
}
45+
46+
@Test
47+
fun `should generate build method correctly`() {
48+
Assertions.assertNotNull(generatedClass.companionObject)
49+
val companionObject = generatedClass.companionObject!!
50+
51+
// Assert against the member functions.
52+
val memberFunctions = companionObject.memberFunctions
53+
54+
// Search for the builder method
55+
val builderMethod = memberFunctions.firstOrNull { it.name.endsWith("BuildByMap") }
56+
Assertions.assertNotNull(builderMethod)
57+
58+
// Cast to not null.
59+
builderMethod!!
60+
61+
// Assert that the return type of the function is the generated type.
62+
Assertions.assertEquals(generatedClass.starProjectedType, builderMethod.returnType)
63+
64+
val builderMethodParameters = builderMethod.parameters
65+
val mapBuilderMethodParameter = builderMethodParameters.firstOrNull { it.name == "map" }
66+
Assertions.assertNotNull(mapBuilderMethodParameter)
67+
}
68+
}
69+
70+
val testInputObject = GraphQLInputObjectType.newInputObject()
71+
.name("TestInput")
72+
.field(
73+
GraphQLInputObjectField.newInputObjectField()
74+
.name("name")
75+
.type(GraphQLNonNull(Scalars.GraphQLString))
76+
.build()
77+
)
78+
.field(
79+
GraphQLInputObjectField.newInputObjectField()
80+
.name("number")
81+
.type(Scalars.GraphQLInt)
82+
.build()
83+
)
84+
.build()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.auritylab.graphql.kotlin.toolkit.codegen.generator
2+
3+
import com.auritylab.graphql.kotlin.toolkit.codegen._test.AbstractCompilationTest
4+
import com.auritylab.graphql.kotlin.toolkit.codegen._test.TestObject
5+
import graphql.Scalars
6+
import graphql.schema.GraphQLFieldDefinition
7+
import graphql.schema.GraphQLObjectType
8+
import org.junit.jupiter.api.Assertions
9+
import org.junit.jupiter.api.BeforeAll
10+
import org.junit.jupiter.api.Test
11+
import org.junit.jupiter.api.TestInstance
12+
import kotlin.reflect.KClass
13+
import kotlin.reflect.full.memberProperties
14+
15+
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
16+
internal class ObjectTypeGeneratorTest : AbstractCompilationTest() {
17+
val generator = ObjectTypeGenerator(
18+
testObjectType,
19+
TestObject.options,
20+
TestObject.kotlinTypeMapper,
21+
TestObject.generatedMapper
22+
)
23+
24+
lateinit var generatedClass: KClass<*>
25+
26+
@BeforeAll
27+
fun compileCode() {
28+
// Compile the code of the generator
29+
generatedClass = compile(generator)
30+
}
31+
32+
@Test
33+
fun `should generate properties correctly`() {
34+
val memberProperties = generatedClass.memberProperties.toList()
35+
Assertions.assertEquals(2, memberProperties.size)
36+
37+
Assertions.assertEquals("name", memberProperties[0].name)
38+
Assertions.assertEquals("number", memberProperties[1].name)
39+
}
40+
}
41+
42+
val testObjectType = GraphQLObjectType.newObject()
43+
.name("TestObjectType")
44+
.field(
45+
GraphQLFieldDefinition.newFieldDefinition()
46+
.name("name")
47+
.type(Scalars.GraphQLString)
48+
).field(
49+
GraphQLFieldDefinition.newFieldDefinition()
50+
.name("number")
51+
.type(Scalars.GraphQLInt)
52+
)
53+
.build()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.auritylab.graphql.kotlin.toolkit.codegen.generator
2+
3+
import com.auritylab.graphql.kotlin.toolkit.codegen._test.AbstractCompilationTest
4+
import com.auritylab.graphql.kotlin.toolkit.codegen._test.TestObject
5+
import com.squareup.kotlinpoet.asTypeName
6+
import com.squareup.kotlinpoet.asTypeVariableName
7+
import org.junit.jupiter.api.Assertions
8+
import org.junit.jupiter.api.Test
9+
import kotlin.reflect.full.memberProperties
10+
11+
internal class ValueWrapperGeneratorTest: AbstractCompilationTest() {
12+
@Test
13+
fun shouldGenerateCompilableCode() {
14+
val generator =
15+
ValueWrapperGenerator(TestObject.options, TestObject.kotlinTypeMapper, TestObject.generatedMapper)
16+
17+
// Compile the generated code.
18+
val generatedClass = compile(generator)
19+
20+
// Assert against the member properties.
21+
val memberProperties = generatedClass.memberProperties
22+
Assertions.assertEquals(1, memberProperties.size)
23+
Assertions.assertEquals("value", memberProperties.first().name)
24+
25+
val typeParameters = generatedClass.typeParameters
26+
Assertions.assertEquals(1, typeParameters.size)
27+
28+
// Assert that the return type of the property is equal to the type parameter of the class.
29+
Assertions.assertEquals(typeParameters[0].asTypeVariableName(), memberProperties.first().returnType.asTypeName())
30+
}
31+
}

graphql-kotlin-toolkit-codegen/src/test/kotlin/com/auritylab/graphql/kotlin/toolkit/codegen/helper/GraphQLWrapTypeHelperTest.kt

Lines changed: 0 additions & 66 deletions
This file was deleted.

0 commit comments

Comments
 (0)