Skip to content

Commit

Permalink
General improvements for logic/* unit tests. WIP #173
Browse files Browse the repository at this point in the history
  • Loading branch information
jarenal committed Apr 25, 2018
1 parent a8721f8 commit 5a9ed44
Show file tree
Hide file tree
Showing 8 changed files with 352 additions and 316 deletions.
214 changes: 105 additions & 109 deletions tests/unit/logic/account.spec.ts

Large diffs are not rendered by default.

10 changes: 6 additions & 4 deletions tests/unit/logic/appState.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,25 @@ import * as jsonpath from 'jsonpath';
import * as proxyquire from 'proxyquire';
import 'reflect-metadata';
import * as sinon from 'sinon';
import { SinonSpy, SinonStub } from 'sinon';
import { SinonSandbox, SinonSpy, SinonStub } from 'sinon';
import { AppState } from '../../../src/logic';

// tslint:disable no-unused-expression
describe('appState', () => {
let instance: AppState;
let valueSpy: SinonSpy;
let computedStub: SinonStub;
let sandbox: SinonSandbox;

const ProxyAppState = proxyquire('../../../src/logic/appState.ts', {
jsonpath,
});

beforeEach(() => {
valueSpy = sinon.spy(jsonpath, 'value');
sandbox = sinon.sandbox.create();
valueSpy = sandbox.spy(jsonpath, 'value');
instance = new ProxyAppState.AppState();
computedStub = sinon.stub().returns('returnVal');
computedStub = sandbox.stub().returns('returnVal');
instance.states = {
rounds: {
isLoaded: false,
Expand All @@ -31,7 +33,7 @@ describe('appState', () => {
});

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

describe('set', () => {
Expand Down
93 changes: 48 additions & 45 deletions tests/unit/logic/block.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ import * as ByteBuffer from 'bytebuffer';
import * as chai from 'chai';
import 'chai-arrays';
import * as crypto from 'crypto';
import * as proxyquire from 'proxyquire';
import {Container} from 'inversify';
import * as sinon from 'sinon';
import { SinonSpy } from 'sinon';
import { constants } from '../../../src/helpers/';
import { SinonSandbox, SinonSpy } from 'sinon';
import { Ed } from '../../../src/helpers';
import { constants } from '../../../src/helpers/';
import {Symbols} from '../../../src/ioc/symbols';
import { BlockLogic } from '../../../src/logic';
import { BlockRewardLogicStub, TransactionLogicStub, ZSchemaStub } from '../../stubs';
import { createContainer } from '../../utils/containerCreator';

// tslint:disable-next-line no-var-requires
const assertArrays = require('chai-arrays');
Expand All @@ -17,16 +19,16 @@ const { expect } = chai;
chai.use(assertArrays);

const ed = new Ed();
const ProxyBlock = proxyquire('../../../src/logic/block.ts', {
crypto,
});

const passphrase = 'oath polypody manumit effector half sigmoid abound osmium jewfish weed sunproof ramose';
const dummyKeypair = ed.makeKeypair(
crypto.createHash('sha256').update(passphrase, 'utf8').digest()
);

// tslint:disable no-unused-expression
describe('logic/block', () => {
let sandbox: SinonSandbox;
let container: Container;
let dummyBlock;
let dummyTransactions;
let callback;
Expand All @@ -43,67 +45,69 @@ describe('logic/block', () => {
const buffer = bb.toBuffer();

beforeEach(() => {
createHashSpy = sinon.spy(crypto, 'createHash');
sandbox = sinon.sandbox.create();
container = createContainer();
createHashSpy = sandbox.spy(crypto, 'createHash');
dummyTransactions = [
{
type: 1,
amount: 108910891000000,
fee: 5,
timestamp: 0,
id: '8139741256612355994',
recipientId: '15256762582730568272R',
senderId: '14709573872795067383R',
senderPublicKey: '35526f8a1e2f482264e5d4982fc07e73f4ab9f4794b110ceefecd8f880d51892',
signature: 'f8fbf9b8433bf1bbea971dc8b14c6772d33c7dd285d84c5e6c984b10c4141e9fa56ace' +
'902b910e05e98b55898d982b3d5b9bf8bd897083a7d1ca1d5028703e03',
id: '8139741256612355994',
'902b910e05e98b55898d982b3d5b9bf8bd897083a7d1ca1d5028703e03',
timestamp: 0,
type: 1,
},
{
type: 3,
amount: 108910891000000,
fee: 3,
timestamp: 0,
id: '16622990339377112127',
recipientId: '6781920633453960895R',
senderId: '14709573872795067383R',
senderPublicKey: '35526f8a1e2f482264e5d4982fc07e73f4ab9f4794b110ceefecd8f880d51892',
signature: 'e26edb739d93bb415af72f1c288b06560c0111c4505f11076ca20e2f6e8903d3b00730' +
'9c0e04362bfeb8bf2021d0e67ce3c943bfe0c0193f6c9503eb6dfe750c',
id: '16622990339377112127',
'9c0e04362bfeb8bf2021d0e67ce3c943bfe0c0193f6c9503eb6dfe750c',
timestamp: 0,
type: 3,
},
{
type: 2,
amount: 108910891000000,
fee: 3,
timestamp: 0,
id: '16622990339377114578',
recipientId: '6781920633453960895R',
senderId: '14709573872795067383R',
senderPublicKey: '35526f8a1e2f482264e5d4982fc07e73f4ab9f4794b110ceefecd8f880d51892',
signature: 'e26edb739d93bb415af72f1c288b06560c0111c4505f11076ca20e2f6e8903d3b00730' +
'9c0e04362bfeb8bf2021d0e67ce3c943bfe0c0193f6c9503eb6dfe750c',
id: '16622990339377114578',
timestamp: 0,
type: 2,
},
];

dummyBlock = {
version: 0,
totalAmount: 217821782000000,
totalFee: 8,
reward: 30000000,
payloadHash: 'b3cf5bb113442c9ba61ed0a485159b767ca181dd447f5a3d93e9dd73564ae762',
timestamp: 1506889306558,
blockSignature: '8c5f2b088eaf0634e1f6e12f94a1f3e871f21194489c76ad2aae5c1b71acd848bc7b' +
'158fa3b827e97f3f685c772bfe1a72d59975cbd2ccaa0467026d13bae50a',
generatorPublicKey: 'c950f1e6c91485d2e6932fbd689bba636f73970557fe644cd901a438f74883c5',
numberOfTransactions: 2,
payloadHash: 'b3cf5bb113442c9ba61ed0a485159b767ca181dd447f5a3d93e9dd73564ae762',
payloadLength: 8,
previousBlock: '1',
generatorPublicKey: 'c950f1e6c91485d2e6932fbd689bba636f73970557fe644cd901a438f74883c5',
reward: 30000000,
timestamp: 1506889306558,
totalAmount: 217821782000000,
totalFee: 8,
transactions: dummyTransactions,
blockSignature: '8c5f2b088eaf0634e1f6e12f94a1f3e871f21194489c76ad2aae5c1b71acd848bc7b' +
'158fa3b827e97f3f685c772bfe1a72d59975cbd2ccaa0467026d13bae50a',
version: 0,
};

callback = sinon.spy();
zschemastub = new ZSchemaStub();
blockRewardLogicStub = new BlockRewardLogicStub();
callback = sandbox.spy();
zschemastub = container.get(Symbols.generic.zschema);
blockRewardLogicStub = container.get(Symbols.logic.blockReward);
instance = new BlockLogic();
transactionLogicStub = new TransactionLogicStub();
transactionLogicStub = container.get(Symbols.logic.transaction);

// Inject dependencies!
(instance as any).zschema = zschemastub;
Expand All @@ -118,15 +122,14 @@ describe('logic/block', () => {

data = {
keypair: dummyKeypair,
previousBlock: { id: '1', height: 10 },
timestamp: Date.now(),
transactions: dummyTransactions,
previousBlock: { id: '1', height: 10 },
};
});

afterEach(() => {
callback.resetHistory();
createHashSpy.restore();
sandbox.restore();
});

describe('create', () => {
Expand Down Expand Up @@ -310,7 +313,7 @@ describe('logic/block', () => {
expect(() => {
instance.objectNormalize(dummyBlock);
}).to.throw('Failed to validate block schema: 1, 2');
})
});
});

describe('[static] getId', () => {
Expand All @@ -334,20 +337,20 @@ describe('logic/block', () => {

describe('dbRead', () => {
const raw = {
b_id: 10,
b_version: 11,
b_timestamp: Date.now(),
b_blockSignature: 16,
b_confirmations: 1,
b_generatorPublicKey: 'c950f1e6c91485d2e6932fbd689bba636f73970557fe644cd901a438f74883c5',
b_height: 12,
b_previousBlock: 9,
b_id: 10,
b_numberOfTransactions: 1,
b_payloadHash: 14,
b_payloadLength: 13,
b_previousBlock: 9,
b_reward: 50,
b_timestamp: Date.now(),
b_totalAmount: 0,
b_totalFee: 100,
b_reward: 50,
b_payloadLength: 13,
b_payloadHash: 14,
b_generatorPublicKey: 'c950f1e6c91485d2e6932fbd689bba636f73970557fe644cd901a438f74883c5',
b_blockSignature: 16,
b_confirmations: 1,
b_version: 11,
};

it('should return a specific format', () => {
Expand Down
18 changes: 11 additions & 7 deletions tests/unit/logic/blockReward.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as chai from 'chai';
import { SinonSandbox } from 'sinon';
import * as sinon from 'sinon';
import { constants } from '../../../src/helpers';
import { BlockRewardLogic } from '../../../src/logic';
Expand All @@ -8,14 +9,20 @@ const expect = chai.expect;
// tslint:disable no-unused-expression
describe('logic/blockReward', () => {
let instance: BlockRewardLogic;
let sandbox: SinonSandbox;

beforeEach(() => {
sandbox = sinon.sandbox.create();
instance = new BlockRewardLogic();
(instance as any).constants = constants;
// Usually autocalled by inversify on construct
instance.initRewards();
});

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

describe('constructor', () => {
it('should initialize rewards to the constant', () => {
expect((instance as any).rewards).to.be.deep.equal(constants.rewards);
Expand All @@ -34,15 +41,13 @@ describe('logic/blockReward', () => {

describe('calcMilestone', () => {
it('should call parseHeight', () => {
const parseHeightStub = sinon.stub((instance as any), 'parseHeight').returns(1);
const parseHeightStub = sandbox.stub((instance as any), 'parseHeight').returns(1);

instance.calcMilestone(10);

expect(parseHeightStub.calledOnce).to.be.true;
expect(parseHeightStub.getCall(0).args.length).to.equal(1);
expect(parseHeightStub.getCall(0).args[0]).to.equal(10);

parseHeightStub.restore();
});

it('should return correct block height', () => {
Expand All @@ -55,29 +60,28 @@ describe('logic/blockReward', () => {

describe('calcReward', () => {
it('should call calcMilestone', () => {
const calcMilestoneStub = sinon.stub(instance, 'calcMilestone').returns(0);
const calcMilestoneStub = sandbox.stub(instance, 'calcMilestone').returns(0);

instance.calcReward(10);

expect(calcMilestoneStub.calledOnce).to.be.true;
expect(calcMilestoneStub.getCall(0).args.length).to.equal(1);
expect(calcMilestoneStub.getCall(0).args[0]).to.equal(10);
calcMilestoneStub.restore();
});
});

describe('calcSupply', () => {

it('should call parseHeight', () => {
const parseHeightStub = sinon.stub((instance as any), 'parseHeight').returns(10);
const parseHeightStub = sandbox.stub((instance as any), 'parseHeight').returns(10);
instance.calcSupply(10);

expect(parseHeightStub.getCall(0).args.length).to.equal(1);
expect(parseHeightStub.getCall(0).args[0]).to.equal(10);
});

it('should call calcMilestone', () => {
const calcMilestoneStub = sinon.stub(instance, 'calcMilestone').returns(1);
const calcMilestoneStub = sandbox.stub(instance, 'calcMilestone').returns(1);
instance.calcSupply(1);

expect(calcMilestoneStub.getCall(0).args.length).to.equal(1);
Expand Down

0 comments on commit 5a9ed44

Please sign in to comment.