Skip to content

Commit

Permalink
[feat] native query count가 제대로 동작하는지 확인(#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
blaxsior committed Aug 9, 2024
1 parent e9955a4 commit 1e6bf5c
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package hyundai.softeer.orange.comment.dto;

public interface WriteCommentCountDto {
Long getEventUserId();
Long getCount();
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package hyundai.softeer.orange.comment.repository;

import hyundai.softeer.orange.comment.dto.WriteCommentCountDto;
import hyundai.softeer.orange.comment.entity.Comment;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
Expand All @@ -26,4 +27,13 @@ public interface CommentRepository extends JpaRepository<Comment, Long> {
countProjection = "c.id", // 어떤 값으로 count 셀건지 지정. 지정 안하면 count(c.*)가 되어 문제 발생.
nativeQuery = true)
Page<Comment> findAllByEventId(@Param("eventId") String eventId, Pageable pageable);

// 이름 매핑 필요. 필드 이름과 직접 매핑.
@Query(value = "SELECT c.event_user_id as eventUserId, COUNT(c.event_user_id) as count " +
"FROM comment c " +
"JOIN event_frame ef ON c.event_frame_id = ef.id " +
"JOIN event_metadata e ON ef.id = e.event_frame_id " +
"WHERE e.id = :eventRawId " +
"GROUP BY c.event_user_id " , nativeQuery = true)
List<WriteCommentCountDto> countPerEventUserByEventId(@Param("eventRawId") Long eventRawId);
}
26 changes: 26 additions & 0 deletions src/main/resources/sql/CommentRepositoryTest.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
INSERT INTO event_frame(name) VALUES ('test1');
INSERT INTO event_frame(name) VALUES ('test2');

INSERT INTO event_metadata(event_type, event_frame_id, event_id) VALUES (1, 1, 'HD_240808_001');
INSERT INTO event_metadata(event_type, event_frame_id, event_id) VALUES (0, 2, 'HD_240808_002');

INSERT INTO event_user(score, event_frame_id, user_id) VALUES (0, 1, 'user1');
INSERT INTO event_user(score, event_frame_id, user_id) VALUES (0, 1, 'user2');
INSERT INTO event_user(score, event_frame_id, user_id) VALUES (0, 1, 'user3');

-- 3 comments for user1
INSERT INTO comment(event_frame_id, event_user_id) VALUES (1, 1);
INSERT INTO comment(event_frame_id, event_user_id) VALUES (1, 1);
INSERT INTO comment(event_frame_id, event_user_id) VALUES (1, 1);

-- 6 comments for user2
INSERT INTO comment(event_frame_id, event_user_id) VALUES (1, 2);
INSERT INTO comment(event_frame_id, event_user_id) VALUES (1, 2);
INSERT INTO comment(event_frame_id, event_user_id) VALUES (1, 2);
INSERT INTO comment(event_frame_id, event_user_id) VALUES (1, 2);
INSERT INTO comment(event_frame_id, event_user_id) VALUES (1, 2);
INSERT INTO comment(event_frame_id, event_user_id) VALUES (1, 2);

-- 2 comments for user3
INSERT INTO comment(event_frame_id, event_user_id) VALUES (1, 3);
INSERT INTO comment(event_frame_id, event_user_id) VALUES (1, 3);
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package hyundai.softeer.orange.comment.repository;

import hyundai.softeer.orange.comment.dto.WriteCommentCountDto;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.jdbc.Sql;

import java.util.List;

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

// 매 테스트마다 초기화하는 코드 찾아봐야 할듯?
@Sql(value = "classpath:sql/CommentRepositoryTest.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_CLASS)
@DataJpaTest(showSql = false)
@TestPropertySource(locations = "classpath:application-test.yml")
class CommentRepositoryTest {
@Autowired
JdbcTemplate jdbcTemplate;

@Autowired
CommentRepository commentRepository;


@DisplayName("존재하는 대상 이벤트에 대해 작성된 댓글이 있다면 유저 별로 개수를 구해 반환")
@Test
void getCountOfCommentPerUserIfCommentExist() {
List<WriteCommentCountDto> counts = commentRepository.countPerEventUserByEventId(1L);

assertThat(counts).hasSize(3);
assertThat(counts.get(0).getCount()).isEqualTo(3);
assertThat(counts.get(1).getCount()).isEqualTo(6);
assertThat(counts.get(2).getCount()).isEqualTo(2);
}

@DisplayName("존재하지 않는 대상 이벤트는 빈 배열 반환")
@Test
void getCountOfCommentPerUserIfEventNotExist() {
List<WriteCommentCountDto> counts = commentRepository.countPerEventUserByEventId(3L);
assertThat(counts).hasSize(0);
}

@DisplayName("존재해도 댓글 없으면 빈 배열 반환")
@Test
void getCountOfCommentPerUserIfEventExistButNoComment() {
List<WriteCommentCountDto> counts = commentRepository.countPerEventUserByEventId(2L);
assertThat(counts).hasSize(0);
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
package hyundai.softeer.orange.event.draw.component.picker;

import lombok.extern.slf4j.Slf4j;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;

@Slf4j
class AccSumBasedWinnerPickerTest {
Expand Down

0 comments on commit 1e6bf5c

Please sign in to comment.