package roomescape.theme.implement import io.kotest.assertions.throwables.shouldThrow import io.kotest.core.spec.style.FunSpec import io.kotest.matchers.collections.shouldHaveSize import io.kotest.matchers.shouldBe import io.mockk.every import io.mockk.mockk import io.mockk.verify import org.springframework.data.repository.findByIdOrNull import roomescape.theme.exception.ThemeErrorCode import roomescape.theme.exception.ThemeException import roomescape.theme.infrastructure.persistence.ThemeRepository import roomescape.util.ThemeFixture import java.time.LocalDate class ThemeFinderTest : FunSpec({ val themeRepository: ThemeRepository = mockk() val themeFinder = ThemeFinder(themeRepository) context("findAll") { test("모든 테마를 조회한다.") { every { themeRepository.findAll() } returns listOf(mockk(), mockk(), mockk()) themeRepository.findAll() shouldHaveSize 3 } } context("findById") { val timeId = 1L test("동일한 ID인 테마를 찾아 응답한다.") { every { themeRepository.findByIdOrNull(timeId) } returns mockk() themeFinder.findById(timeId) verify(exactly = 1) { themeRepository.findByIdOrNull(timeId) } } test("동일한 ID인 테마가 없으면 실패한다.") { every { themeRepository.findByIdOrNull(timeId) } returns null shouldThrow { themeFinder.findById(timeId) }.also { it.errorCode shouldBe ThemeErrorCode.THEME_NOT_FOUND } } } context("findMostReservedThemes") { test("입력된 개수보다 조회된 개수가 작으면 조회된 개수만큼 반환한다.") { val count = 10 val startFrom = LocalDate.now().minusDays(7) val endAt = LocalDate.now().minusDays(1) every { themeRepository.findPopularThemes(startFrom, endAt, count) } returns listOf( ThemeFixture.create(id = 1, name = "name1"), ThemeFixture.create(id = 2, name = "name2"), ThemeFixture.create(id = 3, name = "name3"), ) themeFinder.findMostReservedThemes(count, startFrom, endAt) shouldHaveSize 3 } } })