test: postのテストを追加

This commit is contained in:
usbharu 2024-06-05 10:45:10 +09:00
parent 5e7b538d1d
commit 5de217fecc
6 changed files with 206 additions and 16 deletions

View File

@ -54,6 +54,7 @@ class Post(
var visibility = visibility
set(value) {
require(visibility != Visibility.DIRECT)
require(value != Visibility.DIRECT)
require(field.ordinal >= value.ordinal)

View File

@ -12,7 +12,7 @@ import kotlin.test.assertNull
class ActorsTest {
@Test
fun suspendがtrueのときactorSuspendイベントが発生する() {
val actor = TestActor2Factory.create(publicKey = ActorPublicKey(""))
val actor = TestActorFactory.create(publicKey = ActorPublicKey(""))
actor.suspend = true
@ -21,7 +21,7 @@ class ActorsTest {
@Test
fun suspendがfalseになったときactorUnsuspendイベントが発生する() {
val actor = TestActor2Factory.create(publicKey = ActorPublicKey(""), suspend = true)
val actor = TestActorFactory.create(publicKey = ActorPublicKey(""), suspend = true)
actor.suspend = false
@ -30,7 +30,7 @@ class ActorsTest {
@Test
fun alsoKnownAsに自分自身が含まれない場合更新される() {
val actor = TestActor2Factory.create(publicKey = ActorPublicKey(""))
val actor = TestActorFactory.create(publicKey = ActorPublicKey(""))
val actorIds = setOf(ActorId(100), ActorId(200))
actor.alsoKnownAs = actorIds
@ -40,7 +40,7 @@ class ActorsTest {
@Test
fun moveToに自分自身が設定された場合moveイベントが発生し更新される() {
val actor = TestActor2Factory.create(publicKey = ActorPublicKey(""))
val actor = TestActorFactory.create(publicKey = ActorPublicKey(""))
actor.moveTo = ActorId(100)
@ -50,7 +50,7 @@ class ActorsTest {
@Test
fun alsoKnownAsに自分自身が含まれてはいけない() {
val actor = TestActor2Factory.create(publicKey = ActorPublicKey(""))
val actor = TestActorFactory.create(publicKey = ActorPublicKey(""))
assertThrows<IllegalArgumentException> {
actor.alsoKnownAs = setOf(actor.id)
@ -59,7 +59,7 @@ class ActorsTest {
@Test
fun moveToに自分自身が設定されてはいけない() {
val actor = TestActor2Factory.create(publicKey = ActorPublicKey(""))
val actor = TestActorFactory.create(publicKey = ActorPublicKey(""))
assertThrows<IllegalArgumentException> {
actor.moveTo = actor.id
@ -68,7 +68,7 @@ class ActorsTest {
@Test
fun descriptionが更新されたときupdateイベントが発生する() {
val actor = TestActor2Factory.create(publicKey = ActorPublicKey(""))
val actor = TestActorFactory.create(publicKey = ActorPublicKey(""))
actor.description = ActorDescription("hoge fuga")
@ -77,7 +77,7 @@ class ActorsTest {
@Test
fun screenNameが更新されたときupdateイベントが発生する() {
val actor = TestActor2Factory.create(publicKey = ActorPublicKey(""))
val actor = TestActorFactory.create(publicKey = ActorPublicKey(""))
actor.screenName = ActorScreenName("fuga hoge")
@ -86,7 +86,7 @@ class ActorsTest {
@Test
fun deleteが実行されたときすでにdeletedがtrueなら何もしない() {
val actor = TestActor2Factory.create(publicKey = ActorPublicKey(""), deleted = true)
val actor = TestActorFactory.create(publicKey = ActorPublicKey(""), deleted = true)
actor.delete()
@ -95,7 +95,7 @@ class ActorsTest {
@Test
fun deleteが実行されたときdeletedがfalseならdeleteイベントが発生する() {
val actor = TestActor2Factory.create(publicKey = ActorPublicKey(""))
val actor = TestActorFactory.create(publicKey = ActorPublicKey(""))
actor.delete()
@ -111,7 +111,7 @@ class ActorsTest {
@Test
fun restoreが実行されたときcheckUpdateイベントが発生する() {
val actor = TestActor2Factory.create(publicKey = ActorPublicKey(""), deleted = true)
val actor = TestActorFactory.create(publicKey = ActorPublicKey(""), deleted = true)
actor.restore()
@ -121,7 +121,7 @@ class ActorsTest {
@Test
fun checkUpdateが実行されたときcheckUpdateイベントがh() {
val actor = TestActor2Factory.create(publicKey = ActorPublicKey(""))
val actor = TestActorFactory.create(publicKey = ActorPublicKey(""))
actor.checkUpdate()

View File

@ -8,7 +8,7 @@ import kotlinx.coroutines.runBlocking
import java.net.URI
import java.time.Instant
object TestActor2Factory {
object TestActorFactory {
private val idGenerateService = TwitterSnowflakeIdGenerateService
fun create(

View File

@ -1,10 +1,145 @@
package dev.usbharu.hideout.core.domain.model.post
import dev.usbharu.hideout.core.domain.event.post.PostEvent
import dev.usbharu.hideout.core.domain.model.actor.ActorId
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertDoesNotThrow
import org.junit.jupiter.api.assertThrows
import utils.AssertDomainEvent.assertContainsEvent
import utils.AssertDomainEvent.assertEmpty
import kotlin.test.assertEquals
class PostTest {
@Test
fun deletedがtrueのときghostのidが返される() {
val post = TestPostFactory.create(deleted = true)
assertEquals(ActorId.ghost, post.actorId)
}
@Test
fun deletedがfalseの時actorのIDが返される() {
val post = TestPostFactory.create(deleted = false, actorId = 100)
assertEquals(ActorId(100), post.actorId)
}
@Test
fun visibilityがDIRECTのとき変更できない() {
val post = TestPostFactory.create(visibility = Visibility.DIRECT)
assertThrows<IllegalArgumentException> {
post.visibility = Visibility.PUBLIC
}
assertThrows<IllegalArgumentException> {
post.visibility = Visibility.UNLISTED
}
assertThrows<IllegalArgumentException> {
post.visibility = Visibility.FOLLOWERS
}
}
@Test
fun visibilityを小さくすることはできないPUBLIC() {
val post = TestPostFactory.create(visibility = Visibility.PUBLIC)
assertThrows<IllegalArgumentException> {
post.visibility = Visibility.DIRECT
}
assertThrows<IllegalArgumentException> {
post.visibility = Visibility.UNLISTED
}
assertThrows<IllegalArgumentException> {
post.visibility = Visibility.FOLLOWERS
}
}
@Test
fun visibilityを小さくすることはできないUNLISTED() {
val post = TestPostFactory.create(visibility = Visibility.UNLISTED)
assertThrows<IllegalArgumentException> {
post.visibility = Visibility.DIRECT
}
assertThrows<IllegalArgumentException> {
post.visibility = Visibility.FOLLOWERS
}
}
@Test
fun visibilityを小さくすることはできないFOLLOWERS() {
val post = TestPostFactory.create(visibility = Visibility.FOLLOWERS)
assertThrows<IllegalArgumentException> {
post.visibility = Visibility.DIRECT
}
}
@Test
fun visibilityをDIRECTにあとからすることはできない() {
val post = TestPostFactory.create(visibility = Visibility.DIRECT)
assertThrows<IllegalArgumentException> {
post.visibility = Visibility.DIRECT
}
}
@Test
fun visibilityを大きくすることができるFOLLOWERS() {
val post = TestPostFactory.create(visibility = Visibility.FOLLOWERS)
assertDoesNotThrow {
post.visibility = Visibility.UNLISTED
}
val post2 = TestPostFactory.create(visibility = Visibility.FOLLOWERS)
assertDoesNotThrow {
post2.visibility = Visibility.PUBLIC
}
}
@Test
fun visibilityを大きくすることができるUNLISTED() {
val post = TestPostFactory.create(visibility = Visibility.UNLISTED)
assertDoesNotThrow {
post.visibility = Visibility.PUBLIC
}
}
@Test
fun deletedがtrueのときvisibilityを変更できない() {
val post = TestPostFactory.create(visibility = Visibility.UNLISTED, deleted = true)
assertThrows<IllegalArgumentException> {
post.visibility = Visibility.PUBLIC
}
}
@Test
fun visibilityが変更されない限りドメインイベントは発生しない() {
val post = TestPostFactory.create(visibility = Visibility.UNLISTED)
post.visibility = Visibility.UNLISTED
assertEmpty(post)
}
@Test
fun visibilityが変更されるとupdateイベントが発生する() {
val post = TestPostFactory.create(visibility = Visibility.UNLISTED)
post.visibility = Visibility.PUBLIC
assertContainsEvent(post, PostEvent.update.eventName)
}
@Test
fun deletedがtrueのときvisibleActorsを変更できない() {
val post = TestPostFactory.create(deleted = true)
assertThrows<IllegalArgumentException> {
post.visibleActors = listOf(ActorId(100))
}
}
}

View File

@ -0,0 +1,54 @@
package dev.usbharu.hideout.core.domain.model.post
import dev.usbharu.hideout.core.domain.model.actor.ActorId
import dev.usbharu.hideout.core.domain.model.media.MediaId
import dev.usbharu.hideout.core.infrastructure.other.TwitterSnowflakeIdGenerateService
import kotlinx.coroutines.runBlocking
import java.net.URI
import java.time.Instant
object TestPostFactory {
private val idGenerateService = TwitterSnowflakeIdGenerateService
fun create(
id: Long = generateId(),
actorId: Long = 1,
overview: String? = null,
content: String = "This is test content",
createdAt: Instant = Instant.now(),
visibility: Visibility = Visibility.PUBLIC,
url: URI = URI.create("https://example.com/$actorId/posts/$id"),
repostId: Long? = null,
replyId: Long? = null,
sensitive: Boolean = false,
apId: URI = URI.create("https://example.com/$actorId/posts/$id"),
deleted: Boolean = false,
mediaIds: List<Long> = emptyList(),
visibleActors: List<Long> = emptyList(),
hide: Boolean = false,
moveTo: Long? = null,
): Post {
return Post(
PostId(id),
ActorId(actorId),
overview = overview?.let { PostOverview(it) },
content = PostContent(content, content, emptyList()),
createdAt = createdAt,
visibility = visibility,
url = url,
repostId = repostId?.let { PostId(it) },
replyId?.let { PostId(it) },
sensitive = sensitive,
apId = apId,
deleted = deleted,
mediaIds.map { MediaId(it) },
visibleActors.map { ActorId(it) },
hide = hide,
moveTo?.let { PostId(it) }
)
}
private fun generateId(): Long = runBlocking {
idGenerateService.generateId()
}
}

View File

@ -2,7 +2,7 @@ package dev.usbharu.hideout.core.domain.service.actor
import dev.usbharu.hideout.core.config.ApplicationConfig
import dev.usbharu.hideout.core.domain.model.actor.ActorPublicKey
import dev.usbharu.hideout.core.domain.model.actor.TestActor2Factory
import dev.usbharu.hideout.core.domain.model.actor.TestActorFactory
import org.junit.jupiter.api.Test
import java.net.URI
import kotlin.test.assertFalse
@ -11,7 +11,7 @@ import kotlin.test.assertTrue
class RemoteActorCheckDomainServiceTest {
@Test
fun リモートのドメインならtrueを返す() {
val actor = TestActor2Factory.create(publicKey = ActorPublicKey(""))
val actor = TestActorFactory.create(publicKey = ActorPublicKey(""))
val remoteActor = RemoteActorCheckDomainService(
ApplicationConfig(
@ -26,7 +26,7 @@ class RemoteActorCheckDomainServiceTest {
@Test
fun ローカルのActorならfalseを返す() {
val actor = TestActor2Factory.create(domain = "local.example.com", publicKey = ActorPublicKey(""))
val actor = TestActorFactory.create(domain = "local.example.com", publicKey = ActorPublicKey(""))
val localActor = RemoteActorCheckDomainService(
ApplicationConfig(