test: postのテストを追加

This commit is contained in:
usbharu 2024-06-05 11:47:19 +09:00
parent 5de217fecc
commit 2c298d2d26
5 changed files with 71 additions and 11 deletions

View File

@ -19,6 +19,7 @@ package dev.usbharu.hideout.core.domain.model.post
import dev.usbharu.hideout.core.domain.event.post.PostDomainEventFactory
import dev.usbharu.hideout.core.domain.event.post.PostEvent
import dev.usbharu.hideout.core.domain.model.actor.ActorId
import dev.usbharu.hideout.core.domain.model.emoji.EmojiId
import dev.usbharu.hideout.core.domain.model.media.MediaId
import dev.usbharu.hideout.core.domain.shared.domainevent.DomainEventStorable
import java.net.URI
@ -38,7 +39,7 @@ class Post(
val apId: URI,
deleted: Boolean,
mediaIds: List<MediaId>,
visibleActors: List<ActorId> = emptyList(),
visibleActors: Set<ActorId> = emptySet(),
hide: Boolean = false,
moveTo: PostId? = null,
) : DomainEventStorable() {
@ -71,7 +72,7 @@ class Post(
require(deleted.not())
if (visibility == Visibility.DIRECT) {
addDomainEvent(PostDomainEventFactory(this).createEvent(PostEvent.update))
field = field.plus(value).distinct()
field = field.plus(value)
}
}
@ -114,11 +115,21 @@ class Post(
field = value
}
val text
get() = content.text
val text: String
get() {
if (hide) {
return PostContent.empty.text
}
return content.text
}
val emojiIds
get() = content.emojiIds
val emojiIds: List<EmojiId>
get() {
if (hide) {
return PostContent.empty.emojiIds
}
return content.emojiIds
}
var mediaIds = mediaIds
get() {
@ -207,7 +218,7 @@ class Post(
apId: URI,
deleted: Boolean,
mediaIds: List<MediaId>,
visibleActors: List<ActorId> = emptyList(),
visibleActors: Set<ActorId> = emptySet(),
hide: Boolean = false,
moveTo: PostId? = null,
): Post {

View File

@ -57,7 +57,7 @@ class PostQueryMapper(private val postResultRowMapper: ResultRowMapper<Post>) :
resultRow
.getOrNull(PostsVisibleActors.actorId)
?.let { actorId -> ActorId(actorId) }
}
}.toSet()
clearDomainEvents()
}
}

View File

@ -40,7 +40,7 @@ class PostResultRowMapper : ResultRowMapper<Post> {
apId = URI.create(resultRow[Posts.apId]),
deleted = resultRow[Posts.deleted],
mediaIds = emptyList(),
visibleActors = emptyList(),
visibleActors = emptySet(),
hide = resultRow[Posts.hide],
moveTo = resultRow[Posts.moveTo]?.let { PostId(it) }
)

View File

@ -139,7 +139,56 @@ class PostTest {
val post = TestPostFactory.create(deleted = true)
assertThrows<IllegalArgumentException> {
post.visibleActors = listOf(ActorId(100))
post.visibleActors = setOf(ActorId(100))
}
}
@Test
fun visibilityがDIRECTの時visibleActorsを変更できる() {
val post = TestPostFactory.create(visibility = Visibility.DIRECT)
post.visibleActors = setOf(ActorId(100))
assertEquals(setOf(ActorId(100)), post.visibleActors)
}
@Test
fun visibleActorsから削除されることはない() {
val post = TestPostFactory.create(visibility = Visibility.DIRECT, visibleActors = listOf(100))
post.visibleActors = setOf(ActorId(200))
assertEquals(setOf(ActorId(100), ActorId(200)), post.visibleActors)
}
@Test
fun visibleActorsに追加された時updateイベントが発生する() {
val post = TestPostFactory.create(visibility = Visibility.DIRECT)
post.visibleActors = setOf(ActorId(100))
assertContainsEvent(post, PostEvent.update.eventName)
}
@Test
fun hideがtrueのときcontetnがemptyを返す() {
val post = TestPostFactory.create(hide = true)
assertEquals(PostContent.empty, post.content)
}
@Test
fun deletedがtrueの時contentをセットできない() {
val post = TestPostFactory.create(deleted = true)
assertThrows<IllegalArgumentException> {
post.content = PostContent("test", "test", emptyList())
}
}
@Test
fun contentの内容が変更されたらupdateイベントが発生する() {
val post = TestPostFactory.create()
post.content = PostContent("test", "test", emptyList())
assertContainsEvent(post, PostEvent.update.eventName)
}
}

View File

@ -42,7 +42,7 @@ object TestPostFactory {
apId = apId,
deleted = deleted,
mediaIds.map { MediaId(it) },
visibleActors.map { ActorId(it) },
visibleActors.map { ActorId(it) }.toSet(),
hide = hide,
moveTo?.let { PostId(it) }
)