From 23ea8d13ea63dfddf770be3272e1fb9d7321a665 Mon Sep 17 00:00:00 2001 From: pricelees Date: Sat, 27 Sep 2025 15:19:59 +0900 Subject: [PATCH] =?UTF-8?q?test:=20=EC=9D=B8=EA=B8=B0=20=ED=85=8C=EB=A7=88?= =?UTF-8?q?=20=EA=B4=80=EB=A0=A8=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../reservation/ReservationApiTest.kt | 95 ------------- .../kotlin/roomescape/theme/ThemeApiTest.kt | 131 ++++++++++++++---- 2 files changed, 103 insertions(+), 123 deletions(-) diff --git a/src/test/kotlin/roomescape/reservation/ReservationApiTest.kt b/src/test/kotlin/roomescape/reservation/ReservationApiTest.kt index 53be12c3..6f6d9f8a 100644 --- a/src/test/kotlin/roomescape/reservation/ReservationApiTest.kt +++ b/src/test/kotlin/roomescape/reservation/ReservationApiTest.kt @@ -7,9 +7,7 @@ import org.springframework.data.repository.findByIdOrNull import org.springframework.http.HttpMethod import org.springframework.http.HttpStatus import roomescape.auth.exception.AuthErrorCode -import roomescape.common.config.next import roomescape.common.exception.CommonErrorCode -import roomescape.common.util.DateUtils import roomescape.payment.infrastructure.common.BankCode import roomescape.payment.infrastructure.common.CardIssuerCode import roomescape.payment.infrastructure.common.EasyPayCompanyCode @@ -19,7 +17,6 @@ import roomescape.reservation.infrastructure.persistence.CanceledReservationRepo import roomescape.reservation.infrastructure.persistence.ReservationEntity import roomescape.reservation.infrastructure.persistence.ReservationRepository import roomescape.reservation.infrastructure.persistence.ReservationStatus -import roomescape.reservation.web.MostReservedThemeIdListResponse import roomescape.reservation.web.ReservationCancelRequest import roomescape.reservation.web.ReservationOverviewResponse import roomescape.schedule.infrastructure.persistence.ScheduleEntity @@ -28,8 +25,6 @@ import roomescape.schedule.infrastructure.persistence.ScheduleStatus import roomescape.supports.* import roomescape.theme.infrastructure.persistence.ThemeEntity import roomescape.theme.infrastructure.persistence.ThemeRepository -import roomescape.theme.web.toEntity -import roomescape.user.infrastructure.persistence.UserEntity import java.time.LocalDate import java.time.LocalTime @@ -566,24 +561,6 @@ class ReservationApiTest( ) } } - - context("가장 많이 예약된 테마 ID를 조회한다.") { - test("정상 응답") { - val expectedResult: MostReservedThemeIdListResponse = initializeForPopularThemeTest() - - runTest( - on = { - get("/reservations/popular-themes?count=10") - }, - expect = { - statusCode(HttpStatus.OK.value()) - } - ).also { - val result: List = it.extract().path("data.themeIds") - result shouldBe expectedResult.themeIds - } - } - } } fun runDetailRetrieveTest( @@ -605,76 +582,4 @@ class ReservationApiTest( it.extract().path("data.user.id") shouldBe reservation.userId }.extract().path("data.payment") } - - private fun initializeForPopularThemeTest(): MostReservedThemeIdListResponse { - val user: UserEntity = testAuthUtil.defaultUser() - - val themeIds: List = (1..5).map { - themeRepository.save(ThemeFixture.createRequest.copy().toEntity(id = tsidFactory.next())).id - } - - val store = dummyInitializer.createStore() - - // 첫 번째 테마: 유효한 2개 예약 - (1L..2L).forEach { - dummyInitializer.createConfirmReservation( - user = user, - storeId = store.id, - scheduleRequest = ScheduleFixture.createRequest.copy( - date = DateUtils.getSundayOfPreviousWeek(LocalDate.now()).plusDays(it), - themeId = themeIds[0], - ) - ) - } - - // 두 번째 테마: 유효한 1개 예약 - dummyInitializer.createConfirmReservation( - user = user, - storeId = store.id, - scheduleRequest = ScheduleFixture.createRequest.copy( - date = DateUtils.getSundayOfPreviousWeek(LocalDate.now()), - themeId = themeIds[1], - ) - ) - - // 세 번째 테마: 유효한 3개 예약 - (1L..3L).forEach { - dummyInitializer.createConfirmReservation( - user = user, - storeId = store.id, - scheduleRequest = ScheduleFixture.createRequest.copy( - date = DateUtils.getSundayOfPreviousWeek(LocalDate.now()).plusDays(it), - themeId = themeIds[2], - ) - ) - } - - // 네 번째 테마: Pending 상태인 3개 예약 -> 집계되지 않음. - (1L..3L).forEach { - dummyInitializer.createPendingReservation( - user = user, - storeId = store.id, - scheduleRequest = ScheduleFixture.createRequest.copy( - date = DateUtils.getSundayOfPreviousWeek(LocalDate.now()).plusDays(it), - themeId = themeIds[3], - ) - ) - } - - // 다섯 번째 테마: 이번주의 확정 예약 -> 집계되지 않음. - (1L..3L).forEach { i -> - val thisMonday = DateUtils.getSundayOfPreviousWeek(LocalDate.now()).plusDays(8) - dummyInitializer.createConfirmReservation( - user = user, - storeId = store.id, - scheduleRequest = ScheduleFixture.createRequest.copy( - date = thisMonday.plusDays(i), - themeId = themeIds[4], - ) - ) - } - - // 조회 예상 결과: 세번째, 첫번째, 두번째 테마 순서 - return MostReservedThemeIdListResponse(listOf(themeIds[2], themeIds[0], themeIds[1])) - } } diff --git a/src/test/kotlin/roomescape/theme/ThemeApiTest.kt b/src/test/kotlin/roomescape/theme/ThemeApiTest.kt index 1ca12a4b..1fe3e095 100644 --- a/src/test/kotlin/roomescape/theme/ThemeApiTest.kt +++ b/src/test/kotlin/roomescape/theme/ThemeApiTest.kt @@ -1,41 +1,24 @@ package roomescape.theme +import io.kotest.matchers.collections.shouldContainInOrder +import io.kotest.matchers.collections.shouldHaveSize import org.hamcrest.CoreMatchers.equalTo import org.springframework.http.HttpMethod import org.springframework.http.HttpStatus +import roomescape.common.config.next +import roomescape.common.util.DateUtils import roomescape.supports.* import roomescape.theme.exception.ThemeErrorCode import roomescape.theme.infrastructure.persistence.ThemeEntity -import roomescape.theme.web.ThemeIdListRequest +import roomescape.theme.infrastructure.persistence.ThemeRepository +import roomescape.theme.web.toEntity +import roomescape.user.infrastructure.persistence.UserEntity +import java.time.LocalDate -class ThemeApiTest : FunSpecSpringbootTest() { +class ThemeApiTest( + private val themeRepository: ThemeRepository +) : FunSpecSpringbootTest() { init { - context("입력된 모든 ID에 대한 테마를 조회한다.") { - test("정상 응답 + 없는 테마가 있으면 생략한다.") { - val themeIds: List = initialize("목록 조회를 위한 3개의 테마 생성 및 일부 존재하지 않는 ID 추가") { - val themeIds = mutableListOf(INVALID_PK) - (1..3).forEach { _ -> - themeIds.add(dummyInitializer.createTheme().id) - } - - themeIds - } - - runTest( - using = { - body(ThemeIdListRequest(themeIds)) - }, - on = { - post("/themes/batch") - }, - expect = { - statusCode(HttpStatus.OK.value()) - body("data.themes.size()", equalTo(themeIds.filter { it != INVALID_PK }.size)) - } - ) - } - } - context("ID로 테마 정보를 조회한다.") { test("정상 응답") { val createdTheme: ThemeEntity = initialize("조회를 위한 테마 생성") { @@ -68,5 +51,97 @@ class ThemeApiTest : FunSpecSpringbootTest() { ) } } + + context("인기 테마를 조회한다.") { + test("정상 응답") { + val expectedResult: List = initializeForPopularThemeTest() + + runTest( + on = { + get("/themes/most-reserved?count=10") + }, + expect = { + statusCode(HttpStatus.OK.value()) + } + ).also { res -> + val response: List> = res.extract().path("data.themes") + + response shouldHaveSize expectedResult.size + response.map { it["id"] as Long }.shouldContainInOrder(expectedResult) + } + } + } + } + + private fun initializeForPopularThemeTest(): List { + val user: UserEntity = testAuthUtil.defaultUser() + + val themeIds: List = (1..5).map { + themeRepository.save(ThemeFixture.createRequest.copy().toEntity(id = tsidFactory.next())).id + } + + val store = dummyInitializer.createStore() + + // 첫 번째 테마: 유효한 2개 예약 + (1L..2L).forEach { + dummyInitializer.createConfirmReservation( + user = user, + storeId = store.id, + scheduleRequest = ScheduleFixture.createRequest.copy( + date = DateUtils.getSundayOfPreviousWeek(LocalDate.now()).plusDays(it), + themeId = themeIds[0], + ) + ) + } + + // 두 번째 테마: 유효한 1개 예약 + dummyInitializer.createConfirmReservation( + user = user, + storeId = store.id, + scheduleRequest = ScheduleFixture.createRequest.copy( + date = DateUtils.getSundayOfPreviousWeek(LocalDate.now()), + themeId = themeIds[1], + ) + ) + + // 세 번째 테마: 유효한 3개 예약 + (1L..3L).forEach { + dummyInitializer.createConfirmReservation( + user = user, + storeId = store.id, + scheduleRequest = ScheduleFixture.createRequest.copy( + date = DateUtils.getSundayOfPreviousWeek(LocalDate.now()).plusDays(it), + themeId = themeIds[2], + ) + ) + } + + // 네 번째 테마: Pending 상태인 3개 예약 -> 집계되지 않음. + (1L..3L).forEach { + dummyInitializer.createPendingReservation( + user = user, + storeId = store.id, + scheduleRequest = ScheduleFixture.createRequest.copy( + date = DateUtils.getSundayOfPreviousWeek(LocalDate.now()).plusDays(it), + themeId = themeIds[3], + ) + ) + } + + // 다섯 번째 테마: 이번주의 확정 예약 -> 집계되지 않음. + (1L..3L).forEach { i -> + val thisMonday = DateUtils.getSundayOfPreviousWeek(LocalDate.now()).plusDays(8) + dummyInitializer.createConfirmReservation( + user = user, + storeId = store.id, + scheduleRequest = ScheduleFixture.createRequest.copy( + date = thisMonday.plusDays(i), + themeId = themeIds[4], + ) + ) + } + + // 조회 예상 결과: 세번째, 첫번째, 두번째 테마 순서 + return listOf(themeIds[2], themeIds[0], themeIds[1]) } }