Skip to content

Commit

Permalink
[WIP] General improvements for modules/* unit tests. #175
Browse files Browse the repository at this point in the history
  • Loading branch information
jarenal committed May 1, 2018
1 parent d8601b4 commit 4200949
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 78 deletions.
25 changes: 15 additions & 10 deletions tests/unit/modules/accounts.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,42 @@ import { expect } from 'chai';
import * as chai from 'chai';
import * as chaiAsPromised from 'chai-as-promised';
import { Container } from 'inversify';
import { IAccountLogic } from '../../../src/ioc/interfaces/logic';
import * as sinon from 'sinon';
import {SinonSandbox} from 'sinon';
import { IAccountsModule } from '../../../src/ioc/interfaces/modules';
import { Symbols } from '../../../src/ioc/symbols';
import { AccountsModule } from '../../../src/modules';
import AccountLogicStub from '../../stubs/logic/AccountLogicStub';
import { createContainer } from '../../utils/containerCreator';

chai.use(chaiAsPromised);

// tslint:disable no-unused-expression

describe('modules/accounts', () => {
let sandbox: SinonSandbox;
let accountLogicStub: AccountLogicStub;
let accountModule: IAccountsModule;
let container: Container;
before(() => {
container = new Container();
container.bind<IAccountLogic>(Symbols.logic.account).to(AccountLogicStub).inSingletonScope();
container.bind<IAccountsModule>(Symbols.modules.accounts).to(AccountsModule).inSingletonScope();
accountLogicStub = container.get<any>(Symbols.logic.account);
});

beforeEach(() => {
accountLogicStub.reset();
accountModule = container.get<any>(Symbols.modules.accounts);
sandbox = sinon.sandbox.create();
container = createContainer();
accountLogicStub = container.get(Symbols.logic.account);
container.rebind<IAccountsModule>(Symbols.modules.accounts).to(AccountsModule).inSingletonScope();
accountModule = container.get<any>(Symbols.modules.accounts);
});

afterEach(() => {
sandbox.restore();
});

describe('cleanup', () => {
it('should return resolved promise', async () => {
await expect(accountModule.cleanup()).to.be.fulfilled;
});
});

describe('.getAccount', () => {
it('should call accountLogic.get', async () => {
accountLogicStub.enqueueResponse('get', 'diocan');
Expand Down
19 changes: 11 additions & 8 deletions tests/unit/modules/blocks.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { expect } from 'chai';
import * as chai from 'chai';
// import 'chai-as-promised';
import * as chaiAsPromised from 'chai-as-promised';
import { Container } from 'inversify';
import * as sinon from 'sinon';
import { SinonSandbox } from 'sinon';
import { IBlocksModule } from '../../../src/ioc/interfaces/modules';
import { Symbols } from '../../../src/ioc/symbols';
import { BlocksModule } from '../../../src/modules';
import { LoggerStub } from '../../stubs';
import { createContainer } from '../../utils/containerCreator';

chai.use(chaiAsPromised);

Expand All @@ -16,16 +16,19 @@ describe('modules/blocks', () => {
let inst: IBlocksModule;
let instB: BlocksModule;
let container: Container;
before(() => {
container = new Container();
container.bind(Symbols.helpers.logger).to(LoggerStub);
container.bind(Symbols.modules.blocks).to(BlocksModule);
container.bind(Symbols.helpers.constants).toConstantValue({ blockReceiptTimeOut: 10 });
});
let sandbox: SinonSandbox;

beforeEach(() => {
sandbox = sinon.sandbox.create();
container = createContainer();
container.rebind(Symbols.modules.blocks).to(BlocksModule);
inst = instB = container.get<any>(Symbols.modules.blocks);
});

afterEach(() => {
sandbox.restore();
});

describe('instance fields', () => {
it('should have lastReceipt', () => {
expect(inst.lastReceipt).to.exist;
Expand Down
23 changes: 8 additions & 15 deletions tests/unit/modules/cache.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import * as helpers from '../../../src/helpers';
import { cbToPromise, TransactionType } from '../../../src/helpers';
import { Symbols } from '../../../src/ioc/symbols';
import { Cache, DummyCache } from '../../../src/modules';
import { LoggerStub, RedisClientStub } from '../../stubs';
import { RedisClientStub } from '../../stubs';
import { createContainer } from '../../utils/containerCreator';

chai.use(chaiAsPromised);

Expand All @@ -20,21 +21,12 @@ describe('modules/cache', () => {
let sandbox: SinonSandbox;
let spyHelper;
let redisClientStub: RedisClientStub;
const appConfig = {
cacheEnabled: true,
};

before(() => {
container = new Container();
container.bind(Symbols.generic.appConfig).toConstantValue(appConfig);
beforeEach(() => {
sandbox = sinon.sandbox.create();
container = createContainer();
container.bind(Symbols.generic.redisClient).to(RedisClientStub).inSingletonScope();
container.bind(Symbols.helpers.logger).to(LoggerStub);
container.bind(Symbols.modules.cache).to(Cache);
});

beforeEach(() => {
sandbox = sinon.sandbox.create();

cache = container.get(Symbols.modules.cache);
redisClientStub = container.get(Symbols.generic.redisClient);
cache.onSyncFinished();
Expand Down Expand Up @@ -466,7 +458,8 @@ describe('modules/cache', () => {
removeByPatternStub = sandbox.stub(cache, 'removeByPattern');
});

it('success', async () => {
// tslint:disable-next-line: max-line-length
it('Should call to removeByPattern() with parameter /api/delegates* for to remove all cache entries', async () => {
await cache.onFinishRound();

expect(spyHelper.cache.assertConnectedAndReady.calledOnce).to.be.true;
Expand Down Expand Up @@ -500,7 +493,7 @@ describe('modules/cache', () => {
senderPublicKey: 'c094ebee7ec0c50ebee32918655e089f6e1a604b83bcaa760293c61e0f18ab6f',
signature : 'd8103d0ea2004c3dea8076a6a22c6db8bae95bc0db819240c77fc5335f32920e91b9f41f58b01fc86dfda11019c9fd1c6c3dcbab0a4e478e3c9186ff6090dc05',
timestamp : 0,
type : TransactionType.DAPP,
type : TransactionType.MULTI,
},
];
// tslint:enable max-line-length
Expand Down
79 changes: 34 additions & 45 deletions tests/unit/modules/delegates.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,41 @@ import { expect } from 'chai';
import * as chai from 'chai';
import * as chaiAsPromised from 'chai-as-promised';
import * as crypto from 'crypto';
import {Container} from 'inversify';
import { SinonSandbox, SinonStub } from 'sinon';
import * as sinon from 'sinon';
import * as helpers from '../../../src/helpers';
import { constants } from '../../../src/helpers';
import {Symbols} from '../../../src/ioc/symbols';
import { SignedBlockType } from '../../../src/logic';
import { DelegatesModule } from '../../../src/modules';

import {
AccountsModuleStub,
AppStateStub,
BlockRewardLogicStub,
BlocksModuleStub,
ExceptionsManagerStub,
LoggerStub,
RoundsLogicStub,
SlotsStub, TransactionsModuleStub,
SlotsStub,
ZSchemaStub
} from '../../stubs';
import { CreateHashSpy } from '../../stubs/utils/CreateHashSpy';
import { generateAccounts } from '../../utils/accountsUtils';
import { createContainer } from '../../utils/containerCreator';

chai.use(chaiAsPromised);

// tslint:disable no-unused-expression
describe('modules/delegates', () => {
let sandbox: SinonSandbox;
let container: Container;
let instance: DelegatesModule;
let excManagerStub: ExceptionsManagerStub;

let accountsModuleStub: AccountsModuleStub;
let blocksModuleStub: BlocksModuleStub;
let blockRewardLogicStub: BlockRewardLogicStub;
let loggerStub: LoggerStub;
let roundsLogicStub: RoundsLogicStub;
let slotsStub: SlotsStub;
let schemaStub: ZSchemaStub;
let appStateStub: AppStateStub;
let blockRewardLogicStub: BlockRewardLogicStub;
let roundsLogicStub: RoundsLogicStub;
let blocksModuleStub: BlocksModuleStub;
let accountsModuleStub: AccountsModuleStub;
let transactionsModuleStub: TransactionsModuleStub;

let createHashSpy: CreateHashSpy;

Expand All @@ -60,30 +58,18 @@ describe('modules/delegates', () => {

beforeEach(() => {
sandbox = sinon.sandbox.create();
container = createContainer();

excManagerStub = new ExceptionsManagerStub();
loggerStub = new LoggerStub();
slotsStub = new SlotsStub();
schemaStub = new ZSchemaStub();
appStateStub = new AppStateStub();
blockRewardLogicStub = new BlockRewardLogicStub();
roundsLogicStub = new RoundsLogicStub();
blocksModuleStub = new BlocksModuleStub();
accountsModuleStub = new AccountsModuleStub();
transactionsModuleStub = new TransactionsModuleStub();

instance = new DelegatesModule();
(instance as any).constants = constants;
(instance as any).excManager = excManagerStub;
(instance as any).logger = loggerStub;
(instance as any).slots = slotsStub;
(instance as any).schema = schemaStub;
(instance as any).appState = appStateStub;
(instance as any).blockReward = blockRewardLogicStub;
(instance as any).roundsLogic = roundsLogicStub;
(instance as any).blocksModule = blocksModuleStub;
(instance as any).accountsModule = accountsModuleStub;
(instance as any).transactionsModule = transactionsModuleStub;
roundsLogicStub = container.get(Symbols.logic.rounds);
accountsModuleStub = container.get(Symbols.modules.accounts);
blocksModuleStub = container.get(Symbols.modules.blocks);
blockRewardLogicStub = container.get(Symbols.logic.blockReward);
slotsStub = container.get(Symbols.helpers.slots);
loggerStub = container.get(Symbols.helpers.logger);
schemaStub = container.get(Symbols.generic.zschema);

container.rebind(Symbols.modules.delegates).to(DelegatesModule).inSingletonScope();
instance = container.get(Symbols.modules.delegates);

// Init frequently used test values
pubKey = 'e22c25bcd696b94a3f4b017fdc681d714e275427a5112c2873e57c9637af3eed';
Expand All @@ -93,20 +79,21 @@ describe('modules/delegates', () => {

createHashSpy = new CreateHashSpy(crypto, sandbox);
const lastBlock = {
blockSignature : 'blockSignature',
generatorPublicKey : 'pubKey',
height : 12422,
id : 'blockID',
blockSignature : 'blockSignature',
version : 1,
totalAmount : 0,
totalFee : 0,
reward : 15,
payloadHash : '',
timestamp : Date.now(),
numberOfTransactions: 0,
payloadHash : '',
payloadLength : 0,
previousBlock : 'previous',
generatorPublicKey : 'pubKey',
reward : 15,
timestamp : Date.now(),
totalAmount : 0,
totalFee : 0,
version : 1,
};

blocksModuleStub.lastBlock = lastBlock;
blockRewardLogicStub.stubConfig.calcSupply.return = totalSupply;
signedBlock = Object.assign({}, lastBlock);
Expand Down Expand Up @@ -214,9 +201,11 @@ describe('modules/delegates', () => {
const pk = new Array(101).fill(null).map((a, idx) => (idx).toString(16));
getKeysSortByVoteStub.resolves(pk);
expect(await instance.generateDelegateList(10)).to.be.deep.eq(
// tslint:disable-next-line: max-line-length
['1', '41', '3f', '0', '42', '5a', '11', 'd', 'b', '8', '31', '5c', '4f', '1c', '15', '32', '3d', '25', '2f', '13', '46', '56', '29', '61', '58', '33', '38', '1f', '3a', '47', '17', '9', '43', 'e', '2b', '36', '37', '24', 'a', '30', '14', '4e', '48', '5d', '2', '28', '2d', '39', '64', '26', '3c', '3e', '19', '23', '1e', '44', '34', '57', '2a', '3b', '5', '1a', '27', '2c', 'f', '59', '6', '40', '4b', '45', '4c', '1d', '7', '49', '4a', '53', '2e', '18', '4', '60', '54', '10', '5e', '12', '50', '1b', '21', '16', '5b', '3', '20', '62', '55', '22', '52', '5f', 'c', '35', '4d', '63', '51']
);
expect(await instance.generateDelegateList(1000)).to.be.deep.eq(
// tslint:disable-next-line: max-line-length
['41', '59', '2c', '1', '6', '20', '25', '1c', '5c', 'b', '26', '55', '60', '3a', '56', '3c', '1a', '24', '39', '13', '4c', '21', '4e', '35', '5b', '3e', '34', '9', '2a', '1d', '61', '8', '40', '15', '5d', '1e', '44', '37', '31', '64', '46', '4', '7', '22', '3f', '14', '28', '57', '51', 'a', '5', '27', '33', '36', '17', '4b', '19', '16', '48', '3b', '5a', '38', '30', '2', '32', '3', '11', 'f', '53', '45', '2e', '47', 'd', '49', '4a', '12', '2d', '58', '42', 'c', '50', '3d', '52', '2f', '54', '1f', 'e', '29', '62', '0', '43', '4d', '1b', '2b', '5e', '5f', '4f', '23', '18', '63', '10']
);
});
Expand Down Expand Up @@ -273,7 +262,7 @@ describe('modules/delegates', () => {
expect(Array.isArray(retVal.delegates)).to.be.true;
retVal.delegates.forEach((delegate, key) => {
expect(delegate.rank).to.be.equal(key + 1);
expect(delegate.approval).to.be.equal(Math.round((delegate.vote / totalSupply) * 1e4) / 1e2);
expect(delegate.approval).to.be.equal(Math.round((parseInt(delegate.vote, 10) / totalSupply) * 1e4) / 1e2);
const percent = Math.abs(
100 - (delegate.missedblocks / ((delegate.producedblocks + delegate.missedblocks) / 100))
) || 0;
Expand Down Expand Up @@ -454,8 +443,8 @@ describe('modules/delegates', () => {
await (instance as any).checkDelegates(theAccount.publicKey, votes, 'confirmed');
expect(accountsModuleStub.stubs.getAccount.callCount).to.be.equal(2);
expect(accountsModuleStub.stubs.getAccount.secondCall.args[0]).to.be.deep.equal({
publicKey : votes[0].substr(1),
isDelegate: 1,
publicKey : votes[0].substr(1),
});
});

Expand Down

0 comments on commit 4200949

Please sign in to comment.