Skip to content

Commit

Permalink
test : add e2e test of user domain [ISSUE-65]
Browse files Browse the repository at this point in the history
  • Loading branch information
ttkmw committed Jun 20, 2019
1 parent ebce797 commit cac0e25
Show file tree
Hide file tree
Showing 7 changed files with 411 additions and 25 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
53 changes: 42 additions & 11 deletions server/src/app/user/user.service.impl.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ 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, NotAcceptableException, NotFoundException} from '@nestjs/common';
import {NotAcceptableException, NotFoundException} from '@nestjs/common';
import {UserDto} from './dto/user.dto';
import {ValidationException} from '../../domain/exception/ValidationException';
import {InvalidParameterException} from '../../domain/exception/InvalidParameterException';
Expand Down Expand Up @@ -47,13 +47,13 @@ describe('UserServiceImpl', () => {

expect(await service.get(id)).toBe(user);
});
it('should throw BadRequestException', async () => {
it('should throw NotFoundException', async () => {
when(mockRepository.findById(null)).thenReturn(undefined);
service = new UserServiceImpl(instance(mockRepository));

await expect(service.get(null))
.rejects
.toThrowError(BadRequestException);
.toThrowError(NotFoundException);
});
});
describe('#create()', () => {
Expand Down Expand Up @@ -125,31 +125,41 @@ describe('UserServiceImpl', () => {
});
describe('#increasePoint()', () => {
const id = 1;
const amount = 10;
const validAmount = 10;
const invalidAmount = -1;

it('should return user with increased point', async () => {
when(mockRepository.findById(id)).thenReturn(new Promise((resolve => resolve(user))));
service = new UserServiceImpl(instance(mockRepository));

const userReturned = await service.increasePoint(id, amount);
const userReturned = await service.increasePoint(id, validAmount);

expect(userReturned).toBeDefined();
expect(userReturned.getPoint()).toBe(amount);
expect(userReturned.getPoint()).toBe(validAmount);
});
it('should throw error NotFoundException', async () => {
when(mockRepository.findById(id)).thenReturn(undefined);
service = new UserServiceImpl(instance(mockRepository));

await expect(service.increasePoint(id, amount))
await expect(service.increasePoint(id, validAmount))
.rejects
.toThrowError(NotFoundException);
});
it('should throw ValidationException when amount is negative', async () => {
when(mockRepository.findById(id)).thenReturn(new Promise((resolve => resolve(user))));
service = new UserServiceImpl(instance(mockRepository));

await expect(service.increasePoint(id, invalidAmount))
.rejects
.toThrowError(ValidationException);

});
});
describe('#decreasePoint()', () => {
const id = 1;
const point = 100;
const validAmount = 10;
const invalidAmount = 1000;
let invalidAmount: number;

it('should return user with increased level', async () => {
user = new User(address, name, point);
Expand All @@ -169,7 +179,18 @@ describe('UserServiceImpl', () => {
.rejects
.toThrowError(NotFoundException);
});
it('should throw error ValidationException', async () => {
it('should throw ValidationException when amount is negative', async () => {
invalidAmount = -1;
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(ValidationException);
});
it('should throw ValidationException when point becomes less than MIN_POINT', async () => {
invalidAmount = 1000;
user = new User(address, name, point);
when(mockRepository.findById(id)).thenReturn(new Promise((resolve => resolve(user))));
service = new UserServiceImpl(instance(mockRepository));
Expand All @@ -182,7 +203,7 @@ describe('UserServiceImpl', () => {
describe('#increaseLevel()', () => {
const id = 1;
const validAmount = 10;
const invalidAmount = 1000;
let invalidAmount: number;

it('should return user with increased level', async () => {
when(mockRepository.findById(id)).thenReturn(new Promise((resolve => resolve(user))));
Expand All @@ -201,7 +222,17 @@ describe('UserServiceImpl', () => {
.rejects
.toThrowError(NotFoundException);
});
it('should throw error ValidationException', async () => {
it('should throw ValidationException when amount is negative', async () => {
invalidAmount = -1;
when(mockRepository.findById(id)).thenReturn(new Promise((resolve => resolve(user))));
service = new UserServiceImpl(instance(mockRepository));

await expect(service.increaseLevel(id, invalidAmount))
.rejects
.toThrowError(ValidationException);
});
it('should throw ValidationException when level becomes more than MAX_LEVEL', async () => {
invalidAmount = 1000;
when(mockRepository.findById(id)).thenReturn(new Promise((resolve => resolve(user))));
service = new UserServiceImpl(instance(mockRepository));

Expand Down
2 changes: 1 addition & 1 deletion server/src/app/user/user.service.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class UserServiceImpl implements UserService {
async get(id: number): Promise<User> {
const user = await this.userRepository.findById(id);
if (user === undefined) {
throw new BadRequestException('no user with the id');
throw new NotFoundException('user with the id is not found');
}
return user;
}
Expand Down
31 changes: 25 additions & 6 deletions server/src/domain/user/user.entity.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,21 @@ describe('User.Entity', () => {
});
describe('#increasePoint()', () => {
const validAmount = 10;

it('should point to be increased', async () => {
it('should return user with increased point', async () => {
await expect(user.increasePoint(validAmount)).toBeDefined();
await expect(user.getPoint()).toBe(validAmount);
});
it('should throw ValidationException when amount is negative', async () => {
const invalidAmount = -1;
await expect(user.increasePoint(invalidAmount))
.rejects
.toThrowError(ValidationException);
});
});
describe('#decreasePoint()', () => {
const point = 100;
const validAmount = 10;
const invalidAmount = 1000;
let invalidAmount: number;

beforeEach(() => {
user = new User(address, name, point);
Expand All @@ -44,7 +49,14 @@ describe('User.Entity', () => {
await expect(await user.decreasePoint(validAmount)).toBeDefined();
await expect(user.getPoint()).toBe(point - validAmount);
});
it('should throw Error ValidationException', async () => {
it('should throw ValidationException when amount is negative', async () => {
invalidAmount = -1;
await expect(user.decreasePoint(invalidAmount))
.rejects
.toThrowError(ValidationException);
});
it('should throw ValidationException when point becomes less than MIN_POINT', async () => {
invalidAmount = 1000;
await expect(user.decreasePoint(invalidAmount))
.rejects
.toThrowError(ValidationException);
Expand All @@ -60,13 +72,20 @@ describe('User.Entity', () => {
});
describe('#increaseLevel()', () => {
const validAmount = 10;
const invalidAmount = 1000;
let invalidAmount: number;

it('should return user with increased level', async () => {
await expect(await user.increaseLevel(validAmount)).toBeDefined();
await expect(user.getLevel()).toBe(validAmount);
});
it('should throw error ValidationException', async () => {
it('should throw ValidationException when amount is negative', async () => {
invalidAmount = -1;
await expect(user.increaseLevel(invalidAmount))
.rejects
.toThrowError(ValidationException);
});
it('should throw ValidationException when level becomes more than MAX_LEVEL', async () => {
invalidAmount = 1000;
await expect(user.increaseLevel(invalidAmount))
.rejects
.toThrowError(ValidationException);
Expand Down
16 changes: 13 additions & 3 deletions server/src/domain/user/user.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,31 @@ export class User {
public getLevel(): number {
return this.level;
}
public increasePoint(amount: number): User {
public async increasePoint(amount: number): Promise<User> {
if (amount < 0) {
throw new ValidationException('amount should be positive');
}

this.point += amount;
return this;
}
public async decreasePoint(amount: number): Promise<User> {
if (amount < 0) {
throw new ValidationException('amount should be positive');
}
if (this.point - amount < MIN_POINT) {
throw new ValidationException('can not decrease point');
throw new ValidationException('can not decrease point under MIN_POINT');
}

this.point -= amount;
return this;
}
public async increaseLevel(amount: number): Promise<User> {
if (amount < 0) {
throw new ValidationException('amount should be positive');
}
if (MAX_LEVEL < this.level + amount) {
throw new ValidationException('can not increase level');
throw new ValidationException('can not increase level over MAX_LEVEL');
}

this.level += amount;
Expand Down
8 changes: 4 additions & 4 deletions server/src/web/user/user.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ 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, NotAcceptableException, NotFoundException} from '@nestjs/common';
import {NotAcceptableException, NotFoundException} from '@nestjs/common';
import {InvalidParameterException} from '../../domain/exception/InvalidParameterException';
import {ValidationException} from '../../domain/exception/ValidationException';

Expand Down Expand Up @@ -50,13 +50,13 @@ describe('User Controller', () => {
expect(await controller.get(id)).toBe(user);

});
it('should return undefined', async () => {
when(mockUserService.get(id)).thenThrow(new BadRequestException('no user with the id'));
it('should throw NotFoundException', async () => {
when(mockUserService.get(id)).thenThrow(new NotFoundException('user with the id is not found'));
controller = new UserController(instance(mockUserService));

await expect(controller.get(id))
.rejects
.toThrowError(BadRequestException);
.toThrowError(NotFoundException);
});
});
describe('#create()', () => {
Expand Down
Loading

0 comments on commit cac0e25

Please sign in to comment.