Add tests for getTokensAsync including schema validation
This commit is contained in:
		
							
								
								
									
										12
									
								
								src/schemas/token_schema.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								src/schemas/token_schema.ts
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,12 @@
 | 
			
		||||
export const tokenSchema = {
 | 
			
		||||
    id: '/token',
 | 
			
		||||
    properties: {
 | 
			
		||||
        name: {type: 'string'},
 | 
			
		||||
        symbol: {type: 'string'},
 | 
			
		||||
        decimals: {type: 'number'},
 | 
			
		||||
        address: {type: 'string'},
 | 
			
		||||
        url: {type: 'string'},
 | 
			
		||||
    },
 | 
			
		||||
    required: ['name', 'symbol', 'decimals', 'address', 'url'],
 | 
			
		||||
    type: 'object',
 | 
			
		||||
};
 | 
			
		||||
@@ -1,5 +1,6 @@
 | 
			
		||||
import {Validator, ValidatorResult} from 'jsonschema';
 | 
			
		||||
import {ecSignatureSchema, ecSignatureParameter} from '../schemas/ec_signature_schema';
 | 
			
		||||
import {tokenSchema} from '../schemas/token_schema';
 | 
			
		||||
 | 
			
		||||
export class SchemaValidator {
 | 
			
		||||
    private validator: Validator;
 | 
			
		||||
@@ -7,6 +8,7 @@ export class SchemaValidator {
 | 
			
		||||
        this.validator = new Validator();
 | 
			
		||||
        this.validator.addSchema(ecSignatureParameter, ecSignatureParameter.id);
 | 
			
		||||
        this.validator.addSchema(ecSignatureSchema, ecSignatureSchema.id);
 | 
			
		||||
        this.validator.addSchema(tokenSchema, tokenSchema.id);
 | 
			
		||||
    }
 | 
			
		||||
    public validate(instance: object, schema: Schema): ValidatorResult {
 | 
			
		||||
        return this.validator.validate(instance, schema);
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										43
									
								
								test/token_registry_wrapper_test.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										43
									
								
								test/token_registry_wrapper_test.ts
									
									
									
									
									
										Normal file
									
								
							@@ -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);
 | 
			
		||||
            });
 | 
			
		||||
        });
 | 
			
		||||
    });
 | 
			
		||||
});
 | 
			
		||||
		Reference in New Issue
	
	Block a user