MicroUtils/repos/exposed/src/jvmTest/kotlin/ExposedCRUDRepoTests.kt

63 lines
2.0 KiB
Kotlin
Raw Normal View History

2024-08-09 13:22:32 +00:00
package full
import com.benasher44.uuid.uuid4
import dev.inmo.micro_utils.repos.CRUDRepo
2024-08-09 15:58:09 +00:00
import dev.inmo.micro_utils.repos.common.tests.CommonCRUDRepoTests
2024-08-09 13:22:32 +00:00
import dev.inmo.micro_utils.repos.exposed.AbstractExposedCRUDRepo
import dev.inmo.micro_utils.repos.exposed.initTable
import org.jetbrains.exposed.sql.Database
import org.jetbrains.exposed.sql.ISqlExpressionBuilder
import org.jetbrains.exposed.sql.Op
import org.jetbrains.exposed.sql.ResultRow
import org.jetbrains.exposed.sql.statements.InsertStatement
import org.jetbrains.exposed.sql.statements.UpdateBuilder
import java.io.File
2024-08-09 15:58:09 +00:00
import kotlin.test.AfterTest
import kotlin.test.BeforeTest
2024-08-09 13:22:32 +00:00
class ExposedCRUDRepoTests : CommonCRUDRepoTests() {
2024-08-09 15:58:09 +00:00
class Repo(override val database: Database) : AbstractExposedCRUDRepo<Registered, String, New>() {
2024-08-09 13:22:32 +00:00
val idColumn = text("_id")
val dataColumn = text("data")
override val primaryKey: PrimaryKey = PrimaryKey(idColumn)
override val ResultRow.asId: String
get() = get(idColumn)
2024-08-09 15:58:09 +00:00
override val ResultRow.asObject: Registered
get() = Registered(
2024-08-09 13:22:32 +00:00
asId,
get(dataColumn)
)
override val selectById: ISqlExpressionBuilder.(String) -> Op<Boolean> = { idColumn.eq(it) }
init {
initTable()
}
2024-08-09 15:58:09 +00:00
override fun update(id: String?, value: New, it: UpdateBuilder<Int>) {
2024-08-09 13:22:32 +00:00
it[idColumn] = id ?: uuid4().toString()
it[dataColumn] = value.data
}
2024-08-09 15:58:09 +00:00
override fun InsertStatement<Number>.asObject(value: New): Registered {
return Registered(
2024-08-09 13:22:32 +00:00
get(idColumn),
get(dataColumn)
)
}
}
val filename = filename()
var database: Database? = null
override val repoCreator: suspend () -> CRUDRepo<Registered, String, New> = { Repo(database!!) }
@BeforeTest
fun beforeTest() {
database = createDatabase(filename)
}
@AfterTest
fun afterTest() {
database = null
File(filename).delete()
}
}