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

View File

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

View File

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

View File

@ -139,7 +139,56 @@ class PostTest {
val post = TestPostFactory.create(deleted = true) val post = TestPostFactory.create(deleted = true)
assertThrows<IllegalArgumentException> { 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, apId = apId,
deleted = deleted, deleted = deleted,
mediaIds.map { MediaId(it) }, mediaIds.map { MediaId(it) },
visibleActors.map { ActorId(it) }, visibleActors.map { ActorId(it) }.toSet(),
hide = hide, hide = hide,
moveTo?.let { PostId(it) } moveTo?.let { PostId(it) }
) )