-
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.
Merge pull request #317 from tmddus2/feature/240306-test-code
Refactor(#318): UserService ํ ์คํธ์ฝ๋ ์ถ๊ฐ
- Loading branch information
Showing
9 changed files
with
171 additions
and
66 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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 |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import { Test } from '@nestjs/testing'; | ||
import { UserService } from './user.service'; | ||
import { getModelToken } from '@nestjs/mongoose'; | ||
import { User } from './user.schema'; | ||
import { Model, Types } from 'mongoose'; | ||
import { NotFoundException } from '@nestjs/common'; | ||
|
||
|
||
describe('UserModule', () => { | ||
let service: UserService; | ||
let model: Model<User>; | ||
beforeAll(async () => { | ||
const module = await Test.createTestingModule({ | ||
providers: [UserService, | ||
{ | ||
provide: getModelToken(User.name), | ||
useValue: Model | ||
} | ||
] | ||
}).compile(); | ||
|
||
service = module.get<UserService>(UserService); | ||
model = module.get<Model<User>>(getModelToken(User.name)); | ||
}); | ||
|
||
it('should have UserService', () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
|
||
describe('findOneByEmail', () => { | ||
const id = new Types.ObjectId(); | ||
const email = "[email protected]"; | ||
|
||
it('should throw NotFoundException', async () => { | ||
jest.spyOn(model, 'findOne').mockResolvedValue(null); | ||
await expect(service.findOneByEmail(email)).rejects.toThrow(NotFoundException); | ||
}) | ||
|
||
it('should find user', async () => { | ||
jest.spyOn(model, 'findOne').mockResolvedValue({ | ||
_id: id, | ||
email: email | ||
}); | ||
const user = await service.findOneByEmail(email); | ||
expect(user._id).toEqual(id); | ||
expect(user.email).toEqual(email); | ||
}) | ||
}); | ||
|
||
describe('updateUsername', () => { | ||
const id = new Types.ObjectId(); | ||
const email = "[email protected]"; | ||
const username = "testname"; | ||
|
||
it('should throw NotFoundException', async () => { | ||
jest.spyOn(model, 'findOneAndUpdate').mockResolvedValue(null); | ||
await expect(service.updateUsername(email, username)).rejects.toThrow(NotFoundException); | ||
}) | ||
|
||
it('should update username', async () => { | ||
jest.spyOn(model, 'findOneAndUpdate').mockResolvedValue({ | ||
_id: id, | ||
email: email, | ||
username: username | ||
}); | ||
|
||
const user = await service.updateUsername(email, username); | ||
expect(user.username).toEqual(username); | ||
}) | ||
}); | ||
|
||
|
||
describe('findLectureList', () => { | ||
const email = "[email protected]"; | ||
|
||
it('should throw NotFoundException', async () => { | ||
jest.spyOn(model, 'findOne').mockResolvedValue(null); | ||
await expect(service.findLectureList(email)).rejects.toThrow(NotFoundException); | ||
}) | ||
}); | ||
|
||
describe('updateLectureList', () => { | ||
const email = '[email protected]'; | ||
const id = new Types.ObjectId(); | ||
|
||
it('should throw NotFoundException', async () => { | ||
jest.spyOn(model, 'findOneAndUpdate').mockResolvedValue(null); | ||
await expect(service.updateLectureList(email, id)).rejects.toThrow(NotFoundException); | ||
}) | ||
|
||
it('should call with', async () => { | ||
const findOneAndUpdateMock = jest.spyOn(model, 'findOneAndUpdate').mockResolvedValue({}); | ||
|
||
await service.updateLectureList(email, id); | ||
expect(findOneAndUpdateMock).toHaveBeenCalledWith( | ||
{ email: email }, | ||
{ $push: { lecture_id: id } }, | ||
{ new: true } | ||
); | ||
}) | ||
}); | ||
}); |
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,44 +1,50 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { Injectable, NotFoundException } from '@nestjs/common'; | ||
import { InjectModel } from '@nestjs/mongoose'; | ||
import { User, UserDocument } from './user.schema'; | ||
import { Model } from 'mongoose'; | ||
import { LectureService } from 'src/lecture/lecture.service'; | ||
import { EnterCode } from 'src/lecture/schema/lecture-code.schema'; | ||
import { Model, Types } from 'mongoose'; | ||
|
||
@Injectable() | ||
export class UserService { | ||
constructor( | ||
@InjectModel(User.name) private userModel: Model<User>, | ||
private lectureService: LectureService | ||
@InjectModel(User.name) private userModel: Model<User> | ||
) {} | ||
|
||
async findOneByEmail(email: string): Promise<UserDocument> { | ||
return await this.userModel.findOne({ email: email }); | ||
const user = await this.userModel.findOne({ email: email }); | ||
if (!user) { | ||
throw new NotFoundException('user not found'); | ||
} | ||
return user; | ||
} | ||
|
||
async updateUsername(email: string, username: string) { | ||
return await this.userModel.findOneAndUpdate({ email: email }, { username: username }, { new: true }); | ||
const user = await this.userModel.findOneAndUpdate({ email: email }, { username: username }, { new: true }); | ||
if (!user) { | ||
throw new NotFoundException('user not found'); | ||
} | ||
return user; | ||
} | ||
|
||
async updateLecture(email: string, enterCode: EnterCode) { | ||
const lecture = await this.lectureService.findLectureInfo(enterCode); | ||
return await this.userModel.findOneAndUpdate( | ||
{ email: email }, | ||
{ $push: { lecture_id: lecture.id } }, | ||
{ new: true } | ||
); | ||
async findLectureList(email: string) { | ||
let user = await this.findOneByEmail(email); | ||
user = await user.populate({ | ||
path: 'lecture_id', | ||
select: '-__v', | ||
match: { is_end: true }, | ||
populate: { path: 'presenter_id', select: '-_id username' } | ||
}); | ||
return user.lecture_id; | ||
} | ||
|
||
async findLectureList(email: string) { | ||
return ( | ||
await ( | ||
await this.findOneByEmail(email) | ||
).populate({ | ||
path: 'lecture_id', | ||
select: '-__v', | ||
match: { is_end: true }, | ||
populate: { path: 'presenter_id', select: '-_id username' } | ||
}) | ||
).lecture_id; | ||
async updateLectureList(email: string, id: Types.ObjectId) { | ||
const user = await this.userModel.findOneAndUpdate( | ||
{ email: email }, | ||
{ $push: { lecture_id: id } }, | ||
{ new: true } | ||
); | ||
if (!user) { | ||
throw new NotFoundException('user not found'); | ||
} | ||
return user; | ||
} | ||
} |