Skip to content

Commit

Permalink
[GWL-211] Matches API 테스트 코드 작성 및 리팩토링 (#270)
Browse files Browse the repository at this point in the history
* chore: matches/random

* chore: create-match.dto 테스트 코드 작성

* test: random-match.dto 테스트 코드 작성

* chore: format 적용

* refactor: 레디스 expired 추가

* test: 서비스 코드에 expired 추가

* test: 올바르지 못한 코드 제거

* chore: matches 스웨거 작성

* chore: expired 타임 조정
  • Loading branch information
wonholim authored Dec 7, 2023
1 parent c14c49d commit e7b667b
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 13 deletions.
2 changes: 1 addition & 1 deletion BackEnd/src/images/images.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { ImagesService } from './images.service';
import { MAX_IMAGE_SIZE } from './constant/images.constant';
import { ValidateFilesPipe } from './pipe/validate-files.pip';
import { ImageRequestDto, ImageResponseDto } from './dto/images.response';
import { FilesInterceptor } from "@nestjs/platform-express";
import { FilesInterceptor } from '@nestjs/platform-express';

@ApiTags('이미지 업로드 API')
@Controller('api/v1/images')
Expand Down
7 changes: 6 additions & 1 deletion BackEnd/src/images/intercepters/wetri-files.interceptor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { Injectable, ExecutionContext, CallHandler, Logger } from '@nestjs/common';
import {
Injectable,
ExecutionContext,
CallHandler,
Logger,
} from '@nestjs/common';
import { FilesInterceptor } from '@nestjs/platform-express';
import { Observable, throwError } from 'rxjs';
import { catchError } from 'rxjs/operators';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ export const ALONE_USER = 1;
export const WAITING_20_TIME = 20;
export const MATCHING_DELAY = 15;
export const UTC_REMOVE_TIME = 600;
export const MATCHES_API_TIME_OUT = 10000;
23 changes: 23 additions & 0 deletions BackEnd/src/live-workouts/matches/dto/create-match.dto.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { plainToInstance } from 'class-transformer';
import { validate } from 'class-validator';
import { CreateMatchDto } from './create-match.dto';

describe('CreateMatchDto', () => {
it('workoutId가 숫자일 때, 에러는 발생하지 않는다.', async () => {
const dto = plainToInstance(CreateMatchDto, { workoutId: 1 });
const errors = await validate(dto);
expect(errors.length).toBe(0);
});

it('workoutId가 문자로 들어왔을 때, 에러가 발생한다.', async () => {
const dto = plainToInstance(CreateMatchDto, { workoutId: '닌자' });
const errors = await validate(dto);
expect(errors.length).toBeGreaterThan(0);
});

it('만약 dto가 비어 있다면, 에러가 발생한다.', async () => {
const dto = plainToInstance(CreateMatchDto, {});
const errors = await validate(dto);
expect(errors.length).toBeGreaterThan(0);
});
});
44 changes: 44 additions & 0 deletions BackEnd/src/live-workouts/matches/dto/random-match.dto.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { plainToInstance } from 'class-transformer';
import { validate } from 'class-validator';
import {
RandomMatchDto,
RandomMatch,
RandomMatchResDto,
randomNoMatchingResDto,
} from './random-match.dto';

describe('RandomMatchDto', () => {
it('workoutId와 waitingTime를 둘다 가질 경우에는 오류가 발생하지 않는다.', async () => {
const dto = plainToInstance(RandomMatchDto, {
workoutId: 1,
waitingTime: 60,
});
const errors = await validate(dto);
expect(errors.length).toBe(0);
});

it('workoutId와 waitingTime를 둘 중 한개라도 숫자가 아닌 경우에는 오류가 발생한다.', async () => {
const dto = plainToInstance(RandomMatchDto, {
workoutId: '진',
waitingTime: '육백만초',
});
const errors = await validate(dto);
expect(errors.length).toBeGreaterThan(0);
});

it('만약 값이 비어있다면, 오류가 발생한다.', async () => {
const dto = plainToInstance(RandomMatchDto, {});
const errors = await validate(dto);
expect(errors.length).toBeGreaterThan(0);
});
});

describe('RandomMatchResDto', () => {
it('만약 매칭이 잡힌 경우, true를 리턴한다.', async () => {
const resDto = plainToInstance(RandomMatchResDto, {
data: { matched: true },
});
const errors = await validate(resDto);
expect(errors.length).toBe(0);
});
});
20 changes: 14 additions & 6 deletions BackEnd/src/live-workouts/matches/dto/random-match.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,22 @@ export class RandomMatch {
@ApiProperty({
example: [
{
nickname: 'nickname',
publicId: 'profileImage',
etc: '그 외 나머지 모든 컬럼',
nickname: '닌자',
publicId: 'aaa-bbb-ccc',
profileImage: 'https://cdn.com/asdasd.png',
birthdate: '1999-01-01',
createdAt: '2021-01-01 00:00:00',
updatedAt: '2021-01-01 00:00:00',
gender: 'male'
},
{
nickname: 'nickname',
publicId: 'profileImage',
etc: '그 외 나머지 모든 컬럼',
nickname: '닌자2',
publicId: 'aaa-bbb-111',
profileImage: 'https://cdn.com/asdasd.png',
birthdate: '1999-01-01',
createdAt: '2021-01-01 00:00:00',
updatedAt: '2021-01-01 00:00:00',
gender: 'male'
},
],
})
Expand Down
2 changes: 1 addition & 1 deletion BackEnd/src/live-workouts/matches/matches.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export class MatchesController {
description: '매칭이 안되었을 경우',
schema: randomNoMatchingResDto(),
})
@Get('random')
@Post('random')
isRandomMatched(
@ProfileDeco() profile: Profile,
@Body() randomMatchDto: RandomMatchDto,
Expand Down
3 changes: 2 additions & 1 deletion BackEnd/src/live-workouts/matches/matches.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Test, TestingModule } from '@nestjs/testing';
import { MatchesService } from './matches.service';
import { Profile } from '../../profiles/entities/profiles.entity';
import { RandomMatchDto } from './dto/random-match.dto';
import {MATCHES_API_TIME_OUT} from "./constant/matches.constant";

describe('MatchesService', () => {
let service: MatchesService;
Expand Down Expand Up @@ -65,7 +66,7 @@ describe('MatchesService', () => {

it('매칭을 시작하면 redis에는 직렬화된 profile를 matching:1 key에 value로 저장한다.', async () => {
await service.startMatch(profile, createMatchDto);
expect(rpush).toHaveBeenCalledWith(`matching:1`, JSON.stringify(profile));
expect(rpush).toHaveBeenCalledWith(`matching:1`, JSON.stringify(profile), 'EX', MATCHES_API_TIME_OUT);
});

it('매칭을 취소하면, maching:1에 있는 value는 삭제가 되어야 한다.', async () => {
Expand Down
17 changes: 14 additions & 3 deletions BackEnd/src/live-workouts/matches/matches.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
ALONE_USER,
MATCHING_DELAY,
UTC_REMOVE_TIME,
MATCHES_API_TIME_OUT,
} from './constant/matches.constant';

@Injectable()
Expand All @@ -32,7 +33,12 @@ export class MatchesService {
this.logger.log(`startMatch: ${publicId} ${workoutId}`);

await this.initMatch(profile, workoutId);
await this.redis.rpush(`matching:${workoutId}`, JSON.stringify(profile));
await this.redis.rpush(
`matching:${workoutId}`,
JSON.stringify(profile),
'EX',
MATCHES_API_TIME_OUT,
);
}

async cancelMatch(
Expand Down Expand Up @@ -102,10 +108,15 @@ export class MatchesService {

const multi = this.redis.multi();
for (const { publicId } of profiles) {
multi.set(`userMatch:${publicId}`, roomId);
multi.set(`userMatch:${publicId}`, roomId, 'EX', MATCHES_API_TIME_OUT);
}

multi.set(`matchProfiles:${roomId}`, JSON.stringify(profiles));
multi.set(
`matchProfiles:${roomId}`,
JSON.stringify(profiles),
'EX',
MATCHES_API_TIME_OUT,
);
multi.set(
`matchStartTime:${roomId}`,
JSON.stringify(liveWorkoutStartTimeUTC),
Expand Down

0 comments on commit e7b667b

Please sign in to comment.