delete 부분 까지 달리고 pr 을 날려주도록 하자.

지난번 하면서 유의해야할 부분으로는 jpa 에서 쿼리 짜서 보내야 쿼리가 2방 안나가는것 그것만 조심하면 될것같다.

 

바로 컨트롤러 테스트 들어가자

지난번 사용한거 들고오자 

더보기
@DeleteMapping("{memberContentId}/comment/{commentId}")
public ResponseEntity<?> deleteContentComment(
        @PathVariable("memberContentId") Long memberContentId,
        @PathVariable("commentId") Long commentId
){

//        memberContentService.contentConmmentDeltet(memberContentId,commentId);

    return new ResponseEntity<>(
            CommonResponse.ok(),
            HttpStatus.OK
    );
}

테스트 는 통과 된다.


삭제 의 실패 케이스 는 서비스 쪽에서 테스트 해주자.

 

이렇게 테스트 해주면 될것 같다.

1~4 번은 수정 과 유사하니 한번에 테스트 하겠다.

 

더보기
@Test
@DisplayName("멤버 피드 댓글 삭제 실패 [로그인 유저 미 일치]")
public void doesNotMatchLogInUser() throws Exception{
    //given
    given(commonRequestContext.getMemberEmail()).willReturn("True-Lover");
    //when
    MemberException exception = assertThrows(MemberException.class,
            ()->memberContentService.contentConmmentDeltet(contentId,
                    commentId));
    //then
    assertEquals(MemberErrorCode.MEMBER_EMAIL_ERROR,exception.getErrorCode());
}

@Test
@DisplayName("멤버 피드 댓글 삭제 실패 [피드 가 존재하지 않는 경우]")
public void doesNotExistFeed() throws Exception{
    //given
    doReturn(Optional.of(m))
            .when(memberRepository).findByEmail(any());
    //when
    MemberException exception = assertThrows(MemberException.class,
            ()->memberContentService.contentConmmentDeltet(contentId,
                    commentId));
    //then
    assertEquals(MemberErrorCode.MEMBER_CONTENT_DOES_NOT_EXIST,exception.getErrorCode());
}

@Test
@DisplayName("멤버 피드 댓글 삭제 실패 [피드가 삭제된 경우]")
public void hasDeletedFeed() throws Exception{
    //given
    doReturn(Optional.of(m))
            .when(memberRepository).findByEmail(any());
    doReturn(Optional.of(MemberContent.builder().deletedYn(true).build()))
            .when(memberContentRepository).findById(any());
    //when
    MemberException exception = assertThrows(MemberException.class,
            ()->memberContentService.contentConmmentDeltet(contentId,
                    commentId));
    //then
    assertEquals(MemberErrorCode.MEMBER_CONTENT_DELETED,exception.getErrorCode());
}
@Test
@DisplayName("멤버 피드 댓글 삭제 실패 [댓글 이 존재하지 않는 경우]")
public void doesNotExistComment() throws Exception{
    //given
    doReturn(Optional.of(m))
            .when(memberRepository).findByEmail(any());
    doReturn(Optional.of(MemberContent.builder().build()))
            .when(memberContentRepository).findById(any());
    //when
    MemberException exception = assertThrows(MemberException.class,
            ()->memberContentService.contentConmmentDeltet(contentId,
                    commentId));
    //then
    assertEquals(MemberErrorCode.MEMBER_COMMENT_DOES_NOT_EXIST,exception.getErrorCode());
}

@Test
@DisplayName("멤버 피드 댓글 삭제 실패 [댓글 이 삭제된 경우]")
public void hasDeletedComment() throws Exception{
    //given
    doReturn(Optional.of(m))
            .when(memberRepository).findByEmail(any());
    doReturn(Optional.of(mc)).when(memberContentRepository).findById(any());
    doReturn(Optional.of(MemberContentComment.builder()
            .deletedYn(true)
            .build()))
            .when(memberContentCommentRepository)
            .findByIdAndMemberAndMemberContent(commentId,m,mc);
    //when
    MemberException exception = assertThrows(MemberException.class,
            ()->memberContentService.contentConmmentDeltet(contentId,
                    commentId));
    //then
    assertEquals(MemberErrorCode.MEMBER_COMMENT_DELETED,exception.getErrorCode());
}

성공 케이스 테스트 를 하러가자.

테스트 코드

더보기
@Test
@DisplayName("멤버 피드 댓글 삭제 성공")
public void success() throws Exception{

    //given
    doReturn(Optional.of(m))
            .when(memberRepository).findByEmail(any());
    doReturn(Optional.of(mc)).when(memberContentRepository).findById(any());
    doReturn(Optional.of(MemberContentComment.builder().build()))
            .when(memberContentCommentRepository)
            .findByIdAndMemberAndMemberContent(commentId,m,mc);

    //when
    memberContentService.contentConmmentDeltet(contentId,commentId);
    //then
    verify(memberContentCommentRepository,times(1)).deleteById(any());
}

서비스,레포지토리 코드

더보기
@Modifying
@Query("delete from MemberContentComment mcc where mcc.id = :commentId")
void deleteById(@Param("commentId") Long commentId);

//Service

@Override
public void contentConmmentDeltet(Long memberContentId, Long commentId) {
    Member m = validCheckLoggedInUser();
    MemberContent mc = getContent(memberContentId);
    MemberContentComment memberContentComment = getMemberContentComment(commentId, m, mc);

    memberContentCommentRepository.deleteById(memberContentComment.getId());
}

테스트 는 통과되는데 음 커맨트 좋아요 가 있지 않겠는가 ? 그것도 날려줘야한다. 까먹고 있엇다.

 

memberContentCommentLikeRepository.deleteAllByMemberContentCommentIn(
        new ArrayList<>(List.of(memberContentComment))
);

지난번 피드 만들때 생성한 함수를 이용하자 서비스 코드 쪽에 추가해주자 .

 

부트 를 띄워서 테스트 를 해보자. 

멤버,피드,댓글 가져오는 쿼리 총 3개, 지우는 쿼리 2개

 

다시 생각해보니깐 우리 워크듀오는 지우지 않고 피드 를 바꿔준다. 업데이트 쿼리 1개로 바꿔주고 terminate 라는 함수를 구현해주자.

public void terminate(){
    this.deletedAt = LocalDateTime.now();
    this.deletedYn = true;
}

삭제된 시간 과 불리언 값을 업데이트 해주는것이다. 서버를 띄워서 테스트 해보자.

멤버,피드,댓글 가져오는 쿼리 총 3개, 지우는 쿼리 1개 업데이트 쿼리 1개

 

더보기
2022-09-28 22:01:32.021 DEBUG 76860 --- [nio-8080-exec-2] org.hibernate.SQL                        : 
    select
        member0_.member_id as member_i1_12_,
        member0_.created_at as created_2_12_,
        member0_.updated_at as updated_3_12_,
        member0_.deleted_at as deleted_4_12_,
        member0_.email as email5_12_,
        member0_.member_status as member_s6_12_,
        member0_.nickname as nickname7_12_,
        member0_.password as password8_12_,
        member0_.phone_number as phone_nu9_12_,
        member0_.profile_img as profile10_12_,
        member0_.status as status11_12_,
        member0_.username as usernam12_12_ 
    from
        member member0_ 
    where
        member0_.email=?
2022-09-28 22:01:32.023  INFO 76860 --- [nio-8080-exec-2] p6spy                                    : #1664370092023 | took 1ms | statement | connection 9| url jdbc:mysql://localhost:3306/workduo?autoReconnect=true&allowPublicKeyRetrieval=true&rewriteBatchedStatements=true
select member0_.member_id as member_i1_12_, member0_.created_at as created_2_12_, member0_.updated_at as updated_3_12_, member0_.deleted_at as deleted_4_12_, member0_.email as email5_12_, member0_.member_status as member_s6_12_, member0_.nickname as nickname7_12_, member0_.password as password8_12_, member0_.phone_number as phone_nu9_12_, member0_.profile_img as profile10_12_, member0_.status as status11_12_, member0_.username as usernam12_12_ from member member0_ where member0_.email=?
select member0_.member_id as member_i1_12_, member0_.created_at as created_2_12_, member0_.updated_at as updated_3_12_, member0_.deleted_at as deleted_4_12_, member0_.email as email5_12_, member0_.member_status as member_s6_12_, member0_.nickname as nickname7_12_, member0_.password as password8_12_, member0_.phone_number as phone_nu9_12_, member0_.profile_img as profile10_12_, member0_.status as status11_12_, member0_.username as usernam12_12_ from member member0_ where member0_.email='client2@client.com';
2022-09-28 22:01:32.030 DEBUG 76860 --- [nio-8080-exec-2] org.hibernate.SQL                        : 
    select
        membercont0_.member_content_id as member_c1_15_0_,
        membercont0_.created_at as created_2_15_0_,
        membercont0_.updated_at as updated_3_15_0_,
        membercont0_.content as content4_15_0_,
        membercont0_.deleted_at as deleted_5_15_0_,
        membercont0_.deleted_yn as deleted_6_15_0_,
        membercont0_.member_id as member_10_15_0_,
        membercont0_.notice_yn as notice_y7_15_0_,
        membercont0_.sort_value as sort_val8_15_0_,
        membercont0_.title as title9_15_0_ 
    from
        member_content membercont0_ 
    where
        membercont0_.member_content_id=?
2022-09-28 22:01:32.034  INFO 76860 --- [nio-8080-exec-2] p6spy                                    : #1664370092034 | took 4ms | statement | connection 9| url jdbc:mysql://localhost:3306/workduo?autoReconnect=true&allowPublicKeyRetrieval=true&rewriteBatchedStatements=true
select membercont0_.member_content_id as member_c1_15_0_, membercont0_.created_at as created_2_15_0_, membercont0_.updated_at as updated_3_15_0_, membercont0_.content as content4_15_0_, membercont0_.deleted_at as deleted_5_15_0_, membercont0_.deleted_yn as deleted_6_15_0_, membercont0_.member_id as member_10_15_0_, membercont0_.notice_yn as notice_y7_15_0_, membercont0_.sort_value as sort_val8_15_0_, membercont0_.title as title9_15_0_ from member_content membercont0_ where membercont0_.member_content_id=?
select membercont0_.member_content_id as member_c1_15_0_, membercont0_.created_at as created_2_15_0_, membercont0_.updated_at as updated_3_15_0_, membercont0_.content as content4_15_0_, membercont0_.deleted_at as deleted_5_15_0_, membercont0_.deleted_yn as deleted_6_15_0_, membercont0_.member_id as member_10_15_0_, membercont0_.notice_yn as notice_y7_15_0_, membercont0_.sort_value as sort_val8_15_0_, membercont0_.title as title9_15_0_ from member_content membercont0_ where membercont0_.member_content_id=4;
2022-09-28 22:01:32.044 DEBUG 76860 --- [nio-8080-exec-2] org.hibernate.SQL                        : 
    select
        membercont0_.member_content_comment_id as member_c1_16_,
        membercont0_.created_at as created_2_16_,
        membercont0_.updated_at as updated_3_16_,
        membercont0_.content as content4_16_,
        membercont0_.deleted_at as deleted_5_16_,
        membercont0_.deleted_yn as deleted_6_16_,
        membercont0_.member_id as member_i7_16_,
        membercont0_.member_content_id as member_c8_16_ 
    from
        member_content_comment membercont0_ 
    where
        membercont0_.member_content_comment_id=? 
        and membercont0_.member_id=? 
        and membercont0_.member_content_id=? 
        and membercont0_.deleted_yn=?
2022-09-28 22:01:32.048  INFO 76860 --- [nio-8080-exec-2] p6spy                                    : #1664370092048 | took 2ms | statement | connection 9| url jdbc:mysql://localhost:3306/workduo?autoReconnect=true&allowPublicKeyRetrieval=true&rewriteBatchedStatements=true
select membercont0_.member_content_comment_id as member_c1_16_, membercont0_.created_at as created_2_16_, membercont0_.updated_at as updated_3_16_, membercont0_.content as content4_16_, membercont0_.deleted_at as deleted_5_16_, membercont0_.deleted_yn as deleted_6_16_, membercont0_.member_id as member_i7_16_, membercont0_.member_content_id as member_c8_16_ from member_content_comment membercont0_ where membercont0_.member_content_comment_id=? and membercont0_.member_id=? and membercont0_.member_content_id=? and membercont0_.deleted_yn=?
select membercont0_.member_content_comment_id as member_c1_16_, membercont0_.created_at as created_2_16_, membercont0_.updated_at as updated_3_16_, membercont0_.content as content4_16_, membercont0_.deleted_at as deleted_5_16_, membercont0_.deleted_yn as deleted_6_16_, membercont0_.member_id as member_i7_16_, membercont0_.member_content_id as member_c8_16_ from member_content_comment membercont0_ where membercont0_.member_content_comment_id=10 and membercont0_.member_id=2 and membercont0_.member_content_id=4 and membercont0_.deleted_yn=false;
2022-09-28 22:01:32.056 DEBUG 76860 --- [nio-8080-exec-2] org.hibernate.SQL                        : 
    delete 
    from
        member_content_comment_like 
    where
        member_content_comment_id in (
            ?
        )
2022-09-28 22:01:32.063  INFO 76860 --- [nio-8080-exec-2] p6spy                                    : #1664370092063 | took 7ms | statement | connection 9| url jdbc:mysql://localhost:3306/workduo?autoReconnect=true&allowPublicKeyRetrieval=true&rewriteBatchedStatements=true
delete from member_content_comment_like where member_content_comment_id in (?)
delete from member_content_comment_like where member_content_comment_id in (10);
2022-09-28 22:01:32.087 DEBUG 76860 --- [nio-8080-exec-2] org.hibernate.SQL                        : 
    update
        member_content_comment 
    set
        updated_at=?,
        content=?,
        deleted_at=?,
        deleted_yn=?,
        member_id=?,
        member_content_id=? 
    where
        member_content_comment_id=?

크 만족 스럽게 나간다.

 

이 딜리트 yn 을 업데이트 함에 따라 커멘트 가져오는 함수를 변경하였다.

@Transactional(readOnly = true)
protected MemberContentComment getMemberContentComment(Long commentId, Member m, MemberContent mc) {
    MemberContentComment memberContentComment = memberContentCommentRepository.
            findByIdAndMemberAndMemberContentAndDeletedYn(commentId, m, mc,false)
            .orElseThrow(() -> new MemberException(MEMBER_COMMENT_DOES_NOT_EXIST));
    isCommentDeleted(memberContentComment);
    return memberContentComment;
}

이렇게 해야 삭제 안된 올바른 쿼리값들을 들고온다.

 

긴글 읽어주셔서 감사합니다.

+ Recent posts