Skip to content

Commit

Permalink
[GWL-212] Workouts API 테스트 코드 작성 및 리팩토링 (#248)
Browse files Browse the repository at this point in the history
* feat: workouts에 3000, 3100 번 커스텀 에러 생성

* chore: response dto에서 Schema Swagger 삭제 및 수정

* test: workouts의 테스트 코드 작성, 만약, 오류가 발생했을때 에러를 잘 처리하는지의 테스트 코드를 작성했습니다.

* chore: format 적용

* chore: 절대 경로에서 상대 경로로 변경

* chore: 절대 경로에서 상대 경로로 변경

* chore: 절대 경로에서 상대 경로로 변경

* test: workouts controller 테스트 코드 작성

* refactor: workout entity에 @isnotempty 문장 추가

* test: findOne 목 추가

* test: entity 테스트 코드 작성

* test: response dto 테스트 코드 작성
wonholim authored Dec 6, 2023
1 parent 1328824 commit d3533ad
Showing 39 changed files with 240 additions and 96 deletions.
2 changes: 1 addition & 1 deletion BackEnd/src/admin/admin.controller.ts
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ import { Body, Controller, Post } from '@nestjs/common';
import { AdminService } from './admin.service';
import { LoginDto } from './dto/login.dto';
import { ApiBody, ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
import { SignupResDto } from 'src/auth/dto/auth-response.dto';
import { SignupResDto } from '../auth/dto/auth-response.dto';

@ApiTags('Admin')
@Controller('api/v1/admin')
2 changes: 1 addition & 1 deletion BackEnd/src/admin/admin.module.ts
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@ import { AdminService } from './admin.service';
import { AdminController } from './admin.controller';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Admin } from './entities/admin.entity';
import { AuthModule } from 'src/auth/auth.module';
import { AuthModule } from '../auth/auth.module';

@Module({
imports: [TypeOrmModule.forFeature([Admin]), AuthModule],
2 changes: 1 addition & 1 deletion BackEnd/src/admin/admin.service.ts
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ import { Injectable } from '@nestjs/common';
import { Admin } from './entities/admin.entity';
import { Repository } from 'typeorm';
import { InjectRepository } from '@nestjs/typeorm';
import { AuthService } from 'src/auth/auth.service';
import { AuthService } from '../auth/auth.service';
import * as bcrypt from 'bcrypt';
import {
IncorrectPasswordException,
2 changes: 1 addition & 1 deletion BackEnd/src/auth/auth-apple.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Injectable } from '@nestjs/common';
import * as jwt from 'jsonwebtoken';
import { jwksApple } from 'src/config/jwksApple.config';
import { jwksApple } from '../config/jwksApple.config';
import { VerificationFailedIdentityToken } from './exceptions/auth.exception';

@Injectable()
2 changes: 1 addition & 1 deletion BackEnd/src/auth/auth.module.ts
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@ import { AuthAppleService } from './auth-apple.service';

@Module({
imports: [JwtModule.register({}), UsersModule, ProfilesModule],
exports: [AuthService],
exports: [AuthService, AuthAppleService],
controllers: [AuthController],
providers: [AuthService, AuthAppleService],
})
2 changes: 1 addition & 1 deletion BackEnd/src/auth/auth.service.ts
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@ import {
NotRefreshTokenException,
} from './exceptions/auth.exception';
import * as process from 'process';
import { WetriWebSocket } from 'src/live-workouts/events/types/custom-websocket.type';
import { WetriWebSocket } from '../live-workouts/events/types/custom-websocket.type';
import { GetuserByUserIdAndProViderDto } from './dto/getUserByUserIdAndProvider.dto';

@Injectable()
2 changes: 1 addition & 1 deletion BackEnd/src/auth/dto/auth-response.dto.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ApiProperty, PickType } from '@nestjs/swagger';
import { SuccessResDto } from 'src/common/dto/SuccessRes.dto';
import { SuccessResDto } from '../../common/dto/SuccessRes.dto';

class Token {
@ApiProperty({
2 changes: 1 addition & 1 deletion BackEnd/src/auth/dto/getUserByUserIdAndProvider.dto.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PickType } from '@nestjs/swagger';
import { User } from 'src/users/entities/users.entity';
import { User } from '../../users/entities/users.entity';

export class GetuserByUserIdAndProViderDto extends PickType(User, [
'userId',
2 changes: 1 addition & 1 deletion BackEnd/src/auth/dto/signinRedirectRes.dto.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsString } from 'class-validator';
import { SuccessResDto } from 'src/common/dto/SuccessRes.dto';
import { SuccessResDto } from '../../common/dto/SuccessRes.dto';

class SigninFirstRes {
@ApiProperty({
2 changes: 1 addition & 1 deletion BackEnd/src/common/redis.module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Module, Global } from '@nestjs/common';
import Redis from 'ioredis';
import { redisConfig } from 'src/config/redis.config';
import { redisConfig } from '../config/redis.config';
import { CommonModule } from './common.module';

@Global()
2 changes: 1 addition & 1 deletion BackEnd/src/live-workouts/matches/matches.module.ts
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@ import { MatchesController } from './matches.controller';
import { JwtModule } from '@nestjs/jwt';
import { UsersModule } from '../../users/users.module';
import { ProfilesModule } from '../../profiles/profiles.module';
import { AuthModule } from 'src/auth/auth.module';
import { AuthModule } from '../../auth/auth.module';

@Module({
imports: [JwtModule.register({}), UsersModule, ProfilesModule, AuthModule],
2 changes: 1 addition & 1 deletion BackEnd/src/posts/dto/delete-post-response.dto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ApiProperty } from '@nestjs/swagger';
import { SuccessResDto } from 'src/common/dto/SuccessRes.dto';
import { SuccessResDto } from '../../common/dto/SuccessRes.dto';

export class DeletePostResponseDto extends SuccessResDto {}
2 changes: 1 addition & 1 deletion BackEnd/src/posts/dto/get-post-response.dto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SuccessResDto } from 'src/common/dto/SuccessRes.dto';
import { SuccessResDto } from '../../common/dto/SuccessRes.dto';
import { ApiProperty } from '@nestjs/swagger';
import { PostDto } from './get-posts-response.dto';

2 changes: 1 addition & 1 deletion BackEnd/src/posts/dto/get-posts-response.dto.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ApiProperty, PickType } from '@nestjs/swagger';
import { SuccessResDto } from 'src/common/dto/SuccessRes.dto';
import { SuccessResDto } from '../../common/dto/SuccessRes.dto';
import { Post } from '../entities/posts.entity';

export class PostDto extends PickType(Post, [
2 changes: 1 addition & 1 deletion BackEnd/src/posts/dto/paginate-post.dto.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { BasePaginationDto } from 'src/common/dto/base-pagination.dto';
import {BasePaginationDto} from "../../common/dto/base-pagination.dto";

export class PaginatePostDto extends BasePaginationDto {}
6 changes: 3 additions & 3 deletions BackEnd/src/posts/posts.controller.ts
Original file line number Diff line number Diff line change
@@ -12,16 +12,16 @@ import {
UseGuards,
} from '@nestjs/common';
import { PostsService } from './posts.service';
import { AccessTokenGuard } from 'src/auth/guard/bearerToken.guard';
import { AccessTokenGuard } from '../auth/guard/bearerToken.guard';
import {
ApiBody,
ApiCreatedResponse,
ApiOperation,
ApiTags,
} from '@nestjs/swagger';
import { CreatePostDto } from './dto/create-post.dto';
import { Profile } from 'src/profiles/entities/profiles.entity';
import { ProfileDeco } from 'src/profiles/decorator/profile.decorator';
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 { GetPostResponseDto } from './dto/get-post-response.dto';
8 changes: 4 additions & 4 deletions BackEnd/src/posts/posts.module.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Module } from '@nestjs/common';
import { PostsService } from './posts.service';
import { PostsController } from './posts.controller';
import { AuthModule } from 'src/auth/auth.module';
import { ProfilesModule } from 'src/profiles/profiles.module';
import { AuthModule } from '../auth/auth.module';
import { ProfilesModule } from '../profiles/profiles.module';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Post } from './entities/posts.entity';
import { RecordsModule } from 'src/records/records.module';
import { CommonModule } from 'src/common/common.module';
import { RecordsModule } from '../records/records.module';
import { CommonModule } from '../common/common.module';

@Module({
imports: [
2 changes: 1 addition & 1 deletion BackEnd/src/posts/posts.service.ts
Original file line number Diff line number Diff line change
@@ -27,7 +27,7 @@ export class PostsService {
if (record.isPosted) {
throw new ExistPostException();
}
this.recordService.updateIsPostedTrue(record);
await this.recordService.updateIsPostedTrue(record);
return await this.postsRepository.save({
publicId: profile.publicId,
content: postInfo.content,
6 changes: 3 additions & 3 deletions BackEnd/src/profiles/dto/get-profile-posts-response.dto.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ApiProperty, PickType } from '@nestjs/swagger';
import { SuccessResDto } from 'src/common/dto/SuccessRes.dto';
import { MetaDataDto } from 'src/posts/dto/get-posts-response.dto';
import { Post } from 'src/posts/entities/posts.entity';
import { SuccessResDto } from '../../common/dto/SuccessRes.dto';
import { MetaDataDto } from '../../posts/dto/get-posts-response.dto';
import { Post } from '../../posts/entities/posts.entity';

export class ProfilePostDto extends PickType(Post, ['id', 'postUrl']) {}

2 changes: 1 addition & 1 deletion BackEnd/src/profiles/dto/get-profile-response.dto.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ApiProperty, PickType } from '@nestjs/swagger';
import { Profile } from '../entities/profiles.entity';
import { SuccessResDto } from 'src/common/dto/SuccessRes.dto';
import { SuccessResDto } from '../../common/dto/SuccessRes.dto';

class GetProfileDto extends PickType(Profile, [
'nickname',
2 changes: 1 addition & 1 deletion BackEnd/src/profiles/dto/paginate-profile-post.dto.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { BasePaginationDto } from 'src/common/dto/base-pagination.dto';
import { BasePaginationDto } from '../../common/dto/base-pagination.dto';

export class PaginateProfilePostDto extends BasePaginationDto {}
2 changes: 1 addition & 1 deletion BackEnd/src/profiles/profiles.module.ts
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@ import { JwtModule } from '@nestjs/jwt';
import { UsersService } from '../users/users.service';
import { AuthAppleService } from '../auth/auth-apple.service';
import { User } from '../users/entities/users.entity';
import { CommonModule } from 'src/common/common.module';
import { CommonModule } from '../common/common.module';
@Module({
imports: [
TypeOrmModule.forFeature([Profile, Post, User]),
6 changes: 3 additions & 3 deletions BackEnd/src/profiles/profiles.service.ts
Original file line number Diff line number Diff line change
@@ -5,8 +5,8 @@ import { InjectRepository } from '@nestjs/typeorm';
import { UpdateProfileDto } from './dto/update-profile.dto';
import { Post } from '../posts/entities/posts.entity';
import { PaginateProfilePostDto } from './dto/paginate-profile-post.dto';
import { CommonService } from 'src/common/common.service';
import { NicknameDuplicateException } from 'src/auth/exceptions/auth.exception';
import { CommonService } from '../common/common.service';
import { NicknameDuplicateException } from '../auth/exceptions/auth.exception';

@Injectable()
export class ProfilesService {
@@ -24,7 +24,7 @@ export class ProfilesService {
}
await this.profilesRepository.update({ publicId }, updateProfileDto);
return this.getProfile(publicId);
}
}

async deleteProfile(publicId: string) {
return this.profilesRepository.delete({ publicId });
2 changes: 1 addition & 1 deletion BackEnd/src/records/dto/record-response.dto.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ApiProperty, PickType } from '@nestjs/swagger';
import { SuccessResDto } from 'src/common/dto/SuccessRes.dto';
import { SuccessResDto } from '../../common/dto/SuccessRes.dto';
import { Record } from '../entities/records.entity';

class GetRecord extends PickType(Record, [
6 changes: 3 additions & 3 deletions BackEnd/src/records/records.controller.ts
Original file line number Diff line number Diff line change
@@ -9,9 +9,9 @@ import {
} from '@nestjs/common';
import { RecordsService } from './records.service';
import { CreateExerciseLogDto } from './dto/create-exerciseLog.dto';
import { AccessTokenGuard } from 'src/auth/guard/bearerToken.guard';
import { ProfileDeco } from 'src/profiles/decorator/profile.decorator';
import { Profile } from 'src/profiles/entities/profiles.entity';
import { AccessTokenGuard } from '../auth/guard/bearerToken.guard';
import { ProfileDeco } from '../profiles/decorator/profile.decorator';
import { Profile } from '../profiles/entities/profiles.entity';
import {
ApiBody,
ApiCreatedResponse,
6 changes: 3 additions & 3 deletions BackEnd/src/records/records.module.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Module } from '@nestjs/common';
import { RecordsService } from './records.service';
import { RecordsController } from './records.controller';
import { AuthModule } from 'src/auth/auth.module';
import { ProfilesModule } from 'src/profiles/profiles.module';
import { AuthModule } from '../auth/auth.module';
import { ProfilesModule } from '../profiles/profiles.module';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Record } from './entities/records.entity';
import { WorkoutsModule } from 'src/workouts/workouts.module';
import { WorkoutsModule } from '../workouts/workouts.module';

@Module({
imports: [
8 changes: 4 additions & 4 deletions BackEnd/src/users/users.controller.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Controller, Delete, Get, UseGuards } from '@nestjs/common';
import { UsersService } from './users.service';
import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
import { AccessTokenGuard } from 'src/auth/guard/bearerToken.guard';
import { ProfileDeco } from 'src/profiles/decorator/profile.decorator';
import { Profile } from 'src/profiles/entities/profiles.entity';
import { SuccessResDto } from 'src/common/dto/SuccessRes.dto';
import { AccessTokenGuard } from '../auth/guard/bearerToken.guard';
import { ProfileDeco } from '../profiles/decorator/profile.decorator';
import { Profile } from '../profiles/entities/profiles.entity';
import { SuccessResDto } from '../common/dto/SuccessRes.dto';

@ApiTags('유저 API')
@Controller('api/v1/users')
6 changes: 3 additions & 3 deletions BackEnd/src/users/users.module.ts
Original file line number Diff line number Diff line change
@@ -3,9 +3,9 @@ import { UsersService } from './users.service';
import { UsersController } from './users.controller';
import { TypeOrmModule } from '@nestjs/typeorm';
import { User } from './entities/users.entity';
import { AuthModule } from 'src/auth/auth.module';
import { ProfilesModule } from 'src/profiles/profiles.module';
import { PostsModule } from 'src/posts/posts.module';
import { AuthModule } from '../auth/auth.module';
import { ProfilesModule } from '../profiles/profiles.module';
import { PostsModule } from '../posts/posts.module';

@Module({
imports: [
4 changes: 2 additions & 2 deletions BackEnd/src/users/users.service.ts
Original file line number Diff line number Diff line change
@@ -5,8 +5,8 @@ import { User } from './entities/users.entity';
import { Repository } from 'typeorm';
import { InjectRepository } from '@nestjs/typeorm';
import { SignupDto } from '../auth/dto/signup.dto';
import { ProfilesService } from 'src/profiles/profiles.service';
import { Profile } from 'src/profiles/entities/profiles.entity';
import { ProfilesService } from '../profiles/profiles.service';
import { Profile } from '../profiles/entities/profiles.entity';
import { NotFoundUserException } from './exceptions/users.exception';

@Injectable()
25 changes: 25 additions & 0 deletions BackEnd/src/workouts/dto/workout-response.dto.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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';

const workout2 = new Workout();
workout2.id = 2;
workout2.name = '수영';
workout2.icon = 'swimming.svg';

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');
});
});
30 changes: 2 additions & 28 deletions BackEnd/src/workouts/dto/workout-response.dto.ts
Original file line number Diff line number Diff line change
@@ -3,32 +3,6 @@ import { SuccessResDto } from '../../common/dto/SuccessRes.dto';
import { Workout } from '../entities/workout.entity';

export class WorkoutResDto extends SuccessResDto {
@ApiProperty({ type: () => PickType(Workout, ['id', 'name']) })
data: Pick<Workout, 'id' | 'name'>;
@ApiProperty({ type: () => [Workout] })
data: Workout[];
}

export const WorkoutResDtoSwagger = () => {
return {
example: {
code: null,
errorMessage: null,
data: [
{
id: 1,
name: '달리기',
icon: 'figure.outdoor.a',
},
{
id: 2,
name: '수영',
icon: 'figure.outdoor.b',
},
{
id: 3,
name: '사이클',
icon: 'figure.outdoor.c',
},
],
},
};
};
35 changes: 35 additions & 0 deletions BackEnd/src/workouts/entities/workout.entity.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
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';

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';

const errors = await validate(workout);
expect(errors).toHaveLength(1);
});

it('Workout에서 icon이 공백이면, 에러가 발생한다.', async () => {
const workout = new Workout();
workout.id = 1;
workout.name = '달리기';
workout.icon = '';

const errors = await validate(workout);
expect(errors).toHaveLength(1);
});

});
Loading

0 comments on commit d3533ad

Please sign in to comment.