Skip to content

Commit

Permalink
[KAN-104] 음식점 유닛테스트 코드 (테스트 커버리지 100%) (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
sinkyoungdeok authored May 28, 2024
1 parent 0b73ceb commit d8214a3
Show file tree
Hide file tree
Showing 8 changed files with 337 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import com.restaurant.be.hello.presentation.dto.HelloRequest
import com.restaurant.be.hello.presentation.dto.HelloResponse
import io.swagger.annotations.ApiOperation
import io.swagger.v3.oas.annotations.responses.ApiResponse
import org.springframework.security.access.prepost.PreAuthorize
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
Expand Down Expand Up @@ -38,16 +37,4 @@ class HelloController(
val response = helloService.saveHello(request)
return CommonResponse.success(response)
}

@PreAuthorize("hasRole('USER')")
@GetMapping("/hello/security-test")
@ApiOperation(value = "Hello API")
@ApiResponse(
responseCode = "200",
description = "성공"
)
fun helloSecurityTest(): CommonResponse<HelloResponse> {
val response = helloService.findHellos()
return CommonResponse.success(response)
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package com.restaurant.be.restaurant.repository.dto

import com.restaurant.be.restaurant.presentation.controller.dto.common.MenuDto
import com.restaurant.be.restaurant.presentation.controller.dto.common.RestaurantDetailDto
import com.restaurant.be.restaurant.presentation.controller.dto.common.RestaurantDto
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import org.springframework.data.jpa.domain.AbstractPersistable_.id

@Serializable
data class RestaurantEsDocument(
Expand All @@ -14,37 +10,14 @@ data class RestaurantEsDocument(
@SerialName("original_category") val originalCategory: String,
@SerialName("naver_review_count") val naverReviewCount: Long,
@SerialName("address") val address: String,
@SerialName("naver_rating_avg") val naverRating: Double?,
@SerialName("naver_rating_avg") val naverRatingAvg: Double?,
@SerialName("image_url") val imageUrl: String,
@SerialName("category") val category: String,
@SerialName("discount_content") val discountContent: String?,
@SerialName("menus") val menus: List<MenuEsDocument>,
@SerialName("review_count") val reviewCount: Long,
@SerialName("rating_avg") val ratingAvg: Double?
) {
fun toDto() = RestaurantDto(
id = id,
representativeImageUrl = imageUrl,
name = name,
ratingAvg = naverRating ?: 0.0, // RDB로 변경 필요
reviewCount = naverReviewCount, // RDB로 변경 필요
likeCount = 0, // RDB
categories = category.split(",").map { it.trim() },
representativeMenu = menus.firstOrNull()?.toDto(),
representativeReviewContent = "", // RDB
isLike = false, // RDB
discountContent = discountContent,
longitude = 0.0, // RDB
latitude = 0.0, // RDB
naverRatingAvg = naverRating ?: 0.0,
naverReviewCount = naverReviewCount.toInt(),
detailInfo = RestaurantDetailDto(
contactNumber = "",
address = address,
menus = menus.map { it.toDto() }
)
)
}
)

@Serializable
data class MenuEsDocument(
Expand All @@ -53,12 +26,4 @@ data class MenuEsDocument(
@SerialName("price") val price: Int,
@SerialName("description") val description: String,
@SerialName("image_url") val imageUrl: String? = null
) {
fun toDto() = MenuDto(
name = menuName,
price = price,
description = description,
isRepresentative = false,
imageUrl = imageUrl
)
}
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.restaurant.be.restaurant.domain.entity

import io.kotest.core.spec.style.DescribeSpec
import io.kotest.matchers.shouldBe

class MenuTest : DescribeSpec({

describe("Menu entity") {
it("should create a correct Menu instance") {
// Arrange
val menu = Menu(
id = 1L,
restaurantId = 1L,
name = "Pizza",
price = 1500,
description = "Delicious cheese pizza",
isRepresentative = true,
imageUrl = "http://example.com/pizza.jpg"
)

// Assert
menu.id shouldBe 1L
menu.restaurantId shouldBe 1L
menu.name shouldBe "Pizza"
menu.price shouldBe 1500
menu.description shouldBe "Delicious cheese pizza"
menu.isRepresentative shouldBe true
menu.imageUrl shouldBe "http://example.com/pizza.jpg"
}

it("should correctly convert to MenuDto") {
// Arrange
val menu = Menu(
id = 1L,
restaurantId = 1L,
name = "Pizza",
price = 1500,
description = "Delicious cheese pizza",
isRepresentative = true,
imageUrl = "http://example.com/pizza.jpg"
)

// Act
val menuDto = menu.toDto()

// Assert
menuDto.name shouldBe "Pizza"
menuDto.price shouldBe 1500
menuDto.description shouldBe "Delicious cheese pizza"
menuDto.isRepresentative shouldBe true
menuDto.imageUrl shouldBe "http://example.com/pizza.jpg"
}
}
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.restaurant.be.restaurant.domain.entity

import io.kotest.core.spec.style.DescribeSpec
import io.kotest.matchers.shouldBe

class RestaurantCategoryTest : DescribeSpec({

describe("RestaurantCategory") {
it("should create a correct RestaurantCategory instance") {
// Given
val restaurantCategory = RestaurantCategory(
id = 1L,
restaurantId = 1,
categoryId = 1
)

// When

// Then
restaurantCategory.id shouldBe 1L
restaurantCategory.restaurantId shouldBe 1
restaurantCategory.categoryId shouldBe 1
}
}
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.restaurant.be.restaurant.domain.entity

import io.kotest.core.spec.style.DescribeSpec
import io.kotest.matchers.shouldBe

class RestaurantLikeTest : DescribeSpec({
describe("RestaurantLike") {
it("should create a correct RestaurantLike instance") {
// Given
val restaurantLike = RestaurantLike(
id = 1L,
restaurantId = 1,
userId = 1
)

// When

// Then
restaurantLike.id shouldBe 1L
restaurantLike.restaurantId shouldBe 1
restaurantLike.userId shouldBe 1
}
}
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package com.restaurant.be.restaurant.domain.entity

import io.kotest.core.spec.style.DescribeSpec
import io.kotest.matchers.doubles.shouldBeExactly
import io.kotest.matchers.shouldBe

class RestaurantTest : DescribeSpec({
describe("Restaurant entity") {
lateinit var restaurant: Restaurant

beforeEach {
restaurant = Restaurant(
id = 1L,
name = "Test Restaurant",
originalCategories = "Category1",
reviewCount = 0,
likeCount = 0,
address = "123 Test St",
contactNumber = "123-456-7890",
ratingAvg = 0.0,
representativeImageUrl = "http://example.com/image.jpg",
viewCount = 0,
discountContent = null,
longitude = 0.0,
latitude = 0.0,
naverRatingAvg = 0.0,
naverReviewCount = 0,
menus = mutableListOf()
)
}
context("should create a correct Restaurant instance") {
it("should create a correct Restaurant instance") {
restaurant.id shouldBe 1L
restaurant.name shouldBe "Test Restaurant"
restaurant.originalCategories shouldBe "Category1"
restaurant.reviewCount shouldBe 0
restaurant.likeCount shouldBe 0
restaurant.address shouldBe "123 Test St"
restaurant.contactNumber shouldBe "123-456-7890"
restaurant.ratingAvg shouldBeExactly 0.0
restaurant.representativeImageUrl shouldBe "http://example.com/image.jpg"
restaurant.viewCount shouldBe 0
restaurant.discountContent shouldBe null
restaurant.longitude shouldBeExactly 0.0
restaurant.latitude shouldBeExactly 0.0
restaurant.naverRatingAvg shouldBeExactly 0.0
restaurant.naverReviewCount shouldBe 0
restaurant.menus shouldBe mutableListOf()
}
}

context("createReview") {
it("should update ratingAvg and reviewCount when a new review is added") {
restaurant.createReview(4.0)
restaurant.reviewCount shouldBe 1
restaurant.ratingAvg shouldBeExactly 4.0

restaurant.createReview(2.0)
restaurant.reviewCount shouldBe 2
restaurant.ratingAvg shouldBeExactly 3.0
}
}

context("deleteReview") {
it("should update ratingAvg and reviewCount when a review is deleted") {
// Add initial reviews
restaurant.createReview(4.0)
restaurant.createReview(2.0)

restaurant.deleteReview(4.0)
restaurant.reviewCount shouldBe 1
restaurant.ratingAvg shouldBeExactly 2.0

restaurant.deleteReview(2.0)
restaurant.reviewCount shouldBe 0
restaurant.ratingAvg shouldBeExactly 0.0
}
}

context("updateReview") {
it("should update ratingAvg when a review is updated") {
// Add initial reviews
restaurant.createReview(4.0)
restaurant.createReview(2.0)

restaurant.updateReview(2.0, 5.0)
restaurant.reviewCount shouldBe 2
restaurant.ratingAvg shouldBeExactly 4.5

restaurant.updateReview(4.0, 3.0)
restaurant.reviewCount shouldBe 2
restaurant.ratingAvg shouldBeExactly 4.0
}
}
}
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.restaurant.be.restaurant.presentation.controller.dto.common

import com.restaurant.be.restaurant.repository.dto.RestaurantEsDocument
import io.kotest.core.spec.style.DescribeSpec
import io.kotest.matchers.shouldBe

class RestaurantEsDocumentTest : DescribeSpec({
describe("MenuDto") {
it("should create a correct RestaurantEsDocument instance") {
// Given
val restaurantEsDocument = RestaurantEsDocument(
id = 1L,
name = "Pizza",
originalCategory = "Italian",
naverReviewCount = 100,
address = "Seoul",
naverRatingAvg = 4.5,
imageUrl = "http://example.com/pizza.jpg",
category = "Italian",
discountContent = "10% off",
menus = mutableListOf(),
reviewCount = 1L,
ratingAvg = 4.5
)

// When

// Then
restaurantEsDocument.id shouldBe 1L
restaurantEsDocument.name shouldBe "Pizza"
restaurantEsDocument.originalCategory shouldBe "Italian"
restaurantEsDocument.naverReviewCount shouldBe 100
restaurantEsDocument.address shouldBe "Seoul"
restaurantEsDocument.naverRatingAvg shouldBe 4.5
restaurantEsDocument.imageUrl shouldBe "http://example.com/pizza.jpg"
restaurantEsDocument.category shouldBe "Italian"
restaurantEsDocument.discountContent shouldBe "10% off"
restaurantEsDocument.menus shouldBe mutableListOf()
restaurantEsDocument.reviewCount shouldBe 1L
restaurantEsDocument.ratingAvg shouldBe 4.5
}

it("should create a correct MenuDto instance") {
// Given
val menuEsDocument = MenuDto(
name = "Pasta",
price = 15000,
description = "Delicious pasta",
isRepresentative = true,
imageUrl = "http://example.com/pasta.jpg"
)

// When

// Then
menuEsDocument.name shouldBe "Pasta"
menuEsDocument.price shouldBe 15000
menuEsDocument.description shouldBe "Delicious pasta"
menuEsDocument.isRepresentative shouldBe true
menuEsDocument.imageUrl shouldBe "http://example.com/pasta.jpg"
}
}
})
Loading

0 comments on commit d8214a3

Please sign in to comment.