one integration test working

This commit is contained in:
Manuel Araoz 2015-04-29 20:10:29 -03:00
parent 6974c35a05
commit 739e306c50
5 changed files with 30 additions and 16 deletions

View File

@ -97,7 +97,9 @@ Blocks.list = function(req, res) {
// TODO: return block_summary instead of block_full
node.blockService.listBlocks(from, to, offset, limit)
.then(function(blocks) {
res.send(blocks);
res.send(blocks.map(function(b) {
return b.toObject();
}));
});
};
@ -110,7 +112,7 @@ Blocks.getLatest = function(req, res) {
};
Blocks.get = function(req, res) {
$.checkState(req.block instanceof Block);
$.checkState(req.block instanceof Block, JSON.stringify(req.block));
res.send(req.block.toObject());
};

View File

@ -1,7 +1,15 @@
'use strict';
var BitcoreHTTP = require('../lib/http');
var bitcore = require('bitcore');
module.exports = function(nodeMock) {
return process.env.INTEGRATION === 'true' ? BitcoreHTTP.create().app : new BitcoreHTTP(nodeMock).app;
if (process.env.INTEGRATION === 'true') {
var config = require('config');
var network = config.get('BitcoreHTTP.BitcoreNode').network;
console.log('Starting test suite', network, 'network');
bitcore.Networks.defaultNetwork = bitcore.Networks.get(network);
return BitcoreHTTP.create(config.get('BitcoreHTTP')).app;
}
return new BitcoreHTTP(nodeMock).app;
};

View File

@ -9,7 +9,7 @@ blockHexs.map(function(hex) {
var block = new Block(new Buffer(hex, 'hex'));
return block;
}).forEach(function(block) {
mockBlocks[block.id] = block.toObject();
mockBlocks[block.id] = block;
});
module.exports = mockBlocks;

View File

@ -54,50 +54,53 @@ describe('BitcoreHTTP v1 blocks routes', function() {
var end = to - 1e5;
var section = blockList.slice(start, end);
var ret = section.slice(offset, offset + limit);
console.log(ret);
return Promise.resolve(ret);
};
app = require('../app')(nodeMock);
agent = request(app);
});
describe.only('/blocks', function() {
var toObject = function(b) {
return b.toObject();
};
describe('/blocks', function() {
it('works with default parameters', function(cb) {
agent.get('/v1/blocks/')
.expect(200)
.expect(JSON.stringify(blockList), cb);
.expect(blockList.map(toObject), cb);
});
it('fails with to<from', function(cb) {
agent.get('/v1/blocks/?from=100000&to=99999')
.expect(422)
.expect('/v1/blocks/ "to" must be >= "from"', cb);
});
it('works with to/from parameters', function(cb) {
it.only('works with to/from parameters', function(cb) {
agent.get('/v1/blocks/?from=100000&to=100001')
.expect(200)
.expect(JSON.stringify([firstBlock]), cb);
.expect([firstBlock.toObject()], cb);
});
it('works with limit/offset parameters', function(cb) {
agent.get('/v1/blocks/?limit=1&offset=1')
.expect(200)
.expect(JSON.stringify([secondBlock]), cb);
.expect([secondBlock.toObject()], cb);
});
it('works with all parameters', function(cb) {
agent.get('/v1/blocks/?from=100005&to=100020&limit=3&offset=2')
.expect(200)
.expect(JSON.stringify(last3), cb);
.expect(last3.map(toObject), cb);
});
it('works with all parameters 2', function(cb) {
agent.get('/v1/blocks/?from=100000&to=100005&limit=2&offset=2')
.expect(200)
.expect(JSON.stringify(some2), cb);
.expect(some2.map(toObject), cb);
});
});
describe('/blocks/latest', function() {
it('returns latest block', function(cb) {
agent.get('/v1/blocks/latest')
.expect(200)
.expect(lastBlock, cb);
.expect(lastBlock.toObject(), cb);
});
});
describe('/blocks/:blockHash', function() {

View File

@ -91,7 +91,7 @@ BlockService.blockRPCtoBitcore = function(blockData) {
nonce: blockData.nonce,
bits: new bitcore.deps.bnjs(
new bitcore.deps.Buffer(blockData.bits, 'hex')
),
).toNumber(),
merkleRoot: bitcore.util.buffer.reverse(
new bitcore.deps.Buffer(blockData.merkleroot, 'hex')
)
@ -192,6 +192,7 @@ BlockService.prototype.getBlockByHeight = function(height) {
* @param {Number} to ditto, but for the upper limit, non inclusive
* @param {Number} offset skip the first offset blocks
* @param {Number} limit max amount of blocks returned
* @return {Array} a list of blocks
*
*/
BlockService.prototype.listBlocks = function(from, to, offset, limit) {
@ -207,12 +208,12 @@ BlockService.prototype.listBlocks = function(from, to, offset, limit) {
var blocks = [];
// TODO: optimize: precompute heights and fetch all blocks in parallel?
var fetchBlock = function(height) {
if (height > end) {
if (height >= end) {
return;
}
return self.getBlockByHeight(height)
.then(function(block) {
blocks.push(block.toObject());
blocks.push(block);
return fetchBlock(height + 1);
})
.catch(function(err) {