Skip to content

Commit 4312528

Browse files
authored
[25.03.14 / TASK-78] Test - Service 계층 Test Code 작성 (postService) (#22)
* modify : 현재 파일 구조에 맞춰 readme 최신신화 * feature : userService 테스트 파일 작성성 * modify : userService 테스트 파일 작성(임시) * test : postService 테스트 코드 구현현 * refactor : 초기 연습용 코드 제거거 * modify : 불필요한 출력문 제거, mockPool 타입 추가
1 parent 4a73e7f commit 4312528

File tree

2 files changed

+271
-3
lines changed

2 files changed

+271
-3
lines changed

Diff for: README.md

+12-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
```bash
1313
pnpm install
14-
NODE_ENV=development pnpm install # devDpe 설치 위해
14+
NODE_ENV=development pnpm install # devDependencies 설치 위해
1515

1616
# 만약 pnpm 이 없다면
1717
brew install pnpm
@@ -37,6 +37,9 @@ pnpm dev # 개발 서버 실행
3737
pnpm test # 테스트 실행
3838
pnpm lint # 린트 검사
3939
pnpm lint:fix # 린트 자동 수정
40+
41+
pnpm build # 프로젝트 빌드
42+
pnpm start # 빌드된 프로젝트 시작
4043
```
4144

4245
## Project Structure
@@ -45,9 +48,15 @@ pnpm lint:fix # 린트 자동 수정
4548
├── src/
4649
├── __test__/ # 테스트 파일
4750
├── configs/ # 설정 파일 (DB 등)
51+
├── constants/ # 상수 데이터 파일
4852
├── controllers/ # API 컨트롤러
49-
├── models/ # 데이터 모델
53+
├── exception/ # 커스텀 에러 파일
54+
├── middlewares/ # 각종 미들웨어 (인증, 에러, 데이터 검증 등)
55+
├── modules/ # 모듈 파일 (슬랙 등)
5056
├── repositories/ # 데이터 액세스 레이어
5157
├── routers/ # API 라우트 정의
52-
└── services/ # 비즈니스 로직
58+
├── services/ # 비즈니스 로직
59+
├┬── types/ # Enum, DTO 등 데이터 타입 정의
60+
│└── models/ # 데이터 모델
61+
└── utils/ # 편의성 함수 정의
5362
```

Diff for: src/services/__test__/test.post.service.test.ts

+259
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+
import { Pool } from 'pg';
5+
6+
jest.mock('@/repositories/post.repository');
7+
8+
// 모든 파라미터는 Route 단에서 검증하기 때문에 파라미터를 제대로 받았는지는 확인하지 않음
9+
describe('PostService', () => {
10+
let postService: PostService;
11+
let postRepo: jest.Mocked<PostRepository>;
12+
let mockPool: jest.Mocked<Pool>;
13+
14+
beforeEach(() => {
15+
mockPool = {} as jest.Mocked<Pool>;
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+
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)