Skip to content

Commit bca24c6

Browse files
committed
test 공유 제안 게시판 좋아요 수정
- redis에도 count 저장 - 페이지 조회 시 캐시값을 먼저 조회하여 정합성 증가 - 첫 토글 시 Redis 조회 후 없을 경우 DB 조회하여 캐싱
1 parent 83bb650 commit bca24c6

File tree

1 file changed

+84
-17
lines changed

1 file changed

+84
-17
lines changed

src/test/java/io/crops/warmletter/domain/share/cache/PostLikeRedisManagerTest.java

Lines changed: 84 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
package io.crops.warmletter.domain.share.cache;
2-
32
import org.junit.jupiter.api.BeforeEach;
43
import org.junit.jupiter.api.DisplayName;
54
import org.junit.jupiter.api.Test;
@@ -9,8 +8,8 @@
98
import org.mockito.junit.jupiter.MockitoExtension;
109
import org.springframework.data.redis.core.StringRedisTemplate;
1110
import org.springframework.data.redis.core.ValueOperations;
12-
1311
import java.util.Map;
12+
import java.util.Optional;
1413
import java.util.Set;
1514

1615
import static org.junit.jupiter.api.Assertions.*;
@@ -34,19 +33,21 @@ void setUp() {
3433
}
3534

3635
@Test
37-
@DisplayName("좋아요 토글 - 최초 좋아요")
36+
@DisplayName("좋아요 토글 - 좋아요 추가")
3837
void toggleLike_Success() {
3938
// given
4039
Long postId = 1L;
4140
Long memberId = 1L;
4241
String key = "post:1:like:memberId:1";
43-
when(valueOperations.get(key)).thenReturn(null);
42+
String countKey = "post:1:like:count";
43+
boolean status = true;
4444

4545
// when
46-
postLikeRedisManager.toggleLike(postId, memberId);
46+
postLikeRedisManager.toggleLike(postId, memberId,status);
4747

4848
// then
4949
verify(valueOperations).set(key, "true");
50+
verify(valueOperations).increment(countKey);
5051
}
5152

5253
@Test
@@ -56,13 +57,48 @@ void toggleLike_Cancel() {
5657
Long postId = 1L;
5758
Long memberId = 1L;
5859
String key = "post:1:like:memberId:1";
59-
when(valueOperations.get(key)).thenReturn("true");
60+
String countKey = "post:1:like:count";
61+
boolean status = false;
6062

6163
// when
62-
postLikeRedisManager.toggleLike(postId, memberId);
64+
postLikeRedisManager.toggleLike(postId, memberId,status);
6365

6466
// then
6567
verify(valueOperations).set(key, "false");
68+
verify(valueOperations).decrement(countKey);
69+
}
70+
71+
@Test
72+
@DisplayName("좋아요 상태 조회 - Redis에 데이터 있음")
73+
void getLikedStatus_DataExists() {
74+
// given
75+
Long postId = 1L;
76+
Long memberId = 1L;
77+
String key = "post:1:like:memberId:1";
78+
when(valueOperations.get(key)).thenReturn("true");
79+
80+
// when
81+
Optional<Boolean> result = postLikeRedisManager.getLikedStatus(postId, memberId);
82+
83+
// then
84+
assertTrue(result.isPresent());
85+
assertTrue(result.get());
86+
}
87+
88+
@Test
89+
@DisplayName("좋아요 상태 조회 - Redis에 데이터 없음")
90+
void getLikedStatus_NoData() {
91+
// given
92+
Long postId = 1L;
93+
Long memberId = 1L;
94+
String key = "post:1:like:memberId:1";
95+
when(valueOperations.get(key)).thenReturn(null);
96+
97+
// when
98+
Optional<Boolean> result = postLikeRedisManager.getLikedStatus(postId, memberId);
99+
100+
// then
101+
assertFalse(result.isPresent());
66102
}
67103

68104
@Test
@@ -79,7 +115,7 @@ void getAllLikeStatus() {
79115
assertTrue(likeStatus.get("post:1:like:memberId:1"));
80116
assertFalse(likeStatus.get("post:2:like:memberId:1"));
81117
}
82-
// String value = redisTemplate.opsForValue().get(key);
118+
83119
@Test
84120
@DisplayName("상태 확인 - 좋아요 없음")
85121
void isLiked_False() {
@@ -137,23 +173,54 @@ void isLiked_WithFalseValue() {
137173
assertFalse(result);
138174
verify(valueOperations).get(key);
139175
}
176+
@Test
177+
@DisplayName("좋아요 카운트 조회")
178+
void getLikeCount() {
179+
// given
180+
Long postId = 1L;
181+
String countKey = "post:1:like:count";
182+
when(valueOperations.get(countKey)).thenReturn("5");
183+
184+
// when
185+
int count = postLikeRedisManager.getLikeCount(postId);
186+
187+
// then
188+
assertEquals(5, count);
189+
verify(valueOperations).get(countKey);
190+
}
140191

141192
@Test
142-
@DisplayName("좋아요 토글 - false에서 true로 변경")
143-
void toggleLike_FromFalseToTrue() {
193+
@DisplayName("좋아요 카운트 조회 - 데이터 없음")
194+
void getLikeCount_NoData() {
144195
// given
145196
Long postId = 1L;
146-
Long memberId = 1L;
147-
String key = "post:1:like:memberId:1";
148-
when(valueOperations.get(key)).thenReturn("false");
197+
String countKey = "post:1:like:count";
198+
when(valueOperations.get(countKey)).thenReturn(null);
149199

150200
// when
151-
postLikeRedisManager.toggleLike(postId, memberId);
201+
int count = postLikeRedisManager.getLikeCount(postId);
152202

153203
// then
154-
verify(valueOperations).set(key, "true");
204+
assertEquals(0, count);
205+
verify(valueOperations).get(countKey);
155206
}
156207

208+
// @Test
209+
// @DisplayName("좋아요 토글 - false에서 true로 변경")
210+
// void toggleLike_FromFalseToTrue() {
211+
// // given
212+
// Long postId = 1L;
213+
// Long memberId = 1L;
214+
// String key = "post:1:like:memberId:1";
215+
// when(valueOperations.get(key)).thenReturn("false");
216+
//
217+
// // when
218+
// postLikeRedisManager.toggleLike(postId, memberId,);
219+
//
220+
// // then
221+
// verify(valueOperations).set(key, "true");
222+
// }
223+
157224
@Test
158225
@DisplayName("isLiked - 빈 문자열 값일 경우 false 반환")
159226
void isLiked_WithEmptyString() {
@@ -176,10 +243,9 @@ void isLiked_WithEmptyString() {
176243
void clearCache_WithNullKeys() {
177244
// given
178245
when(redisTemplate.keys("post:*:like:memberId:*")).thenReturn(null);
179-
246+
when(redisTemplate.keys("post:*:like:count")).thenReturn(null);
180247
// when
181248
postLikeRedisManager.clearCache();
182-
183249
// then
184250
verify(redisTemplate, never()).delete(any(Set.class));
185251
}
@@ -207,6 +273,7 @@ void getAllLikeStatus_MixedNullValues() {
207273
void clearCache_WithEmptySet() {
208274
// given
209275
when(redisTemplate.keys("post:*:like:memberId:*")).thenReturn(Set.of());
276+
when(redisTemplate.keys("post:*:like:count")).thenReturn(Set.of());
210277

211278
// when
212279
postLikeRedisManager.clearCache();

0 commit comments

Comments
 (0)