From 739e306c50848efcaaf85373a3e15093740d1dbd Mon Sep 17 00:00:00 2001 From: Manuel Araoz Date: Wed, 29 Apr 2015 20:10:29 -0300 Subject: [PATCH] one integration test working --- api/controllers/blocks.js | 6 ++++-- api/test/app.js | 10 +++++++++- api/test/data/blocks.js | 2 +- api/test/v1/blocks.js | 21 ++++++++++++--------- lib/services/block.js | 7 ++++--- 5 files changed, 30 insertions(+), 16 deletions(-) diff --git a/api/controllers/blocks.js b/api/controllers/blocks.js index 38514522..2fc43835 100644 --- a/api/controllers/blocks.js +++ b/api/controllers/blocks.js @@ -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()); }; diff --git a/api/test/app.js b/api/test/app.js index b0443d8e..96449c15 100644 --- a/api/test/app.js +++ b/api/test/app.js @@ -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; }; diff --git a/api/test/data/blocks.js b/api/test/data/blocks.js index cd82ccbf..f43d8d67 100644 --- a/api/test/data/blocks.js +++ b/api/test/data/blocks.js @@ -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; diff --git a/api/test/v1/blocks.js b/api/test/v1/blocks.js index 6a1537a2..0502a64d 100644 --- a/api/test/v1/blocks.js +++ b/api/test/v1/blocks.js @@ -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"', 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() { diff --git a/lib/services/block.js b/lib/services/block.js index 418a5944..523c22ba 100644 --- a/lib/services/block.js +++ b/lib/services/block.js @@ -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) {