Skip to content

Commit

Permalink
test : 검색 태그 테스트 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
seokho-1116 committed May 29, 2024
1 parent 5d10046 commit 7a767b0
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.ActiveProfiles;
import site.timecapsulearchive.core.global.config.JpaAuditingConfig;
import site.timecapsulearchive.core.global.config.QueryDSLConfig;
import site.timecapsulearchive.core.global.config.repository.JpaAuditingConfig;
import site.timecapsulearchive.core.global.config.repository.QueryDSLConfig;

@Import(value = {JpaAuditingConfig.class, QueryDSLConfig.class})
@DataJpaTest
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package site.timecapsulearchive.core.domain.friend.repository;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.SoftAssertions.assertSoftly;

import com.querydsl.jpa.impl.JPAQueryFactory;
Expand All @@ -11,6 +12,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
Expand All @@ -19,7 +21,6 @@
import org.springframework.data.domain.Slice;
import org.springframework.test.context.TestConstructor;
import org.springframework.test.context.TestConstructor.AutowireMode;
import org.springframework.transaction.annotation.Transactional;
import site.timecapsulearchive.core.common.RepositoryTest;
import site.timecapsulearchive.core.common.fixture.domain.FriendInviteFixture;
import site.timecapsulearchive.core.common.fixture.domain.MemberFixture;
Expand All @@ -41,19 +42,19 @@ class MemberFriendQueryRepositoryTest extends RepositoryTest {
private static final Long FRIEND_ID_TO_INVITE_OWNER = 11L;
private static final Long NOT_FRIEND_MEMBER_START_ID = 12L;

private final MemberFriendQueryRepository memberFriendQueryRepository;

private final List<byte[]> hashedNotMemberPhones = new ArrayList<>();
private final List<byte[]> hashedFriendPhones = new ArrayList<>();
private final List<byte[]> hashedNotFriendPhones = new ArrayList<>();
private final MemberFriendQueryRepository memberFriendQueryRepository;
private Long ownerId;
private Long friendId;

MemberFriendQueryRepositoryTest(EntityManager entityManager) {
MemberFriendQueryRepositoryTest(@Autowired EntityManager entityManager) {
this.memberFriendQueryRepository = new MemberFriendQueryRepositoryImpl(
new JPAQueryFactory(entityManager));
}

@Transactional
@BeforeEach
void setup(@Autowired EntityManager entityManager) {
Member owner = MemberFixture.member(0);
Expand All @@ -78,19 +79,18 @@ void setup(@Autowired EntityManager entityManager) {
Member inviteFriendToOwner = MemberFixture.member(FRIEND_ID_TO_INVITE_OWNER.intValue());
entityManager.persist(inviteFriendToOwner);

FriendInvite friendInvite = FriendInviteFixture.friendInvite(inviteFriendToOwner, owner);
FriendInvite friendInvite = FriendInviteFixture.friendInvite(inviteFriendToOwner,
owner);
entityManager.persist(friendInvite);

//owner와 친구가 아닌 멤버 데이터
List<Member> notFriendMembers = MemberFixture.members(NOT_FRIEND_MEMBER_START_ID.intValue(),
List<Member> notFriendMembers = MemberFixture.members(
NOT_FRIEND_MEMBER_START_ID.intValue(),
MAX_COUNT);
for (Member notFriend : notFriendMembers) {
entityManager.persist(notFriend);
hashedNotFriendPhones.add(notFriend.getPhone_hash());
}

//회원이 아닌 휴대전화번호 데이터
hashedNotMemberPhones.addAll(MemberFixture.getPhoneBytesList(23, MAX_COUNT));
}

@ParameterizedTest
Expand Down Expand Up @@ -274,31 +274,96 @@ void setup(@Autowired EntityManager entityManager) {
assertThat(friends).isEmpty();
}

@ParameterizedTest
@ValueSource(ints = {3, 6, 10, 5})
void 사용자가_친구_태그로_친구관계를_조회하면_친구인_경우_True를_반환한다(int friendId) {
@Test
void 태그로_검색하면_가장_비슷한_태그를_가진_사용자_한_명만_반환한다() {
//given
String tag = "test_tag";

//when
Optional<SearchFriendSummaryDtoByTag> dto = memberFriendQueryRepository.findFriendsByTag(
ownerId, tag);

//then
assertThat(dto).isPresent();
}

@Test
void 일치하는_태그로_검색하면_일치하는_태그를_가진_사용자_한_명만만_나온다() {
//given
String tag = "test_tag_9999";

//when
SearchFriendSummaryDtoByTag dto = memberFriendQueryRepository.findFriendsByTag(
ownerId, tag).orElseThrow();

//then
assertThat(dto.nickname()).isEqualTo("test_nickname_9999");
}

@Test
void 일치하지_않는_태그로_검색하면_결과가_나오지_않는다() {
//given
String tag = "trash";

//when
//then
assertThatThrownBy(() -> memberFriendQueryRepository.findFriendsByTag(
ownerId, tag).orElseThrow());
}

@Test
void 사용자가_친구_태그로_친구관계를_조회하면_친구인_경우_True를_반환한다() {
//given
String tag = "test_tag_9999";
Long ownerId = 10000L;

//when
SearchFriendSummaryDtoByTag dto = memberFriendQueryRepository.findFriendsByTag(
ownerId, friendId + "testTag").orElseThrow();
ownerId, tag).orElseThrow();

//then
assertThat(dto.isFriend()).isTrue();
}

@Test
void 사용자가_친구_태그로_친구관계를_조회하면_친구가_아닌_경우_False를_반환한다() {
//given
String tag = "test_tag_9999";
Long ownerId = 10003L;

//when
Boolean isFriend = dto.isFriend();
SearchFriendSummaryDtoByTag dto = memberFriendQueryRepository.findFriendsByTag(
ownerId, tag).orElseThrow();

//then
assertThat(isFriend).isTrue();
assertThat(dto.isFriend()).isFalse();
}

@ParameterizedTest
@ValueSource(ints = {12, 15, 19, 21})
void 사용자가_친구_태그로_친구관계를_조회하면_친구가_아닌_경우_False를_반환한다(int friendId) {
@Test
void 사용자가_친구_태그로_친구초대관계를_조회하면_친구_초대한_경우_True를_반환한다() {
//given
String tag = "test_tag_9999";
Long ownerId = 10001L;

//when
SearchFriendSummaryDtoByTag dto = memberFriendQueryRepository.findFriendsByTag(
ownerId, friendId + "testTag").orElseThrow();
ownerId, tag).orElseThrow();

//then
assertThat(dto.isFriend()).isTrue();
}

@Test
void 사용자가_친구_태그로_친구초대관계를_조회하면_친구_초대하지_않은_경우_False를_반환한다() {
//given
String tag = "test_tag_9999";
Long ownerId = 10003L;

//when
Boolean isFriend = dto.isFriend();
SearchFriendSummaryDtoByTag dto = memberFriendQueryRepository.findFriendsByTag(
ownerId, tag).orElseThrow();

//then
assertThat(isFriend).isFalse();
assertThat(dto.isFriend()).isFalse();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class FriendQueryServiceTest {
.willReturn(summaryDtoByTag);

//when
SearchTagFriendSummaryResponse actualResponse = friendQueryService.searchFriend(
SearchFriendSummaryDtoByTag actualResponse = friendQueryService.searchFriend(
memberId, tag);

//then
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
insert into member (is_verified, notification_enabled, created_at, member_id, updated_at, phone, nickname, social_type, email, fcm_token, profile_url, auth_id, phone_hash, tag, password)
values (true, false, now(), 9999, now(), null, 'test_nickname_9999', 'KAKAO', '[email protected]', null, 'http://test_profile_url_9999.com', 'test_auth_id_9999', null, 'test_tag_9999', null),
(true, false, now(), 10000, now(), null, 'test_nickname_10000', 'KAKAO', '[email protected]', null, 'http://test_profile_url_10000.com', 'test_auth_id_10000', null, 'test_tag_10000', null),
(true, false, now(), 10001, now(), null, 'test_nickname_10001', 'KAKAO', '[email protected]', null, 'http://test_profile_url_10001.com', 'test_auth_id_10001', null, 'test_tag_10001', null),
(true, false, now(), 10002, now(), null, 'test_nickname_10002', 'KAKAO', '[email protected]', null, 'http://test_profile_url_10002.com', 'test_auth_id_10002', null, 'test_tag_10002', null),
(true, false, now(), 10003, now(), null, 'test_nickname_10003', 'KAKAO', '[email protected]', null, 'http://test_profile_url_10003.com', 'test_auth_id_10003', null, 'test_tag_10003', null);

insert into member_friend(member_friend_id, owner_id, friend_id, created_at, updated_at)
values (1, 10000, 9999, now(), now()),
(2, 9999, 10000, now(), now()),
(3, 9999, 10001, now(), now()),
(4, 10001, 9999, now(), now());

insert into friend_invite(friend_invite_id, owner_id, friend_id, created_at, updated_at)
values (1, 10002, 9999, now(), now()),
(2, 9999, 10002, now(), now()),
(3, 9999, 10003, now(), now()),
(4, 10003, 9999, now(), now());

0 comments on commit 7a767b0

Please sign in to comment.