Skip to content

Commit

Permalink
test: 테스트 코드 작성
Browse files Browse the repository at this point in the history
  • Loading branch information
splitCoding committed Sep 11, 2023
1 parent e7b45a2 commit 630d3f8
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package shook.shook.song.domain.killingpart.repository;

import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import shook.shook.member.domain.Member;
Expand All @@ -9,4 +10,6 @@
public interface KillingPartCommentRepository extends JpaRepository<KillingPartComment, Long> {

void deleteAllByMember(final Member member);

List<KillingPartComment> findAllByMember(final Member member);
}
Original file line number Diff line number Diff line change
@@ -1,33 +1,51 @@
package shook.shook.member.application;

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

import org.assertj.core.api.AssertionsForClassTypes;
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.test.context.jdbc.Sql;
import shook.shook.auth.exception.AuthorizationException;
import shook.shook.auth.ui.Authority;
import shook.shook.auth.ui.argumentresolver.MemberInfo;
import shook.shook.member.domain.Member;
import shook.shook.member.domain.Nickname;
import shook.shook.member.domain.repository.MemberRepository;
import shook.shook.member.exception.MemberException;
import shook.shook.song.domain.killingpart.KillingPart;
import shook.shook.song.domain.killingpart.KillingPartComment;
import shook.shook.song.domain.killingpart.KillingPartLike;
import shook.shook.song.domain.killingpart.repository.KillingPartCommentRepository;
import shook.shook.song.domain.killingpart.repository.KillingPartLikeRepository;
import shook.shook.song.domain.killingpart.repository.KillingPartRepository;
import shook.shook.support.UsingJpaTest;

@Sql("classpath:/killingpart/initialize_killing_part_song.sql")
class MemberServiceTest extends UsingJpaTest {

private static Member savedMember;

@Autowired
private MemberRepository memberRepository;

@Autowired
private KillingPartRepository killingPartRepository;

@Autowired
private KillingPartCommentRepository partCommentRepository;

@Autowired
private KillingPartLikeRepository likeRepository;

private MemberService memberService;

@BeforeEach
void setUp() {
memberService = new MemberService(memberRepository);
memberService = new MemberService(memberRepository, partCommentRepository, likeRepository);
savedMember = memberRepository.save(new Member("[email protected]", "shook"));
}

Expand All @@ -43,8 +61,8 @@ void register() {
//then
final String nickname = email.split("@")[0];

assertThat(result.getEmail()).isEqualTo(email);
assertThat(result.getNickname()).isEqualTo(nickname);
AssertionsForClassTypes.assertThat(result.getEmail()).isEqualTo(email);
AssertionsForClassTypes.assertThat(result.getNickname()).isEqualTo(nickname);
}

@DisplayName("중복된 이메일로 회원을 등록되는 경우 예외를 던진다.")
Expand All @@ -67,9 +85,10 @@ void findByEmail() {
final Member result = memberService.findByEmail(savedMember.getEmail()).get();

//then
assertThat(result.getId()).isEqualTo(savedMember.getId());
assertThat(result.getEmail()).isEqualTo(savedMember.getEmail());
assertThat(result.getNickname()).isEqualTo(savedMember.getNickname());
AssertionsForClassTypes.assertThat(result.getId()).isEqualTo(savedMember.getId());
AssertionsForClassTypes.assertThat(result.getEmail()).isEqualTo(savedMember.getEmail());
AssertionsForClassTypes.assertThat(result.getNickname())
.isEqualTo(savedMember.getNickname());
}

@DisplayName("회원을 id와 nickname으로 조회한다.")
Expand All @@ -82,7 +101,8 @@ void success_findByIdAndNickname() {
new Nickname(savedMember.getNickname()));

//then
assertThat(result).usingRecursiveComparison().isEqualTo(savedMember);
AssertionsForClassTypes.assertThat(result).usingRecursiveComparison()
.isEqualTo(savedMember);
}

@DisplayName("회원을 id와 nickname으로 조회할 때 회원의 닉네임이 잘못되면 예외를 던진다.")
Expand Down Expand Up @@ -121,19 +141,24 @@ void fail_findByIdAndNickname_wrong_memberId_and_nickname() {
.isInstanceOf(MemberException.MemberNotExistException.class);
}

@DisplayName("회원 id로 회원을 삭제한다.")
@DisplayName("회원 id로 회원을 삭제시 회원의 댓글을 모두 삭제하고 회원의 좋아요는 삭제 상태로 변환한다.")
@Test
void success_delete() {
// given
final Long targetId = savedMember.getId();
final Long requestId = targetId;

final KillingPart killingPart = killingPartRepository.findById(1L).get();
likeRepository.save(new KillingPartLike(killingPart, savedMember));
partCommentRepository.save(KillingPartComment.forSave(killingPart, "hi", savedMember));

saveAndClearEntityManager();
// when
memberService.deleteById(targetId, new MemberInfo(requestId, Authority.MEMBER));
memberService.deleteById(targetId, new MemberInfo(targetId, Authority.MEMBER));

// then
assertThat(memberRepository.findById(targetId))
.isEmpty();
assertThat(likeRepository.findAllByMemberAndIsDeleted(savedMember, false)).isEmpty();
assertThat(partCommentRepository.findAllByMember(savedMember)).isEmpty();
assertThat(memberRepository.findById(targetId)).isEmpty();
}

@DisplayName("회원 id로 회원을 삭제할 때, 존재하지 않는 id 라면 예외가 발생한다.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ void createdAt() {
assertThat(partComment.getCreatedAt()).isBetween(prev, after);
}

@DisplayName("KillingPartComment 를 저장할 때의 시간 정보로 createAt이 자동 생성된다.")
@DisplayName("멤버가 작성한 모든 파트 댓글을 삭제한다.")
@Test
void deleteAllByMemberId() {
void deleteAllByMember() {
//given
killingPartRepository.deleteAll();
final KillingPartComment partComment1 = KillingPartComment.forSave(SAVED_KILLING_PART,
Expand All @@ -82,7 +82,7 @@ void deleteAllByMemberId() {
killingPartCommentRepository.save(partComment2);

//when
killingPartCommentRepository.deleteAllByMember_Id(MEMBER.getId());
killingPartCommentRepository.deleteAllByMember(MEMBER);

//then
final List<KillingPart> all = killingPartRepository.findAll();
Expand Down

0 comments on commit 630d3f8

Please sign in to comment.