Skip to content

Commit

Permalink
Add unit tests for processor create...() functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Thijsiez committed Nov 6, 2023
1 parent c28f8ce commit 667806c
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,19 +84,19 @@ class PanacheEntityBaseProcessor(
primaryConstructor(constructorBuilder.build())

// Generate properties
ksProperties.forEach { ksPropertyDeclaration ->
val propertyName = ksPropertyDeclaration.simpleName.asString()
val isJoinColumn = ksPropertyDeclaration.hasAnnotation(JakartaPersistenceJoinColumn)
ksProperties.forEach { ksProperty ->
val propertyName = ksProperty.simpleName.asString()
val isJoinColumn = ksProperty.hasAnnotation(JakartaPersistenceJoinColumn)

val propertyBuilder = if (isJoinColumn) {
val joinObjectName = ksPropertyDeclaration.typeName + COLUMN_NAME_OBJECT_SUFFIX
val joinObjectName = ksProperty.typeName + COLUMN_NAME_OBJECT_SUFFIX
val joinBaseClassName = joinObjectName + COLUMN_NAME_BASE_CLASS_SUFFIX
val joinBaseClass = ClassName(packageName, joinBaseClassName)

PropertySpec.builder(propertyName, joinBaseClass)
.initializer("%T(%S)", joinBaseClass, "$propertyName.")
} else {
val ksPropertyType = ksPropertyDeclaration.type.resolve()
val ksPropertyType = ksProperty.type.resolve()
val columnNameParameterType = ksPropertyType.toClassName()
.copy(nullable = ksPropertyType.isMarkedNullable)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,23 @@

package ch.icken.processor

import ch.icken.processor.ClassNames.LongClassName
import ch.icken.processor.GenerationOptions.ADD_GENERATED_ANNOTATION
import ch.icken.processor.QualifiedNames.HibernatePanacheCompanionBase
import ch.icken.processor.QualifiedNames.HibernatePanacheEntityBase
import ch.icken.processor.QualifiedNames.JakartaPersistenceEntity
import ch.icken.processor.QualifiedNames.JakartaPersistenceId
import com.google.devtools.ksp.processing.CodeGenerator
import com.google.devtools.ksp.processing.KSPLogger
import com.google.devtools.ksp.processing.Resolver
import com.google.devtools.ksp.symbol.KSClassDeclaration
import com.google.devtools.ksp.symbol.KSFunctionDeclaration
import com.google.devtools.ksp.symbol.KSName
import com.google.devtools.ksp.symbol.*
import com.google.devtools.ksp.validate
import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.ksp.toClassName
import io.mockk.*
import io.mockk.impl.annotations.MockK
import io.mockk.junit5.MockKExtension
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith

Expand All @@ -41,16 +42,14 @@ class PanacheCompanionBaseTests : TestCommon() {
@MockK
private lateinit var resolver: Resolver

private lateinit var processor: PanacheCompanionBaseProcessor

@BeforeEach
fun beforeEach() {
processor = spyk(PanacheCompanionBaseProcessor(
options = mapOf(ADD_GENERATED_ANNOTATION to "false"),
codeGenerator = mockk<CodeGenerator>(),
logger = mockk<KSPLogger>()
))
}
private val codeGenerator = mockk<CodeGenerator>(relaxed = true)
private val processor = spyk(PanacheCompanionBaseProcessor(
options = mapOf(ADD_GENERATED_ANNOTATION to "false"),
codeGenerator = codeGenerator,
logger = mockk<KSPLogger>().also {
every { it.info(any()) } just Runs
}
))

//region process
@Test
Expand Down Expand Up @@ -111,7 +110,9 @@ class PanacheCompanionBaseTests : TestCommon() {
val invalid = processor.process(resolver)

// Then
verify(exactly = 0) { processor.createQueryBuilderExtensions(any(), any(), any()) }
verify(exactly = 0) {
processor.createQueryBuilderExtensions(any(), any(), any())
}
assertEquals(0, invalid.size)
}

Expand All @@ -133,7 +134,9 @@ class PanacheCompanionBaseTests : TestCommon() {
val invalid = processor.process(resolver)

// Then
verify(exactly = 0) { processor.createQueryBuilderExtensions(any(), any(), any()) }
verify(exactly = 0) {
processor.createQueryBuilderExtensions(any(), any(), any())
}
assertEquals(0, invalid.size)
}

Expand All @@ -154,7 +157,9 @@ class PanacheCompanionBaseTests : TestCommon() {
val invalid = processor.process(resolver)

// Then
verify(exactly = 0) { processor.createQueryBuilderExtensions(any(), any(), any()) }
verify(exactly = 0) {
processor.createQueryBuilderExtensions(any(), any(), any())
}
assertEquals(0, invalid.size)
}

Expand All @@ -173,7 +178,9 @@ class PanacheCompanionBaseTests : TestCommon() {
val invalid = processor.process(resolver)

// Then
verify(exactly = 0) { processor.createQueryBuilderExtensions(any(), any(), any()) }
verify(exactly = 0) {
processor.createQueryBuilderExtensions(any(), any(), any())
}
assertEquals(0, invalid.size)
}

Expand All @@ -191,7 +198,9 @@ class PanacheCompanionBaseTests : TestCommon() {
val invalid = processor.process(resolver)

// Then
verify(exactly = 0) { processor.createQueryBuilderExtensions(any(), any(), any()) }
verify(exactly = 0) {
processor.createQueryBuilderExtensions(any(), any(), any())
}
assertEquals(0, invalid.size)
}

Expand All @@ -208,7 +217,9 @@ class PanacheCompanionBaseTests : TestCommon() {
val invalid = processor.process(resolver)

// Then
verify(exactly = 0) { processor.createQueryBuilderExtensions(any(), any(), any()) }
verify(exactly = 0) {
processor.createQueryBuilderExtensions(any(), any(), any())
}
assertEquals(0, invalid.size)
}

Expand All @@ -225,8 +236,49 @@ class PanacheCompanionBaseTests : TestCommon() {
val invalid = processor.process(resolver)

// Then
verify(exactly = 0) { processor.createQueryBuilderExtensions(any(), any(), any()) }
verify(exactly = 0) {
processor.createQueryBuilderExtensions(any(), any(), any())
}
assertEquals(1, invalid.size)
}
//endregion

//region createQueryBuilderExtensions
@Test
fun testCreateQueryBuilderExtensions() {

// Given
val packageName = "ch.icken.model"
val simpleName = "Employee"
val className = ClassName(packageName, simpleName)

val classSimpleName = mockk<KSName>()
every { classSimpleName.asString() } returns simpleName

val idPropertyType = mockk<KSType>()
every { idPropertyType.toClassName() } returns LongClassName

val idPropertyTypeReference = mockk<KSTypeReference>()
every { idPropertyTypeReference.resolve() } returns idPropertyType

val idProperty = mockk<KSPropertyDeclaration>()
every { idProperty.hasAnnotation(eq(JakartaPersistenceId)) } returns true
every { idProperty.type } returns idPropertyTypeReference

val ksClass = mockk<KSClassDeclaration>()
every { ksClass.toClassName() } returns className
every { ksClass.simpleName } returns classSimpleName
every { ksClass.getAllProperties() } returns sequenceOf(idProperty)

val ksClasses = listOf(ksClass)

// When
processor.createQueryBuilderExtensions(packageName, ksClasses, false)

// Then
verify(exactly = 1) {
codeGenerator.createNewFile(any(), any(), any())
}
}
//endregion
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package ch.icken.processor

import ch.icken.processor.ClassNames.StringClassName
import ch.icken.processor.GenerationOptions.ADD_GENERATED_ANNOTATION
import ch.icken.processor.QualifiedNames.HibernatePanacheEntityBase
import ch.icken.processor.QualifiedNames.JakartaPersistenceColumn
Expand All @@ -24,15 +25,13 @@ import ch.icken.processor.QualifiedNames.JakartaPersistenceJoinColumn
import com.google.devtools.ksp.processing.CodeGenerator
import com.google.devtools.ksp.processing.KSPLogger
import com.google.devtools.ksp.processing.Resolver
import com.google.devtools.ksp.symbol.KSClassDeclaration
import com.google.devtools.ksp.symbol.KSFunctionDeclaration
import com.google.devtools.ksp.symbol.KSPropertyDeclaration
import com.google.devtools.ksp.symbol.*
import com.google.devtools.ksp.validate
import com.squareup.kotlinpoet.ksp.toClassName
import io.mockk.*
import io.mockk.impl.annotations.MockK
import io.mockk.junit5.MockKExtension
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith

Expand All @@ -42,16 +41,14 @@ class PanacheEntityBaseTests : TestCommon() {
@MockK
private lateinit var resolver: Resolver

private lateinit var processor: PanacheEntityBaseProcessor

@BeforeEach
fun beforeEach() {
processor = spyk(PanacheEntityBaseProcessor(
options = mapOf(ADD_GENERATED_ANNOTATION to "false"),
codeGenerator = mockk<CodeGenerator>(),
logger = mockk<KSPLogger>()
))
}
private val codeGenerator = mockk<CodeGenerator>(relaxed = true)
private val processor = spyk(PanacheEntityBaseProcessor(
options = mapOf(ADD_GENERATED_ANNOTATION to "false"),
codeGenerator = codeGenerator,
logger = mockk<KSPLogger>().also {
every { it.info(any()) } just Runs
}
))

//region process
@Test
Expand Down Expand Up @@ -193,7 +190,9 @@ class PanacheEntityBaseTests : TestCommon() {
val invalid = processor.process(resolver)

// Then
verify(exactly = 0) { processor.createColumnNamesObject(any(), any(), any()) }
verify(exactly = 0) {
processor.createColumnNamesObject(any(), any(), any())
}
assertEquals(0, invalid.size)
}

Expand All @@ -210,7 +209,9 @@ class PanacheEntityBaseTests : TestCommon() {
val invalid = processor.process(resolver)

// Then
verify(exactly = 0) { processor.createColumnNamesObject(any(), any(), any()) }
verify(exactly = 0) {
processor.createColumnNamesObject(any(), any(), any())
}
assertEquals(0, invalid.size)
}

Expand All @@ -227,8 +228,61 @@ class PanacheEntityBaseTests : TestCommon() {
val invalid = processor.process(resolver)

// Then
verify(exactly = 0) { processor.createColumnNamesObject(any(), any(), any()) }
verify(exactly = 0) {
processor.createColumnNamesObject(any(), any(), any())
}
assertEquals(1, invalid.size)
}
//endregion

//region createColumnNamesObject
@Test
fun testCreateColumnNamesObject() {

// Given
val packageName = "ch.icken.model"
val classPackageName = mockk<KSName>()
every { classPackageName.asString() } returns packageName

val classSimpleName = mockk<KSName>()
every { classSimpleName.asString() } returns "Employee"

val ksClass = mockk<KSClassDeclaration>()
every { ksClass.packageName } returns classPackageName
every { ksClass.simpleName } returns classSimpleName

val firstNameSimpleName = mockk<KSName>()
every { firstNameSimpleName.asString() } returns "firstName"

val firstNameType = mockk<KSType>()
every { firstNameType.toClassName() } returns StringClassName
every { firstNameType.isMarkedNullable } returns false

val firstNameTypeReference = mockk<KSTypeReference>()
every { firstNameTypeReference.resolve() } returns firstNameType

val firstName = mockk<KSPropertyDeclaration>()
every { firstName.simpleName } returns firstNameSimpleName
every { firstName.hasAnnotation(eq(JakartaPersistenceJoinColumn)) } returns false
every { firstName.type } returns firstNameTypeReference

val departmentSimpleName = mockk<KSName>()
every { departmentSimpleName.asString() } returns "department"

val department = mockk<KSPropertyDeclaration>()
every { department.simpleName } returns departmentSimpleName
every { department.hasAnnotation(eq(JakartaPersistenceJoinColumn)) } returns true
every { department.typeName } returns "Department"

val ksProperties = listOf(firstName, department)

// When
processor.createColumnNamesObject(ksClass, ksProperties, true)

// Then
verify(exactly = 1) {
codeGenerator.createNewFile(any(), any(), any())
}
}
//endregion
}
4 changes: 4 additions & 0 deletions library/src/test/kotlin/ch/icken/processor/TestCommon.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package ch.icken.processor

import com.google.devtools.ksp.symbol.*
import com.google.devtools.ksp.validate
import com.squareup.kotlinpoet.ksp.toClassName
import io.mockk.mockkStatic
import org.junit.jupiter.api.BeforeAll

Expand All @@ -29,8 +30,11 @@ abstract class TestCommon {
mockkStatic(KSAnnotated::hasAnnotation)
mockkStatic(KSAnnotation::isClass)
mockkStatic(KSClassDeclaration::isSubclass)
mockkStatic(KSClassDeclaration::toClassName)
mockkStatic(KSDeclaration::isClass)
mockkStatic(KSNode::validate)
mockkStatic(KSPropertyDeclaration::typeName)
mockkStatic(KSType::toClassName)
}
}
}

0 comments on commit 667806c

Please sign in to comment.