Skip to content

Commit

Permalink
refactor: 쿼리 수정, 테스트 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
donghoony committed Nov 16, 2024
1 parent fb27b4c commit 7a33c4d
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ public interface HighlightRepository extends JpaRepository<Highlight, Long> {

@Modifying
@Query("""
DELETE FROM Highlight highlight WHERE highlight in (
SELECT h FROM Highlight h
JOIN Answer a ON h.answerId = a.id
DELETE FROM Highlight h
WHERE h.answerId IN (
SELECT a.id FROM Answer a
JOIN Review r ON a.reviewId = r.id
WHERE r.reviewGroupId = :reviewGroupId AND a.questionId = :questionId
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,34 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertAll;
import static reviewme.fixture.ReviewGroupFixture.리뷰_그룹;

import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import reviewme.highlight.domain.Highlight;
import reviewme.highlight.domain.HighlightRange;
import reviewme.review.domain.Answer;
import reviewme.review.domain.Review;
import reviewme.review.domain.TextAnswer;
import reviewme.review.repository.ReviewRepository;
import reviewme.reviewgroup.domain.ReviewGroup;
import reviewme.reviewgroup.repository.ReviewGroupRepository;

@DataJpaTest
class HighlightRepositoryTest {

@Autowired
private HighlightRepository highlightRepository;

@Autowired
private ReviewRepository reviewRepository;

@Autowired
private ReviewGroupRepository reviewGroupRepository;

@Test
void 하이라이트를_줄번호_시작_인덱스_순서대로_정렬해서_가져온다() {
// given
Expand Down Expand Up @@ -44,4 +58,43 @@ class HighlightRepositoryTest {
.containsExactly(1, 4, 2, 6, 3)
);
}

@Test
void 그룹_아이디와_질문_아이디로_하이라이트를_삭제한다() {
// given
ReviewGroup reviewGroup1 = reviewGroupRepository.save(리뷰_그룹());
ReviewGroup reviewGroup2 = reviewGroupRepository.save(리뷰_그룹());

List<Answer> answers1 = List.of(
new TextAnswer(1L, "A1"),
new TextAnswer(2L, "A2"),
new TextAnswer(3L, "A3")
);
List<Answer> answers2 = List.of(
new TextAnswer(1L, "B1"),
new TextAnswer(2L, "B2"),
new TextAnswer(3L, "B3")
);
reviewRepository.save(new Review(1L, reviewGroup1.getId(), answers1));
reviewRepository.save(new Review(2L, reviewGroup2.getId(), answers2));

List<Long> answerIds = new ArrayList<>();
answerIds.addAll(answers1.stream().map(Answer::getId).toList());
answerIds.addAll(answers2.stream().map(Answer::getId).toList());

HighlightRange range = new HighlightRange(0, 1);
answerIds.stream()
.map(answerId -> new Highlight(answerId, 0, range))
.forEach(highlightRepository::save);

// when
highlightRepository.deleteByReviewGroupIdAndQuestionId(reviewGroup1.getId(), 1L);

// then
List<Highlight> actual = highlightRepository.findAllByAnswerIdsOrderedAsc(answerIds);
assertAll(
() -> assertThat(actual).hasSize(5),
() -> assertThat(actual).extracting(Highlight::getAnswerId).doesNotContain(answers1.get(0).getId())
);
}
}

0 comments on commit 7a33c4d

Please sign in to comment.