test: テストを追加

This commit is contained in:
usbharu 2024-02-14 14:04:37 +09:00
parent 0fec05fa49
commit 240b82b66c
2 changed files with 267 additions and 0 deletions

View File

@ -214,4 +214,175 @@ class MuteProcessServiceImplTest {
assertThat(actual).isEqualTo(FilterResult(filterQueryModel, "e t"))
}
@Test
fun cw文字にマッチする() = runTest {
val muteProcessServiceImpl = MuteProcessServiceImpl()
val post = PostBuilder.of(overview = "mute test", text = "hello")
val filterQueryModel = FilterQueryModel(
1,
2,
"mute test",
FilterType.entries,
FilterAction.warn,
listOf(
FilterKeyword(
1,
1,
"e\\st",
FilterMode.REGEX
)
)
)
val actual = muteProcessServiceImpl.processMute(
post, FilterType.entries.toList(), listOf(
filterQueryModel
)
)
assertThat(actual).isEqualTo(FilterResult(filterQueryModel, "e t"))
}
@Test
fun 文字列と単語と正規表現を同時に使える() = runTest {
val muteProcessServiceImpl = MuteProcessServiceImpl()
val post = PostBuilder.of(text = "mute test")
val filterQueryModel = FilterQueryModel(
1,
2,
"mute test",
FilterType.entries,
FilterAction.warn,
listOf(
FilterKeyword(
1,
1,
"e\\st",
FilterMode.REGEX
),
FilterKeyword(
2,
1,
"mute",
FilterMode.NONE
),
FilterKeyword(
3,
1,
"test",
FilterMode.WHOLE_WORD
)
)
)
val actual = muteProcessServiceImpl.processMute(
post, FilterType.entries.toList(), listOf(
filterQueryModel
)
)
assertThat(actual).isEqualTo(FilterResult(filterQueryModel, "mute"))
}
@Test
fun 複数の投稿を処理できる() = runTest {
val muteProcessServiceImpl = MuteProcessServiceImpl()
val filterQueryModel = FilterQueryModel(
1,
2,
"mute test",
FilterType.entries,
FilterAction.warn,
listOf(
FilterKeyword(
1,
1,
"mute",
FilterMode.NONE
)
)
)
val posts = listOf(
PostBuilder.of(text = "mute"), PostBuilder.of(text = "mutes"), PostBuilder.of(text = "hoge")
)
val actual = muteProcessServiceImpl.processMutes(
posts,
FilterType.entries.toList(),
listOf(
filterQueryModel
)
)
assertThat(actual)
.hasSize(2)
.containsEntry(posts[0], FilterResult(filterQueryModel, "mute"))
.containsEntry(posts[1], FilterResult(filterQueryModel, "mute"))
}
@Test
fun 何もマッチしないとnullが返ってくる() = runTest {
val muteProcessServiceImpl = MuteProcessServiceImpl()
val post = PostBuilder.of(text = "mute test")
val filterQueryModel = FilterQueryModel(
1,
2,
"mute test",
FilterType.entries,
FilterAction.warn,
listOf(
FilterKeyword(
1,
1,
"fuga",
FilterMode.NONE
)
)
)
val actual = muteProcessServiceImpl.processMute(
post, FilterType.entries.toList(), listOf(
filterQueryModel
)
)
assertThat(actual).isNull()
}
@Test
fun Cwで何もマッチしないと本文を確認する() = runTest {
val muteProcessServiceImpl = MuteProcessServiceImpl()
val post = PostBuilder.of(overview = "hage", text = "mute test")
val filterQueryModel = FilterQueryModel(
1,
2,
"mute test",
FilterType.entries,
FilterAction.warn,
listOf(
FilterKeyword(
1,
1,
"fuga",
FilterMode.NONE
)
)
)
val actual = muteProcessServiceImpl.processMute(
post, FilterType.entries.toList(), listOf(
filterQueryModel
)
)
assertThat(actual).isNull()
}
}

View File

@ -0,0 +1,96 @@
package dev.usbharu.hideout.core.service.filter
import dev.usbharu.hideout.core.domain.model.filter.*
import dev.usbharu.hideout.core.domain.model.filterkeyword.FilterKeywordRepository
import dev.usbharu.hideout.core.query.model.FilterQueryModel
import dev.usbharu.hideout.core.query.model.FilterQueryService
import kotlinx.coroutines.test.runTest
import org.assertj.core.api.Assertions.assertThat
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.*
@ExtendWith(MockitoExtension::class)
class MuteServiceImplTest {
@Mock
private lateinit var filterRepository: FilterRepository
@Mock
private lateinit var filterKeywordRepository: FilterKeywordRepository
@Mock
private lateinit var filterQueryService: FilterQueryService
@InjectMocks
private lateinit var muteServiceImpl: MuteServiceImpl
@Test
fun createFilter() = runTest {
whenever(filterRepository.generateId()).doReturn(1)
whenever(filterKeywordRepository.generateId()).doReturn(1)
whenever(filterRepository.save(any())).doAnswer { it.arguments[0]!! as Filter }
val createFilter = muteServiceImpl.createFilter(
title = "hoge",
context = listOf(FilterType.home, FilterType.public),
action = FilterAction.warn,
keywords = listOf(
FilterKeyword(
"fuga",
FilterMode.NONE
)
),
loginUser = 1
)
assertThat(createFilter).isEqualTo(
FilterQueryModel(
1,
1,
"hoge",
listOf(FilterType.home, FilterType.public),
FilterAction.warn,
keywords = listOf(
dev.usbharu.hideout.core.domain.model.filterkeyword.FilterKeyword(1, 1, "fuga", FilterMode.NONE)
)
)
)
}
@Test
fun getFilters() = runTest {
whenever(filterQueryService.findByUserIdAndType(any(), any())).doReturn(
listOf(
FilterQueryModel(
1,
1,
"hoge",
listOf(FilterType.home),
FilterAction.warn,
listOf(
dev.usbharu.hideout.core.domain.model.filterkeyword.FilterKeyword(
1,
1,
"fuga",
FilterMode.NONE
)
)
)
)
)
muteServiceImpl.getFilters(1, listOf(FilterType.home))
}
@Test
fun `getFilters 何も指定しない`() = runTest {
whenever(filterQueryService.findByUserIdAndType(any(), eq(emptyList()))).doReturn(emptyList())
muteServiceImpl.getFilters(1)
}
}