Skip to content

Commit

Permalink
Set up basic compile testing of processors
Browse files Browse the repository at this point in the history
  • Loading branch information
Thijsiez committed Oct 23, 2024
1 parent 8bd4292 commit 97b1e8f
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 8 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@
# panache-kotlin-dsl
A dynamic, type-safe way to write your queries

## Requirements
- Quarkus version `3.9.2` or newer
- Kotlin version `1.9.23` or newer
- KSP version `1.9.23-1.0.20` or newer
- Your KSP version needs to match your Kotlin version. This is a strict requirement!
- For example, when your Kotlin version is `2.0.21`, your KSP version needs to be built for and start with the same version, such as `2.0.21-1.0.25`

## Features
### Queries
- Supports the following expressions in a type-safe and null-safe way for all columns
Expand All @@ -30,5 +37,6 @@ Allows you to handle no/multiple results with a `when (result) { ... }` block in
- Code generation does not handle fields of generic types (e.g. `List<E>`, `Set<E>`, etc.), but as far as I'm aware this is difficult to get working with Hibernate anyway. Consider using a JPA `@Converter` in combination with `@ColumnType`

## Planned Features
These features will be added some time in the future. Please do submit an issue if you'd like these sooner rather than later :)
- DSL for sorting expressions
- DSL for UPDATE queries
3 changes: 0 additions & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ dependencies {

testImplementation("io.quarkus:quarkus-junit5")
testImplementation("io.mockk:mockk:$mockkVersion")
testImplementation("com.github.tschuchortdev:kotlin-compile-testing:$compileTestingVersion")
testImplementation("com.github.tschuchortdev:kotlin-compile-testing-ksp:$compileTestingVersion")
}

Expand All @@ -60,11 +59,9 @@ kotlin {
}
mavenPublishing {
publishToMavenCentral(SonatypeHost.CENTRAL_PORTAL, false)

signAllPublications()

coordinates(groupId, artifactId, version)

pom {
name = artifactId
description = "A dynamic, type-safe way to write your queries"
Expand Down
3 changes: 1 addition & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ kotlinPoetVersion=1.16.0
mockkVersion=1.13.11

#https://github.com/tschuchortdev/kotlin-compile-testing
# TODO might be upgradable, test implementation needed to be sure
compileTestingVersion=1.5.0
compileTestingVersion=1.6.0

#https://github.com/Kotlin/kotlinx-kover
koverVersion=0.8.3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith

@ExtendWith(MockKExtension::class)
class PanacheCompanionBaseTests : TestCommon() {
class PanacheCompanionBaseProcessorMockTests : ProcessorMockTestCommon() {

@MockK
private lateinit var resolver: Resolver
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith

@ExtendWith(MockKExtension::class)
class PanacheEntityBaseTests : TestCommon() {
class PanacheEntityBaseProcessorMockTests : ProcessorMockTestCommon() {

@MockK
private lateinit var resolver: Resolver
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import com.squareup.kotlinpoet.ksp.toClassName
import io.mockk.mockkStatic
import org.junit.jupiter.api.BeforeAll

abstract class TestCommon {
abstract class ProcessorMockTestCommon {
companion object {
@JvmStatic
@BeforeAll
Expand Down
75 changes: 75 additions & 0 deletions src/test/kotlin/ch/icken/processor/ProcessorTests.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright 2024 Thijs Koppen
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package ch.icken.processor

import com.tschuchort.compiletesting.KotlinCompilation
import com.tschuchort.compiletesting.SourceFile
import com.tschuchort.compiletesting.kspSourcesDir
import com.tschuchort.compiletesting.symbolProcessorProviders
import org.intellij.lang.annotations.Language
import org.jetbrains.kotlin.compiler.plugin.ExperimentalCompilerApi
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test

@OptIn(ExperimentalCompilerApi::class)
class ProcessorTests {

@Test
fun testEmptyEntity() {

// Given
val compilation = kotlinCompilation("Employee.kt",
"""
import io.quarkus.hibernate.orm.panache.kotlin.PanacheEntity
import jakarta.persistence.Entity
@Entity
class Employee : PanacheEntity()
"""
)

// When
val result = compilation.compile()

// Then
assertEquals(KotlinCompilation.ExitCode.OK, result.exitCode)

compilation.kspSourcesDir.walkTopDown().forEach { println(it.absolutePath) }
//TODO validate generated file content
}

//region Utils
private fun kotlinResource(resourceName: String, sourceFileName: String = resourceName) =
SourceFile.kotlin(sourceFileName, javaClass.classLoader.getResource(resourceName)?.readText() ?: "")

private fun compilation(vararg source: SourceFile) = KotlinCompilation().apply {
inheritClassPath = true
sources = listOf(*source)
symbolProcessorProviders = listOf(
PanacheCompanionBaseProcessorProvider(),
PanacheEntityBaseProcessorProvider()
)
verbose = false
}

private fun kotlinCompilation(sourceFileName: String, @Language("kotlin") contents: String) =
compilation(SourceFile.kotlin(sourceFileName, contents))

private fun kotlinResourceCompilation(resourceName: String, sourceFileName: String = resourceName) =
compilation(kotlinResource(resourceName, sourceFileName))
//endregion
}

0 comments on commit 97b1e8f

Please sign in to comment.