Skip to content

Commit

Permalink
[TEST] refactor user repository
Browse files Browse the repository at this point in the history
  • Loading branch information
nogamsung committed Jan 25, 2024
1 parent 89e4929 commit 620d926
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@

import java.util.List;

import static org.assertj.core.api.Assertions.*;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

@ActiveProfiles("test")
@SpringBootTest
Expand All @@ -21,10 +22,10 @@ class ContactRepositoryTest {
@Autowired private ContactRepository contactRepository;

@Test
@DisplayName("이메일로 연락처 단건 조회를 한다.")
@DisplayName("이메일로 연락처 단건 조회가 정상 작동한다")
void givenValid_whenFindByEmail_thenReturn() {
// given
Contact contact = createContact("tester@gabojait.com");
Contact contact = createContact();
contactRepository.save(contact);

// when
Expand All @@ -35,10 +36,10 @@ void givenValid_whenFindByEmail_thenReturn() {
}

@Test
@DisplayName("이메일로 인증된 연락처 단건 조회를 한다.")
@DisplayName("이메일로 인증된 연락처 단건 조회가 정상 작동한다")
void givenVerified_whenFindByEmailAndIsVerified_thenReturn() {
// given
Contact contact = createContact("tester@gabojait.com");
Contact contact = createContact();
contact.verified();

contactRepository.save(contact);
Expand All @@ -51,10 +52,10 @@ void givenVerified_whenFindByEmailAndIsVerified_thenReturn() {
}

@Test
@DisplayName("이메일로 인증되지 않은 연락처 단건 조회를 한다.")
@DisplayName("이메일로 인증되지 않은 연락처 단건 조회가 정상 작동한다")
void givenUnverified_whenFindByEmailAndIsVerified_thenReturn() {
// given
Contact contact = createContact("tester@gabojait.com");
Contact contact = createContact();
contactRepository.save(contact);

// when
Expand All @@ -65,10 +66,10 @@ void givenUnverified_whenFindByEmailAndIsVerified_thenReturn() {
}

@Test
@DisplayName("존재하는 연락처 이메일로 연락처 존재 여부를 확인한다.")
@DisplayName("존재하는 연락처 이메일로 연락처 존재 여부 조회시 참을 반환한다")
void givenExisting_whenExistsByEmail_thenReturn() {
// given
Contact contact = createContact("tester@gabojait.com");
Contact contact = createContact();
contactRepository.save(contact);

// when
Expand All @@ -79,7 +80,7 @@ void givenExisting_whenExistsByEmail_thenReturn() {
}

@Test
@DisplayName("존재하지 않은 연락처 이메일로 연락처 존재 여부를 확인한다.")
@DisplayName("존재하지 않은 연락처 이메일로 연락처 존재 여부 조회시 거짓을 반환한다")
void givenNonExisting_whenExistsByEmail_thenReturn() {
// given
String email = "tester@gabojait.com";
Expand All @@ -92,10 +93,10 @@ void givenNonExisting_whenExistsByEmail_thenReturn() {
}

@Test
@DisplayName("이메일로 연락처를 삭제한다.")
@DisplayName("이메일로 연락처를 삭제가 정상 작동한다")
void givenValid_whenDeleteByEmail_thenReturn() {
// given
Contact contact = createContact("tester@gabojait.com");
Contact contact = createContact();
contactRepository.save(contact);

// when
Expand All @@ -106,20 +107,20 @@ void givenValid_whenDeleteByEmail_thenReturn() {
}

@Test
@DisplayName("동일한 이메일 저장시 예외가 발생한다.")
@DisplayName("동일한 이메일 저장시 예외가 발생한다")
void givenSameEmails_whenSave_thenThrow() {
// given
Contact contact1 = createContact("tester@gabojait.com");
Contact contact2 = createContact("tester@gabojait.com");
Contact contact1 = createContact();
Contact contact2 = createContact();

// when & then
assertThatThrownBy(() -> contactRepository.saveAll(List.of(contact1, contact2)))
.isInstanceOf(DataIntegrityViolationException.class);
}

private Contact createContact(String email) {
private Contact createContact() {
return Contact.builder()
.email(email)
.email("tester@gabojait.com")
.verificationCode("000000")
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
import java.time.LocalDateTime;
import java.util.List;

import static org.assertj.core.api.Assertions.*;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertAll;

@ActiveProfiles("test")
Expand All @@ -29,7 +30,7 @@ class UserRepositoryTest {
@Autowired private ContactRepository contactRepository;

@Test
@DisplayName("연락처로 회원 단건 조회를 한다.")
@DisplayName("연락처로 회원 단건 조회가 정상 작동한다")
void givenValid_whenFindByContact_thenReturn() {
// given
Contact contact = createContact("tester@gabojait.com");
Expand All @@ -47,7 +48,7 @@ void givenValid_whenFindByContact_thenReturn() {
}

@Test
@DisplayName("아이디로 회원 단건 조회를 한다.")
@DisplayName("아이디로 회원 단건 조회가 정상 작동한다")
void givenValid_whenFindByUsername_thenReturn() {
// given
Contact contact = createContact("tester@gabojait.com");
Expand All @@ -65,7 +66,7 @@ void givenValid_whenFindByUsername_thenReturn() {
}

@Test
@DisplayName("닉네임으로 회원 단건 조회를 한다.")
@DisplayName("닉네임으로 회원 단건 조회가 정상 작동한다")
void givenValid_whenFindByNickname_thenReturn() {
// given
Contact contact = createContact("tester@gabojait.com");
Expand All @@ -83,7 +84,7 @@ void givenValid_whenFindByNickname_thenReturn() {
}

@Test
@DisplayName("존재하는 회원 아이디로 회원 존재 여부를 확인한다.")
@DisplayName("존재하는 회원 아이디로 회원 존재 여부 조회시 참을 반환한다")
void givenExisting_whenExistsByUsername_thenReturn() {
// given
Contact contact = createContact("tester@gabojait.com");
Expand All @@ -101,7 +102,7 @@ void givenExisting_whenExistsByUsername_thenReturn() {
}

@Test
@DisplayName("존재하지 않은 회원 식별자로 회원 존재 여부를 확인한다.")
@DisplayName("존재하지 않은 회원 식별자로 회원 존재 여부 조회시 거짓을 반환한다")
void givenNonExisting_whenExistsByUsername_thenReturn() {
// given
String username = "tester";
Expand All @@ -114,7 +115,7 @@ void givenNonExisting_whenExistsByUsername_thenReturn() {
}

@Test
@DisplayName("존재하는 회원 닉네임으로 회원 존재 여부를 확인한다.")
@DisplayName("존재하는 회원 닉네임으로 회원 존재 여부 조회시 참을 반환한다")
void givenExisting_whenExistsByNickname_thenReturn() {
// given
Contact contact = createContact("tester@gabojait.com");
Expand All @@ -132,7 +133,7 @@ void givenExisting_whenExistsByNickname_thenReturn() {
}

@Test
@DisplayName("존재하지 않은 닉네임으로 회원 존재 여부를 확인한다.")
@DisplayName("존재하지 않은 닉네임으로 회원 존재 여부 조회시 거짓을 반환한다")
void givenNonExisting_whenExistsByNickname_thenReturn() {
// given
String nickname = "테스터";
Expand All @@ -145,7 +146,7 @@ void givenNonExisting_whenExistsByNickname_thenReturn() {
}

@Test
@DisplayName("동일한 아이디 저장시 예외가 발생한다.")
@DisplayName("동일한 아이디 저장시 예외가 발생한다")
void givenSameUsernames_whenSave_thenThrow() {
// given
Contact contact1 = createContact("tester1@gabojait.com");
Expand All @@ -163,7 +164,7 @@ void givenSameUsernames_whenSave_thenThrow() {
}

@Test
@DisplayName("동일한 닉네임 저장시 예외가 발생한다.")
@DisplayName("동일한 닉네임 저장시 예외가 발생한다")
void givenSameNicknames_whenSave_thenThrow() {
// given
Contact contact1 = createContact("tester1@gabojait.com");
Expand All @@ -181,7 +182,7 @@ void givenSameNicknames_whenSave_thenThrow() {
}

@Test
@DisplayName("연락처가 없는 회원 저장시 예외가 발생한다.")
@DisplayName("연락처가 없는 회원 저장시 예외가 발생한다")
void givenNoContact_whenSave_thenThrow() {
// given
User user = createUser("tester", "테스터", null);
Expand All @@ -192,7 +193,7 @@ void givenNoContact_whenSave_thenThrow() {
}

@Test
@DisplayName("전체 포지션으로 회원 페이징 조회를 한다.")
@DisplayName("전체 포지션으로 회원 페이징 조회가 정상 작동한다")
void givenNonePosition_whenFindPage_thenReturn() {
// given
Contact contact1 = createContact("tester1@gabojait.com");
Expand Down Expand Up @@ -225,7 +226,7 @@ void givenNonePosition_whenFindPage_thenReturn() {
}

@Test
@DisplayName("특정 포지션으로 회원 페이징 조회를 한다.")
@DisplayName("특정 포지션으로 회원 페이징 조회가 정상 작동한다")
void givenPosition_whenFindPage_thenReturn() {
// given
Contact contact1 = createContact("tester1@gabojait.com");
Expand Down Expand Up @@ -260,7 +261,7 @@ void givenPosition_whenFindPage_thenReturn() {
}

@Test
@DisplayName("결과가 없는 회원 페이징 조회를 한다.")
@DisplayName("결과가 없는 회원 페이징 조회가 정상 작동한다")
void givenNoResult_whenFindPage_thenReturn() {
// given
long pageFrom = Long.MAX_VALUE;
Expand All @@ -278,7 +279,7 @@ void givenNoResult_whenFindPage_thenReturn() {
}

@Test
@DisplayName("회원 식별자로 팀을 찾는 회원 단건 조회를 한다.")
@DisplayName("회원 식별자로 팀을 찾는 회원 단건 조회가 정상 작동한다")
void givenValid_whenFindSeekingTeam_thenReturn() {
// given
Contact contact = createContact("tester@gaobjait.com");
Expand All @@ -294,7 +295,7 @@ void givenValid_whenFindSeekingTeam_thenReturn() {
}

@Test
@DisplayName("이메일로 회원 단건 조회를 한다.")
@DisplayName("이메일로 회원 단건 조회가 정상 작동한다")
void givenValid_whenFind_thenReturn() {
// given
Contact contact = createContact("tester@gabojait.com");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.tuple;
import static org.junit.jupiter.api.Assertions.assertAll;

@ActiveProfiles("test")
Expand All @@ -26,10 +25,10 @@ class UserRoleRepositoryTest {
@Autowired private ContactRepository contactRepository;

@Test
@DisplayName("회원 아이디로 전체 권한 조회를 한다.")
@DisplayName("회원 아이디로 전체 권한 조회가 정상 작동한다")
void givenUsername_whenFindAll_thenReturn() {
// given
User user = createSavedDefaultUser("tester@gabojait.com", "tester", "테스터");
User user = createSavedDefaultUser();
Role role = Role.USER;
UserRole userRole = createUserRole(user, role);
userRoleRepository.save(userRole);
Expand All @@ -45,10 +44,10 @@ void givenUsername_whenFindAll_thenReturn() {
}

@Test
@DisplayName("회원 식별자로 전체 권한 조회를 한다.")
@DisplayName("회원 식별자로 전체 권한 조회가 정상 작동한다")
void givenUserId_findAll_thenReturn() {
// given
User user = createSavedDefaultUser("tester@gabojait.com", "tester", "테스터");
User user = createSavedDefaultUser();
Role role = Role.USER;
UserRole userRole = createUserRole(user, role);
userRoleRepository.save(userRole);
Expand All @@ -59,12 +58,9 @@ void givenUserId_findAll_thenReturn() {
// then
assertAll(
() -> assertThat(userRoles)
.extracting("id", "role", "createdAt", "updatedAt")
.containsExactlyInAnyOrder(
tuple(userRole.getId(), userRole.getRole(), userRole.getCreatedAt(),
userRole.getUpdatedAt())
),
() -> assertThat(userRoles).extracting("user").containsExactlyInAnyOrder(user)
.containsExactlyInAnyOrder(userRole),
() -> assertThat(userRoles).extracting("user")
.containsExactlyInAnyOrder(user)
);
}

Expand All @@ -75,18 +71,18 @@ private UserRole createUserRole(User user, Role role) {
.build();
}

private User createSavedDefaultUser(String email, String username, String nickname) {
private User createSavedDefaultUser() {
Contact contact = Contact.builder()
.email(email)
.email("tester@gabojait.com")
.verificationCode("000000")
.build();
contact.verified();
contactRepository.save(contact);

User user = User.builder()
.username(username)
.username("tester")
.password("password1!")
.nickname(nickname)
.nickname("테스터")
.gender(Gender.M)
.birthdate(LocalDate.of(1997, 2, 11))
.lastRequestAt(LocalDateTime.now())
Expand Down

0 comments on commit 620d926

Please sign in to comment.