Skip to content

Commit

Permalink
refactor: (#513) Report 도메인 관련 테스트 코드 리팩토링
Browse files Browse the repository at this point in the history
  • Loading branch information
tjdtls690 committed Sep 7, 2023
1 parent f0aad00 commit f7a1f0e
Show file tree
Hide file tree
Showing 9 changed files with 212 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
@Embeddable
class Content {
public class Content {

private static final int MAXIMUM_LENGTH = 500;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import com.votogether.global.exception.BadRequestException;
import com.votogether.test.annotation.ServiceTest;
import com.votogether.test.fixtures.MemberFixtures;
import com.votogether.test.persister.MemberTestPersister;
import jakarta.persistence.EntityManager;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
Expand Down Expand Up @@ -60,9 +59,6 @@ class MemberServiceTest {
@Autowired
CommentRepository commentRepository;

@Autowired
MemberTestPersister memberTestPersister;

@Autowired
EntityManager em;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.votogether.domain.post.entity.comment.Comment;
import com.votogether.test.annotation.RepositoryTest;
import com.votogether.test.fixtures.MemberFixtures;
import com.votogether.test.persister.PostTestPersister;
import java.time.LocalDateTime;
import java.util.List;
import org.junit.jupiter.api.DisplayName;
Expand All @@ -27,18 +28,21 @@ class CommentRepositoryTest {
@Autowired
PostRepository postRepository;

@Autowired
PostTestPersister postTestPersister;

@Test
@DisplayName("게시글의 댓글 목록을 조회한다.")
void findAllByPost() {
// given
Member member = memberRepository.save(MemberFixtures.MALE_20.get());
Post post = postRepository.save(
Post.builder()
.writer(member)
.postBody(PostBody.builder().title("titleA").content("contentA").build())
.deadline(LocalDateTime.of(2100, 7, 12, 0, 0))
.build()
);

final Post post = postTestPersister.builder()
.writer(member)
.postBody(PostBody.builder().title("titleA").content("contentA").build())
.deadline(LocalDateTime.of(2100, 7, 12, 0, 0))
.save();

Comment commentA = commentRepository.save(
Comment.builder()
.member(member)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ void reportPost() throws Exception {
.body(request)
.when().post("/report")
.then().log().all()
.statusCode(HttpStatus.OK.value());
.assertThat()
.status(HttpStatus.OK);
}

@Test
Expand Down Expand Up @@ -110,7 +111,8 @@ void reportComment() throws Exception {
.body(request)
.when().post("/report")
.then().log().all()
.statusCode(HttpStatus.OK.value());
.assertThat()
.status(HttpStatus.OK);
}

@Test
Expand Down Expand Up @@ -141,7 +143,8 @@ void reportNickname() throws Exception {
.body(request)
.when().post("/report")
.then().log().all()
.statusCode(HttpStatus.OK.value());
.assertThat()
.status(HttpStatus.OK);
}

}
Expand Down Expand Up @@ -175,7 +178,8 @@ void report(Long id) throws Exception {
.body(request)
.when().post("/report")
.then().log().all()
.statusCode(HttpStatus.BAD_REQUEST.value());
.assertThat()
.status(HttpStatus.BAD_REQUEST);
}

@ParameterizedTest
Expand Down Expand Up @@ -207,7 +211,8 @@ void reportBadRequest(String reason) throws Exception {
.body(request)
.when().post("/report")
.then().log().all()
.statusCode(HttpStatus.BAD_REQUEST.value());
.assertThat()
.status(HttpStatus.BAD_REQUEST);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
import com.votogether.domain.report.entity.vo.ReportType;
import com.votogether.test.annotation.RepositoryTest;
import com.votogether.test.fixtures.MemberFixtures;
import com.votogether.test.persister.PostTestPersister;
import com.votogether.test.persister.ReportTestPersister;
import java.time.LocalDateTime;
import lombok.RequiredArgsConstructor;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -29,34 +32,37 @@ class ReportRepositoryTest {
@Autowired
PostRepository postRepository;

@Autowired
PostTestPersister postTestPersister;

@Autowired
ReportTestPersister reportTestPersister;


@Test
@DisplayName("회원, 신고타입, 대상ID를 통해서 신고 횟수를 반환한다.")
void countByMemberAndReportTypeAndTargetId() {
// given
Member member = MemberFixtures.MALE_20.get();
ReportType reportType = ReportType.POST;

PostBody postBody = PostBody.builder()
.title("title")
.content("content")
.build();

Post post = Post.builder()
memberRepository.save(member);
Post post = postTestPersister.builder()
.writer(member)
.postBody(postBody)
.deadline(LocalDateTime.of(2100, 7, 12, 0, 0))
.build();

memberRepository.save(member);
postRepository.save(post);
.save();

Report report = Report.builder()
reportTestPersister.builder()
.member(member)
.reportType(reportType)
.targetId(post.getId())
.reason("불건전한 게시글")
.build();
reportRepository.save(report);
.save();

// when
int reportCount = reportRepository.countByReportTypeAndTargetId(reportType, post.getId());
Expand All @@ -76,22 +82,19 @@ void findByMemberAndReportTypeAndTargetId() {
.content("content")
.build();

Post post = Post.builder()
memberRepository.save(member);
Post post = postTestPersister.builder()
.writer(member)
.postBody(postBody)
.deadline(LocalDateTime.of(2100, 7, 12, 0, 0))
.build();

memberRepository.save(member);
postRepository.save(post);
.save();

Report report = Report.builder()
.targetId(post.getId())
.reportType(ReportType.POST)
reportTestPersister.builder()
.member(member)
.reportType(ReportType.POST)
.targetId(post.getId())
.reason("불건전한 게시글")
.build();
reportRepository.save(report);
.save();

// when
Report actualReport = reportRepository.findByMemberAndReportTypeAndTargetId(
Expand Down
Loading

0 comments on commit f7a1f0e

Please sign in to comment.