integration tests

This commit is contained in:
Ivan Socolsky 2014-11-20 12:20:47 -03:00
parent 51b6416f1a
commit c07b770685
3 changed files with 109 additions and 8 deletions

View File

@ -96,7 +96,6 @@ exports.multitxs = function(req, res, next) {
function processTxs(txs, from, to, cb) {
txs = _.uniq(_.flatten(txs), 'txid');
var nbTxs = txs.length;
var paginated = !_.isUndefined(from) || !_.isUndefined(to);
@ -109,20 +108,18 @@ exports.multitxs = function(req, res, next) {
txs = txs.slice(start, end);
}
txs = _.pluck(txs, 'txid');
var transactions = [];
async.each(txs, function (tx, callback) {
tDb.fromIdWithInfo(tx, function(err, tx) {
tDb.fromIdWithInfo(tx.txid, function(err, tx) {
if (err) console.log(err);
if (tx && tx.info) {
transactions.push(tx.info);
_.find(txs, { txid: tx.txid }).info = tx.info;
}
callback();
});
}, function (err) {
if (err) return cb(err);
var transactions = _.pluck(txs, 'info');
if (paginated) {
transactions = {
totalItems: nbTxs,
@ -144,7 +141,7 @@ exports.multitxs = function(req, res, next) {
async.each(as, function(a, callback) {
a.update(function(err) {
if (err) callback(err);
txs = txs.concat(a.transactions);
txs.push(a.transactions);
callback();
}, {ignoreCache: req.param('noCache'), includeTxInfo: true});
}, function(err) { // finished callback
@ -152,6 +149,7 @@ exports.multitxs = function(req, res, next) {
processTxs(txs, from, to, function (err, transactions) {
if (err) return common.handleErrors(err, res);
res.jsonp(transactions);
return next();
});
});
}

71
test/integration/txs.js Normal file
View File

@ -0,0 +1,71 @@
#!/usr/bin/env node
'use strict';
process.env.NODE_ENV = process.env.NODE_ENV || 'development';
var _ = require('lodash');
var async = require('async');
var assert = require('assert');
var fs = require('fs');
var Address = require('../../app/models/Address');
var addresses = require('../../app/controllers/addresses');
var TransactionDb = require('../../lib/TransactionDb').default();
var fixture = JSON.parse(fs.readFileSync('test/integration/txs.json'));
var should = require('chai');
var sinon = require('sinon');
var txDb;
describe('Transactions for multiple addresses', function() {
this.timeout(5000);
var req, res;
before(function(c) {
txDb = TransactionDb;
var i = 0;
_.each(_.flatten(_.pluck(fixture, 'addrs')), function(addr) {
TransactionDb.deleteCacheForAddress(addr, function() {
if (++i === fixture.length) return c();
});
});
});
beforeEach(function(c) {
req = {};
res = {};
req.query = {};
res.jsonp = sinon.spy();
return c();
});
describe('All', function () {
_.each(fixture, function (f) {
it(f.test, function (done) {
req.param = sinon.stub().withArgs('addrs').returns(f.addrs.join(','));
var paginated = !_.isUndefined(f.from) || !_.isUndefined(f.to);
if (paginated) {
req.query = {
from: f.from,
to: f.to
};
}
addresses.multitxs(req, res, function() {
var txs = res.jsonp.getCall(0).args[0];
txs.should.exist;
if (paginated) {
txs.totalItems.should.equal(f.totalTransactions);
txs.items.length.should.equal(f.returnedTransactions);
if (f.transactions) {
JSON.stringify(_.pluck(txs.items, 'txid')).should.equal(JSON.stringify(f.transactions));
}
} else {
txs.should.be.instanceof(Array);
txs.length.should.equal(f.returnedTransactions);
}
done();
});
});
});
});
});

32
test/integration/txs.json Normal file
View File

@ -0,0 +1,32 @@
[{
"test": "should return non-paginated txs for single address",
"addrs": ["mtA6woo1wjCeu1dLkWgpSD3tRnRfrHt3FL"],
"totalTransactions": 13,
"returnedTransactions": 13
},{
"test": "should return paginated txs for single address",
"addrs": ["mtA6woo1wjCeu1dLkWgpSD3tRnRfrHt3FL"],
"totalTransactions": 13,
"from": 8,
"to": 12,
"returnedTransactions": 4
},{
"test": "should return non-paginated txs for multiple addresses",
"addrs": ["mqtYvjsUg59XpngBAYjFF51Xauc28a6Ckd","myDZmGN9euuRhM1mniSaXhQjNdqFmUzUkj"],
"totalTransactions": 4,
"returnedTransactions": 4
},{
"test": "should return paginated txs for multiple addresses",
"addrs": ["mqtYvjsUg59XpngBAYjFF51Xauc28a6Ckd","myDZmGN9euuRhM1mniSaXhQjNdqFmUzUkj", "n2NbTWCuUyTvpA2YKTpVD4SmCouFLcm8rS"],
"totalTransactions": 6,
"from": 2,
"to": 6,
"returnedTransactions": 4,
"transactions": [
"3e81723d069b12983b2ef694c9782d32fca26cc978de744acbc32c3d3496e915",
"b701b0680bc2b6f8b58f13d035a754486ecefd2438e3495cd8f395ab7f5e7ba9",
"80fb4fd2a6e5e795a4f30aebeda49424e7ed4d3630b128efb946aa964e0bc9c0",
"855e881892d7cd4a8fa81dbd8f6e9d142d02bffc10ffbe5428036ee55c3e3e0f"
]
}
]