-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #104 from cvs-go/feature#103
태그 매칭률 조회 기능 추가
- Loading branch information
Showing
7 changed files
with
197 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
src/test/java/com/cvsgo/repository/UserTagRepositoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package com.cvsgo.repository; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import com.cvsgo.config.TestConfig; | ||
import com.cvsgo.entity.Tag; | ||
import com.cvsgo.entity.User; | ||
import com.cvsgo.entity.UserTag; | ||
import java.util.List; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; | ||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; | ||
import org.springframework.context.annotation.Import; | ||
|
||
@Import(TestConfig.class) | ||
@DataJpaTest | ||
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) | ||
class UserTagRepositoryTest { | ||
|
||
@Autowired | ||
private UserTagRepository userTagRepository; | ||
|
||
@Autowired | ||
private UserRepository userRepository; | ||
|
||
@Autowired | ||
private TagRepository tagRepository; | ||
|
||
User user; | ||
|
||
@BeforeEach | ||
void initData() { | ||
tag1 = Tag.builder().name("맵찔이").group(1).build(); | ||
tag2 = Tag.builder().name("맵부심").group(1).build(); | ||
tag3 = Tag.builder().name("초코러버").group(2).build(); | ||
tag4 = Tag.builder().name("비건").group(3).build(); | ||
tag5 = Tag.builder().name("다이어터").group(4).build(); | ||
tag6 = Tag.builder().name("대식가").group(5).build(); | ||
tag7 = Tag.builder().name("소식가").group(5).build(); | ||
tagRepository.saveAll(List.of(tag1, tag2, tag3, tag4, tag5, tag6, tag7)); | ||
|
||
user1 = User.create("[email protected]", "password1!", "닉네임1", List.of(tag1, tag3, tag6)); | ||
user2 = User.create("[email protected]", "password1!", "닉네임2", List.of(tag2, tag4, tag6)); | ||
userRepository.saveAll(List.of(user1, user2)); | ||
} | ||
|
||
@Test | ||
@DisplayName("유저 태그를 조회한다") | ||
void find_user_tags_by_user() { | ||
// when | ||
List<UserTag> userTags = userTagRepository.findAllByUser(user1); | ||
|
||
// then | ||
List<Tag> tags = userTags.stream().map(UserTag::getTag).toList(); | ||
assertThat(userTags).hasSize(3); | ||
assertThat(tags).extracting("name").containsExactly("맵찔이", "초코러버", "대식가"); | ||
assertThat(tags).extracting("name").doesNotContain("맵부심", "비건", "다이어터", "소식가"); | ||
} | ||
|
||
private Tag tag1; | ||
private Tag tag2; | ||
private Tag tag3; | ||
private Tag tag4; | ||
private Tag tag5; | ||
private Tag tag6; | ||
private Tag tag7; | ||
private User user1; | ||
private User user2; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,6 @@ | |
import static org.mockito.BDDMockito.then; | ||
import static org.mockito.Mockito.times; | ||
|
||
import com.cvsgo.dto.review.ReadProductReviewResponseDto; | ||
import com.cvsgo.dto.user.SignUpRequestDto; | ||
import com.cvsgo.dto.user.UpdateUserRequestDto; | ||
import com.cvsgo.entity.Review; | ||
|
@@ -26,6 +25,7 @@ | |
import com.cvsgo.repository.TagRepository; | ||
import com.cvsgo.repository.UserFollowRepository; | ||
import com.cvsgo.repository.UserRepository; | ||
import com.cvsgo.repository.UserTagRepository; | ||
import jakarta.persistence.EntityManager; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
@@ -51,6 +51,9 @@ class UserServiceTest { | |
@Mock | ||
private TagRepository tagRepository; | ||
|
||
@Mock | ||
private UserTagRepository userTagRepository; | ||
|
||
@Mock | ||
private UserFollowRepository userFollowRepository; | ||
|
||
|
@@ -310,6 +313,30 @@ void should_throw_NotFoundException_when_delete_user_follow_but_user_follow_does | |
then(userFollowRepository).should(times(1)).findByUserAndFollower(any(), any()); | ||
} | ||
|
||
@Test | ||
@DisplayName("태그 매칭률을 조회한다") | ||
void succeed_to_read_user_tag_match_percentage() { | ||
given(userRepository.findById(anyLong())).willReturn(Optional.of(user2)); | ||
given(userTagRepository.findAllByUser(any())).willReturn(List.of(userTag1, userTag2)); | ||
given(userTagRepository.findAllByUser(any())).willReturn(List.of(userTag3, userTag4)); | ||
|
||
userService.readUserTagMatchPercentage(user, user2.getId()); | ||
|
||
then(userRepository).should(times(1)).findById(user2.getId()); | ||
then(userTagRepository).should(times(2)).findAllByUser(any()); | ||
} | ||
|
||
@Test | ||
@DisplayName("태그 매칭률 조회 시 해당하는 아이디를 가진 사용자가 없으면 NotFoundException이 발생한다") | ||
void should_throw_NotFoundException_when_read_user_tag_match_percentage_but_user_does_not_exist() { | ||
final Long userId = 10000L; | ||
|
||
given(userRepository.findById(anyLong())).willReturn(Optional.empty()); | ||
|
||
assertThrows(NotFoundException.class, () -> userService.readUserTagMatchPercentage(user, userId)); | ||
then(userRepository).should(times(1)).findById(anyLong()); | ||
} | ||
|
||
User user = User.builder() | ||
.id(1L) | ||
.userId("[email protected]") | ||
|
@@ -336,16 +363,32 @@ void should_throw_NotFoundException_when_delete_user_follow_but_user_follow_does | |
.group(2) | ||
.build(); | ||
|
||
Tag tag3 = Tag.builder() | ||
.id(5L) | ||
.name("다이어터") | ||
.group(4) | ||
.build(); | ||
|
||
UserTag userTag1 = UserTag.builder() | ||
.user(user) | ||
.tag(tag1) | ||
.build(); | ||
|
||
UserTag userTag2 = UserTag.builder() | ||
.user(user) | ||
.tag(tag3) | ||
.build(); | ||
|
||
UserTag userTag3 = UserTag.builder() | ||
.user(user2) | ||
.tag(tag2) | ||
.build(); | ||
|
||
UserTag userTag4 = UserTag.builder() | ||
.user(user2) | ||
.tag(tag3) | ||
.build(); | ||
|
||
Review review1 = Review.builder() | ||
.user(user) | ||
.build(); | ||
|