Open berryberrybin opened 1 year ago
@Test
void 댓글_삭제_성공() {
//given
Board board = new Board("testBoard");
User user = userRepository.save(new User("testUserNickname", "email", User.Role.USER));
Post post = postRepository.save(new Post(board, user, "testPostTitle", "testPostContent"));
Comment commentA = commentRepository.save(new Comment(user, post, "testCommentContentA", null));
Comment commentB = commentRepository.save(new Comment(user, post, "testCommentContentB", null));
CommentCommand.DeleteCommentCommand command = CommentCommand.DeleteCommentCommand.builder()
.userId(user.getId())
.postId(post.getId())
.commentId(commentA.getId()).build();
//when
commentCommandService.deleteComment(command);
Optional<Comment> deletedComment = commentRepository.findById(commentA.getId());
Optional<Comment> notDeletedComment = commentRepository.findById(commentB.getId());
//then
assertThat(deletedComment.isPresent()).isEqualTo(false);
assertThat(notDeletedComment.isPresent()).isEqualTo(true);
}
@Test
@DisplayName("Shop soft delete")
public void delete() {
Shop shop = new Shop("가게이름", "대구광역시");
Shop savedShop = shopRepository.save(shop);
assertThat(savedShop.getId()).isNotNull();
assertThat(savedShop.isDeleted()).isFalse();
shopRepository.delete(savedShop);
entityManager.flush();
Optional<Shop> afterDelete = shopRepository.findById(savedShop.getId());
assertThat(afterDelete).isNotEmpty();
assertThat(afterDelete.get().isDeleted()).isTrue();
}
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn
@SQLDelete(sql = "UPDATE shop SET deleted = true WHERE id = ?")
public abstract class Shop {
}
@Entity
@DiscriminatorValue("C")
@OnDelete(action = OnDeleteAction.CASCADE)
public class Restaurant extends Shop {
}
EntityManager (Persistence Context)
persist, flush, clear
@SpringBootTest
@Transactional
class CommentCommandServiceIntegrationTest {
@Autowired
private CommentCommandService commentCommandService;
@Autowired
private CommentRepository commentRepository;
@Autowired
private PostRepository postRepository;
@Autowired
private UserRepository userRepository;
@PersistenceContext
EntityManager entityManager;
@Test
void 댓글_삭제_성공() {
//given
Board board = new Board("testBoard");
User user = userRepository.save(new User("testUserNickname", "email", User.Role.USER));
Post post = postRepository.save(new Post(board, user, "testPostTitle", "testPostContent"));
Comment commentA = commentRepository.save(new Comment(user, post, "testCommentContentA", null));
Comment commentB = commentRepository.save(new Comment(user, post, "testCommentContentB", null));
entityManager.persist(board); // 영속성 컨텍스트에 저장
entityManager.persist(user);
entityManager.persist(post);
entityManager.persist(commentA);
entityManager.persist(commentB);
CommentCommand.DeleteCommentCommand command = CommentCommand.DeleteCommentCommand.builder()
.userId(user.getId())
.postId(post.getId())
.commentId(commentA.getId()).build();
//when
commentCommandService.deleteComment(command);
entityManager.flush(); // 영속성 컨텍스트에 있는 데이터를 DB로 쿼리 전송
entityManager.clear(); // 영속성 컨텍스트에 있는 데이터를 제거
Optional<Comment> deletedComment = commentRepository.findById(commentA.getId());
Optional<Comment> notDeletedComment = commentRepository.findById(commentB.getId());
//then
assertThat(deletedComment.get().getIsDeleted()).isEqualTo(true);
assertThat(notDeletedComment.isPresent()).isEqualTo(true);
}
}
soft delete 구현
@SQLDelete(sql = "UPDATE comments SET deleted = true WHERE id = ?")
이용@Where(clause = "deleted = false")
@Where(clause = "deleted = false")
= deleted가 false인것만 조회 = 삭제된것은 제외하고 조회