-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[GWL-257] common service 에러 처리, 테스트 코드 작성, 리팩토링 (#272)
* chore: queryBuilder 에러 처리 * add: base-paginate-res.dto.ts 파일 추가 * test: test 환경 구축 * test: common.service.spec.ts 작성 * chore: format 적용 --------- Co-authored-by: jeong-yong-shin <[email protected]>
- Loading branch information
Showing
6 changed files
with
198 additions
and
25 deletions.
There are no files selected for viewing
This file contains 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,64 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { CommonService } from './common.service'; | ||
import { QueryFailedError, Repository } from 'typeorm'; | ||
import { posts } from '../posts/mocks/mocks'; | ||
import { Post } from '../posts/entities/posts.entity'; | ||
import { | ||
mockGetCreateUpdateQueryOptions, | ||
mockItems, | ||
mockPaginationDto, | ||
} from './mocks/mocks'; | ||
import { InternalServerErrorException } from '@nestjs/common'; | ||
import { getRepositoryToken } from '@nestjs/typeorm'; | ||
|
||
describe('commonService', () => { | ||
let service: CommonService; | ||
let postsRepository: Repository<Post>; | ||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [ | ||
CommonService, | ||
{ | ||
provide: getRepositoryToken(Post), | ||
useValue: { | ||
createQueryBuilder: jest.fn(), | ||
}, | ||
}, | ||
], | ||
}).compile(); | ||
service = module.get<CommonService>(CommonService); | ||
postsRepository = module.get<Repository<Post>>(getRepositoryToken(Post)); | ||
}); | ||
|
||
describe('paginate', () => { | ||
const mockQueryBuilder = { | ||
getMany: jest.fn(), | ||
} as any; | ||
it('QueryBuilder가 잘못 설정되어 있다면 InternalServerErrorException 에러', async () => { | ||
jest.spyOn(service, 'makeQueryBuilder').mockReturnValue(mockQueryBuilder); | ||
jest | ||
.spyOn(mockQueryBuilder, 'getMany') | ||
.mockRejectedValue( | ||
new QueryFailedError('SELECT *', [], 'Error message'), | ||
); | ||
await expect( | ||
service.paginate( | ||
mockPaginationDto, | ||
postsRepository, | ||
mockGetCreateUpdateQueryOptions, | ||
), | ||
).rejects.toThrow(InternalServerErrorException); | ||
}); | ||
|
||
it('게시글 요청하면 paginate 함수를 실행해서 요청한 게시글들, metaData 반환', async () => { | ||
jest.spyOn(service, 'makeQueryBuilder').mockReturnValue(mockQueryBuilder); | ||
jest.spyOn(mockQueryBuilder, 'getMany').mockResolvedValue(mockItems); | ||
const result = await service.paginate( | ||
mockPaginationDto, | ||
postsRepository, | ||
mockGetCreateUpdateQueryOptions, | ||
); | ||
expect(result).toEqual(posts); | ||
}); | ||
}); | ||
}); |
This file contains 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 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,8 @@ | ||
export class PaginateResponseDto { | ||
items: any[]; | ||
metaData: { | ||
lastItemId: number | null; | ||
isLastCursor: boolean; | ||
count: number; | ||
}; | ||
} |
This file contains 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,85 @@ | ||
import { BasePaginationDto } from '../dto/base-pagination.dto'; | ||
import { QueryOptions } from '../../common/type/query-options.type'; | ||
import { Profile } from '../../profiles/entities/profiles.entity'; | ||
|
||
export const mockPaginationDto: BasePaginationDto = { | ||
order__createdAt: 'DESC', | ||
take: 5, | ||
where__id__less_then: 7, | ||
}; | ||
export const profile = { | ||
publicId: 'XVZXC-ASFSA123-ASFSF', | ||
nickname: 'testNickname', | ||
} as Profile; | ||
|
||
export const mockItems = [ | ||
{ | ||
id: 6, | ||
publicId: profile.publicId, | ||
content: '안녕하세요 누구 누구 입니다.', | ||
like: null, | ||
createdAt: new Date('2023-12-04T05:14:15.879Z'), | ||
updatedAt: new Date('2023-12-04T05:14:15.879Z'), | ||
deletedAt: null, | ||
postUrl: 'https://www.naver.com', | ||
record: { | ||
id: 2, | ||
workoutTime: 6000000, | ||
distance: 100000, | ||
calorie: 360, | ||
avgHeartRate: 60, | ||
minHeartRate: 120, | ||
maxHeartRate: 180, | ||
}, | ||
profile: { | ||
nickname: profile.nickname, | ||
}, | ||
}, | ||
{ | ||
id: 5, | ||
publicId: profile.publicId, | ||
content: '수정한 내용입니다.', | ||
like: null, | ||
createdAt: new Date('2023-12-03T13:47:08.677Z'), | ||
updatedAt: new Date('2023-12-04T12:44:44.000Z'), | ||
deletedAt: null, | ||
postUrl: 'google.com', | ||
record: { | ||
id: 1, | ||
workoutTime: 100, | ||
distance: 100, | ||
calorie: 100, | ||
avgHeartRate: 100, | ||
minHeartRate: 100, | ||
maxHeartRate: 100, | ||
}, | ||
profile: { | ||
nickname: profile.nickname, | ||
}, | ||
}, | ||
]; | ||
|
||
export const mockGetCreateUpdateQueryOptions: QueryOptions = { | ||
mainAlias: 'post', | ||
joins: [ | ||
{ | ||
joinColumn: 'post.record', | ||
joinAlias: 'record', | ||
}, | ||
{ | ||
joinColumn: 'post.profile', | ||
joinAlias: 'profile', | ||
}, | ||
], | ||
selects: [ | ||
'post', | ||
'record.id', | ||
'record.workoutTime', | ||
'record.distance', | ||
'record.calorie', | ||
'record.avgHeartRate', | ||
'record.minHeartRate', | ||
'record.maxHeartRate', | ||
'profile.nickname', | ||
], | ||
}; |
This file contains 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 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