Skip to content

Commit

Permalink
fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentlauvlwj committed Jul 8, 2023
1 parent 88fb1f2 commit 45155f4
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import com.squareup.kotlinpoet.CodeBlock
import com.squareup.kotlinpoet.MemberName
import com.squareup.kotlinpoet.TypeName
import com.squareup.kotlinpoet.ksp.KotlinPoetKspPreview
import com.squareup.kotlinpoet.ksp.toClassName
import com.squareup.kotlinpoet.ksp.toTypeName
import org.ktorm.ksp.spi.ColumnMetadata
import org.ktorm.schema.*
Expand Down Expand Up @@ -107,9 +108,14 @@ internal fun ColumnMetadata.getRegisteringCodeBlock(): CodeBlock {

val declaration = sqlType.declaration as KSClassDeclaration
if (declaration.classKind == ClassKind.OBJECT) {
return CodeBlock.of("registerColumn(%S,·%T)", name, sqlType.toTypeName())
return CodeBlock.of("registerColumn(%S,·%T)", name, declaration.toClassName())
} else {
return CodeBlock.of("registerColumn(%S,·%T(TODO))", name, sqlType.toTypeName())
return CodeBlock.of("registerColumn(%S,·%T(%M<%T>()))",
name,
declaration.toClassName(),
MemberName("org.ktorm.schema", "typeRef"),
getKotlinType()
)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ abstract class BaseKspTest {
import org.ktorm.database.*
import org.ktorm.dsl.*
import org.ktorm.entity.*
import org.ktorm.ksp.api.*
import org.ktorm.ksp.annotation.*
lateinit var database: Database
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,28 +123,6 @@ class MetadataParserTest : BaseKspTest() {
}
""".trimIndent())

@Test
fun testSqlTypeShouldBeObject() = kspFailing("Parse sqlType error for property User.name: the sqlType class must be a Kotlin singleton object.", """
@Table
interface User : Entity<User> {
val id: Int
@Column(sqlType = org.ktorm.schema.EnumSqlType::class)
val name: String
}
""".trimIndent())

@Test
fun testSqlTypeShouldBeSqlType() = kspFailing("Parse sqlType error for property User.name: the sqlType class must be subtype of SqlType/SqlTypeFactory.", """
@Table
interface User : Entity<User> {
val id: Int
@Column(sqlType = Test::class)
val name: String
}
object Test
""".trimIndent())

@Test
fun testReferencesWithColumn() = kspFailing("Parse ref column error for property User.profile: @Column and @References cannot be used together.", """
@Table
Expand All @@ -163,7 +141,7 @@ class MetadataParserTest : BaseKspTest() {
""".trimIndent())

@Test
fun testReferencesFromClassEntity() = kspFailing("Parse ref column error for property User.profile: @References can only be used in interface-based entities", """
fun testReferencesFromClassEntity() = kspFailing("Parse ref column error for property User.profile: @References only allowed in interface-based entities", """
@Table
class User(
val id: Int,
Expand Down Expand Up @@ -195,7 +173,7 @@ class MetadataParserTest : BaseKspTest() {
""".trimIndent())

@Test
fun testReferencesToNonTableClass() = kspFailing("Parse ref column error for property User.profile: the referenced entity class must be annotated with @Table", """
fun testReferencesToNonTableClass() = kspFailing("Parse ref column error for property User.profile: the referenced entity must be annotated with @Table", """
@Table
interface User : Entity<User> {
val id: Int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,14 @@ class SqlTypeTest : BaseKspTest() {
""".trimIndent())

@Test
fun testCustomSqlTypeFactory() = runKotlin("""
fun testCustomSqlTypeWithConstructor() = runKotlin("""
@Table
data class User(
@PrimaryKey
var id: Int,
var username: String,
var age: Int,
@Column(sqlType = IntEnumSqlTypeFactory::class)
@Column(sqlType = IntEnumSqlType::class)
var gender: Gender
)
Expand All @@ -117,26 +117,16 @@ class SqlTypeTest : BaseKspTest() {
FEMALE
}
object IntEnumSqlTypeFactory : SqlTypeFactory {
class IntEnumSqlType<E : Enum<E>>(val enumClass: Class<E>) : org.ktorm.schema.SqlType<E>(Types.INTEGER, "int") {
@Suppress("UNCHECKED_CAST")
override fun <T : Any> createSqlType(property: KProperty1<*, T?>): org.ktorm.schema.SqlType<T> {
val returnType = property.returnType.jvmErasure.java
if (returnType.isEnum) {
return IntEnumSqlType(returnType as Class<out Enum<*>>) as org.ktorm.schema.SqlType<T>
} else {
throw IllegalArgumentException("The property is required to be typed of enum but actually: ${"$"}returnType")
}
}
constructor(typeRef: org.ktorm.schema.TypeReference<E>) : this(typeRef.referencedType as Class<E>)
private class IntEnumSqlType<E : Enum<E>>(val enumClass: Class<E>) : org.ktorm.schema.SqlType<E>(Types.INTEGER, "int") {
override fun doSetParameter(ps: PreparedStatement, index: Int, parameter: E) {
ps.setInt(index, parameter.ordinal)
}
override fun doSetParameter(ps: PreparedStatement, index: Int, parameter: E) {
ps.setInt(index, parameter.ordinal)
}
override fun doGetResult(rs: ResultSet, index: Int): E? {
return enumClass.enumConstants[rs.getInt(index)]
}
override fun doGetResult(rs: ResultSet, index: Int): E? {
return enumClass.enumConstants[rs.getInt(index)]
}
}
Expand Down

0 comments on commit 45155f4

Please sign in to comment.