Skip to content

Commit

Permalink
userController e2e test
Browse files Browse the repository at this point in the history
  • Loading branch information
ttkmw committed Jun 18, 2019
1 parent 1bfcd9e commit 3eb4c3e
Show file tree
Hide file tree
Showing 9 changed files with 365 additions and 41 deletions.
12 changes: 12 additions & 0 deletions server/docker-compose.test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version: '3.3'

services:
db:
image: postgres
restart: always
environment:
POSTGRES_USER: test
POSTGRES_DB: hatchout
POSTGRES_PASSWORD: test
ports:
- 5432:5432
20 changes: 11 additions & 9 deletions server/src/app/user/user.service.impl.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import {anything, instance, mock, when} from 'ts-mockito';
import {TestingModule, Test} from '@nestjs/testing';
import {User} from '../../domain/user/user.entity';
import {UserRepository} from '../../port/persistence/repository/user.repository.impl';
import {BadRequestException} from '@nestjs/common';
import {BadRequestException, NotAcceptableException, NotFoundException} from '@nestjs/common';
import {UserDto} from './dto/user.dto';
import {InvalidParameterException} from '../exception/InvalidParameterException';
import {ValidationException} from '../exception/ValidationException';

describe('UserServiceImpl', () => {
const address = 'testAddress';
Expand Down Expand Up @@ -77,7 +79,7 @@ describe('UserServiceImpl', () => {

await expect(service.create(userDto))
.rejects
.toThrowError('address should be defined');
.toThrowError(InvalidParameterException);
});
it('should throw "name should be defined"', async () => {
when(mockRepository.findByAddress(address)).thenReturn(
Expand All @@ -89,7 +91,7 @@ describe('UserServiceImpl', () => {

await expect(service.create(userDto))
.rejects
.toThrowError('name should be defined');
.toThrowError(InvalidParameterException);
});
it('should throw "address is already registered"', async () => {
when(mockRepository.findByAddress(address)).thenReturn(
Expand All @@ -101,7 +103,7 @@ describe('UserServiceImpl', () => {

await expect(service.create(userDto))
.rejects
.toThrowError('address is already registered');
.toThrowError(NotAcceptableException);
});
});
describe('#delete()', () => {
Expand Down Expand Up @@ -140,7 +142,7 @@ describe('UserServiceImpl', () => {

await expect(service.increasePoint(id, amount))
.rejects
.toThrowError('user with the id is not found');
.toThrowError(NotFoundException);
});
});
describe('#decreasePoint()', () => {
Expand All @@ -165,7 +167,7 @@ describe('UserServiceImpl', () => {

await expect(service.decreasePoint(id, validAmount))
.rejects
.toThrowError('user with the id is not found');
.toThrowError(NotFoundException);
});
it('should throw error "can not decrease point"', async () => {
user = new User(address, name, point);
Expand All @@ -174,7 +176,7 @@ describe('UserServiceImpl', () => {

await expect(service.decreasePoint(id, invalidAmount))
.rejects
.toThrowError('can not decrease point');
.toThrowError(ValidationException);
});
});
describe('#increaseLevel()', () => {
Expand All @@ -197,15 +199,15 @@ describe('UserServiceImpl', () => {

await expect(service.increaseLevel(id, validAmount))
.rejects
.toThrowError('user with the id is not found');
.toThrowError(NotFoundException);
});
it('should throw error "can not increase level"', async () => {
when(mockRepository.findById(id)).thenReturn(new Promise((resolve => resolve(user))));
service = new UserServiceImpl(instance(mockRepository));

await expect(service.increaseLevel(id, invalidAmount))
.rejects
.toThrowError('can not increase level');
.toThrowError(ValidationException);
});
});
});
20 changes: 11 additions & 9 deletions server/src/app/user/user.service.impl.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import {BadRequestException, Injectable} from '@nestjs/common';
import {BadRequestException, Injectable, NotAcceptableException, NotFoundException} from '@nestjs/common';
import {UserService} from './user.service';
import {User} from '../../domain/user/user.entity';
import {DeleteResult} from 'typeorm';
import {IUserRepository} from '../../domain/user/user.repository';
import {InjectRepository} from '@nestjs/typeorm';
import {UserDto} from './dto/user.dto';
import {InvalidParameterException} from '../exception/InvalidParameterException';
import {ValidationException} from '../exception/ValidationException';

@Injectable()
export class UserServiceImpl implements UserService {
Expand All @@ -20,14 +22,14 @@ export class UserServiceImpl implements UserService {

async create(userDto: UserDto): Promise<User> {
if (userDto.address === undefined) {
throw new Error('address should be defined');
throw new InvalidParameterException('address should be defined');
}
if (userDto.name === undefined) {
throw new Error('name should be defined');
throw new InvalidParameterException('name should be defined');
}
const userRetrieved = await this.userRepository.findByAddress(userDto.address);
if (userRetrieved !== undefined) {
throw new Error('address is already registered');
throw new NotAcceptableException('address is already registered');
}

const user = new User(userDto.address, userDto.name);
Expand All @@ -42,7 +44,7 @@ export class UserServiceImpl implements UserService {
let user: User;
user = await this.userRepository.findById(id);
if (user === undefined) {
throw new Error('user with the id is not found');
throw new NotFoundException('user with the id is not found');
}

return user.increasePoint(amount);
Expand All @@ -52,13 +54,13 @@ export class UserServiceImpl implements UserService {
let user: User;
user = await this.userRepository.findById(id);
if (user === undefined) {
throw new Error('user with the id is not found');
throw new NotFoundException('user with the id is not found');
}

let errors: Error[];
errors = user.canDecreasePoint(amount);
if (errors.length !== 0) {
throw new Error('can not decrease point');
throw new ValidationException('can not decrease point');
}

return await user.decreasePoint(amount);
Expand All @@ -67,13 +69,13 @@ export class UserServiceImpl implements UserService {
let user: User;
user = await this.userRepository.findById(id);
if (user === undefined) {
throw new Error('user with the id is not found');
throw new NotFoundException('user with the id is not found');
}

let errors: Error[];
errors = user.canIncreaseLevel(amount);
if ( errors.length !== 0) {
throw new Error('can not increase level');
throw new ValidationException('can not increase level');
}

return await user.increaseLevel(amount);
Expand Down
1 change: 1 addition & 0 deletions server/src/config/database/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ export default {
logging: false,
synchronize: true,
entities: ['src/domain/**/*.entity{.ts,.js}'],
dropSchema: false,
};
7 changes: 4 additions & 3 deletions server/src/config/database/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ export default {
type: 'postgres',
host: 'localhost',
port: 5432,
username: 'de-labtory',
password: 'de-labtory',
username: 'test',
password: 'test',
database: 'hatchout',
logging: true,
logging: false,
synchronize: true,
entities: ['src/domain/**/*.entity{.ts,.js}'],
dropSchema: true,
};
2 changes: 1 addition & 1 deletion server/src/domain/user/user.entity.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Entity, Column, PrimaryGeneratedColumn, PrimaryColumn} from 'typeorm';
import {Max, Min, validate} from 'class-validator';
import {Max, Min} from 'class-validator';
import {MAX_LEVEL} from './level';
import {MIN_POINT} from './point';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export class RepositoryConfigService implements TypeOrmOptionsFactory {
password: this.config.password,
database: this.config.database,
synchronize: this.config.synchronize,
dropSchema: this.config.dropSchema,
entities: this.config.entities,
};
}
Expand Down
65 changes: 46 additions & 19 deletions server/src/web/user/user.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import {UserService} from '../../app/user/user.service';
import {UserServiceImpl} from '../../app/user/user.service.impl';
import {User} from '../../domain/user/user.entity';
import {UserDto} from '../../app/user/dto/user.dto';
import {BadRequestException} from '@nestjs/common';
import {BadRequestException, NotAcceptableException, NotFoundException} from '@nestjs/common';
import {ValidationException} from '../../app/exception/ValidationException';
import {InvalidParameterException} from '../../app/exception/InvalidParameterException';

describe('User Controller', () => {
const mockUserService = mock(UserServiceImpl);
Expand Down Expand Up @@ -70,14 +72,23 @@ describe('User Controller', () => {
expect(await controller.create(userDto)).toBe(user);

});
it('should return undefined', async () => {
it('should throw InvalidParameterException', async () => {
userDto = new UserDto();
when(mockUserService.create(userDto)).thenThrow(new InvalidParameterException('address should be defined'));
controller = new UserController(instance(mockUserService));

await expect(controller.create(userDto))
.rejects
.toThrowError(InvalidParameterException);
});
it('should throw NotAcceptableException', async () => {
userDto = new UserDto();
when(mockUserService.create(userDto)).thenThrow(new Error('some errors'));
when(mockUserService.create(userDto)).thenThrow(new NotAcceptableException('address is already registered'));
controller = new UserController(instance(mockUserService));

await expect(controller.create(userDto))
.rejects
.toThrowError('some errors');
.toThrowError(NotAcceptableException);
});
});
describe('#delete()', () => {
Expand All @@ -98,15 +109,23 @@ describe('User Controller', () => {
when(mockUserService.increaseLevel(id, amount)).thenReturn(new Promise((resolve => resolve(user))));
controller = new UserController(instance(mockUserService));

expect(await controller.increaseLevel(id, {amount})).toBe(user);
expect(await controller.increaseLevel(id, amount)).toBe(user);
});
it('should throw error', async () => {
when(mockUserService.increaseLevel(id, amount)).thenThrow(new Error('some errors'));
it('should throw NotFoundException', async () => {
when(mockUserService.increaseLevel(id, amount)).thenThrow(new NotFoundException('user with the id is not found'));
controller = new UserController(instance(mockUserService));

await expect(controller.increaseLevel(id, {amount}))
await expect(controller.increaseLevel(id, amount))
.rejects
.toThrowError('some errors');
.toThrowError(NotFoundException);
});
it('should throw ValidationException', async () => {
when(mockUserService.increaseLevel(id, amount)).thenThrow(new ValidationException('can not increase level'));
controller = new UserController(instance(mockUserService));

await expect(controller.increaseLevel(id, amount))
.rejects
.toThrowError(ValidationException);
});
});
describe('#increasePoint()', () => {
Expand All @@ -117,15 +136,15 @@ describe('User Controller', () => {
when(mockUserService.increasePoint(id, amount)).thenReturn(new Promise((resolve => resolve(user))));
controller = new UserController(instance(mockUserService));

expect(await controller.increasePoint(id, {amount})).toBe(user);
expect(await controller.increasePoint(id, amount)).toBe(user);
});
it('should throw error', async () => {
when(mockUserService.increasePoint(id, amount)).thenThrow(new Error('some errors'));
it('should throw NotFoundException', async () => {
when(mockUserService.increasePoint(id, amount)).thenThrow(new NotFoundException('user with the id is not found'));
controller = new UserController(instance(mockUserService));

await expect(controller.increasePoint(id, {amount}))
await expect(controller.increasePoint(id, amount))
.rejects
.toThrowError('some errors');
.toThrowError(NotFoundException);
});
});
describe('#decreasePoint()', () => {
Expand All @@ -136,15 +155,23 @@ describe('User Controller', () => {
when(mockUserService.decreasePoint(id, amount)).thenReturn(new Promise((resolve => resolve(user))));
controller = new UserController(instance(mockUserService));

expect(await controller.decreasePoint(id, {amount})).toBe(user);
expect(await controller.decreasePoint(id, amount)).toBe(user);
});
it('should throw NotFoundException', async () => {
when(mockUserService.decreasePoint(id, amount)).thenThrow(new NotFoundException('user with the id is not found'));
controller = new UserController(instance(mockUserService));

await expect(controller.decreasePoint(id, amount))
.rejects
.toThrowError(NotFoundException);
});
it('should throw error', async () => {
when(mockUserService.decreasePoint(id, amount)).thenThrow(new Error('some errors'));
it('should throw ValidationException', async () => {
when(mockUserService.decreasePoint(id, amount)).thenThrow(new ValidationException('can not decrease point'));
controller = new UserController(instance(mockUserService));

await expect(controller.decreasePoint(id, {amount}))
await expect(controller.decreasePoint(id, amount))
.rejects
.toThrowError('some errors');
.toThrowError(ValidationException);
});
});
});
Loading

0 comments on commit 3eb4c3e

Please sign in to comment.