Skip to content

Commit

Permalink
feat: QuestionsScoreCalculator 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
shin-jisong committed Nov 25, 2024
1 parent 6b52256 commit f0898fa
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.bang_ggood.question.domain;

import java.util.List;

public class QuestionsScoreCalculator {

private static final int NO_QUESTIONS_ANSWERED = 0;
private static final int PERCENT = 100;

private final ChecklistQuestions allAnsweredQuestions;
private final ChecklistQuestions goodAnsweredQuestions;

public QuestionsScoreCalculator(List<ChecklistQuestion> questions) {
this.allAnsweredQuestions = new ChecklistQuestions(questions);
this.goodAnsweredQuestions = new ChecklistQuestions(allAnsweredQuestions.filterByAnswer(Answer.GOOD));
}

public Integer calculateScore() {
int allAnsweredQuestionCount = allAnsweredQuestions.size();
int goodAnsweredQuestionCount = goodAnsweredQuestions.size();

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

double score = ((double) goodAnsweredQuestionCount / allAnsweredQuestionCount) * PERCENT;
return (int) Math.round(score);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package com.bang_ggood.question.domain;

import com.bang_ggood.IntegrationTestSupport;
import com.bang_ggood.checklist.ChecklistFixture;
import com.bang_ggood.checklist.domain.Checklist;
import com.bang_ggood.checklist.repository.ChecklistRepository;
import com.bang_ggood.question.ChecklistQuestionFixture;
import com.bang_ggood.question.QuestionFixture;
import com.bang_ggood.room.RoomFixture;
import com.bang_ggood.room.domain.Room;
import com.bang_ggood.room.repository.RoomRepository;
import com.bang_ggood.user.UserFixture;
import com.bang_ggood.user.domain.User;
import com.bang_ggood.user.repository.UserRepository;
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 java.util.ArrayList;
import java.util.List;

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

class QuestionsScoreCalculatorTest extends IntegrationTestSupport {

@Autowired
private ChecklistRepository checklistRepository;

@Autowired
private RoomRepository roomRepository;

@Autowired
private UserRepository userRepository;

private Checklist checklist;

@BeforeEach
void beforeEach() {
User user = userRepository.save(UserFixture.USER1());
Room room = roomRepository.save(RoomFixture.ROOM_1());
checklist = checklistRepository.save(ChecklistFixture.CHECKLIST1_USER1(room, user));
}

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

// when
QuestionsScoreCalculator calculator = new QuestionsScoreCalculator(checklistQuestions);

// then
assertThat(calculator.calculateScore()).isEqualTo(100);
}

@DisplayName("점수 계산 성공 : 일부 답변만 GOOD인 경우")
@Test
void calculateCategoryScore_partialGoodAnswers() {
// given
List<ChecklistQuestion> checklistQuestions = List.of(
ChecklistQuestionFixture.CHECKLIST1_QUESTION1_BAD(checklist, QuestionFixture.QUESTION1_CATEGORY1),
ChecklistQuestionFixture.CHECKLIST1_QUESTION2_GOOD(checklist, QuestionFixture.QUESTION2_CATEGORY1)
);

// when
QuestionsScoreCalculator calculator = new QuestionsScoreCalculator(checklistQuestions);

// then
assertThat(calculator.calculateScore()).isEqualTo(50);
}

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

// when
QuestionsScoreCalculator calculator = new QuestionsScoreCalculator(checklistQuestions);

// then
assertThat(calculator.calculateScore()).isEqualTo(0);
}

@DisplayName("점수 계산 성공 : 답변이 없는 경우")
@Test
void calculateCategoryScore_noAnswers() {
// given & when
List<ChecklistQuestion> checklistQuestions = new ArrayList<>();

// when
QuestionsScoreCalculator calculator = new QuestionsScoreCalculator(checklistQuestions);

// then
assertThat(calculator.calculateScore()).isEqualTo(null);
}

}

0 comments on commit f0898fa

Please sign in to comment.