From 37c8335165d145591ec91162fc0b973d3d9dbc6d Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Tue, 10 Sep 2024 18:55:55 +0900 Subject: [PATCH] =?UTF-8?q?test:=20InstanceRepositoryImpl=E3=81=AE?= =?UTF-8?q?=E3=83=86=E3=82=B9=E3=83=88=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hideout-core/build.gradle.kts | 1 + .../InstanceRepositoryImpl.kt | 4 +- .../InstanceRepositoryImplTest.kt | 163 ++++++++++++++++-- .../kotlin/utils/AbstractRepositoryTest.kt | 7 +- 4 files changed, 158 insertions(+), 17 deletions(-) diff --git a/hideout-core/build.gradle.kts b/hideout-core/build.gradle.kts index d88f2ac3..d184d19c 100644 --- a/hideout-core/build.gradle.kts +++ b/hideout-core/build.gradle.kts @@ -133,6 +133,7 @@ dependencies { testImplementation(libs.h2db) testImplementation(libs.mockito.kotlin) testImplementation("org.assertj:assertj-db:2.0.2") + testImplementation("com.ninja-squad:DbSetup-kotlin:2.1.0") } detekt { diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/exposedrepository/InstanceRepositoryImpl.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/exposedrepository/InstanceRepositoryImpl.kt index de57ff44..c7259dbb 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/exposedrepository/InstanceRepositoryImpl.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/exposedrepository/InstanceRepositoryImpl.kt @@ -52,7 +52,7 @@ class InstanceRepositoryImpl : InstanceRepository, } override suspend fun findById(id: InstanceId): InstanceEntity? = query { - return@query Instance.selectAll().where { Instance.id eq id.instanceId } + return@query Instance.selectAll().where { Instance.id eq id.instanceId }.limit(1) .singleOrNull()?.toInstance() } @@ -61,7 +61,7 @@ class InstanceRepositoryImpl : InstanceRepository, } override suspend fun findByUrl(url: URI): dev.usbharu.hideout.core.domain.model.instance.Instance? = query { - return@query Instance.selectAll().where { Instance.url eq url.toString() }.singleOrNull()?.toInstance() + return@query Instance.selectAll().where { Instance.url eq url.toString() }.limit(1).singleOrNull()?.toInstance() } companion object { 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 index 4755a2cb..29d1bdfe 100644 --- 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 @@ -1,5 +1,6 @@ package dev.usbharu.hideout.core.infrastructure.exposedrepository +import com.ninja_squad.dbsetup_kotlin.dbSetup import dev.usbharu.hideout.core.domain.model.instance.* import dev.usbharu.hideout.core.domain.model.instance.Instance import kotlinx.coroutines.test.runTest @@ -12,6 +13,9 @@ import utils.value import java.net.URI import java.sql.Timestamp import java.time.Instant +import kotlin.test.assertEquals +import kotlin.test.assertNotNull +import kotlin.test.assertNull import dev.usbharu.hideout.core.infrastructure.exposedrepository.Instance as InstanceTable @TestInstance(TestInstance.Lifecycle.PER_CLASS) @@ -19,7 +23,7 @@ class InstanceRepositoryImplTest : AbstractRepositoryTest(InstanceTable) { @Test - fun save() = runTest { + fun save_idが同じレコードがない場合はinsertされる() = runTest { InstanceRepositoryImpl().save( Instance( id = InstanceId(1), @@ -38,19 +42,152 @@ class InstanceRepositoryImplTest : AbstractRepositoryTest(InstanceTable) { ) val table = assertTable - assertThat(table) - .row(1) - .isEqualTo(InstanceTable.id, 1) - .isEqualTo(InstanceTable.name, "test") + 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"))) + .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"))) + } + + @Test + fun save_idが同じレコードがある場合はupdateされる() = runTest { + dbSetup(to = dataSource) { + insertInto(InstanceTable.tableName) { + columns( + "ID", + "name", + "DESCRIPTION", + "URL", + "ICON_URL", + "SHARED_INBOX", + "SOFTWARE", + "VERSION", + "IS_BLOCKED", + "IS_MUTED", + "MODERATION_NOTE", + "CREATED_AT" + ) + values( + 1, + "system", + "", + "https://example.com", + "", + null, + "", + "", + false, + false, + "", + "2024-09-10 16:59:50.160202" + ) + } + }.launch() + + + 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"))) + } + + @Test + fun findById_指定したidで存在したら返す() = runTest { + dbSetup(to = dataSource) { + insertInto(InstanceTable.tableName) { + columns( + "ID", + "name", + "DESCRIPTION", + "URL", + "ICON_URL", + "SHARED_INBOX", + "SOFTWARE", + "VERSION", + "IS_BLOCKED", + "IS_MUTED", + "MODERATION_NOTE", + "CREATED_AT" + ) + values( + 1, + "test", + "description", + "https://www.example.com", + "https://www.example.com", + null, + "", + "", + false, + false, + "", + Timestamp.from(Instant.parse("2020-01-01T00:00:00Z")) + ) + } + }.launch() + + val actual = InstanceRepositoryImpl().findById(InstanceId(1)) + val expected = Instance( + id = InstanceId(1), + name = InstanceName("test"), + description = InstanceDescription("description"), + 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"), + ) + + assertEquals(expected, actual) + } + + @Test + fun findById_指定したIDで存在しないとnull() = runTest { + assertNull(InstanceRepositoryImpl().findById(InstanceId(1))) + } + + companion object { + fun assertEquals(expected: Instance, actual: Instance?) { + assertNotNull(actual) + kotlin.test.assertEquals(expected, actual) + assertEquals(expected.name, actual.name) + assertEquals(expected.description, actual.description) + assertEquals(expected.url, actual.url) + assertEquals(expected.iconUrl, actual.iconUrl) + assertEquals(expected.sharedInbox, actual.sharedInbox) + assertEquals(expected.software, actual.software) + assertEquals(expected.version, actual.version) + assertEquals(expected.isBlocked, actual.isBlocked) + assertEquals(expected.moderationNote, actual.moderationNote) + assertEquals(expected.createdAt, actual.createdAt) + } } } diff --git a/hideout-core/src/test/kotlin/utils/AbstractRepositoryTest.kt b/hideout-core/src/test/kotlin/utils/AbstractRepositoryTest.kt index e2ea2838..15530abd 100644 --- a/hideout-core/src/test/kotlin/utils/AbstractRepositoryTest.kt +++ b/hideout-core/src/test/kotlin/utils/AbstractRepositoryTest.kt @@ -33,6 +33,8 @@ abstract class AbstractRepositoryTest(private val exposedTable: org.jetbrains.ex @BeforeEach fun setUp() { + flyway.clean() + flyway.migrate() transaction = TransactionManager.currentOrNew(Connection.TRANSACTION_READ_UNCOMMITTED) } @@ -58,8 +60,9 @@ abstract class AbstractRepositoryTest(private val exposedTable: org.jetbrains.ex flyway = Flyway.configure().cleanDisabled(false).dataSource(dataSource).load() - val db = Database.connect(dataSource, databaseConfig = DatabaseConfig { - this.defaultMaxAttempts = 1 + Database.connect(dataSource, databaseConfig = DatabaseConfig { + defaultMaxAttempts = 1 + }) flyway.clean() flyway.migrate()