|
| 1 | +import { Pool, QueryResult } from 'pg'; |
| 2 | +import { PostRepository } from '@/repositories/post.repository'; |
| 3 | +import { DBError } from '@/exception'; |
| 4 | + |
| 5 | +jest.mock('pg'); |
| 6 | + |
| 7 | +const mockPool: { |
| 8 | + query: jest.Mock<Promise<QueryResult<Record<string, unknown>>>, unknown[]>; |
| 9 | +} = { |
| 10 | + query: jest.fn(), |
| 11 | +}; |
| 12 | + |
| 13 | +describe('PostRepository', () => { |
| 14 | + let repo: PostRepository; |
| 15 | + |
| 16 | + beforeEach(() => { |
| 17 | + repo = new PostRepository(mockPool as unknown as Pool); |
| 18 | + }); |
| 19 | + |
| 20 | + describe('findPostsByUserId', () => { |
| 21 | + it('사용자의 게시글과 nextCursor를 반환해야 한다', async () => { |
| 22 | + const mockPosts = [ |
| 23 | + { id: 1, post_released_at: '2025-03-01T00:00:00Z', daily_view_count: 10, daily_like_count: 5 }, |
| 24 | + { id: 2, post_released_at: '2025-03-02T00:00:00Z', daily_view_count: 20, daily_like_count: 15 }, |
| 25 | + ]; |
| 26 | + |
| 27 | + mockPool.query.mockResolvedValue({ |
| 28 | + rows: mockPosts, |
| 29 | + rowCount: mockPosts.length, |
| 30 | + command: '', |
| 31 | + oid: 0, |
| 32 | + fields: [], |
| 33 | + } as QueryResult); |
| 34 | + |
| 35 | + const result = await repo.findPostsByUserId(1, undefined, 'released_at', false); |
| 36 | + |
| 37 | + expect(result.posts).toEqual(mockPosts); |
| 38 | + expect(result).toHaveProperty('nextCursor'); |
| 39 | + }); |
| 40 | + |
| 41 | + it('정렬 순서를 보장해야 한다', async () => { |
| 42 | + const mockPosts = [ |
| 43 | + { id: 2, post_released_at: '2025-03-02T00:00:00Z', daily_view_count: 20, daily_like_count: 15 }, |
| 44 | + { id: 1, post_released_at: '2025-03-01T00:00:00Z', daily_view_count: 10, daily_like_count: 5 }, |
| 45 | + ]; |
| 46 | + |
| 47 | + mockPool.query.mockResolvedValue({ |
| 48 | + rows: mockPosts, |
| 49 | + rowCount: mockPosts.length, |
| 50 | + command: '', |
| 51 | + oid: 0, |
| 52 | + fields: [], |
| 53 | + } as QueryResult); |
| 54 | + |
| 55 | + const result = await repo.findPostsByUserId(1, undefined, 'released_at', false); |
| 56 | + expect(result.posts).toEqual(mockPosts); |
| 57 | + expect(result.posts[0].id).toBeGreaterThan(result.posts[1].id); |
| 58 | + }); |
| 59 | + }); |
| 60 | + |
| 61 | + describe('getTotalPostCounts', () => { |
| 62 | + it('사용자의 총 게시글 수를 반환해야 한다', async () => { |
| 63 | + mockPool.query.mockResolvedValue({ |
| 64 | + rows: [{ count: '10' }], |
| 65 | + rowCount: 1, |
| 66 | + command: '', |
| 67 | + oid: 0, |
| 68 | + fields: [], |
| 69 | + } as QueryResult); |
| 70 | + |
| 71 | + const count = await repo.getTotalPostCounts(1); |
| 72 | + expect(count).toBe(10); |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + describe('에러 발생 시 처리', () => { |
| 77 | + it('쿼리 실행 중 에러가 발생하면 DBError를 던져야 한다', async () => { |
| 78 | + mockPool.query.mockRejectedValue(new Error('DB connection failed')); |
| 79 | + |
| 80 | + await expect(repo.findPostsByUserId(1)).rejects.toThrow(DBError); |
| 81 | + await expect(repo.getTotalPostCounts(1)).rejects.toThrow(DBError); |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + describe('getYesterdayAndTodayViewLikeStats', () => { |
| 86 | + it('어제와 오늘의 조회수 및 좋아요 수를 반환해야 한다', async () => { |
| 87 | + const mockStats = { |
| 88 | + daily_view_count: 20, |
| 89 | + daily_like_count: 15, |
| 90 | + yesterday_views: 10, |
| 91 | + yesterday_likes: 8, |
| 92 | + last_updated_date: '2025-03-08T00:00:00Z', |
| 93 | + }; |
| 94 | + |
| 95 | + mockPool.query.mockResolvedValue({ |
| 96 | + rows: [mockStats], |
| 97 | + rowCount: 1, |
| 98 | + command: '', |
| 99 | + oid: 0, |
| 100 | + fields: [], |
| 101 | + } as QueryResult); |
| 102 | + |
| 103 | + const result = await repo.getYesterdayAndTodayViewLikeStats(1); |
| 104 | + expect(result).toEqual(mockStats); |
| 105 | + }); |
| 106 | + }); |
| 107 | + |
| 108 | + describe('findPostByPostId', () => { |
| 109 | + it('특정 post ID에 대한 통계를 반환해야 한다', async () => { |
| 110 | + const mockStats = [ |
| 111 | + { date: '2025-03-08T00:00:00Z', daily_view_count: 50, daily_like_count: 30 }, |
| 112 | + ]; |
| 113 | + |
| 114 | + mockPool.query.mockResolvedValue({ |
| 115 | + rows: mockStats, |
| 116 | + rowCount: mockStats.length, |
| 117 | + command: '', |
| 118 | + oid: 0, |
| 119 | + fields: [], |
| 120 | + } as QueryResult); |
| 121 | + |
| 122 | + const result = await repo.findPostByPostId(1); |
| 123 | + expect(result).toEqual(mockStats); |
| 124 | + }); |
| 125 | + }); |
| 126 | + |
| 127 | + describe('findPostByPostUUID', () => { |
| 128 | + it('특정 post UUID에 대한 통계를 반환해야 한다', async () => { |
| 129 | + const mockStats = [ |
| 130 | + { date: '2025-03-08T00:00:00Z', daily_view_count: 50, daily_like_count: 30 }, |
| 131 | + ]; |
| 132 | + |
| 133 | + mockPool.query.mockResolvedValue({ |
| 134 | + rows: mockStats, |
| 135 | + rowCount: mockStats.length, |
| 136 | + command: '', |
| 137 | + oid: 0, |
| 138 | + fields: [], |
| 139 | + } as QueryResult); |
| 140 | + |
| 141 | + const result = await repo.findPostByPostUUID('uuid-1234', '2025-03-01', '2025-03-08'); |
| 142 | + expect(result).toEqual(mockStats); |
| 143 | + }); |
| 144 | + }); |
| 145 | +}); |
0 commit comments