From f3d5690d56c3e4c7243f72049c98cfde799413af Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Mon, 29 May 2017 23:52:10 +0200 Subject: Add tests for getTokensAsync including schema validation --- test/token_registry_wrapper_test.ts | 43 +++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 test/token_registry_wrapper_test.ts (limited to 'test') diff --git a/test/token_registry_wrapper_test.ts b/test/token_registry_wrapper_test.ts new file mode 100644 index 000000000..cd435c48c --- /dev/null +++ b/test/token_registry_wrapper_test.ts @@ -0,0 +1,43 @@ +import * as _ from 'lodash'; +import 'mocha'; +import * as chai from 'chai'; +import chaiAsPromised = require('chai-as-promised'); +import * as Web3 from 'web3'; +import {web3Factory} from './utils/web3_factory'; +import {ZeroEx} from '../src/0x.js'; +import {BlockchainLifecycle} from './utils/blockchain_lifecycle'; +import {Token} from '../src/types'; +import {SchemaValidator} from '../src/utils/schema_validator'; +import {tokenSchema} from '../src/schemas/token_schema'; + +const expect = chai.expect; +chai.use(chaiAsPromised); +const blockchainLifecycle = new BlockchainLifecycle(); + +const TOKEN_REGISTRY_SIZE_AFTER_MIGRATION = 7; + +describe('TokenRegistryWrapper', () => { + let zeroEx: ZeroEx; + before(async () => { + const web3 = web3Factory.create(); + zeroEx = new ZeroEx(web3); + }); + beforeEach(async () => { + await blockchainLifecycle.startAsync(); + }); + afterEach(async () => { + await blockchainLifecycle.revertAsync(); + }); + describe('#getTokensAsync', () => { + it('should return all the tokens added to the tokenRegistry during the migration', async () => { + const tokens = await zeroEx.tokenRegistry.getTokensAsync(); + expect(tokens.length).to.be.equal(TOKEN_REGISTRY_SIZE_AFTER_MIGRATION); + + const schemaValidator = new SchemaValidator(); + _.each(tokens, token => { + const validationResult = schemaValidator.validate(token, tokenSchema); + expect(validationResult.errors.length).to.be.equal(0); + }); + }); + }); +}); -- cgit v1.2.3 From 4d63a4d02a68974df75c9cae85d43c0ba628c740 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Tue, 30 May 2017 10:49:34 +0200 Subject: use to.have.lengthOf --- test/token_registry_wrapper_test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'test') diff --git a/test/token_registry_wrapper_test.ts b/test/token_registry_wrapper_test.ts index cd435c48c..c91555d8b 100644 --- a/test/token_registry_wrapper_test.ts +++ b/test/token_registry_wrapper_test.ts @@ -31,12 +31,12 @@ describe('TokenRegistryWrapper', () => { describe('#getTokensAsync', () => { it('should return all the tokens added to the tokenRegistry during the migration', async () => { const tokens = await zeroEx.tokenRegistry.getTokensAsync(); - expect(tokens.length).to.be.equal(TOKEN_REGISTRY_SIZE_AFTER_MIGRATION); + expect(tokens).to.have.lengthOf(TOKEN_REGISTRY_SIZE_AFTER_MIGRATION); const schemaValidator = new SchemaValidator(); _.each(tokens, token => { const validationResult = schemaValidator.validate(token, tokenSchema); - expect(validationResult.errors.length).to.be.equal(0); + expect(validationResult.errors).to.have.lengthOf(0); }); }); }); -- cgit v1.2.3 From c536114b1638e30e5053dfc33d8e16465c0d373c Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Tue, 30 May 2017 12:25:17 +0200 Subject: make web3Wrapper a private instance variable --- test/0x.js_test.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'test') diff --git a/test/0x.js_test.ts b/test/0x.js_test.ts index bb312a00f..c45c70991 100644 --- a/test/0x.js_test.ts +++ b/test/0x.js_test.ts @@ -194,9 +194,9 @@ describe('ZeroEx library', () => { const web3 = web3Factory.create(); const zeroEx = new ZeroEx(web3); stubs = [ - Sinon.stub(zeroEx.web3Wrapper, 'getNodeVersionAsync') + Sinon.stub((zeroEx as any).web3Wrapper, 'getNodeVersionAsync') .returns(Promise.resolve(newParityNodeVersion)), - Sinon.stub(zeroEx.web3Wrapper, 'signTransactionAsync') + Sinon.stub((zeroEx as any).web3Wrapper, 'signTransactionAsync') .returns(Promise.resolve(signature)), Sinon.stub(ZeroEx, 'isValidSignature').returns(true), ]; @@ -218,9 +218,9 @@ describe('ZeroEx library', () => { const web3 = web3Factory.create(); const zeroEx = new ZeroEx(web3); stubs = [ - Sinon.stub(zeroEx.web3Wrapper, 'getNodeVersionAsync') + Sinon.stub((zeroEx as any).web3Wrapper, 'getNodeVersionAsync') .returns(Promise.resolve(newParityNodeVersion)), - Sinon.stub(zeroEx.web3Wrapper, 'signTransactionAsync') + Sinon.stub((zeroEx as any).web3Wrapper, 'signTransactionAsync') .returns(Promise.resolve(signature)), Sinon.stub(ZeroEx, 'isValidSignature').returns(true), ]; -- cgit v1.2.3 From d15002a1109ea00f4d92c7ae6259b5b657f5e4b3 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Tue, 30 May 2017 12:30:38 +0200 Subject: Add tests for setProvider --- test/0x.js_test.ts | 23 +++++++++++++++++++++++ test/utils/web3_factory.ts | 10 +++++++--- 2 files changed, 30 insertions(+), 3 deletions(-) (limited to 'test') diff --git a/test/0x.js_test.ts b/test/0x.js_test.ts index c45c70991..1e76ae71a 100644 --- a/test/0x.js_test.ts +++ b/test/0x.js_test.ts @@ -4,6 +4,7 @@ import 'mocha'; import * as BigNumber from 'bignumber.js'; import ChaiBigNumber = require('chai-bignumber'); import * as Sinon from 'sinon'; +import ProviderEngine = require('web3-provider-engine'); import {ZeroEx} from '../src/0x.js'; import {constants} from './utils/constants'; import {web3Factory} from './utils/web3_factory'; @@ -13,6 +14,28 @@ chai.use(ChaiBigNumber()); const expect = chai.expect; describe('ZeroEx library', () => { + describe('#setProvider', () => { + it('overrides the provider in the nested web3 instance and invalidates contractInstances', async () => { + const web3 = web3Factory.create(); + const zeroEx = new ZeroEx(web3); + // Instantiate the exchangeContract instance with the current provider + await (zeroEx.exchange as any).instantiateExchangeContractIfDoesntExistAsync(); + + const newProvider = web3Factory.getRpcProvider(); + // Add property to newProvider so that we can differentiate it from old provider + (newProvider as any).zeroExTestId = 1; + zeroEx.setProvider(newProvider); + + // Check that exchangeContract instance removed after provider update + expect((zeroEx.exchange as any).exchangeContractIfExists).to.be.an('undefined'); + + // Check that all nested web3 instances return the updated provider + const nestedWeb3WrapperProvider = (zeroEx as any).web3Wrapper.getCurrentProvider(); + expect((nestedWeb3WrapperProvider as any).zeroExTestId).to.be.a('number'); + const contractWrapperWeb3WrapperProvider = zeroEx.exchange.web3Wrapper.getCurrentProvider(); + expect((nestedWeb3WrapperProvider as any).zeroExTestId).to.be.a('number'); + }); + }); describe('#getOrderHash', () => { const expectedOrderHash = '0x103a5e97dab5dbeb8f385636f86a7d1e458a7ccbe1bd194727f0b2f85ab116c7'; it('defaults takerAddress to NULL address', () => { diff --git a/test/utils/web3_factory.ts b/test/utils/web3_factory.ts index 493fbc2df..ffdc0e4cf 100644 --- a/test/utils/web3_factory.ts +++ b/test/utils/web3_factory.ts @@ -10,14 +10,18 @@ import {constants} from './constants'; export const web3Factory = { create(): Web3 { + const provider = this.getRpcProvider(); + const web3 = new Web3(); + web3.setProvider(provider); + return web3; + }, + getRpcProvider(): Web3.Provider { const provider = new ProviderEngine(); const rpcUrl = `http://${constants.RPC_HOST}:${constants.RPC_PORT}`; provider.addProvider(new RpcSubprovider({ rpcUrl, })); provider.start(); - const web3 = new Web3(); - web3.setProvider(provider); - return web3; + return provider; }, }; -- cgit v1.2.3 From a3ce892f85e7a8c1bb0e707f1e64f204a2ce29c6 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Tue, 30 May 2017 12:43:50 +0200 Subject: Make tokenRegistry contract instantiation lazy and clear it on provider update --- test/0x.js_test.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'test') diff --git a/test/0x.js_test.ts b/test/0x.js_test.ts index 1e76ae71a..4125b83fe 100644 --- a/test/0x.js_test.ts +++ b/test/0x.js_test.ts @@ -26,8 +26,9 @@ describe('ZeroEx library', () => { (newProvider as any).zeroExTestId = 1; zeroEx.setProvider(newProvider); - // Check that exchangeContract instance removed after provider update + // Check that contractInstances with old provider are removed after provider update expect((zeroEx.exchange as any).exchangeContractIfExists).to.be.an('undefined'); + expect((zeroEx.tokenRegistry as any).tokenRegistryContractIfExists).to.be.an('undefined'); // Check that all nested web3 instances return the updated provider const nestedWeb3WrapperProvider = (zeroEx as any).web3Wrapper.getCurrentProvider(); -- cgit v1.2.3 From e2d0fd9cf9a712d44470e29dc2c7b95262e34d2c Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Tue, 30 May 2017 12:47:53 +0200 Subject: remove unused import --- test/exchange_wrapper.ts | 1 - 1 file changed, 1 deletion(-) (limited to 'test') diff --git a/test/exchange_wrapper.ts b/test/exchange_wrapper.ts index 4bfc49374..55b84ce36 100644 --- a/test/exchange_wrapper.ts +++ b/test/exchange_wrapper.ts @@ -1,7 +1,6 @@ import 'mocha'; import * as chai from 'chai'; import chaiAsPromised = require('chai-as-promised'); -import * as Web3 from 'web3'; import {web3Factory} from './utils/web3_factory'; import {ZeroEx} from '../src/0x.js'; import {BlockchainLifecycle} from './utils/blockchain_lifecycle'; -- cgit v1.2.3 From 6aded80b2f1fd630323cdc97a4d66f1e82ce1632 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Tue, 30 May 2017 12:51:34 +0200 Subject: remove unused import --- test/0x.js_test.ts | 1 - 1 file changed, 1 deletion(-) (limited to 'test') diff --git a/test/0x.js_test.ts b/test/0x.js_test.ts index 4125b83fe..496b2780a 100644 --- a/test/0x.js_test.ts +++ b/test/0x.js_test.ts @@ -4,7 +4,6 @@ import 'mocha'; import * as BigNumber from 'bignumber.js'; import ChaiBigNumber = require('chai-bignumber'); import * as Sinon from 'sinon'; -import ProviderEngine = require('web3-provider-engine'); import {ZeroEx} from '../src/0x.js'; import {constants} from './utils/constants'; import {web3Factory} from './utils/web3_factory'; -- cgit v1.2.3 From 007c57ec4049ce4c929cbf97ca000a375570aeb4 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Tue, 30 May 2017 12:52:28 +0200 Subject: Also instantiate tokenRegistry contract instance --- test/0x.js_test.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'test') diff --git a/test/0x.js_test.ts b/test/0x.js_test.ts index 496b2780a..21e4b5a3b 100644 --- a/test/0x.js_test.ts +++ b/test/0x.js_test.ts @@ -17,8 +17,9 @@ describe('ZeroEx library', () => { it('overrides the provider in the nested web3 instance and invalidates contractInstances', async () => { const web3 = web3Factory.create(); const zeroEx = new ZeroEx(web3); - // Instantiate the exchangeContract instance with the current provider + // Instantiate the contract instances with the current provider await (zeroEx.exchange as any).instantiateExchangeContractIfDoesntExistAsync(); + await (zeroEx.tokenRegistry as any).instantiateTokenRegistryContractIfDoesntExistAsync(); const newProvider = web3Factory.getRpcProvider(); // Add property to newProvider so that we can differentiate it from old provider -- cgit v1.2.3 From eefc8ca7ecb0543da3dad54ad1146eacb5a446f6 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Tue, 30 May 2017 12:53:42 +0200 Subject: add assertions that contractInstances are not undefined before provider update --- test/0x.js_test.ts | 2 ++ 1 file changed, 2 insertions(+) (limited to 'test') diff --git a/test/0x.js_test.ts b/test/0x.js_test.ts index 21e4b5a3b..041c5b433 100644 --- a/test/0x.js_test.ts +++ b/test/0x.js_test.ts @@ -20,6 +20,8 @@ describe('ZeroEx library', () => { // Instantiate the contract instances with the current provider await (zeroEx.exchange as any).instantiateExchangeContractIfDoesntExistAsync(); await (zeroEx.tokenRegistry as any).instantiateTokenRegistryContractIfDoesntExistAsync(); + expect((zeroEx.exchange as any).exchangeContractIfExists).to.not.be.an('undefined'); + expect((zeroEx.tokenRegistry as any).tokenRegistryContractIfExists).to.not.be.an('undefined'); const newProvider = web3Factory.getRpcProvider(); // Add property to newProvider so that we can differentiate it from old provider -- cgit v1.2.3 From 8561258ded12bc7d284933e61f6d052941258f40 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Tue, 30 May 2017 12:56:10 +0200 Subject: Also test that web3Wrapper nested under tokenRegistryWrapper has updated provider --- test/0x.js_test.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'test') diff --git a/test/0x.js_test.ts b/test/0x.js_test.ts index 041c5b433..0c2afe879 100644 --- a/test/0x.js_test.ts +++ b/test/0x.js_test.ts @@ -35,8 +35,10 @@ describe('ZeroEx library', () => { // Check that all nested web3 instances return the updated provider const nestedWeb3WrapperProvider = (zeroEx as any).web3Wrapper.getCurrentProvider(); expect((nestedWeb3WrapperProvider as any).zeroExTestId).to.be.a('number'); - const contractWrapperWeb3WrapperProvider = zeroEx.exchange.web3Wrapper.getCurrentProvider(); - expect((nestedWeb3WrapperProvider as any).zeroExTestId).to.be.a('number'); + const exchangeWeb3WrapperProvider = zeroEx.exchange.web3Wrapper.getCurrentProvider(); + expect((exchangeWeb3WrapperProvider as any).zeroExTestId).to.be.a('number'); + const tokenRegistryWeb3WrapperProvider = zeroEx.tokenRegistry.web3Wrapper.getCurrentProvider(); + expect((tokenRegistryWeb3WrapperProvider as any).zeroExTestId).to.be.a('number'); }); }); describe('#getOrderHash', () => { -- cgit v1.2.3 From ad14c307cec8e0e7621b47e9e1ecb2652985190f Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Tue, 30 May 2017 14:23:44 +0200 Subject: Refactor exchangeWrapper and tokenRegistryWrapper to use contract getter that instantiates contract instance if not already instantiated --- test/0x.js_test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'test') diff --git a/test/0x.js_test.ts b/test/0x.js_test.ts index 0c2afe879..b404b27ba 100644 --- a/test/0x.js_test.ts +++ b/test/0x.js_test.ts @@ -18,8 +18,8 @@ describe('ZeroEx library', () => { const web3 = web3Factory.create(); const zeroEx = new ZeroEx(web3); // Instantiate the contract instances with the current provider - await (zeroEx.exchange as any).instantiateExchangeContractIfDoesntExistAsync(); - await (zeroEx.tokenRegistry as any).instantiateTokenRegistryContractIfDoesntExistAsync(); + await (zeroEx.exchange as any).getExchangeContractAsync(); + await (zeroEx.tokenRegistry as any).getTokenRegistryContractAsync(); expect((zeroEx.exchange as any).exchangeContractIfExists).to.not.be.an('undefined'); expect((zeroEx.tokenRegistry as any).tokenRegistryContractIfExists).to.not.be.an('undefined'); -- cgit v1.2.3 From 3522f94ff6cde1aad83299def7308025da9432d5 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Tue, 30 May 2017 14:26:07 +0200 Subject: use .be.undefined instead of .be.an('undefined') --- test/0x.js_test.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'test') diff --git a/test/0x.js_test.ts b/test/0x.js_test.ts index b404b27ba..5d23d7094 100644 --- a/test/0x.js_test.ts +++ b/test/0x.js_test.ts @@ -20,8 +20,8 @@ describe('ZeroEx library', () => { // Instantiate the contract instances with the current provider await (zeroEx.exchange as any).getExchangeContractAsync(); await (zeroEx.tokenRegistry as any).getTokenRegistryContractAsync(); - expect((zeroEx.exchange as any).exchangeContractIfExists).to.not.be.an('undefined'); - expect((zeroEx.tokenRegistry as any).tokenRegistryContractIfExists).to.not.be.an('undefined'); + expect((zeroEx.exchange as any).exchangeContractIfExists).to.not.be.undefined; + expect((zeroEx.tokenRegistry as any).tokenRegistryContractIfExists).to.not.be.undefined; const newProvider = web3Factory.getRpcProvider(); // Add property to newProvider so that we can differentiate it from old provider @@ -29,8 +29,8 @@ describe('ZeroEx library', () => { zeroEx.setProvider(newProvider); // Check that contractInstances with old provider are removed after provider update - expect((zeroEx.exchange as any).exchangeContractIfExists).to.be.an('undefined'); - expect((zeroEx.tokenRegistry as any).tokenRegistryContractIfExists).to.be.an('undefined'); + expect((zeroEx.exchange as any).exchangeContractIfExists).to.be.undefined; + expect((zeroEx.tokenRegistry as any).tokenRegistryContractIfExists).to.be.undefined; // Check that all nested web3 instances return the updated provider const nestedWeb3WrapperProvider = (zeroEx as any).web3Wrapper.getCurrentProvider(); -- cgit v1.2.3