Skip to content

Commit

Permalink
fix: 내가 제출한 시험 목록 조회 쿼리에 "내가 제출한"의 필터링 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
alstn113 committed Jan 21, 2025
1 parent ba77f1e commit fdc39dc
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ private List<Long> getMyExamIds(Pageable pageable, Long memberId, ExamStatus sta

@Override
public Page<SubmittedExamSummaryDto> findSubmittedExamSummaries(Pageable pageable, Long memberId) {
JPAQuery<Long> countQuery = queryFactory.select(exam.count())
JPAQuery<Long> countQuery = queryFactory.select(exam.countDistinct())
.from(exam)
.join(submission).on(exam.id.eq(submission.examId))
.where(submission.memberId.eq(memberId));
Expand All @@ -184,7 +184,7 @@ public Page<SubmittedExamSummaryDto> findSubmittedExamSummaries(Pageable pageabl
.from(exam)
.leftJoin(member).on(exam.memberId.eq(member.id))
.join(submission).on(exam.id.eq(submission.examId))
.where(exam.id.in(examIds))
.where(exam.id.in(examIds).and(submission.memberId.eq(memberId)))
.groupBy(
exam.id,
exam.title,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
import com.fluffy.auth.domain.MemberRepository;
import com.fluffy.exam.domain.dto.ExamSummaryDto;
import com.fluffy.exam.domain.dto.MyExamSummaryDto;
import com.fluffy.exam.domain.dto.SubmittedExamSummaryDto;
import com.fluffy.submission.domain.Answer;
import com.fluffy.submission.domain.Submission;
import com.fluffy.submission.domain.SubmissionRepository;
import com.fluffy.support.AbstractIntegrationTest;
import com.fluffy.support.data.MemberTestData;
import java.util.List;
Expand All @@ -24,6 +28,9 @@ class ExamRepositoryTest extends AbstractIntegrationTest {
@Autowired
private MemberRepository memberRepository;

@Autowired
private SubmissionRepository submissionRepository;

@Test
@DisplayName("출제된 시험 요약 목록을 조회할 수 있다.")
void findPublishedExamSummaries() {
Expand Down Expand Up @@ -133,4 +140,76 @@ void findMyExamSummaries() {
.containsExactlyElementsOf(List.of(3L, 1L))
);
}

@Test
@DisplayName("내가 제출한 시험 요약 목록을 조회할 수 있다.")
void findSubmittedExamSummaries() {
// given
Member member1 = MemberTestData.defaultMember().build();
memberRepository.save(member1);

Member member2 = MemberTestData.defaultMember().build();
memberRepository.save(member2);

Exam publishedExam1 = Exam.create("publishedExam1", member1.getId());
publishedExam1.updateQuestions(List.of(Question.shortAnswer("질문1", "지문", "답1")));
publishedExam1.publish();
examRepository.save(publishedExam1);

Exam publishedExam2 = Exam.create("publishedExam2", member1.getId());
publishedExam2.updateQuestions(List.of(Question.shortAnswer("질문1", "지문", "답1")));
publishedExam2.publish();
examRepository.save(publishedExam2);

submissionRepository.save(new Submission(
publishedExam1.getId(),
member1.getId(),
List.of(Answer.textAnswer(1L, "답1"))
));

submissionRepository.save(new Submission(
publishedExam2.getId(),
member1.getId(),
List.of(Answer.textAnswer(1L, "답2"))
));

submissionRepository.save(new Submission(
publishedExam1.getId(),
member2.getId(),
List.of(Answer.textAnswer(1L, "답3"))
));

submissionRepository.save(new Submission(
publishedExam1.getId(),
member1.getId(),
List.of(Answer.textAnswer(1L, "답1"))
));

// when
PageRequest pageable = PageRequest.of(0, 2);
Page<SubmittedExamSummaryDto> submittedExamSummaries = examRepository.findSubmittedExamSummaries(
pageable,
member1.getId()
);

System.out.println(submittedExamSummaries.getContent());
System.out.println(submittedExamSummaries.getTotalElements());
System.out.println(submittedExamSummaries.getTotalPages());
System.out.println(submittedExamSummaries.getNumber());
System.out.println(submittedExamSummaries.getSize());

// then
assertAll(
() -> assertThat(submittedExamSummaries.getTotalElements()).isEqualTo(2),
() -> assertThat(submittedExamSummaries.getTotalPages()).isEqualTo(1),
() -> assertThat(submittedExamSummaries.getContent()
.stream()
.map(SubmittedExamSummaryDto::getSubmissionCount)
).containsExactlyElementsOf(List.of(2L, 1L)),
() -> assertThat(submittedExamSummaries.getContent()
.stream()
.map(SubmittedExamSummaryDto::getTitle)
).containsExactlyElementsOf(List.of("publishedExam1", "publishedExam2"))
);
}
}

0 comments on commit fdc39dc

Please sign in to comment.