Skip to content

Commit

Permalink
feat: ChatRoomEntity에 targetId와 type으로 고유키 설정
Browse files Browse the repository at this point in the history
  • Loading branch information
ay-eonii committed Oct 8, 2024
1 parent ba49616 commit 102028e
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,26 @@
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;
import lombok.NoArgsConstructor;
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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 102028e

Please sign in to comment.