From dc0513f013e4a91191bb9b89710da932ed29eb4a Mon Sep 17 00:00:00 2001 From: jeong-yong-shin <jeong-yong-shin@Sjy-MacBookProM1.local> Date: Wed, 6 Dec 2023 23:03:40 +0900 Subject: [PATCH 1/3] =?UTF-8?q?test:=20posts.service.spec.ts=20=EC=9E=91?= =?UTF-8?q?=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BackEnd/src/common/common.service.ts | 10 +- .../src/posts/dto/get-posts-response.dto.ts | 2 +- BackEnd/src/posts/mocks/mocks.ts | 130 ++++++++++++++++++ BackEnd/src/posts/posts.service.spec.ts | 130 ++++++++++++++++++ BackEnd/src/posts/posts.service.ts | 13 +- 5 files changed, 273 insertions(+), 12 deletions(-) create mode 100644 BackEnd/src/posts/mocks/mocks.ts create mode 100644 BackEnd/src/posts/posts.service.spec.ts diff --git a/BackEnd/src/common/common.service.ts b/BackEnd/src/common/common.service.ts index e68b8384..04c2b82a 100644 --- a/BackEnd/src/common/common.service.ts +++ b/BackEnd/src/common/common.service.ts @@ -66,16 +66,16 @@ export class CommonService { queryOptions: QueryOptions, findManyOptions: FindManyOptions<T> = {}, ) { - let queryBilder = repository.createQueryBuilder(queryOptions.mainAlias); - queryBilder = queryBilder.setFindOptions(findManyOptions); + let queryBuilder = repository.createQueryBuilder(queryOptions.mainAlias); + queryBuilder = queryBuilder.setFindOptions(findManyOptions); if (queryOptions.join) { queryOptions.join.forEach((value: JoinType) => { - queryBilder = queryBilder.leftJoin(value.joinColumn, value.joinAlias); + queryBuilder = queryBuilder.leftJoin(value.joinColumn, value.joinAlias); }); } if (queryOptions.select) { - queryBilder = queryBilder.select(queryOptions.select); + queryBuilder = queryBuilder.select(queryOptions.select); } - return queryBilder; + return queryBuilder; } } diff --git a/BackEnd/src/posts/dto/get-posts-response.dto.ts b/BackEnd/src/posts/dto/get-posts-response.dto.ts index 27621887..dc76988b 100644 --- a/BackEnd/src/posts/dto/get-posts-response.dto.ts +++ b/BackEnd/src/posts/dto/get-posts-response.dto.ts @@ -54,7 +54,7 @@ export class MetaDataDto { count: number; } -class PostsPaginateResDto { +export class PostsPaginateResDto { @ApiProperty({ type: () => [PostDto] }) items: PostDto[]; diff --git a/BackEnd/src/posts/mocks/mocks.ts b/BackEnd/src/posts/mocks/mocks.ts new file mode 100644 index 00000000..159074f0 --- /dev/null +++ b/BackEnd/src/posts/mocks/mocks.ts @@ -0,0 +1,130 @@ +import { UpdateResult } from "typeorm"; +import { Profile } from "../../profiles/entities/profiles.entity"; +import { CreatePostDto } from "../dto/create-post.dto"; +import { PostDto, PostsPaginateResDto } from "../dto/get-posts-response.dto"; +import { PaginatePostDto } from "../dto/paginate-post.dto"; +import { UpdatePostDto } from "../dto/update-post.dto"; +import { Post } from "../entities/posts.entity"; + +export const postInfo: CreatePostDto = { + content: 'test content', + postUrl: 'naver.com', + recordId: 1, +}; + +export const updatePostInfo: UpdatePostDto = { + content: 'update content', + postUrl: 'google.com', +} + +export const profile = { + publicId: 'XVZXC-ASFSA123-ASFSF', + nickname: 'testNickname' +} as Profile + +export const query: PaginatePostDto = { + where__id__less_then: 7, + order__createdAt: 'DESC', + take: 5, +} + +export const post: PostDto = { + id: 5, + publicId: profile.publicId, + content: postInfo.content, + like: null, + createdAt: new Date("2023-12-04T05:14:15.879Z"), + updatedAt: new Date("2023-12-04T05:14:15.879Z"), + deletedAt: null, + postUrl: postInfo.postUrl, + record: { + id: postInfo.recordId, + workoutTime: 6000000, + distance: 100000, + calorie: 360, + avgHeartRate: 60, + minHeartRate: 120, + maxHeartRate: 180 + }, + profile: { + nickname: profile.nickname + } +}; +export const updateResult: UpdateResult = { generatedMaps: [], raw: [], affected: 1 }; + +export const updatedPaginatePost: PostDto = { + id: 5, + publicId: profile.publicId, + content: updatePostInfo.content, + like: null, + createdAt: new Date("2023-12-04T05:14:15.879Z"), + updatedAt: new Date("2023-12-05T05:14:15.879Z"), + deletedAt: null, + postUrl: updatePostInfo.postUrl, + record: { + id: postInfo.recordId, + workoutTime: 6000000, + distance: 100000, + calorie: 360, + avgHeartRate: 60, + minHeartRate: 120, + maxHeartRate: 180 + }, + profile: { + nickname: profile.nickname + } +} + +export const posts: PostsPaginateResDto = { + items: [ + { + 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, + } + } + ], + metaData: { + lastItemId: 5, + isLastCursor: true, + count: 2 + } +} \ No newline at end of file diff --git a/BackEnd/src/posts/posts.service.spec.ts b/BackEnd/src/posts/posts.service.spec.ts new file mode 100644 index 00000000..2c61d8f8 --- /dev/null +++ b/BackEnd/src/posts/posts.service.spec.ts @@ -0,0 +1,130 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { PostsService } from './posts.service'; +import { getRepositoryToken } from '@nestjs/typeorm'; +import { Post } from './entities/posts.entity'; +import { RecordsService } from '../records/records.service'; +import { CommonService } from '../common/common.service'; +import { Repository } from 'typeorm'; +import { Record } from '../records/entities/records.entity'; +import { Profile } from '../profiles/entities/profiles.entity'; +import { ExistPostException, NotFoundPostException } from './exceptions/posts.exception'; +import { post, postInfo, posts, profile, query, updatePostInfo, updateResult, updatedPaginatePost } from './mocks/mocks'; + +describe('postsService', () => { + let service: PostsService; + let repository: Repository<Post> + let recordsService: RecordsService; + let commonService: CommonService; + + const mockQueryBuilder = { + getOne: jest.fn() + } as any + + beforeEach(async () => { + const mockRepository = () => ({ + findOneBy: jest.fn(), + save: jest.fn(), + update: jest.fn(), + softDelete: jest.fn(), + }); + + const mockRecordsService = () => ({ + findById: jest.fn(), + updateIsPostedTrue: jest.fn(), + }); + + const mockCommonService = () => ({ + paginate: jest.fn(), + makeQueryBuilder: jest.fn(), + }); + + const module: TestingModule = await Test.createTestingModule({ + providers: [ + PostsService, + { + provide: getRepositoryToken(Post), + useValue: mockRepository(), + }, + { + provide: RecordsService, + useValue: mockRecordsService(), + }, + { + provide: CommonService, + useValue: mockCommonService(), + } + ], + }).compile(); + service = module.get<PostsService>(PostsService); + repository = module.get<Repository<Post>>(getRepositoryToken(Post)); + recordsService = module.get<RecordsService>(RecordsService); + commonService = module.get<CommonService>(CommonService); + }); + + describe('createPost', () => { + it('이미 record의 post가 존재한다면 throw new ExistPostException 발생', async () => { + const record = { isPosted: true } as Record; + jest.spyOn(recordsService, 'findById').mockResolvedValue(record); + await expect( + service.createPost(postInfo, profile as Profile), + ).rejects.toThrow(ExistPostException); + }); + + it('record의 post가 존재하지 않는다면 post 생성', async () => { + jest.spyOn(recordsService, 'findById').mockResolvedValue({ isPosted: false } as Record); + jest.spyOn(repository, 'save').mockResolvedValue({ id: 5 } as Post); + jest.spyOn(service, 'findOneById').mockResolvedValue(post) + + const result = await service.createPost(postInfo, profile); + expect(result).toEqual(post); + }) + }) + + describe('findOneById', () => { + it("post가 존재하지 않는다면 NotFoundPostException 발생", async () => { + jest.spyOn(commonService, 'makeQueryBuilder').mockImplementation(() => mockQueryBuilder); + jest.spyOn(mockQueryBuilder, 'getOne').mockResolvedValue(null); + await expect( + service.findOneById(5) + ).rejects.toThrow(NotFoundPostException); + }) + + it("post가 존재한다면 post를 반환", async () => { + jest.spyOn(commonService, 'makeQueryBuilder').mockImplementation(() => mockQueryBuilder); + jest.spyOn(mockQueryBuilder, 'getOne').mockResolvedValue(post); + + const result = await service.findOneById(post.id); + expect(result).toEqual(post); + }) + }); + + describe('paginatePosts', () => { + it('posts 반환', async () => { + jest.spyOn(commonService, 'paginate').mockResolvedValue(posts); + + const result = await service.paginatePosts(query); + expect(result).toEqual(posts); + }) + }) + + describe('paginateUserPosts', () => { + it('user의 posts 반환', async () => { + jest.spyOn(commonService, 'paginate').mockResolvedValue(posts); + + const result = await service.paginateUserPosts(profile.publicId, query); + expect(result).toEqual(posts); + }) + }) + + describe('updatePost', () => { + it('update된 post 반환', async () => { + jest.spyOn(service, 'findOneById').mockResolvedValue(post); + jest.spyOn(repository, 'update').mockResolvedValue(updateResult); + jest.spyOn(service, 'findOneById').mockResolvedValue(updatedPaginatePost); + + const result = await service.updatePost(updatedPaginatePost.id, updatePostInfo); + expect(result).toEqual(updatedPaginatePost); + }) + }) +}); + diff --git a/BackEnd/src/posts/posts.service.ts b/BackEnd/src/posts/posts.service.ts index 408a84ea..3ab5e89f 100644 --- a/BackEnd/src/posts/posts.service.ts +++ b/BackEnd/src/posts/posts.service.ts @@ -13,6 +13,7 @@ import { PaginatePostDto } from './dto/paginate-post.dto'; import { CommonService } from '../common/common.service'; import { UpdatePostDto } from './dto/update-post.dto'; import { getCreateUpdateQueryOptions } from './queryOptions/get-create-update.queryOptions'; +import { PostDto, PostsPaginateResDto } from './dto/get-posts-response.dto'; @Injectable() export class PostsService { @@ -23,7 +24,7 @@ export class PostsService { private readonly commonService: CommonService, ) {} - async createPost(postInfo: CreatePostDto, profile: Profile) { + async createPost(postInfo: CreatePostDto, profile: Profile): Promise<PostDto> { const record = await this.recordService.findById(postInfo.recordId); if (record.isPosted) { throw new ExistPostException(); @@ -39,7 +40,7 @@ export class PostsService { return await this.findOneById(post.id); } - async paginatePosts(query: PaginatePostDto) { + async paginatePosts(query: PaginatePostDto): Promise<PostsPaginateResDto> { return await this.commonService.paginate<Post>( query, this.postsRepository, @@ -47,8 +48,8 @@ export class PostsService { ); } - async findOneById(id: number) { - const queryBuilder = this.commonService.makeQueryBuilder( + async findOneById(id: number): Promise<PostDto> { + const queryBuilder = this.commonService.makeQueryBuilder<Post>( this.postsRepository, getCreateUpdateQueryOptions, { where: { id } }, @@ -60,7 +61,7 @@ export class PostsService { return post; } - async paginateUserPosts(publicId: string, query: PaginatePostDto) { + async paginateUserPosts(publicId: string, query: PaginatePostDto): Promise<PostsPaginateResDto> { return await this.commonService.paginate<Post>( query, this.postsRepository, @@ -69,7 +70,7 @@ export class PostsService { ); } - async updatePost(id: number, updatePostInfo: UpdatePostDto) { + async updatePost(id: number, updatePostInfo: UpdatePostDto): Promise<PostDto> { await this.findOneById(id); await this.postsRepository.update(id, updatePostInfo); return await this.findOneById(id); From 0c3385c5a0805eecd5fdda208e2902bfaa01b312 Mon Sep 17 00:00:00 2001 From: jeong-yong-shin <jeong-yong-shin@Sjy-MacBookProM1.local> Date: Thu, 7 Dec 2023 13:08:16 +0900 Subject: [PATCH 2/3] =?UTF-8?q?test:=20posts.controller.spec=20=EC=9E=91?= =?UTF-8?q?=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BackEnd/src/posts/mocks/mocks.ts | 3 +- BackEnd/src/posts/posts.controller.spec.ts | 90 ++++++++++++++++++++++ BackEnd/src/posts/posts.controller.ts | 16 ++-- BackEnd/src/posts/posts.service.spec.ts | 8 +- 4 files changed, 103 insertions(+), 14 deletions(-) create mode 100644 BackEnd/src/posts/posts.controller.spec.ts diff --git a/BackEnd/src/posts/mocks/mocks.ts b/BackEnd/src/posts/mocks/mocks.ts index 159074f0..22d456bb 100644 --- a/BackEnd/src/posts/mocks/mocks.ts +++ b/BackEnd/src/posts/mocks/mocks.ts @@ -4,7 +4,6 @@ import { CreatePostDto } from "../dto/create-post.dto"; import { PostDto, PostsPaginateResDto } from "../dto/get-posts-response.dto"; import { PaginatePostDto } from "../dto/paginate-post.dto"; import { UpdatePostDto } from "../dto/update-post.dto"; -import { Post } from "../entities/posts.entity"; export const postInfo: CreatePostDto = { content: 'test content', @@ -52,7 +51,7 @@ export const post: PostDto = { }; export const updateResult: UpdateResult = { generatedMaps: [], raw: [], affected: 1 }; -export const updatedPaginatePost: PostDto = { +export const updatedPost: PostDto = { id: 5, publicId: profile.publicId, content: updatePostInfo.content, diff --git a/BackEnd/src/posts/posts.controller.spec.ts b/BackEnd/src/posts/posts.controller.spec.ts new file mode 100644 index 00000000..8d49fc8c --- /dev/null +++ b/BackEnd/src/posts/posts.controller.spec.ts @@ -0,0 +1,90 @@ +import { Test, TestingModule } from "@nestjs/testing"; +import { PostsController } from "./posts.controller" +import { PostsService } from "./posts.service"; +import { post, postInfo, posts, profile, query, updatePostInfo, updatedPost } from "./mocks/mocks"; +import { AuthService } from "../auth/auth.service"; +import { ProfilesService } from "../profiles/profiles.service"; + +describe('postsController', () => { + let controller: PostsController; + let service: PostsService; + + beforeEach(async () => { + const mockService = () => ({ + createPost: jest.fn(), + paginatePosts: jest.fn(), + findOneById: jest.fn(), + paginateUserPosts: jest.fn(), + updatePost: jest.fn(), + deletePost: jest.fn(), + }) + + const module: TestingModule = await Test.createTestingModule({ + providers: [ + PostsController, + { + provide: PostsService, + useValue: mockService(), + }, + { + provide: AuthService, + useValue: {} + }, + { + provide: ProfilesService, + useValue: {} + } + ] + }).compile(); + controller = module.get<PostsController>(PostsController); + service = module.get<PostsService>((PostsService)); + }); + + describe('createPost', () => { + it('유저가 입력한 정보로 post 생성', async () => { + jest.spyOn(service, 'createPost').mockResolvedValue(post); + const result = await controller.createPost(postInfo, profile); + expect(result).toEqual(post); + }) + }) + + describe('getPosts', () => { + it('홈에 보여줄 게시글 요청', async () => { + jest.spyOn(service, 'paginatePosts').mockResolvedValue(posts); + const result = await controller.getPosts(query); + expect(result).toEqual(posts); + }) + }) + + describe('getPostById', () => { + it('유저가 id의 해당하는 게시글을 요청', async () => { + jest.spyOn(service, 'findOneById').mockResolvedValue(post); + const result = await controller.getPostById(post.id); + expect(result).toEqual(post); + }) + }) + + describe('getUserPosts', () => { + it('특정 유저의 게시글 요청', async () => { + jest.spyOn(service, 'paginateUserPosts').mockResolvedValue(posts); + const result = await controller.getUserPosts(profile.publicId, query); + expect(result).toEqual(posts); + }) + }) + + describe('getMyPosts', () => { + it('내 게시글 요청', async () => { + jest.spyOn(service, 'paginateUserPosts').mockResolvedValue(posts); + const result = await controller.getMyPosts(profile, query); + expect(result).toEqual(posts); + }) + }) + + describe('updateMyPost', () => { + it('내 게시글 수정 후 수정되었는지 확인하기', async () => { + jest.spyOn(service, 'updatePost').mockResolvedValue(updatedPost); + const result = await controller.updateMyPost(updatedPost.id, updatePostInfo); + expect(result).toEqual(updatedPost); + }) + }) +}) \ No newline at end of file diff --git a/BackEnd/src/posts/posts.controller.ts b/BackEnd/src/posts/posts.controller.ts index b24d240d..53577486 100644 --- a/BackEnd/src/posts/posts.controller.ts +++ b/BackEnd/src/posts/posts.controller.ts @@ -22,7 +22,7 @@ import { CreatePostDto } from './dto/create-post.dto'; import { Profile } from '../profiles/entities/profiles.entity'; import { ProfileDeco } from '../profiles/decorator/profile.decorator'; import { PaginatePostDto } from './dto/paginate-post.dto'; -import { GetPostsResponseDto } from './dto/get-posts-response.dto'; +import { GetPostsResponseDto, PostDto, PostsPaginateResDto } from './dto/get-posts-response.dto'; import { GetPostResponseDto } from './dto/get-create-update-post-response.dto'; import { UpdatePostDto } from './dto/update-post.dto'; import { DeletePostResponseDto } from './dto/delete-post-response.dto'; @@ -40,21 +40,21 @@ export class PostsController { async createPost( @Body() body: CreatePostDto, @ProfileDeco() profile: Profile, - ) { - return await this.postsService.createPost(body, profile); + ): Promise<PostDto> { + return this.postsService.createPost(body, profile); } @Get() @ApiOperation({ summary: '게시글 가져오기' }) @ApiCreatedResponse({ type: GetPostsResponseDto }) - async getPosts(@Query() query: PaginatePostDto) { + async getPosts(@Query() query: PaginatePostDto): Promise<PostsPaginateResDto> { return this.postsService.paginatePosts(query); } @Get(':id') @ApiOperation({ summary: '특정 게시글 가져오기' }) @ApiCreatedResponse({ type: GetPostResponseDto }) - async getPostById(@Param('id', ParseIntPipe) id: number) { + async getPostById(@Param('id', ParseIntPipe) id: number): Promise<PostDto> { return this.postsService.findOneById(id); } @@ -64,7 +64,7 @@ export class PostsController { async getUserPosts( @Param('publicId') publicId: string, @Query() query: PaginatePostDto, - ) { + ): Promise<PostsPaginateResDto> { return this.postsService.paginateUserPosts(publicId, query); } @@ -75,7 +75,7 @@ export class PostsController { async getMyPosts( @ProfileDeco() profile: Profile, @Query() query: PaginatePostDto, - ) { + ): Promise<PostsPaginateResDto> { return this.postsService.paginateUserPosts(profile.publicId, query); } @@ -86,7 +86,7 @@ export class PostsController { async updateMyPost( @Param('id', ParseIntPipe) id: number, @Body() body: UpdatePostDto, - ) { + ): Promise<PostDto> { return this.postsService.updatePost(id, body); } diff --git a/BackEnd/src/posts/posts.service.spec.ts b/BackEnd/src/posts/posts.service.spec.ts index 2c61d8f8..7492ae8e 100644 --- a/BackEnd/src/posts/posts.service.spec.ts +++ b/BackEnd/src/posts/posts.service.spec.ts @@ -8,7 +8,7 @@ import { Repository } from 'typeorm'; import { Record } from '../records/entities/records.entity'; import { Profile } from '../profiles/entities/profiles.entity'; import { ExistPostException, NotFoundPostException } from './exceptions/posts.exception'; -import { post, postInfo, posts, profile, query, updatePostInfo, updateResult, updatedPaginatePost } from './mocks/mocks'; +import { post, postInfo, posts, profile, query, updatePostInfo, updateResult, updatedPost } from './mocks/mocks'; describe('postsService', () => { let service: PostsService; @@ -120,10 +120,10 @@ describe('postsService', () => { it('update된 post 반환', async () => { jest.spyOn(service, 'findOneById').mockResolvedValue(post); jest.spyOn(repository, 'update').mockResolvedValue(updateResult); - jest.spyOn(service, 'findOneById').mockResolvedValue(updatedPaginatePost); + jest.spyOn(service, 'findOneById').mockResolvedValue(updatedPost); - const result = await service.updatePost(updatedPaginatePost.id, updatePostInfo); - expect(result).toEqual(updatedPaginatePost); + const result = await service.updatePost(updatedPost.id, updatePostInfo); + expect(result).toEqual(updatedPost); }) }) }); From 7e26fa12b015541a81cf10273efffeea03e59a6e Mon Sep 17 00:00:00 2001 From: jeong-yong-shin <jeong-yong-shin@Sjy-MacBookProM1.local> Date: Thu, 7 Dec 2023 13:10:49 +0900 Subject: [PATCH 3/3] =?UTF-8?q?chore:=20format=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BackEnd/src/posts/dto/paginate-post.dto.ts | 2 +- BackEnd/src/posts/mocks/mocks.ts | 224 ++++++++-------- BackEnd/src/posts/posts.controller.spec.ts | 171 ++++++------ BackEnd/src/posts/posts.controller.ts | 10 +- BackEnd/src/posts/posts.service.spec.ts | 245 ++++++++++-------- BackEnd/src/posts/posts.service.ts | 15 +- .../workouts/dto/workout-response.dto.spec.ts | 36 +-- .../workouts/entities/workout.entity.spec.ts | 51 ++-- .../src/workouts/entities/workout.entity.ts | 2 +- 9 files changed, 401 insertions(+), 355 deletions(-) diff --git a/BackEnd/src/posts/dto/paginate-post.dto.ts b/BackEnd/src/posts/dto/paginate-post.dto.ts index afab11ec..771db40c 100644 --- a/BackEnd/src/posts/dto/paginate-post.dto.ts +++ b/BackEnd/src/posts/dto/paginate-post.dto.ts @@ -1,3 +1,3 @@ -import {BasePaginationDto} from "../../common/dto/base-pagination.dto"; +import { BasePaginationDto } from '../../common/dto/base-pagination.dto'; export class PaginatePostDto extends BasePaginationDto {} diff --git a/BackEnd/src/posts/mocks/mocks.ts b/BackEnd/src/posts/mocks/mocks.ts index 22d456bb..024f63ec 100644 --- a/BackEnd/src/posts/mocks/mocks.ts +++ b/BackEnd/src/posts/mocks/mocks.ts @@ -1,129 +1,133 @@ -import { UpdateResult } from "typeorm"; -import { Profile } from "../../profiles/entities/profiles.entity"; -import { CreatePostDto } from "../dto/create-post.dto"; -import { PostDto, PostsPaginateResDto } from "../dto/get-posts-response.dto"; -import { PaginatePostDto } from "../dto/paginate-post.dto"; -import { UpdatePostDto } from "../dto/update-post.dto"; +import { UpdateResult } from 'typeorm'; +import { Profile } from '../../profiles/entities/profiles.entity'; +import { CreatePostDto } from '../dto/create-post.dto'; +import { PostDto, PostsPaginateResDto } from '../dto/get-posts-response.dto'; +import { PaginatePostDto } from '../dto/paginate-post.dto'; +import { UpdatePostDto } from '../dto/update-post.dto'; export const postInfo: CreatePostDto = { - content: 'test content', - postUrl: 'naver.com', - recordId: 1, + content: 'test content', + postUrl: 'naver.com', + recordId: 1, }; -export const updatePostInfo: UpdatePostDto = { - content: 'update content', - postUrl: 'google.com', -} +export const updatePostInfo: UpdatePostDto = { + content: 'update content', + postUrl: 'google.com', +}; export const profile = { - publicId: 'XVZXC-ASFSA123-ASFSF', - nickname: 'testNickname' -} as Profile + publicId: 'XVZXC-ASFSA123-ASFSF', + nickname: 'testNickname', +} as Profile; export const query: PaginatePostDto = { - where__id__less_then: 7, - order__createdAt: 'DESC', - take: 5, -} + where__id__less_then: 7, + order__createdAt: 'DESC', + take: 5, +}; export const post: PostDto = { - id: 5, - publicId: profile.publicId, - content: postInfo.content, - like: null, - createdAt: new Date("2023-12-04T05:14:15.879Z"), - updatedAt: new Date("2023-12-04T05:14:15.879Z"), - deletedAt: null, - postUrl: postInfo.postUrl, - record: { - id: postInfo.recordId, - workoutTime: 6000000, - distance: 100000, - calorie: 360, - avgHeartRate: 60, - minHeartRate: 120, - maxHeartRate: 180 - }, - profile: { - nickname: profile.nickname - } + id: 5, + publicId: profile.publicId, + content: postInfo.content, + like: null, + createdAt: new Date('2023-12-04T05:14:15.879Z'), + updatedAt: new Date('2023-12-04T05:14:15.879Z'), + deletedAt: null, + postUrl: postInfo.postUrl, + record: { + id: postInfo.recordId, + workoutTime: 6000000, + distance: 100000, + calorie: 360, + avgHeartRate: 60, + minHeartRate: 120, + maxHeartRate: 180, + }, + profile: { + nickname: profile.nickname, + }, +}; +export const updateResult: UpdateResult = { + generatedMaps: [], + raw: [], + affected: 1, }; -export const updateResult: UpdateResult = { generatedMaps: [], raw: [], affected: 1 }; export const updatedPost: PostDto = { - id: 5, - publicId: profile.publicId, - content: updatePostInfo.content, - like: null, - createdAt: new Date("2023-12-04T05:14:15.879Z"), - updatedAt: new Date("2023-12-05T05:14:15.879Z"), - deletedAt: null, - postUrl: updatePostInfo.postUrl, - record: { - id: postInfo.recordId, + id: 5, + publicId: profile.publicId, + content: updatePostInfo.content, + like: null, + createdAt: new Date('2023-12-04T05:14:15.879Z'), + updatedAt: new Date('2023-12-05T05:14:15.879Z'), + deletedAt: null, + postUrl: updatePostInfo.postUrl, + record: { + id: postInfo.recordId, + workoutTime: 6000000, + distance: 100000, + calorie: 360, + avgHeartRate: 60, + minHeartRate: 120, + maxHeartRate: 180, + }, + profile: { + nickname: profile.nickname, + }, +}; + +export const posts: PostsPaginateResDto = { + items: [ + { + 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 + maxHeartRate: 180, + }, + profile: { + nickname: profile.nickname, + }, }, - profile: { - nickname: profile.nickname - } -} - -export const posts: PostsPaginateResDto = { - items: [ - { - 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, - } - } - ], - metaData: { - lastItemId: 5, - isLastCursor: true, - count: 2 - } -} \ No newline at end of file + { + 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, + }, + }, + ], + metaData: { + lastItemId: 5, + isLastCursor: true, + count: 2, + }, +}; diff --git a/BackEnd/src/posts/posts.controller.spec.ts b/BackEnd/src/posts/posts.controller.spec.ts index 8d49fc8c..b4d1f4e0 100644 --- a/BackEnd/src/posts/posts.controller.spec.ts +++ b/BackEnd/src/posts/posts.controller.spec.ts @@ -1,90 +1,101 @@ -import { Test, TestingModule } from "@nestjs/testing"; -import { PostsController } from "./posts.controller" -import { PostsService } from "./posts.service"; -import { post, postInfo, posts, profile, query, updatePostInfo, updatedPost } from "./mocks/mocks"; -import { AuthService } from "../auth/auth.service"; -import { ProfilesService } from "../profiles/profiles.service"; +import { Test, TestingModule } from '@nestjs/testing'; +import { PostsController } from './posts.controller'; +import { PostsService } from './posts.service'; +import { + post, + postInfo, + posts, + profile, + query, + updatePostInfo, + updatedPost, +} from './mocks/mocks'; +import { AuthService } from '../auth/auth.service'; +import { ProfilesService } from '../profiles/profiles.service'; describe('postsController', () => { - let controller: PostsController; - let service: PostsService; + let controller: PostsController; + let service: PostsService; - beforeEach(async () => { - const mockService = () => ({ - createPost: jest.fn(), - paginatePosts: jest.fn(), - findOneById: jest.fn(), - paginateUserPosts: jest.fn(), - updatePost: jest.fn(), - deletePost: jest.fn(), - }) - - const module: TestingModule = await Test.createTestingModule({ - providers: [ - PostsController, - { - provide: PostsService, - useValue: mockService(), - }, - { - provide: AuthService, - useValue: {} - }, - { - provide: ProfilesService, - useValue: {} - } - ] - }).compile(); - controller = module.get<PostsController>(PostsController); - service = module.get<PostsService>((PostsService)); + beforeEach(async () => { + const mockService = () => ({ + createPost: jest.fn(), + paginatePosts: jest.fn(), + findOneById: jest.fn(), + paginateUserPosts: jest.fn(), + updatePost: jest.fn(), + deletePost: jest.fn(), }); - describe('createPost', () => { - it('유저가 입력한 정보로 post 생성', async () => { - jest.spyOn(service, 'createPost').mockResolvedValue(post); - const result = await controller.createPost(postInfo, profile); - expect(result).toEqual(post); - }) - }) + const module: TestingModule = await Test.createTestingModule({ + providers: [ + PostsController, + { + provide: PostsService, + useValue: mockService(), + }, + { + provide: AuthService, + useValue: {}, + }, + { + provide: ProfilesService, + useValue: {}, + }, + ], + }).compile(); + controller = module.get<PostsController>(PostsController); + service = module.get<PostsService>(PostsService); + }); + + describe('createPost', () => { + it('유저가 입력한 정보로 post 생성', async () => { + jest.spyOn(service, 'createPost').mockResolvedValue(post); + const result = await controller.createPost(postInfo, profile); + expect(result).toEqual(post); + }); + }); - describe('getPosts', () => { - it('홈에 보여줄 게시글 요청', async () => { - jest.spyOn(service, 'paginatePosts').mockResolvedValue(posts); - const result = await controller.getPosts(query); - expect(result).toEqual(posts); - }) - }) + describe('getPosts', () => { + it('홈에 보여줄 게시글 요청', async () => { + jest.spyOn(service, 'paginatePosts').mockResolvedValue(posts); + const result = await controller.getPosts(query); + expect(result).toEqual(posts); + }); + }); - describe('getPostById', () => { - it('유저가 id의 해당하는 게시글을 요청', async () => { - jest.spyOn(service, 'findOneById').mockResolvedValue(post); - const result = await controller.getPostById(post.id); - expect(result).toEqual(post); - }) - }) + describe('getPostById', () => { + it('유저가 id의 해당하는 게시글을 요청', async () => { + jest.spyOn(service, 'findOneById').mockResolvedValue(post); + const result = await controller.getPostById(post.id); + expect(result).toEqual(post); + }); + }); - describe('getUserPosts', () => { - it('특정 유저의 게시글 요청', async () => { - jest.spyOn(service, 'paginateUserPosts').mockResolvedValue(posts); - const result = await controller.getUserPosts(profile.publicId, query); - expect(result).toEqual(posts); - }) - }) + describe('getUserPosts', () => { + it('특정 유저의 게시글 요청', async () => { + jest.spyOn(service, 'paginateUserPosts').mockResolvedValue(posts); + const result = await controller.getUserPosts(profile.publicId, query); + expect(result).toEqual(posts); + }); + }); - describe('getMyPosts', () => { - it('내 게시글 요청', async () => { - jest.spyOn(service, 'paginateUserPosts').mockResolvedValue(posts); - const result = await controller.getMyPosts(profile, query); - expect(result).toEqual(posts); - }) - }) + describe('getMyPosts', () => { + it('내 게시글 요청', async () => { + jest.spyOn(service, 'paginateUserPosts').mockResolvedValue(posts); + const result = await controller.getMyPosts(profile, query); + expect(result).toEqual(posts); + }); + }); - describe('updateMyPost', () => { - it('내 게시글 수정 후 수정되었는지 확인하기', async () => { - jest.spyOn(service, 'updatePost').mockResolvedValue(updatedPost); - const result = await controller.updateMyPost(updatedPost.id, updatePostInfo); - expect(result).toEqual(updatedPost); - }) - }) -}) \ No newline at end of file + describe('updateMyPost', () => { + it('내 게시글 수정 후 수정되었는지 확인하기', async () => { + jest.spyOn(service, 'updatePost').mockResolvedValue(updatedPost); + const result = await controller.updateMyPost( + updatedPost.id, + updatePostInfo, + ); + expect(result).toEqual(updatedPost); + }); + }); +}); diff --git a/BackEnd/src/posts/posts.controller.ts b/BackEnd/src/posts/posts.controller.ts index 53577486..05d81ec7 100644 --- a/BackEnd/src/posts/posts.controller.ts +++ b/BackEnd/src/posts/posts.controller.ts @@ -22,7 +22,11 @@ import { CreatePostDto } from './dto/create-post.dto'; import { Profile } from '../profiles/entities/profiles.entity'; import { ProfileDeco } from '../profiles/decorator/profile.decorator'; import { PaginatePostDto } from './dto/paginate-post.dto'; -import { GetPostsResponseDto, PostDto, PostsPaginateResDto } from './dto/get-posts-response.dto'; +import { + GetPostsResponseDto, + PostDto, + PostsPaginateResDto, +} from './dto/get-posts-response.dto'; import { GetPostResponseDto } from './dto/get-create-update-post-response.dto'; import { UpdatePostDto } from './dto/update-post.dto'; import { DeletePostResponseDto } from './dto/delete-post-response.dto'; @@ -47,7 +51,9 @@ export class PostsController { @Get() @ApiOperation({ summary: '게시글 가져오기' }) @ApiCreatedResponse({ type: GetPostsResponseDto }) - async getPosts(@Query() query: PaginatePostDto): Promise<PostsPaginateResDto> { + async getPosts( + @Query() query: PaginatePostDto, + ): Promise<PostsPaginateResDto> { return this.postsService.paginatePosts(query); } diff --git a/BackEnd/src/posts/posts.service.spec.ts b/BackEnd/src/posts/posts.service.spec.ts index 7492ae8e..48555071 100644 --- a/BackEnd/src/posts/posts.service.spec.ts +++ b/BackEnd/src/posts/posts.service.spec.ts @@ -7,124 +7,141 @@ import { CommonService } from '../common/common.service'; import { Repository } from 'typeorm'; import { Record } from '../records/entities/records.entity'; import { Profile } from '../profiles/entities/profiles.entity'; -import { ExistPostException, NotFoundPostException } from './exceptions/posts.exception'; -import { post, postInfo, posts, profile, query, updatePostInfo, updateResult, updatedPost } from './mocks/mocks'; +import { + ExistPostException, + NotFoundPostException, +} from './exceptions/posts.exception'; +import { + post, + postInfo, + posts, + profile, + query, + updatePostInfo, + updateResult, + updatedPost, +} from './mocks/mocks'; describe('postsService', () => { - let service: PostsService; - let repository: Repository<Post> - let recordsService: RecordsService; - let commonService: CommonService; - - const mockQueryBuilder = { - getOne: jest.fn() - } as any - - beforeEach(async () => { - const mockRepository = () => ({ - findOneBy: jest.fn(), - save: jest.fn(), - update: jest.fn(), - softDelete: jest.fn(), - }); - - const mockRecordsService = () => ({ - findById: jest.fn(), - updateIsPostedTrue: jest.fn(), - }); - - const mockCommonService = () => ({ - paginate: jest.fn(), - makeQueryBuilder: jest.fn(), - }); - - const module: TestingModule = await Test.createTestingModule({ - providers: [ - PostsService, - { - provide: getRepositoryToken(Post), - useValue: mockRepository(), - }, - { - provide: RecordsService, - useValue: mockRecordsService(), - }, - { - provide: CommonService, - useValue: mockCommonService(), - } - ], - }).compile(); - service = module.get<PostsService>(PostsService); - repository = module.get<Repository<Post>>(getRepositoryToken(Post)); - recordsService = module.get<RecordsService>(RecordsService); - commonService = module.get<CommonService>(CommonService); + let service: PostsService; + let repository: Repository<Post>; + let recordsService: RecordsService; + let commonService: CommonService; + + const mockQueryBuilder = { + getOne: jest.fn(), + } as any; + + beforeEach(async () => { + const mockRepository = () => ({ + findOneBy: jest.fn(), + save: jest.fn(), + update: jest.fn(), + softDelete: jest.fn(), }); - describe('createPost', () => { - it('이미 record의 post가 존재한다면 throw new ExistPostException 발생', async () => { - const record = { isPosted: true } as Record; - jest.spyOn(recordsService, 'findById').mockResolvedValue(record); - await expect( - service.createPost(postInfo, profile as Profile), - ).rejects.toThrow(ExistPostException); - }); - - it('record의 post가 존재하지 않는다면 post 생성', async () => { - jest.spyOn(recordsService, 'findById').mockResolvedValue({ isPosted: false } as Record); - jest.spyOn(repository, 'save').mockResolvedValue({ id: 5 } as Post); - jest.spyOn(service, 'findOneById').mockResolvedValue(post) - - const result = await service.createPost(postInfo, profile); - expect(result).toEqual(post); - }) - }) - - describe('findOneById', () => { - it("post가 존재하지 않는다면 NotFoundPostException 발생", async () => { - jest.spyOn(commonService, 'makeQueryBuilder').mockImplementation(() => mockQueryBuilder); - jest.spyOn(mockQueryBuilder, 'getOne').mockResolvedValue(null); - await expect( - service.findOneById(5) - ).rejects.toThrow(NotFoundPostException); - }) - - it("post가 존재한다면 post를 반환", async () => { - jest.spyOn(commonService, 'makeQueryBuilder').mockImplementation(() => mockQueryBuilder); - jest.spyOn(mockQueryBuilder, 'getOne').mockResolvedValue(post); - - const result = await service.findOneById(post.id); - expect(result).toEqual(post); - }) + const mockRecordsService = () => ({ + findById: jest.fn(), + updateIsPostedTrue: jest.fn(), }); - describe('paginatePosts', () => { - it('posts 반환', async () => { - jest.spyOn(commonService, 'paginate').mockResolvedValue(posts); - - const result = await service.paginatePosts(query); - expect(result).toEqual(posts); - }) - }) - - describe('paginateUserPosts', () => { - it('user의 posts 반환', async () => { - jest.spyOn(commonService, 'paginate').mockResolvedValue(posts); - - const result = await service.paginateUserPosts(profile.publicId, query); - expect(result).toEqual(posts); - }) - }) - - describe('updatePost', () => { - it('update된 post 반환', async () => { - jest.spyOn(service, 'findOneById').mockResolvedValue(post); - jest.spyOn(repository, 'update').mockResolvedValue(updateResult); - jest.spyOn(service, 'findOneById').mockResolvedValue(updatedPost); - - const result = await service.updatePost(updatedPost.id, updatePostInfo); - expect(result).toEqual(updatedPost); - }) - }) -}); + const mockCommonService = () => ({ + paginate: jest.fn(), + makeQueryBuilder: jest.fn(), + }); + + const module: TestingModule = await Test.createTestingModule({ + providers: [ + PostsService, + { + provide: getRepositoryToken(Post), + useValue: mockRepository(), + }, + { + provide: RecordsService, + useValue: mockRecordsService(), + }, + { + provide: CommonService, + useValue: mockCommonService(), + }, + ], + }).compile(); + service = module.get<PostsService>(PostsService); + repository = module.get<Repository<Post>>(getRepositoryToken(Post)); + recordsService = module.get<RecordsService>(RecordsService); + commonService = module.get<CommonService>(CommonService); + }); + + describe('createPost', () => { + it('이미 record의 post가 존재한다면 throw new ExistPostException 발생', async () => { + const record = { isPosted: true } as Record; + jest.spyOn(recordsService, 'findById').mockResolvedValue(record); + await expect( + service.createPost(postInfo, profile as Profile), + ).rejects.toThrow(ExistPostException); + }); + + it('record의 post가 존재하지 않는다면 post 생성', async () => { + jest + .spyOn(recordsService, 'findById') + .mockResolvedValue({ isPosted: false } as Record); + jest.spyOn(repository, 'save').mockResolvedValue({ id: 5 } as Post); + jest.spyOn(service, 'findOneById').mockResolvedValue(post); + + const result = await service.createPost(postInfo, profile); + expect(result).toEqual(post); + }); + }); + + describe('findOneById', () => { + it('post가 존재하지 않는다면 NotFoundPostException 발생', async () => { + jest + .spyOn(commonService, 'makeQueryBuilder') + .mockImplementation(() => mockQueryBuilder); + jest.spyOn(mockQueryBuilder, 'getOne').mockResolvedValue(null); + await expect(service.findOneById(5)).rejects.toThrow( + NotFoundPostException, + ); + }); + + it('post가 존재한다면 post를 반환', async () => { + jest + .spyOn(commonService, 'makeQueryBuilder') + .mockImplementation(() => mockQueryBuilder); + jest.spyOn(mockQueryBuilder, 'getOne').mockResolvedValue(post); + + const result = await service.findOneById(post.id); + expect(result).toEqual(post); + }); + }); + + describe('paginatePosts', () => { + it('posts 반환', async () => { + jest.spyOn(commonService, 'paginate').mockResolvedValue(posts); + + const result = await service.paginatePosts(query); + expect(result).toEqual(posts); + }); + }); + + describe('paginateUserPosts', () => { + it('user의 posts 반환', async () => { + jest.spyOn(commonService, 'paginate').mockResolvedValue(posts); + const result = await service.paginateUserPosts(profile.publicId, query); + expect(result).toEqual(posts); + }); + }); + + describe('updatePost', () => { + it('update된 post 반환', async () => { + jest.spyOn(service, 'findOneById').mockResolvedValue(post); + jest.spyOn(repository, 'update').mockResolvedValue(updateResult); + jest.spyOn(service, 'findOneById').mockResolvedValue(updatedPost); + + const result = await service.updatePost(updatedPost.id, updatePostInfo); + expect(result).toEqual(updatedPost); + }); + }); +}); diff --git a/BackEnd/src/posts/posts.service.ts b/BackEnd/src/posts/posts.service.ts index 3ab5e89f..93484788 100644 --- a/BackEnd/src/posts/posts.service.ts +++ b/BackEnd/src/posts/posts.service.ts @@ -24,7 +24,10 @@ export class PostsService { private readonly commonService: CommonService, ) {} - async createPost(postInfo: CreatePostDto, profile: Profile): Promise<PostDto> { + async createPost( + postInfo: CreatePostDto, + profile: Profile, + ): Promise<PostDto> { const record = await this.recordService.findById(postInfo.recordId); if (record.isPosted) { throw new ExistPostException(); @@ -61,7 +64,10 @@ export class PostsService { return post; } - async paginateUserPosts(publicId: string, query: PaginatePostDto): Promise<PostsPaginateResDto> { + async paginateUserPosts( + publicId: string, + query: PaginatePostDto, + ): Promise<PostsPaginateResDto> { return await this.commonService.paginate<Post>( query, this.postsRepository, @@ -70,7 +76,10 @@ export class PostsService { ); } - async updatePost(id: number, updatePostInfo: UpdatePostDto): Promise<PostDto> { + async updatePost( + id: number, + updatePostInfo: UpdatePostDto, + ): Promise<PostDto> { await this.findOneById(id); await this.postsRepository.update(id, updatePostInfo); return await this.findOneById(id); diff --git a/BackEnd/src/workouts/dto/workout-response.dto.spec.ts b/BackEnd/src/workouts/dto/workout-response.dto.spec.ts index bf012f43..21ac641f 100644 --- a/BackEnd/src/workouts/dto/workout-response.dto.spec.ts +++ b/BackEnd/src/workouts/dto/workout-response.dto.spec.ts @@ -2,24 +2,24 @@ import { WorkoutResDto } from './workout-response.dto'; import { Workout } from '../entities/workout.entity'; describe('WorkoutResDto', () => { - it('WorkoutResDto는 배열을 리턴하며 내부에는 id, name, icon이 존재한다.', () => { - const workout1 = new Workout(); - workout1.id = 1; - workout1.name = '달리기'; - workout1.icon = 'running.svg'; + it('WorkoutResDto는 배열을 리턴하며 내부에는 id, name, icon이 존재한다.', () => { + const workout1 = new Workout(); + workout1.id = 1; + workout1.name = '달리기'; + workout1.icon = 'running.svg'; - const workout2 = new Workout(); - workout2.id = 2; - workout2.name = '수영'; - workout2.icon = 'swimming.svg'; + const workout2 = new Workout(); + workout2.id = 2; + workout2.name = '수영'; + workout2.icon = 'swimming.svg'; - const dto = new WorkoutResDto(); - dto.data = [workout1, workout2]; + const dto = new WorkoutResDto(); + dto.data = [workout1, workout2]; - expect(dto.data).toBeInstanceOf(Array); - expect(dto.data[0]).toBeInstanceOf(Workout); - expect(dto.data[0].name).toBe('달리기'); - expect(dto.data[1].name).toBe('수영'); - expect(dto.data[1].icon).toBe('swimming.svg'); - }); -}); \ No newline at end of file + expect(dto.data).toBeInstanceOf(Array); + expect(dto.data[0]).toBeInstanceOf(Workout); + expect(dto.data[0].name).toBe('달리기'); + expect(dto.data[1].name).toBe('수영'); + expect(dto.data[1].icon).toBe('swimming.svg'); + }); +}); diff --git a/BackEnd/src/workouts/entities/workout.entity.spec.ts b/BackEnd/src/workouts/entities/workout.entity.spec.ts index 9112fb7c..00fb7800 100644 --- a/BackEnd/src/workouts/entities/workout.entity.spec.ts +++ b/BackEnd/src/workouts/entities/workout.entity.spec.ts @@ -2,34 +2,33 @@ import { Workout } from './workout.entity'; import { validate } from 'class-validator'; describe('Workout Entity', () => { - it('Workout에서 id, name, icon이 엔티티에 정의한대로 올바르면, 에러가 발생하지 않는다.', async () => { - const workout = new Workout(); - workout.id = 1; - workout.name = '달리기'; - workout.icon = 'running'; + it('Workout에서 id, name, icon이 엔티티에 정의한대로 올바르면, 에러가 발생하지 않는다.', async () => { + const workout = new Workout(); + workout.id = 1; + workout.name = '달리기'; + workout.icon = 'running'; - const errors = await validate(workout); - expect(errors).toHaveLength(0); - }); + const errors = await validate(workout); + expect(errors).toHaveLength(0); + }); - it('Workout에서 name이 공백이면, 에러가 발생한다.', async () => { - const workout = new Workout(); - workout.id = 1; - workout.name = ''; - workout.icon = 'running'; + it('Workout에서 name이 공백이면, 에러가 발생한다.', async () => { + const workout = new Workout(); + workout.id = 1; + workout.name = ''; + workout.icon = 'running'; - const errors = await validate(workout); - expect(errors).toHaveLength(1); - }); + const errors = await validate(workout); + expect(errors).toHaveLength(1); + }); - it('Workout에서 icon이 공백이면, 에러가 발생한다.', async () => { - const workout = new Workout(); - workout.id = 1; - workout.name = '달리기'; - workout.icon = ''; + it('Workout에서 icon이 공백이면, 에러가 발생한다.', async () => { + const workout = new Workout(); + workout.id = 1; + workout.name = '달리기'; + workout.icon = ''; - const errors = await validate(workout); - expect(errors).toHaveLength(1); - }); - -}); \ No newline at end of file + const errors = await validate(workout); + expect(errors).toHaveLength(1); + }); +}); diff --git a/BackEnd/src/workouts/entities/workout.entity.ts b/BackEnd/src/workouts/entities/workout.entity.ts index 23aecdfd..36b4fad4 100644 --- a/BackEnd/src/workouts/entities/workout.entity.ts +++ b/BackEnd/src/workouts/entities/workout.entity.ts @@ -1,7 +1,7 @@ import { ApiProperty } from '@nestjs/swagger'; import { Column, Entity, OneToMany, PrimaryGeneratedColumn } from 'typeorm'; import { Record } from '../../records/entities/records.entity'; -import {IsNotEmpty, IsString} from 'class-validator'; +import { IsNotEmpty, IsString } from 'class-validator'; @Entity() export class Workout {