Skip to content

Commit

Permalink
Merge pull request #164 from KUSITMS-29th-TEAM-B/test/flight-163
Browse files Browse the repository at this point in the history
test: Tag 도메인 테스트 코드 작성(#163)
  • Loading branch information
whereami2048 authored May 23, 2024
2 parents 2e60634 + e724dd3 commit 7785e68
Show file tree
Hide file tree
Showing 8 changed files with 193 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class ExperienceGetService(
.distinctBy { it.parentTagId }
.map { it.parentTagId }

val tagDetails = tagReader.readByIds(parentTagIds).map {
val tagDetails = tagReader.readByParentTagIds(parentTagIds).map {
ExperienceYear.TagDetail(
id = it.id,
name = it.name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class TagGetService(
.take(limit)
.map { it.parentTagId }

return tagReader.readByIds(topParentTagIds).map {
return tagReader.readByParentTagIds(topParentTagIds).map {
GetParentTag.TagDetail(it.id, it.name)
}.let {
GetParentTag.Response(it)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ interface TagRepository {
fun save(newTag : Tag)
fun findById(id : UUID) : Tag
fun findAllParentTagsByUserId(userId: UUID): List<Tag>
fun findAllChildTagsByUserIdAndParentTagId(userId: UUID, parentId: UUID): List<Tag>
fun findAllChildTagsByUserIdAndParentTagId(userId: UUID, parentTagId: UUID): List<Tag>
fun deleteByTagId(tagId: UUID)
fun isExistById(tagId: UUID): Boolean
fun findByParentTagIds(tagParentTagIds: List<UUID>): List<Tag>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ class TagReader(
return tagRepository.findAllParentTagsByUserId(userId)
}

fun readAllChildTagsByUserIdAndParentTagId(userId: UUID, parentId: UUID): List<Tag> {
return tagRepository.findAllChildTagsByUserIdAndParentTagId(userId, parentId)
fun readAllChildTagsByUserIdAndParentTagId(userId: UUID, parentTagId: UUID): List<Tag> {
return tagRepository.findAllChildTagsByUserIdAndParentTagId(userId, parentTagId)
}

fun readById(tagId: UUID): Tag {
return tagRepository.findById(tagId)
}

fun readByIds(parentTagIds: List<UUID>): List<Tag> {
fun readByParentTagIds(parentTagIds: List<UUID>): List<Tag> {
return tagRepository.findByParentTagIds(parentTagIds)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.bamyanggang.domainmodule.domain.tag
package com.bamyanggang.domainmodule.domain.tag.aggregate

import com.bamyanggang.domainmodule.domain.tag.aggregate.Tag
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.core.spec.DisplayName
import io.kotest.core.spec.style.DescribeSpec
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.bamyanggang.domainmodule.domain.tag.service

import com.bamyanggang.domainmodule.domain.tag.repository.TagRepository
import io.kotest.core.spec.style.BehaviorSpec
import io.mockk.mockk
import io.mockk.verify
import java.util.*

class TagAppenderTest : BehaviorSpec({
val mockTagRepository = mockk<TagRepository>(relaxed = true)
val tagAppender = TagAppender(mockTagRepository)

Given("상위 태그 이름과 userId가 주어졌을 때") {
val name = "상위 태그 이름"
val userId = UUID.randomUUID()

When("appendParentTag를 호출하면") {
val newParentTag = tagAppender.appendParentTag(name, userId)

Then("tagRepository.save를 호출한다.") {
verify { mockTagRepository.save(newParentTag) }
}
}
}

Given("하위 태그 이름과 userId, parentTagId가 주어졌을 때") {
val name = "하위 태그 이름"
val userId = UUID.randomUUID()
val parentTagId = UUID.randomUUID()

When("appendChildTag를 호출하면") {
val newChildTag = tagAppender.appendChildTag(
name = name,
userId = userId,
parentTagId = parentTagId
)

Then("tagRepository.save를 호출한다.") {
verify { mockTagRepository.save(newChildTag) }
}
}
}
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package com.bamyanggang.domainmodule.domain.tag.service

import com.bamyanggang.domainmodule.domain.tag.repository.TagRepository
import io.kotest.core.spec.style.BehaviorSpec
import io.mockk.mockk
import io.mockk.verify
import java.util.*

class TagReaderTest : BehaviorSpec({
val mockTagRepository = mockk<TagRepository>(relaxed = true)
val tagReader = TagReader(mockTagRepository)

Given("tagId가 주어졌을 때") {
val tagId = UUID.randomUUID()

When("tagReader.readById가 호출되면") {
tagReader.readById(tagId)

Then("tagRepository.findById가 호촐된다.") {
verify { mockTagRepository.findById(tagId) }
}
}
}

Given("tagId 배열이 주어졌을 때") {
val tagIds = arrayListOf(
UUID.randomUUID(),
UUID.randomUUID()
)

When("tagReader.readByParentTagIds가 호출되면") {
tagReader.readByParentTagIds(tagIds)

Then("tagRepository.findByParentTagIds 호촐된다.") {
verify { mockTagRepository.findByParentTagIds(tagIds) }
}
}
}

Given("userId가 주어졌을 때") {
val userId = UUID.randomUUID()

When("tagReader.readAllParentTagsByUserId 호출되면") {
tagReader.readAllParentTagsByUserId(userId)

Then("tagRepository.findAllParentTagsByUserId 호촐된다.") {
verify { mockTagRepository.findAllParentTagsByUserId(userId) }
}
}
}

Given("userId, parentTagId가 주어졌을 때") {
val userId = UUID.randomUUID()
val parentTagId = UUID.randomUUID()
When("tagReader.readAllChildTagsByUserIdAndParentTagId 호출되면") {
tagReader.readAllChildTagsByUserIdAndParentTagId(
userId = userId,
parentTagId = parentTagId
)

Then("tagRepository.findAllChildTagsByUserIdAndParentTagId 호촐된다.") {
verify { mockTagRepository.findAllChildTagsByUserIdAndParentTagId(
userId = userId,
parentTagId = parentTagId
) }
}
}
}

Given("userId, search 검색 문자열이 주어졌을 때") {
val userId = UUID.randomUUID()
val search = "태그 이름 검색 문자열"
When("tagReader.readIdsByUserIdAndNameContains 호출되면") {
tagReader.readIdsByUserIdAndNameContains(
userId = userId,
search = search
)

Then("tagRepository.findAllChildTagsByUserIdAndParentTagId 호촐된다.") {
verify { mockTagRepository.findByUserIdAndNameContains(
userId = userId,
search = search
) }
}
}
}

Given("parentTagId가 주어졌을 때") {
val parentTagId = UUID.randomUUID()

When("tagReader.readChildTagsByParentTagId 호출되면") {
tagReader.readChildTagsByParentTagId(parentTagId = parentTagId)

Then("tagRepository.findAllChildTagsByParentTagId 호촐된다.") {
verify { mockTagRepository.findAllChildTagsByParentTagId(
parentTagId = parentTagId,
) }
}
}
}
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.bamyanggang.domainmodule.domain.tag.service

import com.bamyanggang.domainmodule.domain.tag.exception.TagException
import com.bamyanggang.domainmodule.domain.tag.repository.TagRepository
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.core.spec.style.BehaviorSpec
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import java.util.*

class TagRemoverTest : BehaviorSpec({
val mockTagRepository = mockk<TagRepository>(relaxed = true)
val tagRemover = TagRemover(mockTagRepository)

Given("삭제할 태그 Id가 주어졌을 때") {
val deleteTagId = UUID.randomUUID()

When("removeTag가 호출되면") {
every { mockTagRepository.isExistById(deleteTagId) } returns true
tagRemover.removeTag(deleteTagId)

Then("tagRepository.deleteById가 호출된다.") {
verify { mockTagRepository.deleteByTagId(deleteTagId) }
}
}
}

Given("존재하지 않는 태그 Id가 주어졌을 때") {
val notExistTagId = UUID.randomUUID()

When("removeTag가 호출되면") {
val exception = shouldThrow<TagException.NotFoundTag> {
tagRemover.removeTag(notExistTagId)
}

Then("TagException.NotFoundTag 예외가 발생한다.") {
assert(exception.message == TagException.NotFoundTag().message)
}
}
}
})

0 comments on commit 7785e68

Please sign in to comment.