diff --git a/hideout-core/build.gradle.kts b/hideout-core/build.gradle.kts index 88b2bca3..d88f2ac3 100644 --- a/hideout-core/build.gradle.kts +++ b/hideout-core/build.gradle.kts @@ -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 { diff --git a/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/infrastructure/exposedrepository/InstanceRepositoryImplTest.kt b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/infrastructure/exposedrepository/InstanceRepositoryImplTest.kt new file mode 100644 index 00000000..4755a2cb --- /dev/null +++ b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/infrastructure/exposedrepository/InstanceRepositoryImplTest.kt @@ -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"))) + } +} + diff --git a/hideout-core/src/test/kotlin/utils/AbstractRepositoryTest.kt b/hideout-core/src/test/kotlin/utils/AbstractRepositoryTest.kt new file mode 100644 index 00000000..e2ea2838 --- /dev/null +++ b/hideout-core/src/test/kotlin/utils/AbstractRepositoryTest.kt @@ -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 TableRowAssert.value(column: Column): TableRowValueAssert = value(column.name) +fun TableRowValueAssert.value(column: Column): TableRowValueAssert = value(column.name) + +fun TableRowAssert.isEqualTo(column: Column, value: T): TableRowValueAssert { + return value(column).isEqualTo(value) +} + +fun TableRowValueAssert.isEqualTo(column: Column, value: T): TableRowValueAssert { + return value(column).isEqualTo(value) +} \ No newline at end of file