From 8530e2683c79b85430c650d77251928296a679ae Mon Sep 17 00:00:00 2001 From: mcodnjs Date: Thu, 1 Feb 2024 02:54:40 +0900 Subject: [PATCH] =?UTF-8?q?test:=20LikeService=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/LikeServiceIntegrationTest.java | 46 +++++++++++++------ 1 file changed, 32 insertions(+), 14 deletions(-) diff --git a/backend/src/test/java/hanglog/integration/service/LikeServiceIntegrationTest.java b/backend/src/test/java/hanglog/integration/service/LikeServiceIntegrationTest.java index e410a9ac5..815b702ac 100644 --- a/backend/src/test/java/hanglog/integration/service/LikeServiceIntegrationTest.java +++ b/backend/src/test/java/hanglog/integration/service/LikeServiceIntegrationTest.java @@ -6,6 +6,8 @@ import static java.lang.Boolean.TRUE; import static org.assertj.core.api.SoftAssertions.assertSoftly; +import hanglog.like.domain.Likes; +import hanglog.like.domain.repository.LikeRepository; import hanglog.like.dto.request.LikeRequest; import hanglog.like.service.LikeService; import hanglog.trip.service.TripService; @@ -27,11 +29,20 @@ class LikeServiceIntegrationTest extends RedisServiceIntegrationTest { @Autowired private RedisTemplate redisTemplate; + @Autowired + private LikeRepository likeRepository; + private SetOperations opsForSet; + private Long memberId; + private Long tripId; + private String key; @BeforeEach void setUp() { opsForSet = redisTemplate.opsForSet(); + memberId = member.getId(); + tripId = tripService.save(memberId, TRIP_CREATE_REQUEST); + key = generateLikeKey(tripId); } @DisplayName("해당 게시물의 좋아요 여부를 변경할 수 있다.") @@ -41,10 +52,6 @@ void update() { final LikeRequest likeTrueRequest = new LikeRequest(true); final LikeRequest likeFalseRequest = new LikeRequest(false); - final Long memberId = member.getId(); - final Long tripId = tripService.save(memberId, TRIP_CREATE_REQUEST); - final String key = generateLikeKey(tripId); - // when & then assertSoftly(softly -> { softly.assertThat(opsForSet.isMember(key, memberId)).isEqualTo(FALSE); @@ -63,13 +70,10 @@ void update_TrueRequestWhenIsMember() { // given final LikeRequest likeTrueRequest = new LikeRequest(true); - final Long memberId = member.getId(); - final Long tripId = tripService.save(memberId, TRIP_CREATE_REQUEST); - final String key = generateLikeKey(tripId); - + // when likeService.update(memberId, tripId, likeTrueRequest); - // when & then + // then assertSoftly(softly -> { softly.assertThat(opsForSet.isMember(key, memberId)).isEqualTo(TRUE); likeService.update(memberId, tripId, likeTrueRequest); @@ -83,17 +87,31 @@ void update_FalseRequestWhenIsNotMember() { // given final LikeRequest likeFalseRequest = new LikeRequest(false); - final Long memberId = member.getId(); - final Long tripId = tripService.save(memberId, TRIP_CREATE_REQUEST); - final String key = generateLikeKey(tripId); - + // when likeService.update(memberId, tripId, likeFalseRequest); - // when & then + // then assertSoftly(softly -> { softly.assertThat(opsForSet.isMember(key, memberId)).isEqualTo(FALSE); likeService.update(memberId, tripId, likeFalseRequest); softly.assertThat(opsForSet.isMember(key, memberId)).isEqualTo(FALSE); }); } + + @DisplayName("TripId 키가 Redis에 존재하지 않을 경우 DB에 조회해서 업데이트한다.") + @Test + void update_WhenTripIdIsNotExistInRedis() { + // given + final LikeRequest likeTrueRequest = new LikeRequest(true); + + final Likes likes = new Likes(tripId, memberId); + likeRepository.save(likes); + + // when & then + assertSoftly(softly -> { + softly.assertThat(opsForSet.isMember(key, memberId)).isEqualTo(FALSE); + likeService.update(memberId, tripId, likeTrueRequest); + softly.assertThat(opsForSet.isMember(key, memberId)).isEqualTo(TRUE); + }); + } }