ref tests

This commit is contained in:
Matias Alejo Garcia 2016-07-27 10:25:13 -03:00
parent 63e6847767
commit 19d95d9a49
No known key found for this signature in database
GPG Key ID: 02470DB551277AB3
2 changed files with 112 additions and 95 deletions

View File

@ -2788,10 +2788,9 @@ WalletService.prototype.getTxHistory = function(opts, cb) {
bc.getTransactions(addressStrs, from, to, function(err, txs, total) {
if (err) return cb(err);
var txsNormalized = self._normalizeTxHistory(txs);
next(err, txsNormalized);
if (addresses.length < Defaults.HISTORY_CACHE_ADDRESS_THRESOLD)
return;
return next(err, txsNormalized);
var txsToCache = _.filter(txsNormalized, function(i) {
return i.confirmations >= Defaults.CONFIRMATIONS_TO_START_CACHING;
@ -2799,7 +2798,7 @@ WalletService.prototype.getTxHistory = function(opts, cb) {
var index = total - to;
if (index < 0) index = 0;
self.storage.storeTxHistoryCache(self.walletId, total, index, txsToCache, function(err) {
if (err) log.warn(err);
next(err, txsNormalized);
})
});
},

View File

@ -5895,7 +5895,7 @@ describe('Wallet service', function() {
});
});
describe.only('#getTxHistory', function() {
describe('#getTxHistory', function() {
var server, wallet, mainAddresses, changeAddresses;
beforeEach(function(done) {
helpers.createAndJoinWallet(1, 1, function(s, w) {
@ -5924,97 +5924,6 @@ describe('Wallet service', function() {
});
});
it('should store partial cache tx history from insight', function(done) {
var h = helpers.historyCacheTest(200);
helpers.stubHistory(h);
var spy = sinon.spy(server.storage, 'storeTxHistoryCache');
var toCache = _.filter(h, function(i) {
return i.confirmations >= server.confirmationsToStartCaching;
});
var skip = 95;
var limit = 10;
server.getTxHistory({
skip: skip,
limit: limit,
}, function(err, txs) {
// FROM the END, we are getting items
// End-1, end-2, end-3.
should.not.exist(err);
should.exist(txs);
txs.length.should.equal(limit);
var calls = spy.getCalls();
calls.length.should.equal(1);
calls[0].args[1].should.equal(200); // total
calls[0].args[2].should.equal(200 - skip - limit); // position
calls[0].args[3].length.should.equal(5); // 5 txs have confirmations>= 100
// should be reversed!
calls[0].args[3][0].confirmations.should.equal(104);
calls[0].args[3][0].txid.should.equal(h[104].txid);
server.storage.storeTxHistoryCache.restore();
done();
});
});
it('should not cache tx history from insight', function(done) {
var h = helpers.historyCacheTest(200);
helpers.stubHistory(h);
var spy = sinon.spy(server.storage, 'storeTxHistoryCache');
server.getTxHistory({
skip:0,
limit: 10,
}, function(err, txs) {
should.not.exist(err);
should.exist(txs);
var calls = spy.getCalls();
calls.length.should.equal(1);
calls[0].args[3].length.should.equal(0);
server.storage.storeTxHistoryCache.restore();
done();
});
});
it('should store cache all tx history from insight', function(done) {
var h = helpers.historyCacheTest(200);
helpers.stubHistory(h);
var spy = sinon.spy(server.storage, 'storeTxHistoryCache');
var toCache = _.filter(h, function(i) {
return i.confirmations >= server.confirmationsToStartCaching;
});
var skip = 195;
var limit = 5;
server.getTxHistory({
skip: skip,
limit: limit,
}, function(err, txs) {
should.not.exist(err);
should.exist(txs);
txs.length.should.equal(limit);
var calls = spy.getCalls();
calls.length.should.equal(1);
calls[0].args[1].should.equal(200); // total
calls[0].args[2].should.equal(200 - skip - limit); // position
calls[0].args[3].length.should.equal(5);
// should be reversed!
calls[0].args[3][0].confirmations.should.equal(199);
calls[0].args[3][0].txid.should.equal(h[199].txid);
server.storage.storeTxHistoryCache.restore();
done();
});
});
it('should get tx history for incoming txs', function(done) {
server._normalizeTxHistory = sinon.stub().returnsArg(0);
var txs = [{
@ -6310,6 +6219,115 @@ describe('Wallet service', function() {
});
});
describe('#getTxHistory cache', function() {
var server, wallet, mainAddresses, changeAddresses;
var _threshold = Defaults.HISTORY_CACHE_ADDRESS_THRESOLD;
beforeEach(function(done) {
Defaults.HISTORY_CACHE_ADDRESS_THRESOLD = 1;
helpers.createAndJoinWallet(1, 1, function(s, w) {
server = s;
wallet = w;
helpers.createAddresses(server, wallet, 1, 1, function(main, change) {
mainAddresses = main;
changeAddresses = change;
done();
});
});
});
afterEach(function() {
Defaults.HISTORY_CACHE_ADDRESS_THRESOLD = _threshold;
});
it('should store partial cache tx history from insight', function(done) {
var h = helpers.historyCacheTest(200);
helpers.stubHistory(h);
var spy = sinon.spy(server.storage, 'storeTxHistoryCache');
var toCache = _.filter(h, function(i) {
return i.confirmations >= server.confirmationsToStartCaching;
});
var skip = 95;
var limit = 10;
server.getTxHistory({
skip: skip,
limit: limit,
}, function(err, txs) {
// FROM the END, we are getting items
// End-1, end-2, end-3.
should.not.exist(err);
should.exist(txs);
txs.length.should.equal(limit);
var calls = spy.getCalls();
calls.length.should.equal(1);
calls[0].args[1].should.equal(200); // total
calls[0].args[2].should.equal(200 - skip - limit); // position
calls[0].args[3].length.should.equal(5); // 5 txs have confirmations>= 100
// should be reversed!
calls[0].args[3][0].confirmations.should.equal(104);
calls[0].args[3][0].txid.should.equal(h[104].txid);
server.storage.storeTxHistoryCache.restore();
done();
});
});
it('should not cache tx history from insight', function(done) {
var h = helpers.historyCacheTest(200);
helpers.stubHistory(h);
var spy = sinon.spy(server.storage, 'storeTxHistoryCache');
server.getTxHistory({
skip: 0,
limit: 10,
}, function(err, txs) {
should.not.exist(err);
should.exist(txs);
var calls = spy.getCalls();
calls.length.should.equal(1);
calls[0].args[3].length.should.equal(0);
server.storage.storeTxHistoryCache.restore();
done();
});
});
it('should store cache all tx history from insight', function(done) {
var h = helpers.historyCacheTest(200);
helpers.stubHistory(h);
var spy = sinon.spy(server.storage, 'storeTxHistoryCache');
var toCache = _.filter(h, function(i) {
return i.confirmations >= server.confirmationsToStartCaching;
});
var skip = 195;
var limit = 5;
server.getTxHistory({
skip: skip,
limit: limit,
}, function(err, txs) {
should.not.exist(err);
should.exist(txs);
txs.length.should.equal(limit);
var calls = spy.getCalls();
calls.length.should.equal(1);
calls[0].args[1].should.equal(200); // total
calls[0].args[2].should.equal(200 - skip - limit); // position
calls[0].args[3].length.should.equal(5);
// should be reversed!
calls[0].args[3][0].confirmations.should.equal(199);
calls[0].args[3][0].txid.should.equal(h[199].txid);
server.storage.storeTxHistoryCache.restore();
done();
});
});
});
describe('#scan', function() {
var server, wallet;