Skip to content

Commit

Permalink
add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
scglwsj committed Oct 1, 2024
1 parent 6b917ef commit 765443e
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions src/test/java/com/quemistry/quiz_ms/service/QuizServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,59 @@ void createQuizWithLimitedTotalSize() {
verify(attemptRepository, times(2)).save(argThat(attempt -> attempt.getQuizId().equals(1L)));
}

@Test
void createQuizWithoutMcqs() {
QuizRequest quizRequest =
QuizRequest.builder()
.topics(List.of(5L, 6L))
.skills(List.of(7L, 8L))
.pageSize(1)
.totalSize(10L)
.build();

RetrieveMCQResponse retrieveMCQResponse =
RetrieveMCQResponse.builder().mcqs(List.of()).totalPages(1).totalRecords(0L).build();

when(quizRepository.findOneByStudentIdAndStatus("student1", IN_PROGRESS))
.thenReturn(Optional.empty());
when(questionClient.retrieveMCQs(
argThat(
request ->
request.getExcludeIds().contains(3L)
&& request.getTopics().containsAll(List.of(5L, 6L))
&& request.getSkills().containsAll(List.of(7L, 8L))),
any(),
any(),
any()))
.thenReturn(retrieveMCQResponse);
when(quizRepository.save(any(Quiz.class)))
.thenAnswer(
invocation -> {
Quiz quiz = invocation.getArgument(0);
if (quiz.getId() == null) {
quiz.setId(1L); // Simulate the generation of an ID
}
return quiz;
});
when(quizRepository.findAllByStudentId("student1"))
.thenReturn(List.of(Quiz.builder().id(2L).build()));
when(attemptRepository.findAllByQuizIdIn(List.of(2L)))
.thenReturn(
List.of(
QuizAttempt.builder().quizId(2L).mcqId(3L).optionNo(1).isCorrect(true).build(),
QuizAttempt.builder().quizId(2L).mcqId(4L).optionNo(1).isCorrect(false).build()));

QuizResponse response = quizService.createQuiz(testUserContext, quizRequest);

assertEquals(1, response.getMcqs().getSize());
assertEquals(0, response.getMcqs().getContent().size());
assertEquals(0, response.getMcqs().getNumber());
assertEquals(0, response.getMcqs().getTotalPages());
assertEquals(0L, response.getMcqs().getTotalElements());

assertEquals(COMPLETED, response.getStatus());
}

@Test
void createQuizFailedWithExistingInProgressQuiz() {
QuizRequest quizRequest =
Expand Down Expand Up @@ -607,4 +660,12 @@ private MCQDto generateMCQDto(Long id, String stem) {
.status("PUBLISHED")
.build();
}

@Test
void getMCQByQuestionClient() {
when(questionClient.retrieveMCQsByIds(any(), any(), any(), any()))
.thenReturn(getRetrieveMCQResponse());
RetrieveMCQResponse response = quizService.getMCQByQuestionClient(1L, testUserContext);
assertEquals(1, response.getMcqs().size());
}
}

0 comments on commit 765443e

Please sign in to comment.