Skip to content
This repository has been archived by the owner on Mar 9, 2022. It is now read-only.

Commit

Permalink
using chai bignumber to test return vals
Browse files Browse the repository at this point in the history
  • Loading branch information
aalavandhan committed Sep 20, 2018
1 parent aedc9f3 commit b0dab82
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 24 deletions.
59 changes: 59 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
"truffle": "^4.1.13"
},
"devDependencies": {
"chai": "^4.1.2",
"chai-bignumber": "^2.0.2",
"coveralls": "^3.0.2",
"eslint": "^4.19.1",
"eslint-config-google": "^0.9.1",
Expand Down
38 changes: 21 additions & 17 deletions test/unit/market_oracle.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ const _require = require('app-root-path').require;
const BlockchainCaller = _require('/util/blockchain_caller');
const chain = new BlockchainCaller(web3);

require('chai')
.use(require('chai-bignumber')(web3.BigNumber))
.should();

let oracle, source, source2, deployer, A, B, r;
function nowSeconds () {
return parseInt(Date.now() / 1000);
Expand All @@ -28,15 +32,15 @@ contract('MarketOracle:whitelistSize', async function (accounts) {
await oracle.addSource(A);
await oracle.addSource(A);
await oracle.addSource(A);
expect((await oracle.whitelistSize.call()).toNumber()).to.eq(3);
(await oracle.whitelistSize.call()).should.be.bignumber.eq(3);
});
});

contract('MarketOracle:addSource', async function (accounts) {
describe('when successful', function () {
before(async function () {
await setupContractsAndAccounts(accounts);
expect((await oracle.whitelistSize.call()).toNumber()).to.eq(0);
(await oracle.whitelistSize.call()).should.be.bignumber.eq(0);
r = await oracle.addSource(source.address);
});

Expand All @@ -48,7 +52,7 @@ contract('MarketOracle:addSource', async function (accounts) {
});
it('should add source to the whitelist', async function () {
expect(await oracle._whitelist.call(0)).to.eq(source.address);
expect((await oracle.whitelistSize.call()).toNumber()).to.eq(1);
(await oracle.whitelistSize.call()).should.be.bignumber.eq(1);
});
});
});
Expand Down Expand Up @@ -77,7 +81,7 @@ contract('MarketOracle:removeSource', async function (accounts) {
await setupContractsAndAccounts(accounts);
await oracle.addSource(source.address);
await oracle.addSource(source2.address);
expect((await oracle.whitelistSize.call()).toNumber()).to.eq(2);
(await oracle.whitelistSize.call()).should.be.bignumber.eq(2);
r = await oracle.removeSource(source.address);
});

Expand All @@ -89,7 +93,7 @@ contract('MarketOracle:removeSource', async function (accounts) {
});
it('should remove source from the whitelist', async function () {
expect(await oracle._whitelist.call(0)).to.eq(source2.address);
expect((await oracle.whitelistSize.call()).toNumber()).to.eq(1);
(await oracle.whitelistSize.call()).should.be.bignumber.eq(1);
});
});
});
Expand All @@ -100,7 +104,7 @@ contract('MarketOracle:removeSource', async function (accounts) {
await setupContractsAndAccounts(accounts);
await oracle.addSource(source.address);
await oracle.addSource(source2.address);
expect((await oracle.whitelistSize.call()).toNumber()).to.eq(2);
(await oracle.whitelistSize.call()).should.be.bignumber.eq(2);
r = await oracle.removeSource(A);
});

Expand All @@ -111,7 +115,7 @@ contract('MarketOracle:removeSource', async function (accounts) {
it('should NOT remove source any from the whitelist', async function () {
expect(await oracle._whitelist.call(0)).to.eq(source.address);
expect(await oracle._whitelist.call(1)).to.eq(source2.address);
expect((await oracle.whitelistSize.call()).toNumber()).to.eq(2);
(await oracle.whitelistSize.call()).should.be.bignumber.eq(2);
});
});
});
Expand Down Expand Up @@ -146,8 +150,8 @@ contract('MarketOracle:getPriceAnd24HourVolume', async function (accounts) {
describe('when the sources are live', function () {
it('should calculate the combined market rate and volume', async function () {
const resp = await oracle.getPriceAnd24HourVolume.call();
expect(resp[0].toNumber()).to.eq(1045880000000000000);
expect(resp[1].toNumber()).to.eq(5);
resp[0].should.be.bignumber.eq(1045880000000000000);
resp[1].should.be.bignumber.eq(5);
});
});
});
Expand All @@ -172,8 +176,8 @@ contract('MarketOracle:getPriceAnd24HourVolume', async function (accounts) {
});
it('should calculate the exchange rate', async function () {
const resp = await oracle.getPriceAnd24HourVolume.call();
expect(resp[0].toNumber()).to.eq(1053200000000000000);
expect(resp[1].toNumber()).to.eq(2);
resp[0].should.be.bignumber.eq(1053200000000000000);
resp[1].should.be.bignumber.eq(2);
});
});
});
Expand Down Expand Up @@ -203,7 +207,7 @@ contract('MarketOracle:removeDestructedSources', async function (accounts) {
await oracle.addSource(source.address);
await oracle.addSource(source2.address);
await source2.destroy({ from: B });
expect((await oracle.whitelistSize.call()).toNumber()).to.eq(2);
(await oracle.whitelistSize.call()).should.be.bignumber.eq(2);
r = await oracle.removeDestructedSources();
});

Expand All @@ -216,7 +220,7 @@ contract('MarketOracle:removeDestructedSources', async function (accounts) {

it('should remove the dead source from the whitelist', async function () {
expect(await oracle._whitelist.call(0)).to.eq(source.address);
expect((await oracle.whitelistSize.call()).toNumber()).to.eq(1);
(await oracle.whitelistSize.call()).should.be.bignumber.eq(1);
});
});
});
Expand All @@ -227,7 +231,7 @@ contract('MarketOracle:removeDestructedSources', async function (accounts) {
await setupContractsAndAccounts(accounts);
await oracle.addSource(source.address);
await oracle.addSource(source2.address);
expect((await oracle.whitelistSize.call()).toNumber()).to.eq(2);
(await oracle.whitelistSize.call()).should.be.bignumber.eq(2);
r = await oracle.removeDestructedSources();
});

Expand All @@ -238,7 +242,7 @@ contract('MarketOracle:removeDestructedSources', async function (accounts) {
it('should NOT remove any source from the whitelist', async function () {
expect(await oracle._whitelist.call(0)).to.eq(source.address);
expect(await oracle._whitelist.call(1)).to.eq(source2.address);
expect((await oracle.whitelistSize.call()).toNumber()).to.eq(2);
(await oracle.whitelistSize.call()).should.be.bignumber.eq(2);
});
});

Expand All @@ -251,7 +255,7 @@ contract('MarketOracle:removeDestructedSources', async function (accounts) {
await oracle.addSource(source2.address);
await source.destroy({ from: A });
await source2.destroy({ from: B });
expect((await oracle.whitelistSize.call()).toNumber()).to.eq(3);
(await oracle.whitelistSize.call()).should.be.bignumber.eq(3);
r = await oracle.removeDestructedSources();
});

Expand All @@ -261,7 +265,7 @@ contract('MarketOracle:removeDestructedSources', async function (accounts) {
expect(logs[0].args.source).to.eq(source.address);
expect(logs[1].event).to.eq('LogSourceRemoved');
expect(logs[1].args.source).to.eq(source2.address);
expect((await oracle.whitelistSize.call()).toNumber()).to.eq(1);
(await oracle.whitelistSize.call()).should.be.bignumber.eq(1);
});
});
});
16 changes: 10 additions & 6 deletions test/unit/market_source.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ const _require = require('app-root-path').require;
const BlockchainCaller = _require('/util/blockchain_caller');
const chain = new BlockchainCaller(web3);

require('chai')
.use(require('chai-bignumber')(web3.BigNumber))
.should();

let source, A, B;
function timeNowSeconds () {
return parseInt(Date.now() / 1000);
Expand All @@ -25,7 +29,7 @@ contract('MarketSource:initialization', async function (accounts) {
});

it('should set the expiration time', async function () {
expect((await source._reportExpirationTimeSec.call()).toNumber()).to.eq(600);
(await source._reportExpirationTimeSec.call()).should.be.bignumber.eq(600);
});
});

Expand Down Expand Up @@ -62,15 +66,15 @@ contract('MarketSource:reportRate', async function (accounts) {
it('should update the report', async function () {
const report = await source.getReport.call();
expect(report[0]).to.be.true;
expect(report[1].toNumber()).to.eq(rate);
expect(report[2].toNumber()).to.eq(volume);
report[1].should.be.bignumber.eq(rate);
report[2].should.be.bignumber.eq(volume);
});
it('should emit ExchangeRateReported', async function () {
const reportEvent = r.logs[0];
expect(reportEvent.event).to.eq('LogExchangeRateReported');
expect(reportEvent.args.exchangeRate.toNumber()).to.eq(rate);
expect(reportEvent.args.volume24hrs.toNumber()).to.eq(volume);
expect(reportEvent.args.timestampSecs.toNumber()).to.eq(timestamp);
reportEvent.args.exchangeRate.should.be.bignumber.eq(rate);
reportEvent.args.volume24hrs.should.be.bignumber.eq(volume);
reportEvent.args.timestampSecs.should.be.bignumber.eq(timestamp);
});
});

Expand Down
6 changes: 5 additions & 1 deletion test/unit/market_source_factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ const _require = require('app-root-path').require;
const BlockchainCaller = _require('/util/blockchain_caller');
const chain = new BlockchainCaller(web3);

require('chai')
.use(require('chai-bignumber')(web3.BigNumber))
.should();

contract('MarketSourceFactory', async function (accounts) {
let factory;
const A = accounts[1];
Expand Down Expand Up @@ -32,7 +36,7 @@ contract('MarketSourceFactory', async function (accounts) {
const marketSource = MarketSource.at(sourceContractAddr);
expect(await marketSource.owner.call()).to.eq(A);
expect(await marketSource._name.call()).to.eq('GDAX');
expect((await marketSource._reportExpirationTimeSec.call()).toNumber()).to.eq(3600);
(await marketSource._reportExpirationTimeSec.call()).should.be.bignumber.eq(3600);
});
});
});

0 comments on commit b0dab82

Please sign in to comment.