From c83b990a7880501aa9357b931a2f43f119afa0e7 Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Mon, 8 Jan 2024 17:30:23 +0900 Subject: [PATCH] =?UTF-8?q?test:=20=E3=83=AA=E3=82=A2=E3=82=AF=E3=82=B7?= =?UTF-8?q?=E3=83=A7=E3=83=B3=E9=87=8D=E8=A4=87=E6=99=82=E3=81=AE=E6=8C=99?= =?UTF-8?q?=E5=8B=95=E3=81=AE=E5=A4=89=E6=9B=B4=E3=82=92=E3=83=86=E3=82=B9?= =?UTF-8?q?=E3=83=88=E3=81=AB=E5=8F=8D=E6=98=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../reaction/ReactionServiceImplTest.kt | 45 ++++--------------- 1 file changed, 9 insertions(+), 36 deletions(-) diff --git a/src/test/kotlin/dev/usbharu/hideout/core/service/reaction/ReactionServiceImplTest.kt b/src/test/kotlin/dev/usbharu/hideout/core/service/reaction/ReactionServiceImplTest.kt index 2f54b06f..f3863d64 100644 --- a/src/test/kotlin/dev/usbharu/hideout/core/service/reaction/ReactionServiceImplTest.kt +++ b/src/test/kotlin/dev/usbharu/hideout/core/service/reaction/ReactionServiceImplTest.kt @@ -7,7 +7,6 @@ import dev.usbharu.hideout.core.domain.model.emoji.UnicodeEmoji import dev.usbharu.hideout.core.domain.model.reaction.Reaction import dev.usbharu.hideout.core.domain.model.reaction.ReactionRepository import kotlinx.coroutines.test.runTest -import org.jetbrains.exposed.exceptions.ExposedSQLException import org.junit.jupiter.api.Test import org.junit.jupiter.api.extension.ExtendWith import org.mockito.InjectMocks @@ -33,7 +32,7 @@ class ReactionServiceImplTest { val post = PostBuilder.of() - whenever(reactionRepository.existByPostIdAndActorIdAndEmojiId(eq(post.id), eq(post.actorId), eq(0))).doReturn( + whenever(reactionRepository.existByPostIdAndActor(eq(post.id), eq(post.actorId))).doReturn( false ) val generateId = TwitterSnowflakeIdGenerateService.generateId() @@ -44,48 +43,22 @@ class ReactionServiceImplTest { verify(reactionRepository, times(1)).save(eq(Reaction(generateId, UnicodeEmoji("❤"), post.id, post.actorId))) } - @Test - fun `receiveReaction リアクションが既に作成されていることを検知出来ずに例外が発生した場合は何もしない`() = runTest { - val post = PostBuilder.of() - - whenever(reactionRepository.existByPostIdAndActorIdAndEmojiId(eq(post.id), eq(post.actorId), eq(0))).doReturn( - false - ) - val generateId = TwitterSnowflakeIdGenerateService.generateId() - whenever( - reactionRepository.save( - eq( - Reaction( - id = generateId, - emoji = UnicodeEmoji("❤"), - postId = post.id, - actorId = post.actorId - ) - ) - ) - ).doAnswer { - throw ExposedSQLException( - null, - emptyList(), mock() - ) - } - whenever(reactionRepository.generateId()).doReturn(generateId) - - reactionServiceImpl.receiveReaction(UnicodeEmoji("❤"), post.actorId, post.id) - - verify(reactionRepository, times(1)).save(eq(Reaction(generateId, UnicodeEmoji("❤"), post.id, post.actorId))) - } @Test - fun `receiveReaction リアクションが既に作成されている場合は何もしない`() = runTest() { + fun `receiveReaction リアクションが既に作成されている場合削除して新しく作成`() = runTest() { val post = PostBuilder.of() - whenever(reactionRepository.existByPostIdAndActorIdAndEmojiId(eq(post.id), eq(post.actorId), eq(0))).doReturn( + whenever(reactionRepository.existByPostIdAndActor(eq(post.id), eq(post.actorId))).doReturn( true ) + val generateId = TwitterSnowflakeIdGenerateService.generateId() + + whenever(reactionRepository.generateId()).doReturn(generateId) + reactionServiceImpl.receiveReaction(UnicodeEmoji("❤"), post.actorId, post.id) - verify(reactionRepository, never()).save(any()) + verify(reactionRepository, times(1)).deleteByPostIdAndActorId(post.id, post.actorId) + verify(reactionRepository, times(1)).save(Reaction(generateId, UnicodeEmoji("❤"), post.id, post.actorId)) } @Test