-   Notifications  You must be signed in to change notification settings 
- Fork 0
[25.03.14 / TASK-78] Test - Service 계층 Test Code 작성 (postService) #22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
   Merged  
     Merged  
 Changes from all commits
 Commits 
  Show all changes 
  6 commits   Select commit Hold shift + click to select a range 
 ff469ab  modify : 현재 파일 구조에 맞춰 readme 최신신화 
  cheonHwi ead0de3  feature : userService 테스트 파일 작성성 
  cheonHwi efaf34b  modify : userService 테스트 파일 작성(임시) 
  cheonHwi 4a4746b  test : postService 테스트 코드 구현현 
  cheonHwi 4da4122  refactor : 초기 연습용 코드 제거거 
  cheonHwi 923dd71  modify : 불필요한 출력문 제거, mockPool 타입 추가 
  cheonHwi File filter
Filter by extension
Conversations
 Failed to load comments.  
    Loading  
 Jump to
  Jump to file  
  Failed to load files.  
    Loading  
 Diff view
Diff view
There are no files selected for viewing
   This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters   
        This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters   
     | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,259 @@ | ||
| import { PostService } from '@/services/post.service'; | ||
| import { PostRepository } from '@/repositories/post.repository'; | ||
| import { DBError } from '@/exception'; | ||
| import { Pool } from 'pg'; | ||
|  | ||
| jest.mock('@/repositories/post.repository'); | ||
|  | ||
| // 모든 파라미터는 Route 단에서 검증하기 때문에 파라미터를 제대로 받았는지는 확인하지 않음 | ||
| describe('PostService', () => { | ||
| let postService: PostService; | ||
| let postRepo: jest.Mocked<PostRepository>; | ||
| let mockPool: jest.Mocked<Pool>; | ||
|  | ||
| beforeEach(() => { | ||
| mockPool = {} as jest.Mocked<Pool>; | ||
| postRepo = new PostRepository(mockPool) as jest.Mocked<PostRepository>; | ||
| postService = new PostService(postRepo); | ||
| }); | ||
|  | ||
| describe('getAllposts', () => { | ||
| it('게시물 목록 조회', async () => { | ||
| const mockPosts = { | ||
| nextCursor: '2023-11-19T09:19:36.811Z,519212', | ||
| posts: [ | ||
| { | ||
| id: '519211', | ||
| title: 'velog dashboard test post (2)', | ||
| slug: 'velog-dashboard-test-post-2', | ||
| daily_view_count: 147, | ||
| daily_like_count: 2, | ||
| yesterday_daily_view_count: 147, | ||
| yesterday_daily_like_count: 2, | ||
| post_created_at: '2025-02-08T02:58:24.347Z', | ||
| post_released_at: '2023-11-20T02:15:14.209Z', | ||
| }, | ||
| { | ||
| id: '519212', | ||
| title: 'velog dashboard test post (1)', | ||
| slug: 'velog-dashboard-test-post-1', | ||
| daily_view_count: 208, | ||
| daily_like_count: 1, | ||
| yesterday_daily_view_count: 208, | ||
| yesterday_daily_like_count: 1, | ||
| post_created_at: '2025-02-08T02:58:24.347Z', | ||
| post_released_at: '2023-11-19T09:19:36.811Z', | ||
| }, | ||
| ], | ||
| }; | ||
|  | ||
| postRepo.findPostsByUserId.mockResolvedValue(mockPosts); | ||
|  | ||
| const result = await postService.getAllposts(1); | ||
|  | ||
| expect(result.posts).toEqual([ | ||
| { | ||
| id: '519211', | ||
| title: 'velog dashboard test post (2)', | ||
| slug: 'velog-dashboard-test-post-2', | ||
| views: 147, | ||
| likes: 2, | ||
| yesterdayViews: 147, | ||
| yesterdayLikes: 2, | ||
| createdAt: '2025-02-08T02:58:24.347Z', | ||
| releasedAt: '2023-11-20T02:15:14.209Z', | ||
| }, | ||
| { | ||
| id: '519212', | ||
| title: 'velog dashboard test post (1)', | ||
| slug: 'velog-dashboard-test-post-1', | ||
| views: 208, | ||
| likes: 1, | ||
| yesterdayViews: 208, | ||
| yesterdayLikes: 1, | ||
| createdAt: '2025-02-08T02:58:24.347Z', | ||
| releasedAt: '2023-11-19T09:19:36.811Z', | ||
| }, | ||
| ]); | ||
| expect(result.nextCursor).toBe('2023-11-19T09:19:36.811Z,519212'); | ||
| }); | ||
|  | ||
| it('쿼리 중 오류 발생 시 DBError Throw', async () => { | ||
| const errorMessage = '전체 post 조회 중 문제가 발생했습니다.'; | ||
| postRepo.findPostsByUserId.mockRejectedValue(new DBError(errorMessage)); | ||
|  | ||
| await expect(postService.getAllposts(1)).rejects.toThrow(errorMessage); | ||
| }); | ||
| }); | ||
|  | ||
| describe('getAllPostStatistics', () => { | ||
| it('게시물 전체 통계 조회', async () => { | ||
| const mockStatistics = { | ||
| daily_view_count: '355', | ||
| daily_like_count: '3', | ||
| yesterday_views: '355', | ||
| yesterday_likes: '3', | ||
| last_updated_date: '2025-03-14T15:52:40.767Z', | ||
| }; | ||
|  | ||
| postRepo.getYesterdayAndTodayViewLikeStats.mockResolvedValue(mockStatistics); | ||
|  | ||
| const result = await postService.getAllPostStatistics(1); | ||
|  | ||
| expect(result).toEqual({ | ||
| totalViews: 355, | ||
| totalLikes: 3, | ||
| yesterdayViews: 355, | ||
| yesterdayLikes: 3, | ||
| lastUpdatedDate: '2025-03-14T15:52:40.767Z', | ||
| }); | ||
| }); | ||
|  | ||
| it('쿼리 중 오류 발생 시 DBError Throw', async () => { | ||
| const errorMessage = '통계 조회 중 문제가 발생했습니다.'; | ||
| postRepo.getYesterdayAndTodayViewLikeStats.mockRejectedValue(new DBError(errorMessage)); | ||
|  | ||
| await expect(postService.getAllPostStatistics(1)).rejects.toThrow(errorMessage); | ||
| }); | ||
| }); | ||
|  | ||
| describe('getTotalPostCounts', () => { | ||
| it('게시물 개수 조회', async () => { | ||
| const mockCount = 2; | ||
| postRepo.getTotalPostCounts.mockResolvedValue(mockCount); | ||
|  | ||
| const result = await postService.getTotalPostCounts(1); | ||
|  | ||
| expect(result).toBe(mockCount); | ||
| }); | ||
|  | ||
| it('쿼리 중 오류 발생 시 DBError Throw', async () => { | ||
| const errorMessage = '총 게시물 수 조회 중 문제가 발생했습니다.'; | ||
| postRepo.getTotalPostCounts.mockRejectedValue(new DBError(errorMessage)); | ||
|  | ||
| await expect(postService.getTotalPostCounts(1)).rejects.toThrow(errorMessage); | ||
| }); | ||
| }); | ||
|  | ||
| describe('getPostByPostId', () => { | ||
| it('게시물 상세 통계 조회', async () => { | ||
| const mockPosts = [ | ||
| { | ||
| date: '2025-03-08T00:00:00.000Z', | ||
| daily_view_count: 145, | ||
| daily_like_count: 2, | ||
| }, | ||
| { | ||
| date: '2025-03-09T00:00:00.000Z', | ||
| daily_view_count: 145, | ||
| daily_like_count: 2, | ||
| }, | ||
| { | ||
| date: '2025-03-10T00:00:00.000Z', | ||
| daily_view_count: 147, | ||
| daily_like_count: 2, | ||
| }, | ||
| { | ||
| date: '2025-03-11T00:00:00.000Z', | ||
| daily_view_count: 147, | ||
| daily_like_count: 2, | ||
| }, | ||
| ]; | ||
|  | ||
| postRepo.findPostByPostId.mockResolvedValue(mockPosts); | ||
|  | ||
| const result = await postService.getPostByPostId(1); | ||
|  | ||
| expect(result).toEqual([ | ||
| { | ||
| date: '2025-03-08T00:00:00.000Z', | ||
| dailyViewCount: 145, | ||
| dailyLikeCount: 2, | ||
| }, | ||
| { | ||
| date: '2025-03-09T00:00:00.000Z', | ||
| dailyViewCount: 145, | ||
| dailyLikeCount: 2, | ||
| }, | ||
| { | ||
| date: '2025-03-10T00:00:00.000Z', | ||
| dailyViewCount: 147, | ||
| dailyLikeCount: 2, | ||
| }, | ||
| { | ||
| date: '2025-03-11T00:00:00.000Z', | ||
| dailyViewCount: 147, | ||
| dailyLikeCount: 2, | ||
| }, | ||
| ]); | ||
| }); | ||
|  | ||
| it('쿼리 중 오류 발생 시 DBError Throw', async () => { | ||
| const errorMessage = '게시물 조회 중 문제가 발생했습니다.'; | ||
| postRepo.findPostByPostId.mockRejectedValue(new DBError(errorMessage)); | ||
|  | ||
| await expect(postService.getPostByPostId(1)).rejects.toThrow(errorMessage); | ||
| }); | ||
| }); | ||
|  | ||
| describe('getPostByPostUUID', () => { | ||
| it('게시물 상세 통계 조회', async () => { | ||
| const mockPosts = [ | ||
| { | ||
| date: '2025-03-08T00:00:00.000Z', | ||
| daily_view_count: 145, | ||
| daily_like_count: 2, | ||
| }, | ||
| { | ||
| date: '2025-03-09T00:00:00.000Z', | ||
| daily_view_count: 145, | ||
| daily_like_count: 2, | ||
| }, | ||
| { | ||
| date: '2025-03-10T00:00:00.000Z', | ||
| daily_view_count: 147, | ||
| daily_like_count: 2, | ||
| }, | ||
| { | ||
| date: '2025-03-11T00:00:00.000Z', | ||
| daily_view_count: 147, | ||
| daily_like_count: 2, | ||
| }, | ||
| ]; | ||
|  | ||
| postRepo.findPostByPostUUID.mockResolvedValue(mockPosts); | ||
|  | ||
| const result = await postService.getPostByPostUUID('uuid-1234'); | ||
|  | ||
| expect(result).toEqual([ | ||
| { | ||
| date: '2025-03-08T00:00:00.000Z', | ||
| dailyViewCount: 145, | ||
| dailyLikeCount: 2, | ||
| }, | ||
| { | ||
| date: '2025-03-09T00:00:00.000Z', | ||
| dailyViewCount: 145, | ||
| dailyLikeCount: 2, | ||
| }, | ||
| { | ||
| date: '2025-03-10T00:00:00.000Z', | ||
| dailyViewCount: 147, | ||
| dailyLikeCount: 2, | ||
| }, | ||
| { | ||
| date: '2025-03-11T00:00:00.000Z', | ||
| dailyViewCount: 147, | ||
| dailyLikeCount: 2, | ||
| }, | ||
| ]); | ||
| }); | ||
|  | ||
| it('쿼리 중 오류 발생 시 DBError Throw', async () => { | ||
| const errorMessage = 'UUID로 게시물 조회 중 문제가 발생했습니다.'; | ||
| postRepo.findPostByPostUUID.mockRejectedValue(new DBError(errorMessage)); | ||
|  | ||
| await expect(postService.getPostByPostUUID('uuid-1234')).rejects.toThrow(errorMessage); | ||
| }); | ||
| }); | ||
| }); | ||
 Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.    
 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
해당 코드에서는 daily_view_count, daily_like_count, yesterday_views, yesterday_likes 모두 타입이 문자열로 지정되어있는데 아래 expect() 코드에서는 숫자로 비교되어지고 있는 것 같습니다! 혹시 의도하신 부분인지 여쭤보고 싶습니다!