Skip to content

Commit 4a4746b

Browse files
committed
test : postService 테스트 코드 구현현
1 parent efaf34b commit 4a4746b

File tree

1 file changed

+259
-0
lines changed

1 file changed

+259
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,259 @@
1+
import { PostService } from '@/services/post.service';
2+
import { PostRepository } from '@/repositories/post.repository';
3+
import { DBError } from '@/exception';
4+
5+
jest.mock('@/repositories/post.repository');
6+
7+
// 모든 파라미터는 Route 단에서 검증하기 때문에 파라미터를 제대로 받았는지는 확인하지 않음
8+
describe('PostService', () => {
9+
let postService: PostService;
10+
let postRepo: jest.Mocked<PostRepository>;
11+
let mockPool: any;
12+
13+
beforeEach(() => {
14+
mockPool = {};
15+
16+
postRepo = new PostRepository(mockPool) as jest.Mocked<PostRepository>;
17+
postService = new PostService(postRepo);
18+
});
19+
20+
describe('getAllposts', () => {
21+
it('게시물 목록 조회', async () => {
22+
const mockPosts = {
23+
nextCursor: '2023-11-19T09:19:36.811Z,519212',
24+
posts: [
25+
{
26+
id: '519211',
27+
title: 'velog dashboard test post (2)',
28+
slug: 'velog-dashboard-test-post-2',
29+
daily_view_count: 147,
30+
daily_like_count: 2,
31+
yesterday_daily_view_count: 147,
32+
yesterday_daily_like_count: 2,
33+
post_created_at: '2025-02-08T02:58:24.347Z',
34+
post_released_at: '2023-11-20T02:15:14.209Z',
35+
},
36+
{
37+
id: '519212',
38+
title: 'velog dashboard test post (1)',
39+
slug: 'velog-dashboard-test-post-1',
40+
daily_view_count: 208,
41+
daily_like_count: 1,
42+
yesterday_daily_view_count: 208,
43+
yesterday_daily_like_count: 1,
44+
post_created_at: '2025-02-08T02:58:24.347Z',
45+
post_released_at: '2023-11-19T09:19:36.811Z',
46+
},
47+
],
48+
};
49+
50+
postRepo.findPostsByUserId.mockResolvedValue(mockPosts);
51+
52+
const result = await postService.getAllposts(1);
53+
console.log(result);
54+
expect(result.posts).toEqual([
55+
{
56+
id: '519211',
57+
title: 'velog dashboard test post (2)',
58+
slug: 'velog-dashboard-test-post-2',
59+
views: 147,
60+
likes: 2,
61+
yesterdayViews: 147,
62+
yesterdayLikes: 2,
63+
createdAt: '2025-02-08T02:58:24.347Z',
64+
releasedAt: '2023-11-20T02:15:14.209Z',
65+
},
66+
{
67+
id: '519212',
68+
title: 'velog dashboard test post (1)',
69+
slug: 'velog-dashboard-test-post-1',
70+
views: 208,
71+
likes: 1,
72+
yesterdayViews: 208,
73+
yesterdayLikes: 1,
74+
createdAt: '2025-02-08T02:58:24.347Z',
75+
releasedAt: '2023-11-19T09:19:36.811Z',
76+
},
77+
]);
78+
expect(result.nextCursor).toBe('2023-11-19T09:19:36.811Z,519212');
79+
});
80+
81+
it('쿼리 중 오류 발생 시 DBError Throw', async () => {
82+
const errorMessage = '전체 post 조회 중 문제가 발생했습니다.';
83+
postRepo.findPostsByUserId.mockRejectedValue(new DBError(errorMessage));
84+
85+
await expect(postService.getAllposts(1)).rejects.toThrow(errorMessage);
86+
});
87+
});
88+
89+
describe('getAllPostStatistics', () => {
90+
it('게시물 전체 통계 조회', async () => {
91+
const mockStatistics = {
92+
daily_view_count: '355',
93+
daily_like_count: '3',
94+
yesterday_views: '355',
95+
yesterday_likes: '3',
96+
last_updated_date: '2025-03-14T15:52:40.767Z',
97+
};
98+
99+
postRepo.getYesterdayAndTodayViewLikeStats.mockResolvedValue(mockStatistics);
100+
101+
const result = await postService.getAllPostStatistics(1);
102+
103+
expect(result).toEqual({
104+
totalViews: 355,
105+
totalLikes: 3,
106+
yesterdayViews: 355,
107+
yesterdayLikes: 3,
108+
lastUpdatedDate: '2025-03-14T15:52:40.767Z',
109+
});
110+
});
111+
112+
it('쿼리 중 오류 발생 시 DBError Throw', async () => {
113+
const errorMessage = '통계 조회 중 문제가 발생했습니다.';
114+
postRepo.getYesterdayAndTodayViewLikeStats.mockRejectedValue(new DBError(errorMessage));
115+
116+
await expect(postService.getAllPostStatistics(1)).rejects.toThrow(errorMessage);
117+
});
118+
});
119+
120+
describe('getTotalPostCounts', () => {
121+
it('게시물 개수 조회', async () => {
122+
const mockCount = 2;
123+
postRepo.getTotalPostCounts.mockResolvedValue(mockCount);
124+
125+
const result = await postService.getTotalPostCounts(1);
126+
127+
expect(result).toBe(mockCount);
128+
});
129+
130+
it('쿼리 중 오류 발생 시 DBError Throw', async () => {
131+
const errorMessage = '총 게시물 수 조회 중 문제가 발생했습니다.';
132+
postRepo.getTotalPostCounts.mockRejectedValue(new DBError(errorMessage));
133+
134+
await expect(postService.getTotalPostCounts(1)).rejects.toThrow(errorMessage);
135+
});
136+
});
137+
138+
describe('getPostByPostId', () => {
139+
it('게시물 상세 통계 조회', async () => {
140+
const mockPosts = [
141+
{
142+
date: '2025-03-08T00:00:00.000Z',
143+
daily_view_count: 145,
144+
daily_like_count: 2,
145+
},
146+
{
147+
date: '2025-03-09T00:00:00.000Z',
148+
daily_view_count: 145,
149+
daily_like_count: 2,
150+
},
151+
{
152+
date: '2025-03-10T00:00:00.000Z',
153+
daily_view_count: 147,
154+
daily_like_count: 2,
155+
},
156+
{
157+
date: '2025-03-11T00:00:00.000Z',
158+
daily_view_count: 147,
159+
daily_like_count: 2,
160+
},
161+
];
162+
163+
postRepo.findPostByPostId.mockResolvedValue(mockPosts);
164+
165+
const result = await postService.getPostByPostId(1);
166+
167+
expect(result).toEqual([
168+
{
169+
date: '2025-03-08T00:00:00.000Z',
170+
dailyViewCount: 145,
171+
dailyLikeCount: 2,
172+
},
173+
{
174+
date: '2025-03-09T00:00:00.000Z',
175+
dailyViewCount: 145,
176+
dailyLikeCount: 2,
177+
},
178+
{
179+
date: '2025-03-10T00:00:00.000Z',
180+
dailyViewCount: 147,
181+
dailyLikeCount: 2,
182+
},
183+
{
184+
date: '2025-03-11T00:00:00.000Z',
185+
dailyViewCount: 147,
186+
dailyLikeCount: 2,
187+
},
188+
]);
189+
});
190+
191+
it('쿼리 중 오류 발생 시 DBError Throw', async () => {
192+
const errorMessage = '게시물 조회 중 문제가 발생했습니다.';
193+
postRepo.findPostByPostId.mockRejectedValue(new DBError(errorMessage));
194+
195+
await expect(postService.getPostByPostId(1)).rejects.toThrow(errorMessage);
196+
});
197+
});
198+
199+
describe('getPostByPostUUID', () => {
200+
it('게시물 상세 통계 조회', async () => {
201+
const mockPosts = [
202+
{
203+
date: '2025-03-08T00:00:00.000Z',
204+
daily_view_count: 145,
205+
daily_like_count: 2,
206+
},
207+
{
208+
date: '2025-03-09T00:00:00.000Z',
209+
daily_view_count: 145,
210+
daily_like_count: 2,
211+
},
212+
{
213+
date: '2025-03-10T00:00:00.000Z',
214+
daily_view_count: 147,
215+
daily_like_count: 2,
216+
},
217+
{
218+
date: '2025-03-11T00:00:00.000Z',
219+
daily_view_count: 147,
220+
daily_like_count: 2,
221+
},
222+
];
223+
224+
postRepo.findPostByPostUUID.mockResolvedValue(mockPosts);
225+
226+
const result = await postService.getPostByPostUUID('uuid-1234');
227+
228+
expect(result).toEqual([
229+
{
230+
date: '2025-03-08T00:00:00.000Z',
231+
dailyViewCount: 145,
232+
dailyLikeCount: 2,
233+
},
234+
{
235+
date: '2025-03-09T00:00:00.000Z',
236+
dailyViewCount: 145,
237+
dailyLikeCount: 2,
238+
},
239+
{
240+
date: '2025-03-10T00:00:00.000Z',
241+
dailyViewCount: 147,
242+
dailyLikeCount: 2,
243+
},
244+
{
245+
date: '2025-03-11T00:00:00.000Z',
246+
dailyViewCount: 147,
247+
dailyLikeCount: 2,
248+
},
249+
]);
250+
});
251+
252+
it('쿼리 중 오류 발생 시 DBError Throw', async () => {
253+
const errorMessage = 'UUID로 게시물 조회 중 문제가 발생했습니다.';
254+
postRepo.findPostByPostUUID.mockRejectedValue(new DBError(errorMessage));
255+
256+
await expect(postService.getPostByPostUUID('uuid-1234')).rejects.toThrow(errorMessage);
257+
});
258+
});
259+
});

0 commit comments

Comments
 (0)