-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[Test/#83] 채팅 테스트 코드 추가
- Loading branch information
Showing
4 changed files
with
512 additions
and
0 deletions.
There are no files selected for viewing
2 changes: 2 additions & 0 deletions
2
src/main/java/corecord/dev/domain/chat/dto/request/ChatRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
src/test/java/corecord/dev/chat/repository/ChatRepositoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package corecord.dev.chat.repository; | ||
|
||
import corecord.dev.domain.chat.entity.Chat; | ||
import corecord.dev.domain.chat.entity.ChatRoom; | ||
import corecord.dev.domain.chat.repository.ChatRepository; | ||
import corecord.dev.domain.chat.repository.ChatRoomRepository; | ||
import corecord.dev.domain.user.entity.User; | ||
import corecord.dev.domain.user.repository.UserRepository; | ||
import jakarta.persistence.EntityManager; | ||
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.jdbc.AutoConfigureTestDatabase; | ||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.List; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
@DataJpaTest | ||
@Transactional | ||
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) | ||
class ChatRepositoryTest { | ||
|
||
@Autowired | ||
ChatRepository chatRepository; | ||
|
||
@Autowired | ||
ChatRoomRepository chatRoomRepository; | ||
|
||
@Autowired | ||
UserRepository userRepository; | ||
|
||
@Autowired | ||
EntityManager entityManager; | ||
|
||
@Test | ||
@DisplayName("채팅방 ID로 채팅 삭제 테스트") | ||
void deleteByChatRoomId() { | ||
// Given | ||
User user = createTestUser(); | ||
ChatRoom chatRoom = createTestChatRoom(user); | ||
|
||
createTestChat(chatRoom, "First message"); | ||
createTestChat(chatRoom, "Second message"); | ||
|
||
List<Chat> chatsBeforeDelete = chatRepository.findByChatRoomOrderByChatId(chatRoom); | ||
assertEquals(2, chatsBeforeDelete.size()); | ||
|
||
// When | ||
chatRepository.deleteByChatRoomId(chatRoom.getChatRoomId()); | ||
entityManager.flush(); | ||
|
||
// Then | ||
List<Chat> chatsAfterDelete = chatRepository.findByChatRoomOrderByChatId(chatRoom); | ||
assertTrue(chatsAfterDelete.isEmpty()); | ||
} | ||
|
||
@Test | ||
@DisplayName("채팅방에 속한 채팅 조회 테스트") | ||
void findByChatRoomOrderByChatId() { | ||
// Given | ||
User user = createTestUser(); | ||
ChatRoom chatRoom = createTestChatRoom(user); | ||
|
||
createTestChat(chatRoom, "First message"); | ||
createTestChat(chatRoom, "Second message"); | ||
createTestChat(chatRoom, "Third message"); | ||
|
||
// When | ||
List<Chat> chats = chatRepository.findByChatRoomOrderByChatId(chatRoom); | ||
|
||
// Then | ||
assertEquals(3, chats.size()); | ||
assertEquals("First message", chats.get(0).getContent()); | ||
assertEquals("Second message", chats.get(1).getContent()); | ||
assertEquals("Third message", chats.get(2).getContent()); | ||
} | ||
|
||
private User createTestUser() { | ||
// 사용자 저장 시 persist 호출 | ||
User user = User.builder() | ||
.providerId("testProvider") | ||
.nickName("TestUser") | ||
.status(corecord.dev.domain.user.entity.Status.UNIVERSITY_STUDENT) | ||
.build(); | ||
userRepository.save(user); | ||
return user; | ||
} | ||
|
||
private ChatRoom createTestChatRoom(User user) { | ||
ChatRoom chatRoom = ChatRoom.builder() | ||
.user(user) | ||
.build(); | ||
chatRoomRepository.save(chatRoom); | ||
return chatRoom; | ||
} | ||
|
||
private void createTestChat(ChatRoom chatRoom, String content) { | ||
Chat chat = Chat.builder() | ||
.author(1) | ||
.content(content) | ||
.chatRoom(chatRoom) | ||
.build(); | ||
chatRepository.save(chat); | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
src/test/java/corecord/dev/chat/repository/ChatRoomRepositoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package corecord.dev.chat.repository; | ||
|
||
import corecord.dev.domain.chat.entity.ChatRoom; | ||
import corecord.dev.domain.chat.repository.ChatRoomRepository; | ||
import corecord.dev.domain.user.entity.Status; | ||
import corecord.dev.domain.user.entity.User; | ||
import corecord.dev.domain.user.repository.UserRepository; | ||
import jakarta.persistence.EntityManager; | ||
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.jdbc.AutoConfigureTestDatabase; | ||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.Optional; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
@DataJpaTest | ||
@Transactional | ||
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) | ||
class ChatRoomRepositoryTest { | ||
|
||
@Autowired | ||
ChatRoomRepository chatRoomRepository; | ||
|
||
@Autowired | ||
UserRepository userRepository; | ||
|
||
@Autowired | ||
EntityManager entityManager; | ||
|
||
@Test | ||
@DisplayName("ChatRoom ID와 User로 채팅방 조회 테스트") | ||
void findByChatRoomIdAndUser() { | ||
// Given | ||
User user = createTestUser(); | ||
ChatRoom chatRoom = createTestChatRoom(user); | ||
|
||
// When | ||
Optional<ChatRoom> foundChatRoom = chatRoomRepository.findByChatRoomIdAndUser(chatRoom.getChatRoomId(), user); | ||
|
||
// Then | ||
assertTrue(foundChatRoom.isPresent()); | ||
assertEquals(foundChatRoom.get().getUser(), user); | ||
} | ||
|
||
@Test | ||
@DisplayName("존재하지 않는 채팅방 조회 테스트") | ||
void findByChatRoomIdAndUser_NotFound() { | ||
// Given | ||
User user = createTestUser(); | ||
|
||
// When | ||
Optional<ChatRoom> foundChatRoom = chatRoomRepository.findByChatRoomIdAndUser(999L, user); | ||
|
||
// Then | ||
assertFalse(foundChatRoom.isPresent()); | ||
} | ||
|
||
private User createTestUser() { | ||
User user = User.builder() | ||
.providerId("testProvider") | ||
.nickName("TestUser") | ||
.status(Status.UNIVERSITY_STUDENT) | ||
.build(); | ||
return userRepository.save(user); | ||
} | ||
|
||
private ChatRoom createTestChatRoom(User user) { | ||
ChatRoom chatRoom = ChatRoom.builder() | ||
.user(user) | ||
.build(); | ||
return chatRoomRepository.save(chatRoom); | ||
} | ||
} |
Oops, something went wrong.