Skip to content

Commit 03525a0

Browse files
committed
modify: pg의 QueryResult 타입을 만족하는 mock 객체를 생성하기 위한 헬퍼 함수 생성
1 parent 0cb309f commit 03525a0

File tree

1 file changed

+31
-97
lines changed

1 file changed

+31
-97
lines changed

src/repositories/__test__/post.repo.test.ts

Lines changed: 31 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@ import { DBError } from '@/exception';
44

55
jest.mock('pg');
66

7+
// pg의 QueryResult 타입을 만족하는 mock 객체를 생성하기 위한 헬퍼 함수 생성
8+
function createMockQueryResult<T extends Record<string, unknown>>(rows: T[]): QueryResult<T> {
9+
return {
10+
rows,
11+
rowCount: rows.length,
12+
command: '',
13+
oid: 0,
14+
fields: [],
15+
} satisfies QueryResult<T>;
16+
}
17+
718
const mockPool: {
819
query: jest.Mock<Promise<QueryResult<Record<string, unknown>>>, unknown[]>;
920
} = {
@@ -15,6 +26,7 @@ describe('PostRepository', () => {
1526

1627
beforeEach(() => {
1728
repo = new PostRepository(mockPool as unknown as Pool);
29+
jest.clearAllMocks();
1830
});
1931

2032
describe('findPostsByUserId', () => {
@@ -24,13 +36,7 @@ describe('PostRepository', () => {
2436
{ id: 2, post_released_at: '2025-03-02T00:00:00Z', daily_view_count: 20, daily_like_count: 15 },
2537
];
2638

27-
mockPool.query.mockResolvedValue({
28-
rows: mockPosts,
29-
rowCount: mockPosts.length,
30-
command: '',
31-
oid: 0,
32-
fields: [],
33-
} as QueryResult);
39+
mockPool.query.mockResolvedValue(createMockQueryResult(mockPosts));
3440

3541
const result = await repo.findPostsByUserId(1, undefined, 'released_at', false);
3642

@@ -44,13 +50,7 @@ describe('PostRepository', () => {
4450
{ id: 1, post_released_at: '2025-03-01T00:00:00Z', daily_view_count: 10, daily_like_count: 5 },
4551
];
4652

47-
mockPool.query.mockResolvedValue({
48-
rows: mockPosts,
49-
rowCount: mockPosts.length,
50-
command: '',
51-
oid: 0,
52-
fields: [],
53-
} as QueryResult);
53+
mockPool.query.mockResolvedValue(createMockQueryResult(mockPosts));
5454

5555
const result = await repo.findPostsByUserId(1, undefined, 'released_at', false);
5656
expect(result.posts).toEqual(mockPosts);
@@ -62,13 +62,7 @@ describe('PostRepository', () => {
6262
{ id: 1, post_released_at: '2025-03-01T00:00:00Z', daily_view_count: 10, daily_like_count: 5 },
6363
];
6464

65-
mockPool.query.mockResolvedValue({
66-
rows: mockPosts,
67-
rowCount: mockPosts.length,
68-
command: '',
69-
oid: 0,
70-
fields: [],
71-
} as QueryResult);
65+
mockPool.query.mockResolvedValue(createMockQueryResult(mockPosts));
7266

7367
await repo.findPostsByUserId(1);
7468

@@ -101,13 +95,7 @@ describe('PostRepository', () => {
10195
},
10296
];
10397

104-
mockPool.query.mockResolvedValue({
105-
rows: mockPosts,
106-
rowCount: mockPosts.length,
107-
command: '',
108-
oid: 0,
109-
fields: [],
110-
} as QueryResult);
98+
mockPool.query.mockResolvedValue(createMockQueryResult(mockPosts));
11199

112100
const result = await repo.findPostsByUserIdWithGrowthMetrics(1);
113101

@@ -135,13 +123,7 @@ describe('PostRepository', () => {
135123
},
136124
];
137125

138-
mockPool.query.mockResolvedValue({
139-
rows: mockPosts,
140-
rowCount: mockPosts.length,
141-
command: '',
142-
oid: 0,
143-
fields: [],
144-
} as QueryResult);
126+
mockPool.query.mockResolvedValue(createMockQueryResult(mockPosts));
145127

146128
const result = await repo.findPostsByUserIdWithGrowthMetrics(1, undefined, false);
147129
expect(result.posts).toEqual(mockPosts);
@@ -159,13 +141,7 @@ describe('PostRepository', () => {
159141
},
160142
];
161143

162-
mockPool.query.mockResolvedValue({
163-
rows: mockPosts,
164-
rowCount: mockPosts.length,
165-
command: '',
166-
oid: 0,
167-
fields: [],
168-
} as QueryResult);
144+
mockPool.query.mockResolvedValue(createMockQueryResult(mockPosts));
169145

170146
const result = await repo.findPostsByUserIdWithGrowthMetrics(1, "10,2", false);
171147
expect(result.posts).toEqual(mockPosts);
@@ -185,13 +161,7 @@ describe('PostRepository', () => {
185161
{ id: 1, view_growth: 20, like_growth: 5 },
186162
];
187163

188-
mockPool.query.mockResolvedValue({
189-
rows: mockPosts,
190-
rowCount: mockPosts.length,
191-
command: '',
192-
oid: 0,
193-
fields: [],
194-
} as QueryResult);
164+
mockPool.query.mockResolvedValue(createMockQueryResult(mockPosts));
195165

196166
await repo.findPostsByUserIdWithGrowthMetrics(1);
197167

@@ -205,29 +175,17 @@ describe('PostRepository', () => {
205175

206176
describe('getTotalPostCounts', () => {
207177
it('사용자의 총 게시글 수를 반환해야 한다', async () => {
208-
mockPool.query.mockResolvedValue({
209-
rows: [{ count: '10' }],
210-
rowCount: 1,
211-
command: '',
212-
oid: 0,
213-
fields: [],
214-
} as QueryResult);
178+
mockPool.query.mockResolvedValue(createMockQueryResult([{ count: '10' }]));
215179

216180
const count = await repo.getTotalPostCounts(1);
217181
expect(count).toBe(10);
218182
});
219183

220184
it('is_active = TRUE인 게시물만 카운트해야 한다', async () => {
221-
mockPool.query.mockResolvedValue({
222-
rows: [{ count: '5' }],
223-
rowCount: 1,
224-
command: '',
225-
oid: 0,
226-
fields: [],
227-
} as QueryResult);
228-
185+
mockPool.query.mockResolvedValue(createMockQueryResult([{ count: '5' }]));
186+
229187
await repo.getTotalPostCounts(1);
230-
188+
231189
// 쿼리 호출 확인
232190
expect(mockPool.query).toHaveBeenCalledWith(
233191
expect.stringContaining("is_active = TRUE"),
@@ -255,35 +213,23 @@ describe('PostRepository', () => {
255213
last_updated_date: '2025-03-08T00:00:00Z',
256214
};
257215

258-
mockPool.query.mockResolvedValue({
259-
rows: [mockStats],
260-
rowCount: 1,
261-
command: '',
262-
oid: 0,
263-
fields: [],
264-
} as QueryResult);
216+
mockPool.query.mockResolvedValue(createMockQueryResult([mockStats]));
265217

266218
const result = await repo.getYesterdayAndTodayViewLikeStats(1);
267219
expect(result).toEqual(mockStats);
268220
});
269221

270222
it('is_active = TRUE인 게시물의 통계만 반환해야 한다', async () => {
271-
mockPool.query.mockResolvedValue({
272-
rows: [{ daily_view_count: 20, daily_like_count: 15 }],
273-
rowCount: 1,
274-
command: '',
275-
oid: 0,
276-
fields: [],
277-
} as QueryResult);
278-
223+
mockPool.query.mockResolvedValue(createMockQueryResult([{ daily_view_count: 20, daily_like_count: 15 }]));
224+
279225
await repo.getYesterdayAndTodayViewLikeStats(1);
280-
226+
281227
// 쿼리 호출 확인
282228
expect(mockPool.query).toHaveBeenCalledWith(
283229
expect.stringContaining("p.is_active = TRUE"),
284230
expect.anything()
285231
);
286-
});
232+
});
287233
});
288234

289235
describe('findPostByPostId', () => {
@@ -292,13 +238,7 @@ describe('PostRepository', () => {
292238
{ date: '2025-03-08T00:00:00Z', daily_view_count: 50, daily_like_count: 30 },
293239
];
294240

295-
mockPool.query.mockResolvedValue({
296-
rows: mockStats,
297-
rowCount: mockStats.length,
298-
command: '',
299-
oid: 0,
300-
fields: [],
301-
} as QueryResult);
241+
mockPool.query.mockResolvedValue(createMockQueryResult(mockStats));
302242

303243
const result = await repo.findPostByPostId(1);
304244
expect(result).toEqual(mockStats);
@@ -311,16 +251,10 @@ describe('PostRepository', () => {
311251
{ date: '2025-03-08T00:00:00Z', daily_view_count: 50, daily_like_count: 30 },
312252
];
313253

314-
mockPool.query.mockResolvedValue({
315-
rows: mockStats,
316-
rowCount: mockStats.length,
317-
command: '',
318-
oid: 0,
319-
fields: [],
320-
} as QueryResult);
254+
mockPool.query.mockResolvedValue(createMockQueryResult(mockStats));
321255

322256
const result = await repo.findPostByPostUUID('uuid-1234', '2025-03-01', '2025-03-08');
323257
expect(result).toEqual(mockStats);
324258
});
325259
});
326-
});
260+
});

0 commit comments

Comments
 (0)