-
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.
- k6를 통해 지연시간 및 처리량 테스트를 위한 환경 구축 - 여러 사용자를 위한 목유저 생성 스크립트 작성 - 목유저를 DB에 작성하기 위한 ADMIN API 작성
- Loading branch information
1 parent
06b1532
commit dfe7625
Showing
19 changed files
with
384 additions
and
30 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
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,20 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { AdminController } from './admin.controller'; | ||
import { AdminService } from './admin.service'; | ||
|
||
describe('AdminController', () => { | ||
let controller: AdminController; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
controllers: [AdminController], | ||
providers: [AdminService], | ||
}).compile(); | ||
|
||
controller = module.get<AdminController>(AdminController); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(controller).toBeDefined(); | ||
}); | ||
}); |
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,47 @@ | ||
import { Body, Controller, Post } from '@nestjs/common'; | ||
import { ApiExcludeEndpoint } from '@nestjs/swagger'; | ||
import { AuthService } from 'src/auth/auth.service'; | ||
import { MATES_MAXIMUM } from 'src/common/const/service-var.const'; | ||
import { MatesService } from 'src/mates/mates.service'; | ||
import { UsersService } from 'src/users/users.service'; | ||
|
||
@Controller('admin') | ||
export class AdminController { | ||
constructor( | ||
private readonly authService: AuthService, | ||
private readonly matesService: MatesService, | ||
private readonly usersService: UsersService, | ||
) {} | ||
|
||
/** | ||
* mockUser를 생성하기 위한 Admin전용 함수입니다. | ||
*/ | ||
@ApiExcludeEndpoint() | ||
@Post('create-mock-user') | ||
async createMockUsers(@Body('emails') emails: Array<{ email: string }>) { | ||
const createdUsers = []; | ||
for (const { email } of emails) { | ||
const { access_token } = await this.authService.loginWithGoogle({ | ||
email, | ||
auth_type: 'google', | ||
}); | ||
const { nickname } = this.authService.verifyToken(access_token); | ||
createdUsers.push({ email, access_token, nickname }); | ||
} | ||
|
||
for (let idx = 0; idx < createdUsers.length; idx++) { | ||
const email = createdUsers[idx].email; | ||
const me = await this.usersService.findUserByEmail(email); | ||
for (let i = 1; i <= MATES_MAXIMUM; i++) { | ||
const friendIdx = (idx + i) % createdUsers.length; | ||
const friendNickname = createdUsers[friendIdx].nickname; | ||
try { | ||
await this.matesService.addMate(me, friendNickname); | ||
} catch (e) { | ||
console.log(e); | ||
} | ||
} | ||
} | ||
return createdUsers; | ||
} | ||
} |
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,11 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { AdminController } from './admin.controller'; | ||
import { AuthModule } from 'src/auth/auth.module'; | ||
import { UsersModule } from 'src/users/users.module'; | ||
import { MatesModule } from 'src/mates/mates.module'; | ||
|
||
@Module({ | ||
imports: [AuthModule, UsersModule, MatesModule], | ||
controllers: [AdminController], | ||
}) | ||
export class AdminModule {} |
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
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,2 @@ | ||
export const MATES_MAXIMUM = 10; | ||
export const CATEGORIES_MAXIMUM = 10; |
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 |
---|---|---|
@@ -1,10 +1,16 @@ | ||
import { Injectable, NestMiddleware } from '@nestjs/common'; | ||
import { NextFunction } from 'express'; | ||
import { v4 as uuid } from 'uuid'; | ||
|
||
@Injectable() | ||
export class LoggingMiddleware implements NestMiddleware { | ||
use(req: Request & { now: number }, res: Response, next: NextFunction) { | ||
use( | ||
req: Request & { now: number; id: string }, | ||
res: Response, | ||
next: NextFunction, | ||
) { | ||
req.now = Date.now(); | ||
req.id = uuid(); | ||
next(); | ||
} | ||
} |
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
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
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 |
---|---|---|
@@ -1,18 +1,114 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { StudyLogsService } from './study-logs.service'; | ||
import { StudyLogs } from './study-logs.entity'; | ||
import { Repository } from 'typeorm'; | ||
import { getRepositoryToken } from '@nestjs/typeorm'; | ||
import { UsersModel } from 'src/users/entity/users.entity'; | ||
import { RedisService } from 'src/common/redis.service'; | ||
import { BadRequestException } from '@nestjs/common'; | ||
|
||
describe('StudyLogsService', () => { | ||
let service: StudyLogsService; | ||
let repository: Repository<StudyLogs>; | ||
let usersRepository: Repository<UsersModel>; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [StudyLogsService], | ||
providers: [ | ||
StudyLogsService, | ||
{ | ||
provide: getRepositoryToken(StudyLogs), | ||
useClass: Repository, | ||
}, | ||
{ | ||
provide: getRepositoryToken(UsersModel), | ||
useClass: Repository, | ||
}, | ||
{ | ||
provide: RedisService, | ||
useValue: {}, // RedisService를 모킹 (필요한 메서드 제공) | ||
}, | ||
], | ||
}).compile(); | ||
|
||
service = module.get<StudyLogsService>(StudyLogsService); | ||
repository = module.get<Repository<StudyLogs>>( | ||
getRepositoryToken(StudyLogs), | ||
); | ||
usersRepository = module.get<Repository<UsersModel>>( | ||
getRepositoryToken(UsersModel), | ||
); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
|
||
describe('.calculateTotalTimes()', () => { | ||
const userId = 1; | ||
const startDate = '2023-01-01'; | ||
const endDate = '2023-01-07'; | ||
it('유효하지 않은 파라미터에 대해 오류를 발생시켜야 한다', async () => { | ||
expect(service.calculateTotalTimes(null, null, null)).rejects.toThrow( | ||
BadRequestException, | ||
); | ||
expect(service.calculateTotalTimes(1, null, null)).rejects.toThrow( | ||
BadRequestException, | ||
); | ||
expect( | ||
service.calculateTotalTimes(null, startDate, endDate), | ||
).rejects.toThrow(BadRequestException); | ||
}); | ||
it('정상적인 범위에서 올바른 결과값을 반환해야 한다.', async () => { | ||
const expectedOutput = [3600, 7200, 0, 0, 3600, 7200, 1800]; | ||
|
||
jest.spyOn(repository, 'query').mockResolvedValueOnce([ | ||
{ date: '2023-01-01', daily_sum: '3600' }, | ||
{ date: '2023-01-02', daily_sum: '7200' }, | ||
{ date: '2023-01-05', daily_sum: '3600' }, | ||
{ date: '2023-01-06', daily_sum: '7200' }, | ||
{ date: '2023-01-07', daily_sum: '1800' }, | ||
]); | ||
|
||
const result = await service.calculateTotalTimes( | ||
userId, | ||
startDate, | ||
endDate, | ||
); | ||
|
||
expect(result).toEqual(expectedOutput); | ||
}); | ||
it('일치하는 데이터가 없을 때, 0으로 채워진 배열을 반환해야 한다.', async () => { | ||
const expectedOutput = [0, 0, 0, 0, 0, 0, 0]; | ||
jest.spyOn(repository, 'query').mockResolvedValueOnce([]); | ||
|
||
const result = await service.calculateTotalTimes( | ||
userId, | ||
startDate, | ||
endDate, | ||
); | ||
|
||
expect(result).toEqual(expectedOutput); | ||
}); | ||
it('단일 날짜에 대해 올바른 학습 시간을 반환해야 한다.', async () => { | ||
const expectedOutput = [3600]; | ||
|
||
jest | ||
.spyOn(repository, 'query') | ||
.mockResolvedValueOnce([{ date: '2023-01-01', daily_sum: '3600' }]); | ||
|
||
const result = await service.calculateTotalTimes( | ||
userId, | ||
startDate, | ||
startDate, | ||
); | ||
|
||
expect(result).toEqual(expectedOutput); | ||
}); | ||
it('잘못된 날짜 범위에 대해 오류를 발생시켜야 한다.', async () => { | ||
expect( | ||
service.calculateTotalTimes(userId, endDate, startDate), | ||
).rejects.toThrow(BadRequestException); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.