-
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.
Browse files
Browse the repository at this point in the history
* test: 미작성 테스트 코드 삭제 * refactor: Group, Member repository 생성 * refactor: DB 이름 넣어주면서 살짝 변경 * refactor: Group * refactor: Member * refactor: HTTP Exception + Class validator * fix: member.controller.spec.ts * fix: Database query built from user-controlled sources
- Loading branch information
Showing
29 changed files
with
704 additions
and
308 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,27 +1,60 @@ | ||
import { GroupController } from './group.controller'; | ||
import { GroupService } from './group.service'; | ||
import { HydratedDocument, Model } from 'mongoose'; | ||
import { Group } from './interfaces/group.interface'; | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { DatabaseModule } from '../database/database.module'; | ||
import { groupProvider } from './group.provider'; | ||
import { MongoMemoryServer } from 'mongodb-memory-server'; | ||
import * as process from 'process'; | ||
import { Group } from './entities/group.entity'; | ||
import mongoose from 'mongoose'; | ||
import { CreateGroupDto } from './dto/create-group.dto'; | ||
import { GroupRepository } from './group.repository'; | ||
|
||
describe('GroupController', () => { | ||
let groupController: GroupController; | ||
let groupService: GroupService; | ||
let groupModel: Model<Group>; | ||
let mongoServer: MongoMemoryServer; | ||
|
||
beforeEach(async () => { | ||
groupService = new GroupService(groupModel); | ||
groupController = new GroupController(groupService); | ||
beforeAll(async () => { | ||
mongoServer = await MongoMemoryServer.create(); | ||
process.env.MONGODB_URI = mongoServer.getUri(); | ||
|
||
const module: TestingModule = await Test.createTestingModule({ | ||
imports: [DatabaseModule], | ||
controllers: [GroupController], | ||
providers: [GroupService, ...groupProvider, GroupRepository], | ||
}).compile(); | ||
|
||
groupService = module.get<GroupService>(GroupService); | ||
groupController = module.get<GroupController>(GroupController); | ||
}); | ||
|
||
describe('findAll', () => { | ||
it('should return an array of groups', async () => { | ||
const result: HydratedDocument<any> = [ | ||
{ _id: 1, name: 'Group 1' }, | ||
{ _id: 2, name: 'Group 2' }, | ||
]; | ||
jest.spyOn(groupService, 'findAll').mockImplementation(() => result); | ||
describe('create', () => { | ||
it('should return a group', async () => { | ||
const group: CreateGroupDto = { | ||
name: 'Group 1', | ||
description: 'Group 1 description', | ||
manager: 'user1', | ||
subManagers: null, | ||
members: ['user1', 'user2'], | ||
}; | ||
|
||
const result: Group = await groupService.create(group); | ||
|
||
expect(await groupController.findAll()).toBe(result); | ||
jest.spyOn(groupService, 'create').mockImplementation(async () => result); | ||
|
||
expect(await groupController.create(group)).toBe(result); | ||
expect(result).toHaveProperty('name', group.name); | ||
expect(result).toHaveProperty('description', group.description); | ||
expect(result).toHaveProperty('manager', group.manager); | ||
expect(result).toHaveProperty('subManagers', group.subManagers); | ||
expect(result).toHaveProperty('members', group.members); | ||
}); | ||
}); | ||
|
||
afterAll(async () => { | ||
await mongoose.connection.dropDatabase(); | ||
await mongoose.connection.close(); | ||
await mongoServer.stop(); | ||
}); | ||
}); |
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,39 @@ | ||
import { Inject, Injectable } from '@nestjs/common'; | ||
import { Model } from 'mongoose'; | ||
import { Group } from './interfaces/group.interface'; | ||
import { CreateGroupDto } from './dto/create-group.dto'; | ||
import { UpdateGroupDto } from './dto/update-group.dto'; | ||
|
||
@Injectable() | ||
export class GroupRepository { | ||
constructor( | ||
@Inject('GROUP_MODEL') | ||
private readonly groupModel: Model<Group>, | ||
) {} | ||
|
||
create(createGroupDto: CreateGroupDto): Promise<Group> { | ||
return this.groupModel.create(createGroupDto); | ||
} | ||
|
||
findAll(): Promise<Group[]> { | ||
return this.groupModel.find().exec(); | ||
} | ||
|
||
findOne(id: string): Promise<Group> { | ||
return this.groupModel.findById(id).exec(); | ||
} | ||
|
||
update(id: string, updateGroupDto: UpdateGroupDto): Promise<Group> { | ||
return this.groupModel | ||
.findByIdAndUpdate(id, updateGroupDto, { new: true }) | ||
.exec(); | ||
} | ||
|
||
delete(id: string) { | ||
this.groupModel.findByIdAndDelete(id); | ||
} | ||
|
||
deleteAll() { | ||
this.groupModel.deleteMany({}).exec(); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.