From 102028efa5580b4a175248285111e070a03bdfa1 Mon Sep 17 00:00:00 2001 From: ay-eonii Date: Tue, 8 Oct 2024 22:18:54 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20ChatRoomEntity=EC=97=90=20targetId?= =?UTF-8?q?=EC=99=80=20type=EC=9C=BC=EB=A1=9C=20=EA=B3=A0=EC=9C=A0?= =?UTF-8?q?=ED=82=A4=20=EC=84=A4=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../backend/chat/entity/ChatRoomEntity.java | 11 +++- .../chat/entity/ChatRoomEntityTest.java | 55 +++++++++++++++++++ 2 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 backend/src/test/java/mouda/backend/chat/entity/ChatRoomEntityTest.java diff --git a/backend/src/main/java/mouda/backend/chat/entity/ChatRoomEntity.java b/backend/src/main/java/mouda/backend/chat/entity/ChatRoomEntity.java index fe1a85611..413a4800d 100644 --- a/backend/src/main/java/mouda/backend/chat/entity/ChatRoomEntity.java +++ b/backend/src/main/java/mouda/backend/chat/entity/ChatRoomEntity.java @@ -5,6 +5,7 @@ import jakarta.persistence.GenerationType; import jakarta.persistence.Id; import jakarta.persistence.Table; +import jakarta.persistence.UniqueConstraint; import lombok.AccessLevel; import lombok.Builder; import lombok.Getter; @@ -12,10 +13,18 @@ import mouda.backend.chat.domain.ChatRoomType; @Entity -@Table(name = "chat_room") @Getter @NoArgsConstructor(access = AccessLevel.PROTECTED) +@Table( + name = "chat_room", + uniqueConstraints = { + @UniqueConstraint( + columnNames = {"target_id", "type"} + ) + } +) public class ChatRoomEntity { + @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; diff --git a/backend/src/test/java/mouda/backend/chat/entity/ChatRoomEntityTest.java b/backend/src/test/java/mouda/backend/chat/entity/ChatRoomEntityTest.java new file mode 100644 index 000000000..a3f5cd765 --- /dev/null +++ b/backend/src/test/java/mouda/backend/chat/entity/ChatRoomEntityTest.java @@ -0,0 +1,55 @@ +package mouda.backend.chat.entity; + +import static org.assertj.core.api.Assertions.*; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.dao.DataIntegrityViolationException; + +import mouda.backend.chat.infrastructure.ChatRoomRepository; +import mouda.backend.common.fixture.ChatRoomEntityFixture; + +@SpringBootTest +class ChatRoomEntityTest { + + @Autowired + ChatRoomRepository chatRoomRepository; + + @DisplayName("targetId와 type이 같은 ChatRoomEntity는 저장할 수 없다.") + @Test + void failToCreateDuplicatedUnique() { + ChatRoomEntity chatRoom = ChatRoomEntityFixture.getChatRoomEntityOfBet(1L, 1L); + chatRoomRepository.save(chatRoom); + + ChatRoomEntity duplicated = ChatRoomEntityFixture.getChatRoomEntityOfBet(1L, 2L); + + assertThatThrownBy(() -> chatRoomRepository.save(duplicated)) + .isInstanceOf(DataIntegrityViolationException.class); + } + + @DisplayName("targetId이 같아도 type이 다르다면 ChatRoomEntity는 저장할 수 있다.") + @Test + void createWithDifferentType() { + ChatRoomEntity chatRoom1 = ChatRoomEntityFixture.getChatRoomEntityOfBet(1L, 1L); + chatRoomRepository.save(chatRoom1); + + ChatRoomEntity chatRoom2 = ChatRoomEntityFixture.getChatRoomEntityOfMoim(1L, 2L); + chatRoomRepository.save(chatRoom2); + + assertThat(chatRoomRepository.findAll()).hasSize(2); + } + + @DisplayName("type이 같아도 targetId이 다르다면 ChatRoomEntity는 저장할 수 있다.") + @Test + void createWithDifferentTargetId() { + ChatRoomEntity chatRoom1 = ChatRoomEntityFixture.getChatRoomEntityOfBet(1L, 1L); + chatRoomRepository.save(chatRoom1); + + ChatRoomEntity chatRoom2 = ChatRoomEntityFixture.getChatRoomEntityOfBet(2L, 2L); + chatRoomRepository.save(chatRoom2); + + assertThat(chatRoomRepository.findAll()).hasSize(2); + } +}