From 428df50b9e7f9039c1492a666857ccd78b0caa6b Mon Sep 17 00:00:00 2001 From: ahnyunki Date: Tue, 16 Apr 2024 17:14:55 +0900 Subject: [PATCH] =?UTF-8?q?test:=20=EC=B5=9C=EA=B7=BC=20=EC=9D=98=EA=B2=AC?= =?UTF-8?q?=EC=9D=B4=20=EB=8B=AC=EB=A6=B0=20=ED=86=A0=EB=A1=A0=EB=B0=A9=20?= =?UTF-8?q?=EC=A1=B0=ED=9A=8C=20API=20=EA=B8=B0=EB=8A=A5=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C=20=EC=9E=91=EC=84=B1=20?= =?UTF-8?q?(#83)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../talkroom/TalkRoomServiceTest.java | 18 +++ .../repository/TalkRoomRepositoryTest.java | 104 ++++++++++++++++++ 2 files changed, 122 insertions(+) diff --git a/src/test/java/com/jisungin/application/talkroom/TalkRoomServiceTest.java b/src/test/java/com/jisungin/application/talkroom/TalkRoomServiceTest.java index 3754388..50e76ef 100644 --- a/src/test/java/com/jisungin/application/talkroom/TalkRoomServiceTest.java +++ b/src/test/java/com/jisungin/application/talkroom/TalkRoomServiceTest.java @@ -32,6 +32,7 @@ import java.util.ArrayList; import java.util.List; import java.util.stream.IntStream; +import org.assertj.core.api.Assertions; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -914,6 +915,23 @@ void createTalkRoomWithImage() throws Exception { } + @Test + @DisplayName("최근 의견이 달린 토론방을 조회 할때 의견이 없다면 빈 값을 보낸다.") + void fetchRecentCommentedDiscussionsWithCommentEmpty() throws Exception { + // given + SearchServiceRequest request = SearchServiceRequest.builder() + .size(3) + .page(1) + .order("recent_comment") + .build(); + + // when + TalkRoomPageResponse response = talkRoomService.findAllTalkRoom(request, null); + + // then + Assertions.assertThat(response.getResponse().getQueryResponse()).isEmpty(); + } + private static List listUsers() { return IntStream.range(0, 10) .mapToObj(i -> User.builder() diff --git a/src/test/java/com/jisungin/domain/talkroom/repository/TalkRoomRepositoryTest.java b/src/test/java/com/jisungin/domain/talkroom/repository/TalkRoomRepositoryTest.java index bbf4f5b..893fec2 100644 --- a/src/test/java/com/jisungin/domain/talkroom/repository/TalkRoomRepositoryTest.java +++ b/src/test/java/com/jisungin/domain/talkroom/repository/TalkRoomRepositoryTest.java @@ -521,6 +521,110 @@ public void getTalkRoomsRelatedBookTotalSize() { assertThat(anotherTotalCount).isEqualTo(10L); } + @Test + @DisplayName("최근 의견이 달린 토론방을 가져온다.") + void fetchRecentCommentedDiscussions() throws Exception { + // given + User user = createUser(); + userRepository.save(user); + + Book book = createBook(); + bookRepository.save(book); + + List talkRooms = IntStream.range(1, 11) + .mapToObj(i -> TalkRoom.builder() + .user(user) + .book(book) + .title("토론방" + i) + .content("내용" + i) + .build()) + .toList(); + talkRoomRepository.saveAll(talkRooms); + + List comments = IntStream.range(3, 6) + .mapToObj(i -> Comment.builder() + .user(user) + .talkRoom(talkRooms.get(i)) + .content("의견") + .build()) + .toList(); + commentRepository.saveAll(comments); + + SearchServiceRequest request = SearchServiceRequest.builder() + .size(3) + .page(1) + .order("recent-comment") + .build(); + + // when + List response = talkRoomRepository.findAllTalkRoom(request.getOffset(), + request.getSize(), request.getOrder(), request.getQuery()); + + // then + assertThat("토론방6").isEqualTo(response.get(0).getTitle()); + assertThat("토론방5").isEqualTo(response.get(1).getTitle()); + assertThat("토론방4").isEqualTo(response.get(2).getTitle()); + } + + @Test + @DisplayName("최근 의견이 달린 토론방을 가져온다.") + void fetchRecentCommentedDiscussions2() throws Exception { + // given + User user = createUser(); + userRepository.save(user); + + Book book = createBook(); + bookRepository.save(book); + + List talkRooms = IntStream.range(1, 11) + .mapToObj(i -> TalkRoom.builder() + .user(user) + .book(book) + .title("토론방" + i) + .content("내용" + i) + .build()) + .toList(); + talkRoomRepository.saveAll(talkRooms); + + Comment comment1 = Comment.builder() + .content("의견") + .user(user) + .talkRoom(talkRooms.get(3)) + .build(); + Comment comment2 = Comment.builder() + .content("의견") + .user(user) + .talkRoom(talkRooms.get(4)) + .build(); + Comment comment3 = Comment.builder() + .content("의견") + .user(user) + .talkRoom(talkRooms.get(5)) + .build(); + Comment comment4 = Comment.builder() + .content("의견 추가") + .user(user) + .talkRoom(talkRooms.get(5)) + .build(); + + commentRepository.saveAll(List.of(comment1, comment2, comment3, comment4)); + + SearchServiceRequest request = SearchServiceRequest.builder() + .size(3) + .page(1) + .order("recent-comment") + .build(); + + // when + List response = talkRoomRepository.findAllTalkRoom(request.getOffset(), + request.getSize(), request.getOrder(), request.getQuery()); + + // then + assertThat("토론방6").isEqualTo(response.get(0).getTitle()); + assertThat("토론방5").isEqualTo(response.get(1).getTitle()); + assertThat("토론방4").isEqualTo(response.get(2).getTitle()); + } + private static Comment createComment(TalkRoom talkRoom, User user) { return Comment.builder() .talkRoom(talkRoom)