Skip to content

Commit

Permalink
feat(ChecklistQuestionService): 계산기 도메인 도입 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
shin-jisong committed Nov 25, 2024
1 parent f0898fa commit a3404d3
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,8 @@ public List<ChecklistQuestion> filterByAnswer(Answer answer) {
.filter(question -> question.matchAnswer(answer))
.toList();
}

public int size() {
return questions.size();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.bang_ggood.question.domain.ChecklistQuestion;
import com.bang_ggood.question.domain.CustomChecklistQuestion;
import com.bang_ggood.question.domain.Question;
import com.bang_ggood.question.domain.QuestionsScoreCalculator;
import com.bang_ggood.question.repository.ChecklistQuestionRepository;
import com.bang_ggood.question.repository.CustomChecklistQuestionRepository;
import com.bang_ggood.user.domain.User;
Expand Down Expand Up @@ -102,22 +103,8 @@ public List<Category> findCategories(User user, Long checklistId) {
public Integer calculateCategoryScore(Long checklistId, Integer categoryId) {
List<ChecklistQuestion> allAnsweredQuestion = checklistQuestionRepository.findAnsweredQuestionsByChecklistIdAndCategoryId(
checklistId, categoryId);

int allAnsweredQuestionCount = allAnsweredQuestion.size();
int goodAnsweredQuestionCount = countGoodAnsweredQuestion(allAnsweredQuestion);

if (allAnsweredQuestionCount == 0) {
return null;
}

double score = ((double) goodAnsweredQuestionCount / allAnsweredQuestionCount) * 100;
return (int) Math.round(score);
}

private int countGoodAnsweredQuestion(List<ChecklistQuestion> questions) {
return (int) questions.stream()
.filter(question -> question.matchAnswer(Answer.GOOD))
.count();
QuestionsScoreCalculator calculator = new QuestionsScoreCalculator(allAnsweredQuestion);
return calculator.calculateScore();
}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,24 +210,7 @@ void findCategories() {
assertThat(categories).hasSize(1);
}

@DisplayName("카테고리 점수 계산 성공 : 모두 GOOD일 경우")
@Test
void calculateCategoryScore() {
// given
List<ChecklistQuestion> checklistQuestions = List.of(
ChecklistQuestionFixture.CHECKLIST1_QUESTION2_GOOD(checklist, QuestionFixture.QUESTION1_CATEGORY1)
);
checklistQuestionService.createQuestions(checklistQuestions);

// when
Integer categoryId = QuestionFixture.QUESTION1_CATEGORY1.getCategory().getId();
Integer score = checklistQuestionService.calculateCategoryScore(checklist.getId(), categoryId);

// then
assertThat(score).isEqualTo(100);
}

@DisplayName("카테고리 점수 계산 성공 : 일부 답변만 GOOD인 경우")
@DisplayName("카테고리 점수 계산 성공")
@Test
void calculateCategoryScore_partialGoodAnswers() {
// given
Expand All @@ -245,34 +228,6 @@ void calculateCategoryScore_partialGoodAnswers() {
assertThat(score).isEqualTo(50);
}

@DisplayName("카테고리 점수 계산 성공 : 모든 답변이 BAD인 경우")
@Test
void calculateCategoryScore_allBadAnswers() {
// given
List<ChecklistQuestion> checklistQuestions = List.of(
ChecklistQuestionFixture.CHECKLIST1_QUESTION1_BAD(checklist, QuestionFixture.QUESTION1_CATEGORY1)
);
checklistQuestionService.createQuestions(checklistQuestions);

// when
Integer categoryId = QuestionFixture.QUESTION1_CATEGORY1.getCategory().getId();
Integer score = checklistQuestionService.calculateCategoryScore(checklist.getId(), categoryId);

// then
assertThat(score).isEqualTo(0);
}

@DisplayName("카테고리 점수 계산 성공 : 답변이 없는 경우")
@Test
void calculateCategoryScore_noAnswers() {
// given & when
Integer categoryId = QuestionFixture.QUESTION1_CATEGORY1.getCategory().getId();
Integer score = checklistQuestionService.calculateCategoryScore(checklist.getId(), categoryId);

// then
assertThat(score).isEqualTo(null);
}


@DisplayName("커스텀 체크리스트 업데이트 성공")
@Test
Expand Down

0 comments on commit a3404d3

Please sign in to comment.