From acd96aaed381eb4a352ceeed672ac491397733a1 Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Sat, 14 Sep 2024 20:41:15 +0900 Subject: [PATCH] =?UTF-8?q?test:=20=E3=83=89=E3=83=A1=E3=82=A4=E3=83=B3?= =?UTF-8?q?=E3=82=A4=E3=83=99=E3=83=B3=E3=83=88=E3=81=AE=E3=83=86=E3=82=B9?= =?UTF-8?q?=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 --- .../ExposedRelationshipRepositoryTest.kt | 56 ++++++++++ .../ExposedTimelineRepositoryTest.kt | 102 ++++++++++++++---- 2 files changed, 136 insertions(+), 22 deletions(-) diff --git a/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/infrastructure/exposedrepository/ExposedRelationshipRepositoryTest.kt b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/infrastructure/exposedrepository/ExposedRelationshipRepositoryTest.kt index 5327ded2..8220d6a2 100644 --- a/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/infrastructure/exposedrepository/ExposedRelationshipRepositoryTest.kt +++ b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/infrastructure/exposedrepository/ExposedRelationshipRepositoryTest.kt @@ -6,11 +6,15 @@ import dev.usbharu.hideout.core.domain.model.relationship.Relationship import dev.usbharu.hideout.core.domain.shared.domainevent.DomainEventPublisher import kotlinx.coroutines.test.runTest import org.assertj.db.api.Assertions.assertThat +import org.jetbrains.exposed.sql.transactions.TransactionManager 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 org.mockito.kotlin.any +import org.mockito.kotlin.times +import org.mockito.kotlin.verify import utils.* import kotlin.test.assertContentEquals import kotlin.test.assertEquals @@ -444,4 +448,56 @@ class ExposedRelationshipRepositoryTest : AbstractRepositoryTest(Relationships) assertContentEquals(expected2, actual2) } + @Test + fun save_ドメインイベントがパブリッシュされる() = runTest { + dbSetup(to = dataSource) { + execute(disableReferenceIntegrityConstraints) + }.launch() + + val relationship = Relationship( + actorId = ActorId(1), + targetActorId = ActorId(2), + following = false, + blocking = true, + muting = false, + followRequesting = false, + mutingFollowRequest = false, + ) + + relationship.block() + + repository.save(relationship) + + TransactionManager.current().commit() + + verify(domainEventPublisher, times(1)).publishEvent(any()) + } + + @Test + fun delete_ドメインイベントがパブリッシュされる() = runTest { + dbSetup(to = dataSource) { + execute(disableReferenceIntegrityConstraints) + insertInto(Relationships.tableName) { + columns(Relationships.columns) + values(1, 2, true, false, false, false, false) + } + }.launch() + + val relationship = Relationship( + actorId = ActorId(1), + targetActorId = ActorId(2), + following = false, + blocking = true, + muting = false, + followRequesting = false, + mutingFollowRequest = false, + ) + relationship.block() + + repository.delete(relationship) + + TransactionManager.current().commit() + + verify(domainEventPublisher, times(1)).publishEvent(any()) + } } \ No newline at end of file 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 index 2802bc3d..82e7e5a7 100644 --- 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 @@ -1,20 +1,27 @@ package dev.usbharu.hideout.core.infrastructure.exposedrepository import com.ninja_squad.dbsetup_kotlin.dbSetup +import dev.usbharu.hideout.core.domain.model.actor.ActorId 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.UserDetail +import dev.usbharu.hideout.core.domain.model.userdetails.UserDetailHashedPassword 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.core.api.Assertions.assertThat import org.assertj.db.api.Assertions.assertThat +import org.jetbrains.exposed.sql.transactions.TransactionManager 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 org.mockito.kotlin.any +import org.mockito.kotlin.times +import org.mockito.kotlin.verify import utils.* import kotlin.test.assertEquals import kotlin.test.assertNotNull @@ -47,13 +54,9 @@ class ExposedTimelineRepositoryTest : AbstractRepositoryTest(Timelines) { 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) + 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 @@ -80,13 +83,9 @@ class ExposedTimelineRepositoryTest : AbstractRepositoryTest(Timelines) { 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) + 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 @@ -115,10 +114,8 @@ class ExposedTimelineRepositoryTest : AbstractRepositoryTest(Timelines) { repository.delete(timeline) } - assertThat(change) - .changeOfDeletionOnTable(Timelines.tableName) - .rowAtStartPoint() - .value(Timelines.id.name).isEqualTo(timeline.id.value) + assertThat(change).changeOfDeletionOnTable(Timelines.tableName).rowAtStartPoint().value(Timelines.id.name) + .isEqualTo(timeline.id.value) } @Test @@ -135,8 +132,7 @@ class ExposedTimelineRepositoryTest : AbstractRepositoryTest(Timelines) { val findByIds = repository.findByIds(listOf(TimelineId(1), TimelineId(3))) - assertThat(findByIds) - .hasSize(2) + assertThat(findByIds).hasSize(2) } @Test @@ -186,7 +182,69 @@ class ExposedTimelineRepositoryTest : AbstractRepositoryTest(Timelines) { val timelines = repository.findAllByUserDetailIdAndVisibilityIn(UserDetailId(1), listOf(TimelineVisibility.PUBLIC)) - assertThat(timelines) - .hasSize(2) + assertThat(timelines).hasSize(2) + } + + @Test + fun save_ドメインイベントがパブリッシュされる() = runTest { + dbSetup(to = dataSource) { + execute(disableReferenceIntegrityConstraints) + }.launch() + val timeline = Timeline( + id = TimelineId(1), + userDetailId = UserDetailId(1), + name = TimelineName("timeline"), + visibility = TimelineVisibility.PRIVATE, + isSystem = false + ) + + timeline.setVisibility( + TimelineVisibility.PUBLIC, UserDetail.create( + UserDetailId(1), ActorId(1), + UserDetailHashedPassword("aaaaaa"), + ) + ) + + repository.save(timeline) + + TransactionManager.current().commit() + + verify(domainEventPublisher, times(1)).publishEvent(any()) + } + + @Test + fun delete_ドメインイベントがパブリッシュされる() = 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 + ) + + timeline.setVisibility( + TimelineVisibility.PUBLIC, UserDetail.create( + UserDetailId(1), ActorId(1), + UserDetailHashedPassword("aaaaaa"), + ) + ) + + repository.delete(timeline) + + TransactionManager.current().commit() + + verify(domainEventPublisher, times(1)).publishEvent(any()) } }