test: Repositoryのテストを追加

This commit is contained in:
usbharu 2024-09-10 17:45:51 +09:00
parent 4c879e3e04
commit 27ccca02c0
Signed by: usbharu
GPG Key ID: 6556747BF94EEBC8
3 changed files with 142 additions and 0 deletions

View File

@ -132,6 +132,7 @@ dependencies {
testImplementation(libs.ktor.client.mock)
testImplementation(libs.h2db)
testImplementation(libs.mockito.kotlin)
testImplementation("org.assertj:assertj-db:2.0.2")
}
detekt {

View File

@ -0,0 +1,56 @@
package dev.usbharu.hideout.core.infrastructure.exposedrepository
import dev.usbharu.hideout.core.domain.model.instance.*
import dev.usbharu.hideout.core.domain.model.instance.Instance
import kotlinx.coroutines.test.runTest
import org.assertj.db.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
import utils.AbstractRepositoryTest
import utils.isEqualTo
import utils.value
import java.net.URI
import java.sql.Timestamp
import java.time.Instant
import dev.usbharu.hideout.core.infrastructure.exposedrepository.Instance as InstanceTable
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class InstanceRepositoryImplTest : AbstractRepositoryTest(InstanceTable) {
@Test
fun save() = runTest {
InstanceRepositoryImpl().save(
Instance(
id = InstanceId(1),
name = InstanceName("test"),
description = InstanceDescription("id"),
url = URI.create("https://www.example.com"),
iconUrl = URI.create("https://www.example.com"),
sharedInbox = null,
software = InstanceSoftware(""),
version = InstanceVersion(""),
isBlocked = false,
isMuted = false,
moderationNote = InstanceModerationNote(""),
createdAt = Instant.parse("2020-01-01T00:00:00Z"),
)
)
val table = assertTable
assertThat(table)
.row(1)
.isEqualTo(InstanceTable.id, 1)
.isEqualTo(InstanceTable.name, "test")
.isEqualTo(InstanceTable.url, "https://www.example.com")
.isEqualTo(InstanceTable.iconUrl, "https://www.example.com")
.isEqualTo(InstanceTable.sharedInbox, null)
.isEqualTo(InstanceTable.software, "")
.isEqualTo(InstanceTable.version, "")
.isEqualTo(InstanceTable.isBlocked, false)
.isEqualTo(InstanceTable.isMuted, false)
.isEqualTo(InstanceTable.moderationNote, "")
.value(InstanceTable.createdAt).isEqualTo(Timestamp.from(Instant.parse("2020-01-01T00:00:00Z")))
}
}

View File

@ -0,0 +1,85 @@
package utils
import com.zaxxer.hikari.HikariConfig
import com.zaxxer.hikari.HikariDataSource
import org.assertj.db.api.TableRowAssert
import org.assertj.db.api.TableRowValueAssert
import org.assertj.db.type.Table
import org.flywaydb.core.Flyway
import org.jetbrains.exposed.sql.Column
import org.jetbrains.exposed.sql.Database
import org.jetbrains.exposed.sql.DatabaseConfig
import org.jetbrains.exposed.sql.Transaction
import org.jetbrains.exposed.sql.transactions.TransactionManager
import org.junit.jupiter.api.AfterAll
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.BeforeEach
import java.sql.Connection
import javax.sql.DataSource
abstract class AbstractRepositoryTest(private val exposedTable: org.jetbrains.exposed.sql.Table) {
protected val assertTable: Table
get() {
return Table(dataSource, exposedTable.tableName)
}
protected fun getTable(name: String): Table {
return Table(dataSource, name)
}
private lateinit var transaction: Transaction
@BeforeEach
fun setUp() {
transaction = TransactionManager.currentOrNew(Connection.TRANSACTION_READ_UNCOMMITTED)
}
@AfterEach
fun tearDown() {
transaction.rollback()
}
companion object {
lateinit var dataSource: DataSource
lateinit var flyway: Flyway
@JvmStatic
@BeforeAll
fun setup() {
val hikariConfig = HikariConfig()
hikariConfig.jdbcUrl =
"jdbc:h2:./test;MODE=POSTGRESQL;DB_CLOSE_DELAY=-1;CASE_INSENSITIVE_IDENTIFIERS=true;TRACE_LEVEL_FILE=4;"
hikariConfig.driverClassName = "org.h2.Driver"
hikariConfig.transactionIsolation = "TRANSACTION_READ_UNCOMMITTED"
dataSource = HikariDataSource(hikariConfig)
flyway = Flyway.configure().cleanDisabled(false).dataSource(dataSource).load()
val db = Database.connect(dataSource, databaseConfig = DatabaseConfig {
this.defaultMaxAttempts = 1
})
flyway.clean()
flyway.migrate()
}
@JvmStatic
@AfterAll
fun clean() {
// flyway.clean()
}
}
}
fun <T> TableRowAssert.value(column: Column<T>): TableRowValueAssert = value(column.name)
fun <T> TableRowValueAssert.value(column: Column<T>): TableRowValueAssert = value(column.name)
fun <T> TableRowAssert.isEqualTo(column: Column<T>, value: T): TableRowValueAssert {
return value(column).isEqualTo(value)
}
fun <T> TableRowValueAssert.isEqualTo(column: Column<T>, value: T): TableRowValueAssert {
return value(column).isEqualTo(value)
}