Skip to content

Commit

Permalink
[BE] test: 객관식 검증, 리뷰 생성 테스트에 픽스처를 적용 (#552)
Browse files Browse the repository at this point in the history
* test: 선택형 응답 검증 테스트에 픽스처 적용

* test: 픽스처에 @component 추가

* test: 리뷰 작성 테스트에 픽스쳐 적용

* test: 불필요한 import문 제거

* chore: 픽스처가 테스트 환경에서만 빈 등록되게 TestConfig 수정

* test: 픽스처 주입 방식 변경

* fix: 생성자 오류 수정

* refactor: static 메서드 적용

* test: 필요없는 데이터 삽입 제거

* test: 가독성을 위한 변수 추출

* test: 픽스처로 대체할 수 있는 부분 추가 반영
  • Loading branch information
skylar1220 authored Sep 7, 2024
1 parent b02b054 commit cc66906
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 89 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package reviewme.review.service;

import static org.assertj.core.api.Assertions.assertThatCode;
import static reviewme.fixture.QuestionFixture.선택형_필수_질문;

import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import reviewme.fixture.OptionGroupFixture;
import reviewme.fixture.OptionItemFixture;
import reviewme.question.domain.OptionGroup;
import reviewme.question.domain.OptionItem;
import reviewme.question.domain.OptionType;
import reviewme.question.domain.Question;
import reviewme.question.domain.QuestionType;
import reviewme.question.repository.OptionGroupRepository;
import reviewme.question.repository.OptionItemRepository;
import reviewme.question.repository.QuestionRepository;
Expand Down Expand Up @@ -38,13 +39,6 @@ class CreateCheckBoxAnswerRequestValidatorTest {
@Autowired
private OptionItemRepository optionItemRepository;

private Question savedQuestion;

@BeforeEach
void setUp() {
savedQuestion = questionRepository.save(new Question(true, QuestionType.CHECKBOX, "질문", null, 1));
}

@Test
void 저장되지_않은_질문에_대한_응답이면_예외가_발생한다() {
// given
Expand All @@ -61,6 +55,7 @@ void setUp() {
@Test
void 선택형_질문에_텍스트_응답을_하면_예외가_발생한다() {
// given
Question savedQuestion = questionRepository.save(선택형_필수_질문());
CreateReviewAnswerRequest request = new CreateReviewAnswerRequest(
savedQuestion.getId(), List.of(1L), "서술형 응답"
);
Expand All @@ -71,10 +66,12 @@ void setUp() {
}

@Test
void 저장되지_않은_옵션그룹에_대해_응답하면_예외가_발생한다() {
void 응답한_질문과_대응하는_옵션그룹이_존재하지_않으면_예외가_발생한다() {
// given
long notselectedOptionId = 1L;
Question savedQuestion = questionRepository.save(선택형_필수_질문());
CreateReviewAnswerRequest request = new CreateReviewAnswerRequest(
savedQuestion.getId(), List.of(1L), null
savedQuestion.getId(), List.of(notselectedOptionId), null
);

// when, then
Expand All @@ -85,6 +82,7 @@ void setUp() {
@Test
void 필수_선택형_질문에_응답을_하지_않으면_예외가_발생한다() {
// given
Question savedQuestion = questionRepository.save(선택형_필수_질문());
optionGroupRepository.save(
new OptionGroup(savedQuestion.getId(), 1, 3)
);
Expand All @@ -101,12 +99,9 @@ void setUp() {
@Test
void 옵션그룹에서_제공하지_않은_옵션아이템을_응답하면_예외가_발생한다() {
// given
OptionGroup savedOptionGroup = optionGroupRepository.save(
new OptionGroup(savedQuestion.getId(), 1, 3)
);
OptionItem savedOptionItem = optionItemRepository.save(
new OptionItem("옵션", savedOptionGroup.getId(), 1, OptionType.KEYWORD)
);
Question savedQuestion = questionRepository.save(선택형_필수_질문());
OptionGroup savedOptionGroup = optionGroupRepository.save(OptionGroupFixture.선택지_그룹(savedQuestion.getId()));
OptionItem savedOptionItem = optionItemRepository.save(OptionItemFixture.선택지(savedOptionGroup.getId()));

CreateReviewAnswerRequest request = new CreateReviewAnswerRequest(
savedQuestion.getId(), List.of(savedOptionItem.getId() + 1L), null
Expand All @@ -120,6 +115,7 @@ void setUp() {
@Test
void 옵션그룹에서_정한_최소_선택_수_보다_적게_선택하면_예외가_발생한다() {
// given
Question savedQuestion = questionRepository.save(선택형_필수_질문());
OptionGroup savedOptionGroup = optionGroupRepository.save(
new OptionGroup(savedQuestion.getId(), 2, 3)
);
Expand All @@ -139,6 +135,7 @@ void setUp() {
@Test
void 옵션그룹에서_정한_최대_선택_수_보다_많이_선택하면_예외가_발생한다() {
// given
Question savedQuestion = questionRepository.save(선택형_필수_질문());
OptionGroup savedOptionGroup = optionGroupRepository.save(
new OptionGroup(savedQuestion.getId(), 1, 1)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,22 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import static reviewme.fixture.OptionGroupFixture.선택지_그룹;
import static reviewme.fixture.OptionItemFixture.선택지;
import static reviewme.fixture.QuestionFixture.서술형_옵션_질문;
import static reviewme.fixture.QuestionFixture.서술형_필수_질문;
import static reviewme.fixture.QuestionFixture.선택형_필수_질문;
import static reviewme.fixture.ReviewGroupFixture.리뷰_그룹;
import static reviewme.fixture.SectionFixture.조건부로_보이는_섹션;
import static reviewme.fixture.SectionFixture.항상_보이는_섹션;
import static reviewme.fixture.TemplateFixture.템플릿;

import java.util.List;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import reviewme.question.domain.OptionGroup;
import reviewme.question.domain.OptionItem;
import reviewme.question.domain.OptionType;
import reviewme.question.domain.Question;
import reviewme.question.domain.QuestionType;
import reviewme.question.repository.OptionGroupRepository;
import reviewme.question.repository.OptionItemRepository;
import reviewme.question.repository.QuestionRepository;
Expand All @@ -21,12 +28,9 @@
import reviewme.review.service.dto.request.CreateReviewRequest;
import reviewme.review.service.exception.MissingRequiredQuestionException;
import reviewme.review.service.exception.UnnecessaryQuestionIncludedException;
import reviewme.reviewgroup.domain.ReviewGroup;
import reviewme.reviewgroup.repository.ReviewGroupRepository;
import reviewme.support.ServiceTest;
import reviewme.template.domain.Section;
import reviewme.template.domain.Template;
import reviewme.template.domain.VisibleType;
import reviewme.template.repository.SectionRepository;
import reviewme.template.repository.TemplateRepository;

Expand Down Expand Up @@ -67,63 +71,36 @@ class CreateReviewServiceTest {
void 필수_질문에_모두_응답하는_경우_예외가_발생하지_않는다() {
// 리뷰 그룹 저장
String reviewRequestCode = "1234";
reviewGroupRepository.save(new ReviewGroup("리뷰어", "프로젝트", reviewRequestCode, "12341234"));
reviewGroupRepository.save(리뷰_그룹(reviewRequestCode, "12341234"));

// 필수 선택형 질문, 섹션 저장
Question alwaysRequiredQuestion = questionRepository.save(
new Question(true, QuestionType.CHECKBOX, "질문", "가이드라인", 1)
);
OptionGroup alwaysRequiredOptionGroup = optionGroupRepository.save(
new OptionGroup(alwaysRequiredQuestion.getId(), 1, 2)
);
OptionItem alwaysRequiredOptionItem1 = optionItemRepository.save(
new OptionItem("선택지", alwaysRequiredOptionGroup.getId(), 1, OptionType.KEYWORD)
);
OptionItem alwaysRequiredOptionItem2 = optionItemRepository.save(
new OptionItem("선택지", alwaysRequiredOptionGroup.getId(), 2, OptionType.KEYWORD)
);
Section alwaysRequiredSection = sectionRepository.save(
new Section(VisibleType.ALWAYS, List.of(alwaysRequiredQuestion.getId()), null, "섹션명", "말머리", 1)
);
Question alwaysRequiredQuestion = questionRepository.save(선택형_필수_질문());
OptionGroup alwaysRequiredOptionGroup = optionGroupRepository.save(선택지_그룹(alwaysRequiredQuestion.getId()));
OptionItem alwaysRequiredOptionItem1 = optionItemRepository.save(선택지(alwaysRequiredOptionGroup.getId(), 1));
OptionItem alwaysRequiredOptionItem2 = optionItemRepository.save(선택지(alwaysRequiredOptionGroup.getId(), 2));
Section alwaysRequiredSection = sectionRepository.save(항상_보이는_섹션(List.of(alwaysRequiredQuestion.getId())));

// 필수가 아닌 서술형 질문 저장
Question notRequiredQuestion = questionRepository.save(
new Question(false, QuestionType.TEXT, "질문", "가이드라인", 1)
);
Section notRequiredSection = sectionRepository.save(
new Section(VisibleType.ALWAYS, List.of(notRequiredQuestion.getId()), null, "섹션명", "말머리", 1)
);
Question notRequiredQuestion = questionRepository.save(서술형_옵션_질문());
Section notRequiredSection = sectionRepository.save(항상_보이는_섹션(List.of(notRequiredQuestion.getId()), 1));

// optionItem 선택에 따라서 required 가 달라지는 섹션1 저장
Question conditionalTextQuestion1 = questionRepository.save(
new Question(true, QuestionType.TEXT, "질문", "가이드라인", 1)
);
Question conditionalCheckQuestion = questionRepository.save(
new Question(true, QuestionType.CHECKBOX, "질문", "가이드라인", 1)
);
OptionGroup conditionalOptionGroup = optionGroupRepository.save(
new OptionGroup(conditionalCheckQuestion.getId(), 1, 2)
);
OptionItem conditionalOptionItem = optionItemRepository.save(
new OptionItem("선택지", conditionalOptionGroup.getId(), 1, OptionType.KEYWORD)
);
Question conditionalTextQuestion1 = questionRepository.save(서술형_필수_질문(1));
Question conditionalCheckQuestion = questionRepository.save(선택형_필수_질문(2));
OptionGroup conditionalOptionGroup = optionGroupRepository.save(선택지_그룹(conditionalCheckQuestion.getId()));
OptionItem conditionalOptionItem = optionItemRepository.save(선택지(conditionalOptionGroup.getId()));
Section conditionalSection1 = sectionRepository.save(
new Section(VisibleType.CONDITIONAL,
List.of(conditionalTextQuestion1.getId(), conditionalCheckQuestion.getId()),
alwaysRequiredOptionItem1.getId(), "섹션명", "말머리", 1)
);
조건부로_보이는_섹션(List.of(conditionalTextQuestion1.getId(), conditionalCheckQuestion.getId()),
alwaysRequiredOptionItem1.getId(), 2));

// optionItem 선택에 따라서 required 가 달라지는 섹션2 저장
Question conditionalQuestion2 = questionRepository.save(
new Question(true, QuestionType.TEXT, "질문", "가이드라인", 1)
);
Question conditionalQuestion2 = questionRepository.save(서술형_필수_질문());
Section conditionalSection2 = sectionRepository.save(
new Section(VisibleType.CONDITIONAL, List.of(conditionalQuestion2.getId()),
alwaysRequiredOptionItem2.getId(), "섹션명", "말머리", 1)
조건부로_보이는_섹션(List.of(conditionalQuestion2.getId()), alwaysRequiredOptionItem2.getId(), 3)
);

// 템플릿 저장
templateRepository.save(new Template(
templateRepository.save(템플릿(
List.of(alwaysRequiredSection.getId(), conditionalSection1.getId(),
conditionalSection2.getId(), notRequiredSection.getId())
));
Expand Down Expand Up @@ -168,12 +145,12 @@ class CreateReviewServiceTest {
void 텍스트가_포함된_리뷰를_저장한다() {
// given
String reviewRequestCode = "0000";
reviewGroupRepository.save(new ReviewGroup("리뷰어", "프로젝트", reviewRequestCode, "12341234"));
Section section = sectionRepository.save(new Section(VisibleType.ALWAYS, List.of(1L), 1L, "섹션명", "말머리", 1));
templateRepository.save(new Template(List.of(section.getId())));
reviewGroupRepository.save(리뷰_그룹(reviewRequestCode, "12341234"));
Section section = sectionRepository.save(항상_보이는_섹션(List.of(1L)));
templateRepository.save(템플릿(List.of(section.getId())));

String expectedTextAnswer = "답".repeat(20);
Question savedQuestion = questionRepository.save(new Question(true, QuestionType.TEXT, "질문", "가이드라인", 1));
Question savedQuestion = questionRepository.save(서술형_필수_질문());
CreateReviewAnswerRequest createReviewAnswerRequest = new CreateReviewAnswerRequest(savedQuestion.getId(), null,
expectedTextAnswer);
CreateReviewRequest createReviewRequest = new CreateReviewRequest(reviewRequestCode,
Expand All @@ -191,12 +168,11 @@ class CreateReviewServiceTest {
void 필수가_아닌_텍스트형_응답에_빈문자열이_들어오면_저장하지_않는다() {
// given
String reviewRequestCode = "0000";
reviewGroupRepository.save(new ReviewGroup("리뷰어", "프로젝트", reviewRequestCode, "12341234"));
Section section = sectionRepository.save(new Section(VisibleType.ALWAYS, List.of(1L), 1L, "섹션명", "말머리", 1));
templateRepository.save(new Template(List.of(section.getId())));
reviewGroupRepository.save(리뷰_그룹(reviewRequestCode, "12341234"));
Section section = sectionRepository.save(항상_보이는_섹션(List.of(1L)));
templateRepository.save(템플릿(List.of(section.getId())));

Question savedQuestion = questionRepository.save(
new Question(false, QuestionType.TEXT, "질문", "가이드라인", 1));
Question savedQuestion = questionRepository.save(서술형_옵션_질문());
CreateReviewAnswerRequest emptyTextReviewRequest = new CreateReviewAnswerRequest(
savedQuestion.getId(), null, "");
CreateReviewAnswerRequest validTextReviewRequest = new CreateReviewAnswerRequest(
Expand All @@ -216,16 +192,14 @@ class CreateReviewServiceTest {
void 체크박스가_포함된_리뷰를_저장한다() {
// given
String reviewRequestCode = "0000";
reviewGroupRepository.save(new ReviewGroup("리뷰어", "프로젝트", reviewRequestCode, "12341234"));
Section section = sectionRepository.save(new Section(VisibleType.ALWAYS, List.of(1L), 1L, "섹션명", "말머리", 1));
templateRepository.save(new Template(List.of(section.getId())));

Question savedQuestion = questionRepository.save(new Question(true, QuestionType.CHECKBOX, "질문", "가이드라인", 1));
OptionGroup savedOptionGroup = optionGroupRepository.save(new OptionGroup(savedQuestion.getId(), 2, 2));
OptionItem savedOptionItem1 = optionItemRepository.save(
new OptionItem("선택지1", savedOptionGroup.getId(), 1, OptionType.KEYWORD));
OptionItem savedOptionItem2 = optionItemRepository.save(
new OptionItem("선택지2", savedOptionGroup.getId(), 2, OptionType.KEYWORD));
reviewGroupRepository.save(리뷰_그룹(reviewRequestCode, "12341234"));
Section section = sectionRepository.save(항상_보이는_섹션(List.of(1L)));
templateRepository.save(템플릿(List.of(section.getId())));

Question savedQuestion = questionRepository.save(선택형_필수_질문());
OptionGroup savedOptionGroup = optionGroupRepository.save(선택지_그룹(savedQuestion.getId()));
OptionItem savedOptionItem1 = optionItemRepository.save(선택지(savedOptionGroup.getId(), 1));
OptionItem savedOptionItem2 = optionItemRepository.save(선택지(savedOptionGroup.getId(), 2));
CreateReviewAnswerRequest createReviewAnswerRequest = new CreateReviewAnswerRequest(savedQuestion.getId(),
List.of(savedOptionItem1.getId(), savedOptionItem2.getId()), null);
CreateReviewRequest createReviewRequest = new CreateReviewRequest(reviewRequestCode,
Expand All @@ -243,12 +217,13 @@ class CreateReviewServiceTest {
void 적정_글자수인_텍스트_응답인_경우_정상_저장된다() {
// given
String reviewRequestCode = "0000";
reviewGroupRepository.save(new ReviewGroup("리뷰어", "프로젝트", reviewRequestCode, "12341234"));
Section section = sectionRepository.save(new Section(VisibleType.ALWAYS, List.of(1L), 1L, "섹션명", "말머리", 1));
templateRepository.save(new Template(List.of(section.getId())));
reviewGroupRepository.save(리뷰_그룹(reviewRequestCode, "12341234"));
Section section = sectionRepository.save(항상_보이는_섹션(List.of(1L)));
templateRepository.save(템플릿(List.of(section.getId())));

String expectedTextAnswer = "답".repeat(1000);
Question savedQuestion = questionRepository.save(new Question(true, QuestionType.TEXT, "질문", "가이드라인", 1));
Question savedQuestion = questionRepository.save(서술형_필수_질문());

CreateReviewAnswerRequest createReviewAnswerRequest = new CreateReviewAnswerRequest(savedQuestion.getId(), null,
expectedTextAnswer);
CreateReviewRequest createReviewRequest = new CreateReviewRequest(reviewRequestCode,
Expand Down

0 comments on commit cc66906

Please sign in to comment.