From d4fd61d31dfed00d1c5615c99a193e2009e17d12 Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Thu, 12 Sep 2024 17:00:04 +0900 Subject: [PATCH] =?UTF-8?q?test:=20ExposedTimelineRepository=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 --- .../ExposedTimelineRepository.kt | 2 +- .../ExposedTimelineRepositoryTest.kt | 90 +++++++++++++++++++ 2 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 hideout-core/src/test/kotlin/dev/usbharu/hideout/core/infrastructure/exposedrepository/ExposedTimelineRepositoryTest.kt diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/exposedrepository/ExposedTimelineRepository.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/exposedrepository/ExposedTimelineRepository.kt index 9fe9f694..8f5f8c65 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/exposedrepository/ExposedTimelineRepository.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/exposedrepository/ExposedTimelineRepository.kt @@ -54,7 +54,7 @@ class ExposedTimelineRepository(override val domainEventPublisher: DomainEventPu override suspend fun findById(id: TimelineId): Timeline? { return query { - Timelines.selectAll().where { Timelines.id eq id.value }.firstOrNull()?.toTimeline() + Timelines.selectAll().where { Timelines.id eq id.value }.limit(1).firstOrNull()?.toTimeline() } } diff --git a/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/infrastructure/exposedrepository/ExposedTimelineRepositoryTest.kt b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/infrastructure/exposedrepository/ExposedTimelineRepositoryTest.kt new file mode 100644 index 00000000..f80bc992 --- /dev/null +++ b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/infrastructure/exposedrepository/ExposedTimelineRepositoryTest.kt @@ -0,0 +1,90 @@ +package dev.usbharu.hideout.core.infrastructure.exposedrepository + +import com.ninja_squad.dbsetup_kotlin.dbSetup +import dev.usbharu.hideout.core.domain.model.timeline.Timeline +import dev.usbharu.hideout.core.domain.model.timeline.TimelineId +import dev.usbharu.hideout.core.domain.model.timeline.TimelineName +import dev.usbharu.hideout.core.domain.model.timeline.TimelineVisibility +import dev.usbharu.hideout.core.domain.model.userdetails.UserDetailId +import dev.usbharu.hideout.core.domain.shared.domainevent.DomainEventPublisher +import kotlinx.coroutines.test.runTest +import org.assertj.db.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.extension.ExtendWith +import org.mockito.InjectMocks +import org.mockito.Mock +import org.mockito.junit.jupiter.MockitoExtension +import utils.AbstractRepositoryTest +import utils.columns +import utils.disableReferenceIntegrityConstraints +import utils.isEqualTo + +@ExtendWith(MockitoExtension::class) +class ExposedTimelineRepositoryTest : AbstractRepositoryTest(Timelines) { + @InjectMocks + lateinit var repository: ExposedTimelineRepository + + @Mock + lateinit var domainEventPublisher: DomainEventPublisher + + @Test + fun save_idが同じレコードが存在しない場合insert() = runTest { + dbSetup(to = dataSource) { + execute(disableReferenceIntegrityConstraints) + insertInto(UserDetails.tableName) { + columns(UserDetails.columns) + values(1, 1, "veeeeeeeeeeeeeeryStrongPassword", true, null, null) + } + }.launch() + val timeline = Timeline( + id = TimelineId(1), + userDetailId = UserDetailId(1), + name = TimelineName("timeline"), + visibility = TimelineVisibility.PUBLIC, + isSystem = false + ) + + repository.save(timeline) + + assertThat(assertTable) + .row(0) + .isEqualTo(Timelines.id, timeline.id.value) + .isEqualTo(Timelines.userDetailId, timeline.userDetailId.id) + .isEqualTo(Timelines.name, timeline.name.value) + .isEqualTo(Timelines.visibility, timeline.visibility.name) + .isEqualTo(Timelines.isSystem, timeline.isSystem) + } + + @Test + fun save_idが同じレコードが存在する場合update() = runTest { + dbSetup(to = dataSource) { + execute(disableReferenceIntegrityConstraints) + insertInto(UserDetails.tableName) { + columns(UserDetails.columns) + values(1, 1, "veeeeeeeeeeeeeeryStrongPassword", true, null, null) + } + insertInto(Timelines.tableName) { + columns(Timelines.columns) + values(1, 1, "test-timeline", "PUBLIC", true) + } + + }.launch() + val timeline = Timeline( + id = TimelineId(1), + userDetailId = UserDetailId(1), + name = TimelineName("timeline"), + visibility = TimelineVisibility.PRIVATE, + isSystem = false + ) + + repository.save(timeline) + + assertThat(assertTable) + .row(0) + .isEqualTo(Timelines.id, timeline.id.value) + .isEqualTo(Timelines.userDetailId, timeline.userDetailId.id) + .isEqualTo(Timelines.name, timeline.name.value) + .isEqualTo(Timelines.visibility, timeline.visibility.name) + .isEqualTo(Timelines.isSystem, timeline.isSystem) + } +}