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 19, 2019
1 parent 1bfcd9e commit c358994
Show file tree
Hide file tree
Showing 12 changed files with 384 additions and 117 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
36 changes: 19 additions & 17 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 {ValidationException} from '../../domain/exception/ValidationException';
import {InvalidParameterException} from '../../domain/exception/InvalidParameterException';

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

expect(await service.create(userDto)).toBe(user);
});
it('should throw "address should be defined"', async () => {
it('should throw InvalidParameterException', async () => {
when(mockRepository.findByAddress(address)).thenReturn(
new Promise((resolve => {resolve(user); }),
),
Expand All @@ -77,9 +79,9 @@ describe('UserServiceImpl', () => {

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

await expect(service.create(userDto))
.rejects
.toThrowError('name should be defined');
.toThrowError(InvalidParameterException);
});
it('should throw "address is already registered"', async () => {
it('should throw NotAcceptableException', async () => {
when(mockRepository.findByAddress(address)).thenReturn(
new Promise((resolve => {resolve(user); }),
),
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 @@ -134,13 +136,13 @@ describe('UserServiceImpl', () => {
expect(userReturned).toBeDefined();
expect(userReturned.getPoint()).toBe(amount);
});
it('should throw error "user with the id is not found"', async () => {
it('should throw error NotFoundException', async () => {
when(mockRepository.findById(id)).thenReturn(undefined);
service = new UserServiceImpl(instance(mockRepository));

await expect(service.increasePoint(id, amount))
.rejects
.toThrowError('user with the id is not found');
.toThrowError(NotFoundException);
});
});
describe('#decreasePoint()', () => {
Expand All @@ -159,22 +161,22 @@ describe('UserServiceImpl', () => {
expect(userReturned).toBeDefined();
expect(userReturned.getPoint()).toBe(point - validAmount);
});
it('should throw error "user with the id is not found"', async () => {
it('should throw error NotFoundException', async () => {
when(mockRepository.findById(id)).thenReturn(undefined);
service = new UserServiceImpl(instance(mockRepository));

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 () => {
it('should throw error ValidationException', async () => {
user = new User(address, name, point);
when(mockRepository.findById(id)).thenReturn(new Promise((resolve => resolve(user))));
service = new UserServiceImpl(instance(mockRepository));

await expect(service.decreasePoint(id, invalidAmount))
.rejects
.toThrowError('can not decrease point');
.toThrowError(ValidationException);
});
});
describe('#increaseLevel()', () => {
Expand All @@ -191,21 +193,21 @@ describe('UserServiceImpl', () => {
expect(userReturned).toBeDefined();
expect(userReturned.getLevel()).toBe(validAmount);
});
it('should throw error "user with the id is not found"', async () => {
it('should throw error NotFoundException', async () => {
when(mockRepository.findById(id)).thenReturn(undefined);
service = new UserServiceImpl(instance(mockRepository));

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 () => {
it('should throw error ValidationException', 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);
});
});
});
36 changes: 11 additions & 25 deletions server/src/app/user/user.service.impl.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
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 '../../domain/exception/InvalidParameterException';

@Injectable()
export class UserServiceImpl implements UserService {
Expand All @@ -20,14 +21,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 @@ -39,41 +40,26 @@ export class UserServiceImpl implements UserService {
}

async increasePoint(id: number, amount: number): Promise<User> {
let user: User;
user = await this.userRepository.findById(id);
const 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);
}

async decreasePoint(id: number, amount: number): Promise<User> {
let user: User;
user = await this.userRepository.findById(id);
const user = await this.userRepository.findById(id);
if (user === undefined) {
throw new Error('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 NotFoundException('user with the id is not found');
}

return await user.decreasePoint(amount);
}
async increaseLevel(id: number, amount: number): Promise<User> {
let user: User;
user = await this.userRepository.findById(id);
const user = await this.userRepository.findById(id);
if (user === undefined) {
throw new Error('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 NotFoundException('user with the id is not found');
}

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,
};
36 changes: 5 additions & 31 deletions server/src/domain/user/user.entity.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { User} from './user.entity';
import {ValidationException} from '../exception/ValidationException';

describe('User.Entity', () => {
const address = 'testAddress';
Expand Down Expand Up @@ -30,22 +31,6 @@ describe('User.Entity', () => {
await expect(user.getPoint()).toBe(validAmount);
});
});
describe('#canDecreasePoint()', () => {
const point = 100;
const validAmount = 10;
const invalidAmount = 1000;

beforeEach(() => {
user = new User(address, name, point);
});

it('should return empty array', async () => {
await expect(user.canDecreasePoint(validAmount).length).toEqual(0);
});
it('should return "not empty" array', async () => {
await expect(user.canDecreasePoint(invalidAmount).length).toBeGreaterThan(0);
});
});
describe('#decreasePoint()', () => {
const point = 100;
const validAmount = 10;
Expand All @@ -59,10 +44,10 @@ describe('User.Entity', () => {
await expect(await user.decreasePoint(validAmount)).toBeDefined();
await expect(user.getPoint()).toBe(point - validAmount);
});
it('should throw Error "can not decrease point with the amount"', async () => {
it('should throw Error ValidationException', async () => {
await expect(user.decreasePoint(invalidAmount))
.rejects
.toThrowError('can not decrease point with the amount');
.toThrowError(ValidationException);
});
});
describe( '#getLevel()', () => {
Expand All @@ -73,17 +58,6 @@ describe('User.Entity', () => {
await expect(user.getLevel()).toBe(level);
});
});
describe('#canIncreaseLevel()', () => {
const validAmount = 10;
const invalidAmount = 1000;

it('should return empty array', async () => {
await expect(user.canIncreaseLevel(validAmount).length).toEqual(0);
});
it('should return "not empty" array', async () => {
await expect(user.canIncreaseLevel(invalidAmount).length).toBeGreaterThan(0);
});
});
describe('#increaseLevel()', () => {
const validAmount = 10;
const invalidAmount = 1000;
Expand All @@ -92,10 +66,10 @@ describe('User.Entity', () => {
await expect(await user.increaseLevel(validAmount)).toBeDefined();
await expect(user.getLevel()).toBe(validAmount);
});
it('should throw error "can not increase level with the amount', async () => {
it('should throw error ValidationException', async () => {
await expect(user.increaseLevel(invalidAmount))
.rejects
.toThrowError('can not increase level with the amount');
.toThrowError(ValidationException);
});
});
});
29 changes: 7 additions & 22 deletions server/src/domain/user/user.entity.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
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';
import {ValidationException} from '../exception/ValidationException';

@Entity()
export class User {
Expand Down Expand Up @@ -59,35 +60,19 @@ export class User {
return this;
}
public async decreasePoint(amount: number): Promise<User> {
if (this.canDecreasePoint(amount).length !== 0) {
throw new Error('can not decrease point with the amount');
if (this.point - amount < MIN_POINT) {
throw new ValidationException('can not decrease point');
}

this.point -= amount;
return this;
}
public canDecreasePoint(amount: number) {
let errors: Error[];
errors = [];
if (this.point - amount < MIN_POINT) {
errors.push(new Error('point can not be less than MIN_POINT'));
}
return errors;
}
public async increaseLevel(amount: number): Promise<User> {
if (this.canIncreaseLevel(amount).length !== 0) {
throw new Error('can not increase level with the amount');
if (MAX_LEVEL < this.level + amount) {
throw new ValidationException('can not increase level');
}

this.level += amount;
return this;
}
public canIncreaseLevel(amount: number): Error[] {
let errors: Error[];
errors = [];
if (MAX_LEVEL < this.level + amount) {
errors.push(new Error('level can not exceed MAX_LEVEL'));
}

return errors;
}
}
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
Loading

0 comments on commit c358994

Please sign in to comment.