Skip to content

Commit

Permalink
store 테스트 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
dojinyou committed Oct 1, 2023
1 parent b92ce49 commit 3ab484f
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 23 deletions.
9 changes: 8 additions & 1 deletion src/main/kotlin/com/mjucow/eatda/common/vo/DayOfWeek.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,18 @@ enum class DayOfWeek(
/**
* 객체가 인자의 전 요일인지 확인합니다
*/
fun isPrevtDayOf(dayOfWeek: DayOfWeek): Boolean {
fun isPrevDayOf(dayOfWeek: DayOfWeek): Boolean {
return ((ordinal + 1) % VALUES.size) == dayOfWeek.ordinal
}

companion object {
val VALUES = entries.toTypedArray()

fun of(ordinal: Int): DayOfWeek {
if (ordinal + VALUES.size < 0) {
throw IllegalArgumentException()
}
return VALUES[(ordinal + VALUES.size) % VALUES.size]
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class Store() : BaseEntity() {
// 같은 날에 시간이 겹치는 경우, 날짜는 다르지만 새벽 영업(36시간 제도)으로 시간이 겹치는 경우
(it.dayOfWeek == newHours.dayOfWeek && (it.openAt <= newHours.closeAt || newHours.openAt <= it.closeAt)) ||
(it.dayOfWeek.isNextDayOf(newHours.dayOfWeek) && (newHours.openAt + StoreHours.ONE_DAY_MINUTE) <= it.closeAt) ||
(it.dayOfWeek.isPrevtDayOf(newHours.dayOfWeek) && (it.openAt + StoreHours.ONE_DAY_MINUTE) <= newHours.closeAt)
(it.dayOfWeek.isPrevDayOf(newHours.dayOfWeek) && (it.openAt + StoreHours.ONE_DAY_MINUTE) <= newHours.closeAt)
}
if (result.isNotEmpty()) {
throw IllegalArgumentException()
Expand Down
111 changes: 110 additions & 1 deletion src/test/kotlin/com/mjucow/eatda/domain/store/entity/StoreTest.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.mjucow.eatda.domain.store.entity

import com.mjucow.eatda.common.vo.DayOfWeek
import com.mjucow.eatda.domain.store.entity.objectmother.StoreHoursMother
import com.mjucow.eatda.domain.store.entity.objectmother.StoreMother
import org.assertj.core.api.Assertions.assertThat
Expand Down Expand Up @@ -210,9 +211,79 @@ class StoreTest {
).isTrue()
}

@DisplayName("새로운 이미지 주소가 빈 값일 경우 예외를 던진다")
@ParameterizedTest
@EmptySource
fun throwExceptionWhenNewImageAddressIsEmpty(newImageAddress: String) {
// given
val store = StoreMother.create()

// when
val throwable = catchThrowable { store.imageAddress = newImageAddress }

// then
assertThat(throwable).isInstanceOf(RuntimeException::class.java)
}

@DisplayName("새로운 주소가 최소 길이 이상일 경우 에외를 던진다")
@Test
fun throwExceptionWhenNewImageAddressLengthGreaterThanMaxLength() {
// given
val store = StoreMother.create()
val newImageAddress = "x".repeat(Store.MAX_IMAGE_ADDRESS_LENGTH + 1)

// when
val throwable = catchThrowable { store.imageAddress = newImageAddress }

// then
assertThat(throwable).isInstanceOf(RuntimeException::class.java)
}

@DisplayName("새로운 연락처를 추가할 수 있다")
@Test
fun addNewPhoneNumber() {
// given
val store = StoreMother.createWithId()
val newPhoneNumber = StoreMother.PHONE_NUMBER

// when
store.phoneNumber = newPhoneNumber

// then
assertThat(store.phoneNumber).isEqualTo(newPhoneNumber)
}

@DisplayName("새로운 이미지 주소를 추가할 수 있다")
@Test
fun addNewImageAddress() {
// given
val store = StoreMother.createWithId()
val newImageAddress = StoreMother.IMAGE_ADDRESS

// when
store.imageAddress = newImageAddress

// then
assertThat(store.imageAddress).isEqualTo(newImageAddress)
}

@DisplayName("새로운 위치 정보를 추가할 수 있다")
@Test
fun addNewLocation() {
// given
val store = StoreMother.createWithId()
val newLocation = StoreMother.LOCATION

// when
store.location = newLocation

// then
assertThat(store.location).isEqualTo(newLocation)
}

@DisplayName("이미 존재하는 시간과 겹친다면 운영시간은 추가되지 않는다: 같은 날짜")
@Test
fun throwExceptionWhenNewStoreHoursOverlapTime() {
fun throwExceptionWhenNewSameDayOfWeekStoreHoursOverlapTime() {
// given
val storeHours = StoreHoursMother.create()
val store = StoreMother.create { it.addStoreHour(storeHours) }
Expand All @@ -223,4 +294,42 @@ class StoreTest {
// then
assertThat(throwable).isNotNull()
}

@DisplayName("이미 존재하는 시간과 겹친다면 운영시간은 추가되지 않는다: 하루 전 새벽 운영")
@Test
fun throwExceptionWhenNewPrevDayOfWeekStoreHoursOverlapTime() {
// given
val currentStoreHours = StoreHoursMother.create()
val store = StoreMother.create(autoFill = true) { it.addStoreHour(currentStoreHours) }
val newStoreHours = StoreHours(
DayOfWeek.of(currentStoreHours.dayOfWeek.ordinal - 1),
openAt = StoreHours.MIN_TIME_MINUTE,
closeAt = currentStoreHours.openAt + StoreHours.ONE_DAY_MINUTE + 1
)

// when
val throwable = catchThrowable { store.addStoreHour(newStoreHours) }

// then
assertThat(throwable).isNotNull()
}

@DisplayName("이미 존재하는 시간과 겹친다면 운영시간은 추가되지 않는다: 기존 시간이 새벽운영")
@Test
fun throwExceptionWhenNewNextDayOfWeekStoreHoursOverlapTime() {
// given
val currentStoreHours = StoreHoursMother.create { it.closeAt = StoreHours.MAX_TIME_MINUTE }
val store = StoreMother.create { it.addStoreHour(currentStoreHours) }
val newStoreHours = StoreHours(
DayOfWeek.of(currentStoreHours.dayOfWeek.ordinal + 1),
openAt = currentStoreHours.closeAt - StoreHours.ONE_DAY_MINUTE,
closeAt = StoreHours.MAX_TIME_MINUTE
)

// when
val throwable = catchThrowable { store.addStoreHour(newStoreHours) }

// then
assertThat(throwable).isNotNull()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ abstract class ObjectMother<T> {
return instance.apply(apply)
}

abstract fun createFillInstance(): T
abstract fun createDefaultInstance(): T
protected abstract fun createFillInstance(): T
protected abstract fun createDefaultInstance(): T
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import com.mjucow.eatda.domain.store.entity.StoreHours

object StoreHoursMother : EntityMother<StoreHours>() {
override fun createDefaultInstance() = StoreHours(
dayOfWeek = DayOfWeek.MON,
openAt = StoreHours.MIN_TIME_MINUTE,
closeAt = StoreHours.ONE_DAY_MINUTE
DayOfWeek.MON,
StoreHours.MIN_TIME_MINUTE,
StoreHours.ONE_DAY_MINUTE
)

override fun createFillInstance() = StoreHours(
dayOfWeek = DayOfWeek.MON,
openAt = StoreHours.MIN_TIME_MINUTE,
closeAt = StoreHours.MAX_TIME_MINUTE
DayOfWeek.MON,
StoreHours.MIN_TIME_MINUTE,
StoreHours.MAX_TIME_MINUTE
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,18 @@ object StoreMother : EntityMother<Store>() {
const val NAME = "명지대학교"
const val ADDRESS = "서울특별시 서대문구 거북골로 34"
const val DISPLAY_NAME = "띵지대"
const val IMAGE_ADDRESS = "/eatda/public/store/232D8241-C6A9-4AD9-B0EA-56F6DD24BADF.jpg"
val PHONE_NUMBER = PhoneNumber("02-300-1656")
val LOCATION = Point(latitude = 37.5802219, longitude = 126.9226047)

override fun createDefaultInstance() = Store(
name = NAME,
address = ADDRESS,
displayName = null,
phoneNumber = null,
location = null
)
override fun createDefaultInstance() = Store(NAME, ADDRESS)

override fun createFillInstance() = Store(
name = NAME,
address = ADDRESS,
displayName = DISPLAY_NAME,
phoneNumber = PHONE_NUMBER,
location = LOCATION
NAME,
ADDRESS,
DISPLAY_NAME,
PHONE_NUMBER,
IMAGE_ADDRESS,
LOCATION
)
}

0 comments on commit 3ab484f

Please sign in to comment.