Skip to content

Commit

Permalink
feat:impl ghost auction item controller [ISSUE-76]
Browse files Browse the repository at this point in the history
  • Loading branch information
als95 authored and junbeomlee committed Jul 14, 2019
1 parent 57e15f4 commit 08f7bdc
Show file tree
Hide file tree
Showing 10 changed files with 391 additions and 22 deletions.
29 changes: 24 additions & 5 deletions server/src/app/auction/auction.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,25 @@ describe('AuctionService', () => {
});
});

describe('#findOneByGene()', async () => {
const repository: AuctionRepository = mock(AuctionRepository);

it('should find one auction by gene', async () => {
const option = {
gene: 'EB2820AC1',
};

when(repository.findOne(objectContaining(option))).thenReturn(new Promise((res) => {
res(mockSpecialAuction);
}));
const repositoryImpl: IAuctionRepository = instance(repository);

service = new AuctionService(repositoryImpl);
const auction = await service.findOneByGene('EB2820AC1');
expect(auction).toEqual(mockSpecialAuction);
});
});

describe('#createAuction()', async () => {
const repository: AuctionRepository = mock(AuctionRepository);
let auctionDto: AuctionDto;
Expand All @@ -60,7 +79,7 @@ describe('AuctionService', () => {
);

service = new AuctionService(repositoryImpl);
service.createAuction(auctionDto);
await service.createAuction(auctionDto);

const auction = await service.findOne(0);
expect(auction).toEqual(mockSaleAuction);
Expand All @@ -81,7 +100,7 @@ describe('AuctionService', () => {
);

service = new AuctionService(repositoryImpl);
service.createAuction(auctionDto);
await service.createAuction(auctionDto);

const auction = await service.findOne(1);
expect(auction).toEqual(mockSpecialAuction);
Expand All @@ -107,7 +126,7 @@ describe('AuctionService', () => {
const repositoryImpl: IAuctionRepository = instance(repository);

service = new AuctionService(repositoryImpl);
service.updateWinner('EB2820AC1', 'user4', 5000);
await service.updateWinner('EB2820AC1', 'user4', 5000);

const auction = await service.findOne(1);
expect(auction.winnerId).toEqual('user4');
Expand All @@ -133,7 +152,7 @@ describe('AuctionService', () => {
const repositoryImpl: IAuctionRepository = instance(repository);

service = new AuctionService(repositoryImpl);
service.cancelAuction('EB2820AC1');
await service.cancelAuction('EB2820AC1');

const auction = await service.findOne(1);
expect(auction.endType).toEqual(AuctionEndType.CANCEL);
Expand All @@ -159,7 +178,7 @@ describe('AuctionService', () => {
const repositoryImpl: IAuctionRepository = instance(repository);

service = new AuctionService(repositoryImpl);
service.endAuction('EB2820AC1');
await service.endAuction('EB2820AC1');

const auction = await service.findOne(1);
expect(auction.endType).toEqual(AuctionEndType.SUCCESS);
Expand Down
8 changes: 8 additions & 0 deletions server/src/app/auction/auction.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ export class AuctionService {
return await this.auctionRepository.findOne(id);
}

async findOneByGene(gene: string): Promise<Auction> {
return await this.auctionRepository.findOne(
{
gene,
},
);
}

async createAuction(auctionDto: AuctionDto): Promise<Auction> {
let newAuction;
if (auctionDto.type === AuctionType.SALE_AUCTION) {
Expand Down
33 changes: 26 additions & 7 deletions server/src/app/ghost/ghost.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,25 @@ describe('GhostService', () => {
});
});

describe('#findOneByGene()', async () => {
const repository: GhostRepository = mock(GhostRepository);

it('should find ghost by gene', async () => {
const option = {
gene: 'FF9182839',
};

when(repository.findOne(objectContaining(option))).thenReturn(new Promise((res) => {
res(mockGhost);
}));
const repositoryImpl: IGhostRepository = instance(repository);

service = new GhostService(repositoryImpl);
const ghost = await service.findOneByGene('FF9182839');
expect(ghost).toEqual(mockGhost);
});
});

describe('#findAllByUser()', async () => {
const repository: GhostRepository = mock(GhostRepository);

Expand Down Expand Up @@ -103,7 +122,7 @@ describe('GhostService', () => {

ghostDto = new GhostDto('userId3', 'EB123F345');
service = new GhostService(repositoryImpl);
service.createEgg(ghostDto);
await service.createEgg(ghostDto);

const ghost = await service.findOne(3);
expect(ghost).toEqual(newGhost);
Expand All @@ -122,14 +141,14 @@ describe('GhostService', () => {
gene: 'ABF938481',
};

when(repository.find(objectContaining(option))).thenReturn(new Promise((res) => {
res([mockGhost2]);
when(repository.findOne(objectContaining(option))).thenReturn(new Promise((res) => {
res(mockGhost2);
}));

const repositoryImpl: IGhostRepository = instance(repository);

service = new GhostService(repositoryImpl);
service.transfer('userId1', 'userId2', 'ABF938481');
await service.transfer('userId1', 'userId2', 'ABF938481');

const ghost = await service.findOne(1);
expect(ghost.userId).toEqual('userId2');
Expand All @@ -148,14 +167,14 @@ describe('GhostService', () => {
gene: 'ABF938481',
};

when(repository.find(objectContaining(option))).thenReturn(new Promise((res) => {
res([mockGhost2]);
when(repository.findOne(objectContaining(option))).thenReturn(new Promise((res) => {
res(mockGhost2);
}));

const repositoryImpl: IGhostRepository = instance(repository);

service = new GhostService(repositoryImpl);
service.levelUp('ABF938481', 2);
await service.levelUp('ABF938481', 2);

const ghost = await service.findOne(1);
expect(ghost.level).toEqual(2);
Expand Down
10 changes: 9 additions & 1 deletion server/src/app/ghost/ghost.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ export class GhostService {
return await this.ghostRepository.findOne(id);
}

async findOneByGene(gene: string): Promise<Ghost> {
return await this.ghostRepository.findOne(
{
gene,
},
);
}

async findAllByUser(userId: string): Promise<Ghost[]> {
return await this.ghostRepository.find(
{
Expand Down Expand Up @@ -53,6 +61,6 @@ export class GhostService {
},
);
updatedGhost.setLevel(level);
return await this.ghostRepository.save(updatedGhost[0]);
return await this.ghostRepository.save(updatedGhost);
}
}
67 changes: 67 additions & 0 deletions server/src/web/auction/auction.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import {AuctionService} from '../../app/auction/auction.service';
import {instance, mock, when} from 'ts-mockito';
import {AuctionController} from './auction.controller';
import {Auction, AuctionType} from '../../domain/auction/auction.entity';
import {Test, TestingModule} from '@nestjs/testing';

describe('Auction Controller', () => {
const mockAuctionService = mock(AuctionService);
let controller: AuctionController;
let saleAuction: Auction;
let specialAuction: Auction;

beforeEach(() => {
saleAuction = new Auction(
'AA239BE27',
'user1',
60,
AuctionType.SALE_AUCTION,
'user3',
2000,
);

specialAuction = new Auction(
'EB2820AC1',
'user2',
120,
AuctionType.SPECIAL_AUCTION,
);
});

describe('dependency resolve', () => {
it('should be defined', async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [AuctionController],
providers: [{
provide: 'AuctionService',
useValue: instance(mockAuctionService),
}],
}).compile();

controller = module.get<AuctionController>(AuctionController);
expect(controller).toBeDefined();
});
});

describe('#findOne()', () => {
it('should find one auction', async () => {
when(mockAuctionService.findOne(1)).thenReturn(new Promise((resolve) => {
resolve(saleAuction);
}));
controller = new AuctionController(instance(mockAuctionService));

expect(await controller.findOne(1)).toBe(saleAuction);
});
});

describe('#findOneByGene()', () => {
it('should find one auction by gene', async () => {
when(mockAuctionService.findOneByGene('AA239BE27')).thenReturn(new Promise((resolve) => {
resolve(saleAuction);
}));
controller = new AuctionController(instance(mockAuctionService));

expect(await controller.findOneByGene('AA239BE27')).toBe(saleAuction);
});
});
});
19 changes: 19 additions & 0 deletions server/src/web/auction/auction.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {Controller, Get, Inject, Injectable, Param, Query} from '@nestjs/common';
import {AuctionService} from '../../app/auction/auction.service';
import {Auction} from '../../domain/auction/auction.entity';

@Injectable()
@Controller('auction')
export class AuctionController {
constructor(@Inject('AuctionService') private service: AuctionService) {}

@Get(':id')
async findOne(@Param('id') id: number): Promise<Auction> {
return await this.service.findOne(id);
}

@Get()
async findOneByGene(@Query('gene') gene): Promise<Auction> {
return await this.service.findOneByGene(gene);
}
}
75 changes: 68 additions & 7 deletions server/src/web/ghost/ghost.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,79 @@
import { Test, TestingModule } from '@nestjs/testing';
import { GhostController } from './ghost.controller';
import {instance, mock, when} from 'ts-mockito';
import {GhostService} from '../../app/ghost/ghost.service';
import {Ghost} from '../../domain/ghost/ghost.entity';

describe('Ghost Controller', () => {
const mockGhostService = mock(GhostService);
let ghost: Ghost;
let controller: GhostController;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [GhostController],
}).compile();
beforeEach(() => {
ghost = new Ghost(
'EE398A811',
0,
'user1',
);
});

describe('dependency resolve', () => {
it('should be defined', async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [GhostController],
providers: [{
provide: 'GhostService',
useValue: instance(mockGhostService),
}],
}).compile();

controller = module.get<GhostController>(GhostController);
controller = module.get<GhostController>(GhostController);
expect(controller).toBeDefined();
});
});

it('should be defined', () => {
expect(controller).toBeDefined();
describe('#findOne()', () => {
it('should find one ghost', async () => {
when(mockGhostService.findOne(1)).thenReturn(new Promise((resolve) => {
resolve(ghost);
}));
controller = new GhostController(instance(mockGhostService));

expect(await controller.findOne(1)).toBe(ghost);
});
});

describe('#findOneByGene', () => {
it('should find one ghost by gene', async () => {
when(mockGhostService.findOneByGene('EE398A811')).thenReturn(new Promise((resolve) => {
resolve(ghost);
}));
controller = new GhostController(instance(mockGhostService));

expect(await controller.findOneByGene('EE398A811')).toBe(ghost);
});
});

describe('#findAllByUser()', () => {
it('should find ghosts by ID of user', async () => {
when(mockGhostService.findAllByUser('user1')).thenReturn(new Promise((resolve) => {
resolve([ghost]);
}));
controller = new GhostController(instance(mockGhostService));

expect(await controller.findAllByUser('user1')).toEqual([ghost]);
});
});

describe('#findAll()', () => {
it('should find page 1 ghosts', async () => {

when(mockGhostService.findAll(1)).thenReturn(new Promise((resolve) => {
resolve([ghost]);
}));
controller = new GhostController(instance(mockGhostService));

expect(await controller.findAll(1)).toEqual([ghost]);
});
});
});
29 changes: 27 additions & 2 deletions server/src/web/ghost/ghost.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,29 @@
import { Controller } from '@nestjs/common';
import {Controller, Get, Inject, Injectable, Param, Query} from '@nestjs/common';
import {Ghost} from '../../domain/ghost/ghost.entity';
import {GhostService} from '../../app/ghost/ghost.service';

@Injectable()
@Controller('ghost')
export class GhostController {}
export class GhostController {
constructor(@Inject('GhostService') private service: GhostService) {}

@Get(':id')
async findOne(@Param('id') id: number): Promise<Ghost> {
return await this.service.findOne(id);
}

@Get()
async findOneByGene(@Query('gene') gene): Promise<Ghost> {
return await this.service.findOneByGene(gene);
}

@Get()
async findAllByUser(@Query('userId') userId): Promise<Ghost[]> {
return await this.service.findAllByUser(userId);
}

@Get(':page')
async findAll(@Param('page') page: number): Promise<Ghost[]> {
return await this.service.findAll(page);
}
}
Loading

0 comments on commit 08f7bdc

Please sign in to comment.