From b0dab821577441dcd40949622fd5a62fd5256eda Mon Sep 17 00:00:00 2001 From: nithin Date: Thu, 20 Sep 2018 14:18:19 -0700 Subject: [PATCH] using chai bignumber to test return vals --- package-lock.json | 59 ++++++++++++++++++++++++++++++ package.json | 2 + test/unit/market_oracle.js | 38 ++++++++++--------- test/unit/market_source.js | 16 +++++--- test/unit/market_source_factory.js | 6 ++- 5 files changed, 97 insertions(+), 24 deletions(-) diff --git a/package-lock.json b/package-lock.json index cd99029..6263a2a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -146,6 +146,12 @@ "integrity": "sha1-8S4PPF13sLHN2RRpQuTpbB5N1SU=", "dev": true }, + "assertion-error": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/assertion-error/-/assertion-error-1.1.0.tgz", + "integrity": "sha512-jgsaNduz+ndvGyFt3uSuWqvy4lCnIJiovtouQN5JZHOKCS2QuhEdbcQHFhVksz2N2U9hXJo8odG7ETyWlEeuDw==", + "dev": true + }, "async": { "version": "1.5.2", "resolved": "https://registry.npmjs.org/async/-/async-1.5.2.tgz", @@ -383,6 +389,26 @@ "lazy-cache": "^1.0.3" } }, + "chai": { + "version": "4.1.2", + "resolved": "https://registry.npmjs.org/chai/-/chai-4.1.2.tgz", + "integrity": "sha1-D2RYS6ZC8PKs4oBiefTwbKI61zw=", + "dev": true, + "requires": { + "assertion-error": "^1.0.1", + "check-error": "^1.0.1", + "deep-eql": "^3.0.0", + "get-func-name": "^2.0.0", + "pathval": "^1.0.0", + "type-detect": "^4.0.0" + } + }, + "chai-bignumber": { + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/chai-bignumber/-/chai-bignumber-2.0.2.tgz", + "integrity": "sha512-BIdRNjRaoRj4bMsZLKbIZPMNKqmwnzNiyxqBYDSs6dFOCs9w8OHPuUE8e1bH60i1IhOzT0NjLtCD+lKEWB1KTQ==", + "dev": true + }, "chalk": { "version": "2.4.1", "resolved": "https://registry.npmjs.org/chalk/-/chalk-2.4.1.tgz", @@ -400,6 +426,12 @@ "integrity": "sha1-tUc7M9yXxCTl2Y3IfVXU2KKci/I=", "dev": true }, + "check-error": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/check-error/-/check-error-1.0.2.tgz", + "integrity": "sha1-V00xLt2Iu13YkS6Sht1sCu1KrII=", + "dev": true + }, "cipher-base": { "version": "1.0.4", "resolved": "https://registry.npmjs.org/cipher-base/-/cipher-base-1.0.4.tgz", @@ -606,6 +638,15 @@ "resolved": "https://registry.npmjs.org/decamelize/-/decamelize-1.2.0.tgz", "integrity": "sha1-9lNNFRSCabIDUue+4m9QH5oZEpA=" }, + "deep-eql": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/deep-eql/-/deep-eql-3.0.1.tgz", + "integrity": "sha512-+QeIQyN5ZuO+3Uk5DYh6/1eKO0m0YmJFGNmFHGACpf1ClL1nmlV/p4gNgbl2pJGxgXb4faqo6UE+M5ACEMyVcw==", + "dev": true, + "requires": { + "type-detect": "^4.0.0" + } + }, "deep-is": { "version": "0.1.3", "resolved": "https://registry.npmjs.org/deep-is/-/deep-is-0.1.3.tgz", @@ -1351,6 +1392,12 @@ "resolved": "https://registry.npmjs.org/get-caller-file/-/get-caller-file-1.0.2.tgz", "integrity": "sha1-9wLmMSfn4jHBYKgMFVSstw1QR+U=" }, + "get-func-name": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/get-func-name/-/get-func-name-2.0.0.tgz", + "integrity": "sha1-6td0q+5y4gQJQzoGY2YCPdaIekE=", + "dev": true + }, "get-stdin": { "version": "5.0.1", "resolved": "https://registry.npmjs.org/get-stdin/-/get-stdin-5.0.1.tgz", @@ -2279,6 +2326,12 @@ "integrity": "sha1-PBrfhx6pzWyUMbbqK9dKD/BVxME=", "dev": true }, + "pathval": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/pathval/-/pathval-1.1.0.tgz", + "integrity": "sha1-uULm1L3mUwBe9rcTYd74cn0GReA=", + "dev": true + }, "pbkdf2": { "version": "3.0.16", "resolved": "https://registry.npmjs.org/pbkdf2/-/pbkdf2-3.0.16.tgz", @@ -3571,6 +3624,12 @@ "prelude-ls": "~1.1.2" } }, + "type-detect": { + "version": "4.0.8", + "resolved": "https://registry.npmjs.org/type-detect/-/type-detect-4.0.8.tgz", + "integrity": "sha512-0fr/mIH1dlO+x7TlcMy+bIDqKPsw/70tVyeHW787goQjhmqaZe10uwLujubK9q9Lg6Fiho1KUKDYz0Z7k7g5/g==", + "dev": true + }, "typedarray": { "version": "0.0.6", "resolved": "https://registry.npmjs.org/typedarray/-/typedarray-0.0.6.tgz", diff --git a/package.json b/package.json index b0a1988..d7dfbd3 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/unit/market_oracle.js b/test/unit/market_oracle.js index 0687bfe..70d5bb4 100644 --- a/test/unit/market_oracle.js +++ b/test/unit/market_oracle.js @@ -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); @@ -28,7 +32,7 @@ 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); }); }); @@ -36,7 +40,7 @@ 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); }); @@ -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); }); }); }); @@ -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); }); @@ -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); }); }); }); @@ -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); }); @@ -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); }); }); }); @@ -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); }); }); }); @@ -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); }); }); }); @@ -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(); }); @@ -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); }); }); }); @@ -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(); }); @@ -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); }); }); @@ -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(); }); @@ -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); }); }); }); diff --git a/test/unit/market_source.js b/test/unit/market_source.js index 7f0428a..c58f8d0 100644 --- a/test/unit/market_source.js +++ b/test/unit/market_source.js @@ -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); @@ -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); }); }); @@ -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); }); }); diff --git a/test/unit/market_source_factory.js b/test/unit/market_source_factory.js index b0b59eb..0597295 100644 --- a/test/unit/market_source_factory.js +++ b/test/unit/market_source_factory.js @@ -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]; @@ -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); }); }); });