Skip to content

Commit 0d7a4e0

Browse files
claude[bot]github-actions[bot]claude
authored
Maintenance: Quiz Service - Improve code consistency and test coverage (#115)
Co-authored-by: claude[bot] <41898282+claude[bot]@users.noreply.github.com> Co-authored-by: Claude <[email protected]>
1 parent 5039ec6 commit 0d7a4e0

File tree

2 files changed

+77
-2
lines changed

2 files changed

+77
-2
lines changed

src/main/java/spring/memewikibe/application/QuizService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515
@RequiredArgsConstructor
1616
@Transactional(readOnly = true)
1717
public class QuizService {
18-
private static final int quizLimit = 10;
18+
private static final int QUIZ_LIMIT = 10;
1919

2020
private final MemeQuizRepository quizRepository;
2121

2222
public List<QuizProblemResponse> getRandomQuizzes() {
23-
return quizRepository.findRandomQuizzes(quizLimit).stream()
23+
return quizRepository.findRandomQuizzes(QUIZ_LIMIT).stream()
2424
.map(this::toProblem)
2525
.toList();
2626
}

src/test/java/spring/memewikibe/application/QuizServiceTest.java

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,81 @@ void tearDown() {
4848
then(result).hasSize(10);
4949
result.forEach(q -> then(q.questions()).hasSize(4));
5050
}
51+
52+
@Test
53+
void 퀴즈가_10개_미만일_때_존재하는_모든_퀴즈를_반환한다() {
54+
List<MemeQuiz> quizzes = IntStream.rangeClosed(1, 5)
55+
.mapToObj(i -> MemeQuiz.builder()
56+
.question("문제 " + i)
57+
.option1("보기1")
58+
.option2("보기2")
59+
.option3("보기3")
60+
.option4("보기4")
61+
.answer(2)
62+
.imageUrl("https://example.com/" + i + ".jpg")
63+
.build())
64+
.toList();
65+
quizRepository.saveAll(quizzes);
66+
67+
List<QuizProblemResponse> result = quizService.getRandomQuizzes();
68+
69+
then(result).hasSize(5);
70+
result.forEach(q -> then(q.questions()).hasSize(4));
71+
}
72+
73+
@Test
74+
void 퀴즈가_정확히_10개일_때_모든_퀴즈를_반환한다() {
75+
List<MemeQuiz> quizzes = IntStream.rangeClosed(1, 10)
76+
.mapToObj(i -> MemeQuiz.builder()
77+
.question("문제 " + i)
78+
.option1("보기1")
79+
.option2("보기2")
80+
.option3("보기3")
81+
.option4("보기4")
82+
.answer(3)
83+
.imageUrl("https://example.com/" + i + ".jpg")
84+
.build())
85+
.toList();
86+
quizRepository.saveAll(quizzes);
87+
88+
List<QuizProblemResponse> result = quizService.getRandomQuizzes();
89+
90+
then(result).hasSize(10);
91+
result.forEach(q -> then(q.questions()).hasSize(4));
92+
}
93+
94+
@Test
95+
void 퀴즈가_없을_때_빈_리스트를_반환한다() {
96+
List<QuizProblemResponse> result = quizService.getRandomQuizzes();
97+
98+
then(result).isEmpty();
99+
}
100+
101+
@Test
102+
void 각_퀴즈의_정답이_올바르게_매핑된다() {
103+
MemeQuiz quiz = MemeQuiz.builder()
104+
.question("정답이 3번인 문제")
105+
.option1("오답1")
106+
.option2("오답2")
107+
.option3("정답")
108+
.option4("오답3")
109+
.answer(3)
110+
.imageUrl("https://example.com/test.jpg")
111+
.build();
112+
quizRepository.save(quiz);
113+
114+
List<QuizProblemResponse> result = quizService.getRandomQuizzes();
115+
116+
then(result).hasSize(1);
117+
QuizProblemResponse response = result.get(0);
118+
then(response.question()).isEqualTo("정답이 3번인 문제");
119+
then(response.image()).isEqualTo("https://example.com/test.jpg");
120+
then(response.questions()).hasSize(4);
121+
then(response.questions().get(0).isRight()).isFalse();
122+
then(response.questions().get(1).isRight()).isFalse();
123+
then(response.questions().get(2).isRight()).isTrue();
124+
then(response.questions().get(3).isRight()).isFalse();
125+
}
51126
}
52127

53128

0 commit comments

Comments
 (0)