add test for abi and blockchain config; fix whisper config

This commit is contained in:
Iuri Matias 2016-10-14 07:01:54 -04:00
parent 2b56ec6790
commit 969e4015cb
29 changed files with 160 additions and 37 deletions

View File

@ -18,10 +18,15 @@ var Blockchain = function(blockchainConfig, Client) {
port: this.blockchainConfig.port || 30303,
nodiscover: this.blockchainConfig.nodiscover || false,
mine: this.blockchainConfig.mine || false,
whisper: this.blockchainConfig.whisper || true,
account: this.blockchainConfig.account || {}
};
if (this.blockchainConfig.whisper === false) {
this.config.whisper = false;
} else {
this.config.whisper = (this.blockchainConfig.whisper || true);
}
this.client = new Client({config: this.config});
};

46
old_test/blockchain.js Normal file
View File

@ -0,0 +1,46 @@
var Config = require('../lib/config/config.js');
var Blockchain = require('../lib/blockchain.js');
var assert = require('assert');
var sinon = require('sinon');
describe('embark.blockchain', function() {
var blockchainConfig = (new Config.Blockchain()).loadConfigFile('test/support/blockchain.yml').config("development");
describe('#generate_basic_command', function() {
var blockchain = new Blockchain(blockchainConfig);
it('should return correct cmd', function() {
assert.strictEqual(blockchain.generate_basic_command(), "geth --datadir=\"/tmp/embark\" --password config/password --port 30303 --rpc --rpcport 8101 --rpcaddr localhost --networkid "+blockchainConfig.networkId+" --rpccorsdomain=\"*\" --minerthreads \"1\" --mine --rpcapi \"eth,web3\" --maxpeers 4 ");
});
});
describe('#list_command', function() {
var blockchain = new Blockchain(blockchainConfig);
blockchain.generate_basic_command = sinon.stub().returns("geth ");
it('should generate command to list accounts', function() {
assert.equal(blockchain.list_command(), "geth --datadir=\"/tmp/embark\" --password config/password account list ");
});
});
describe('#init_command', function() {
var blockchain = new Blockchain(blockchainConfig);
blockchain.generate_basic_command = sinon.stub().returns("geth ");
it('should generate command to create an account', function() {
assert.equal(blockchain.init_command(), "geth --datadir=\"/tmp/embark\" --password config/password account new ");
});
});
describe('#run_command', function() {
describe('with mine when needed config set', function() {
var blockchain = new Blockchain(blockchainConfig);
blockchain.generate_basic_command = sinon.stub().returns("geth ");
it('should generate run command with script ', function() {
assert.equal(blockchain.run_command(), "geth js node_modules/embark-framework/js/mine.js");
});
});
});
});

59
test/abi.js Normal file
View File

@ -0,0 +1,59 @@
/*globals describe, it*/
var ABIGenerator = require('../lib/abi.js');
var assert = require('assert');
// TODO: instead 'eval' the code with a fake web3 object
// and check the generate code interacts as expected
describe('embark.ABIGenerator', function() {
describe('#generateProvider', function() {
var generator = new ABIGenerator({rpcHost: 'somehost', rpcPort: '1234'}, {});
it('should generate code to connect to a provider', function() {
var providerCode = "\nif (typeof web3 !== 'undefined' && typeof Web3 !== 'undefined') {\n\tweb3 = new Web3(web3.currentProvider);\n} else if (typeof Web3 !== 'undefined') {\n\tweb3 = new Web3(new Web3.providers.HttpProvider(\"http://somehost:1234\"));\n}\nweb3.eth.defaultAccount = web3.eth.accounts[0];";
assert.equal(generator.generateProvider(), providerCode);
});
});
describe('#generateContracts', function() {
var generator = new ABIGenerator({}, {
contracts: {
SimpleStorage: {
abiDefinition: [{"constant":true,"inputs":[],"name":"storedData","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"x","type":"uint256"}],"name":"set","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"get","outputs":[{"name":"retVal","type":"uint256"}],"payable":false,"type":"function"},{"inputs":[{"name":"initialValue","type":"uint256"}],"type":"constructor"}],
gasEstimates: 12000,
deployedAddress: "0x123",
code: '12345'
},
Foo: {
abiDefinition: [{"constant":true,"inputs":[],"name":"storedData","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"x","type":"uint256"}],"name":"set","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"get","outputs":[{"name":"retVal","type":"uint256"}],"payable":false,"type":"function"},{"inputs":[{"name":"initialValue","type":"uint256"}],"type":"constructor"}],
gasEstimates: 12000,
deployedAddress: "0x124",
code: '123456'
}
}
});
describe('with EmbarkJS', function() {
var withEmbarkJS = true;
it('should generate contract code', function() {
var contractCode = "\n\nSimpleStorage = new EmbarkJS.Contract({abi: [{\"constant\":true,\"inputs\":[],\"name\":\"storedData\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"x\",\"type\":\"uint256\"}],\"name\":\"set\",\"outputs\":[],\"payable\":false,\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"get\",\"outputs\":[{\"name\":\"retVal\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"inputs\":[{\"name\":\"initialValue\",\"type\":\"uint256\"}],\"type\":\"constructor\"}], address: '0x123', code: '12345', gasEstimates: 12000});\nFoo = new EmbarkJS.Contract({abi: [{\"constant\":true,\"inputs\":[],\"name\":\"storedData\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"x\",\"type\":\"uint256\"}],\"name\":\"set\",\"outputs\":[],\"payable\":false,\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"get\",\"outputs\":[{\"name\":\"retVal\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"inputs\":[{\"name\":\"initialValue\",\"type\":\"uint256\"}],\"type\":\"constructor\"}], address: '0x124', code: '123456', gasEstimates: 12000});";
assert.equal(generator.generateContracts(withEmbarkJS), contractCode);
});
});
describe('with default interface', function() {
var withEmbarkJS = false;
it('should generate contract code', function() {
var contractCode = "\n\nSimpleStorageAbi = [{\"constant\":true,\"inputs\":[],\"name\":\"storedData\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"x\",\"type\":\"uint256\"}],\"name\":\"set\",\"outputs\":[],\"payable\":false,\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"get\",\"outputs\":[{\"name\":\"retVal\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"inputs\":[{\"name\":\"initialValue\",\"type\":\"uint256\"}],\"type\":\"constructor\"}];\nSimpleStorageContract = web3.eth.contract(SimpleStorageAbi);\nSimpleStorage = SimpleStorageContract.at('0x123');\nFooAbi = [{\"constant\":true,\"inputs\":[],\"name\":\"storedData\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"x\",\"type\":\"uint256\"}],\"name\":\"set\",\"outputs\":[],\"payable\":false,\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"get\",\"outputs\":[{\"name\":\"retVal\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"inputs\":[{\"name\":\"initialValue\",\"type\":\"uint256\"}],\"type\":\"constructor\"}];\nFooContract = web3.eth.contract(FooAbi);\nFoo = FooContract.at('0x124');";
assert.equal(generator.generateContracts(withEmbarkJS), contractCode);
});
});
});
//describe('#generateABI', function() {
//});
});

View File

@ -1,46 +1,59 @@
var Config = require('../lib/config/config.js');
/*globals describe, it*/
var Blockchain = require('../lib/blockchain.js');
var assert = require('assert');
var sinon = require('sinon');
describe('embark.blockchain', function() {
var blockchainConfig = (new Config.Blockchain()).loadConfigFile('test/support/blockchain.yml').config("development");
describe('embark.Blockchain', function() {
//var Client = function() {};
//Client.prototype.name = "ClientName";
describe('#generate_basic_command', function() {
var blockchain = new Blockchain(blockchainConfig);
describe('#initializer', function() {
//var client = new Client();
it('should return correct cmd', function() {
assert.strictEqual(blockchain.generate_basic_command(), "geth --datadir=\"/tmp/embark\" --password config/password --port 30303 --rpc --rpcport 8101 --rpcaddr localhost --networkid "+blockchainConfig.networkId+" --rpccorsdomain=\"*\" --minerthreads \"1\" --mine --rpcapi \"eth,web3\" --maxpeers 4 ");
describe('with empty config', function() {
it('should have a default config', function() {
var config = {
networkType: 'custom',
genesisBlock: false,
datadir: false,
mineWhenNeeded: false,
rpcHost: 'localhost',
rpcPort: 8545,
rpcCorsDomain: false,
networkId: 12301,
port: 30303,
nodiscover: false,
mine: false,
whisper: true,
account: {}
};
var blockchain = Blockchain(config, 'geth');
assert.deepEqual(blockchain.config, config);
});
});
describe('#list_command', function() {
var blockchain = new Blockchain(blockchainConfig);
blockchain.generate_basic_command = sinon.stub().returns("geth ");
describe('with config', function() {
it('should take config params', function() {
var config = {
networkType: 'livenet',
genesisBlock: 'foo/bar/genesis.json',
datadir: '/foo/datadir/',
mineWhenNeeded: true,
rpcHost: 'someserver',
rpcPort: 12345,
rpcCorsDomain: true,
networkId: 1,
port: 123456,
nodiscover: true,
mine: true,
whisper: false,
account: {}
};
var blockchain = Blockchain(config, 'geth');
it('should generate command to list accounts', function() {
assert.equal(blockchain.list_command(), "geth --datadir=\"/tmp/embark\" --password config/password account list ");
});
});
describe('#init_command', function() {
var blockchain = new Blockchain(blockchainConfig);
blockchain.generate_basic_command = sinon.stub().returns("geth ");
it('should generate command to create an account', function() {
assert.equal(blockchain.init_command(), "geth --datadir=\"/tmp/embark\" --password config/password account new ");
});
});
describe('#run_command', function() {
describe('with mine when needed config set', function() {
var blockchain = new Blockchain(blockchainConfig);
blockchain.generate_basic_command = sinon.stub().returns("geth ");
it('should generate run command with script ', function() {
assert.equal(blockchain.run_command(), "geth js node_modules/embark-framework/js/mine.js");
});
assert.deepEqual(blockchain.config, config);
});
});
});
});