From f41d7959211e2fbe318399d217c2f2b5448cef37 Mon Sep 17 00:00:00 2001 From: Matias Pando Date: Mon, 15 Dec 2014 18:15:20 -0300 Subject: [PATCH 1/7] Test for getHistoricRate --- js/models/RateService.js | 5 +++- test/RateService.js | 52 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/js/models/RateService.js b/js/models/RateService.js index aec56366d..de0072375 100644 --- a/js/models/RateService.js +++ b/js/models/RateService.js @@ -106,7 +106,10 @@ RateService.prototype.getHistoricRates = function(code, dates, cb) { }, function(err, res, body) { if (err || res.statusCode != 200 || !body) return cb(err || res); if (!_.isArray(body)) { - body = [{ ts: dates[0], rate: body.rate }]; + body = [{ + ts: dates[0], + rate: body.rate + }]; } return cb(null, body); }); diff --git a/test/RateService.js b/test/RateService.js index db2445976..278494bb9 100644 --- a/test/RateService.js +++ b/test/RateService.js @@ -1,5 +1,6 @@ 'use strict'; +var moment = require('moment'); var RateService = copay.RateService; describe('RateService model', function() { @@ -10,10 +11,10 @@ describe('RateService model', function() { describe('Fetching currencies', function() { var clock; - before(function () { + before(function() { clock = sinon.useFakeTimers(); }); - after(function () { + after(function() { clock.restore(); }); it('should retry fetching currencies on error', function() { @@ -174,6 +175,53 @@ describe('RateService model', function() { }); }); + describe('#getHistoricRate', function() { + it('should return historic rate', function() { + var yesterday = moment().subtract(1, 'day'); + var reqStub = sinon.stub(); + reqStub.get = sinon.stub().yields(null, { + statusCode: 200 + }, { + ts: yesterday, + rate: 100 + }); + + var rs = new RateService({ + request: reqStub + }); + rs.isAvailable = sinon.stub().returns(true); + + var params = [{ + code: 'USD', + date: yesterday, + expected: '100.00' + }]; + + _.each(params, function(p) { + rs.getHistoricRate('USD', yesterday, function(err, rate) { + rate.toFixed(2).should.equal(p.expected); + }); + }); + }); + it.only('should return error', function() { + var yesterday = moment().subtract(1, 'day'); + var reqStub = sinon.stub(); + reqStub.get = sinon.stub().yields(null, { + statusCode: 500 + }); + + var rs = new RateService({ + request: reqStub + }); + rs.isAvailable = sinon.stub().returns(true); + + rs.getHistoricRate('USD', yesterday, function(err, rate) { + err.statusCode.should.equal(500); + }); + + }); + }); + describe('#fromFiat', function() { it('should throw error when unavailable', function() { var rs = new RateService(); From 160113e9baa0def39fa4ae3eb12a4cdec51c6337 Mon Sep 17 00:00:00 2001 From: Matias Pando Date: Tue, 16 Dec 2014 15:19:47 -0300 Subject: [PATCH 2/7] Added test for RateService --- test/RateService.js | 98 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 94 insertions(+), 4 deletions(-) diff --git a/test/RateService.js b/test/RateService.js index 278494bb9..eb98c089a 100644 --- a/test/RateService.js +++ b/test/RateService.js @@ -82,7 +82,7 @@ describe('RateService model', function() { rs.isAvailable = sinon.stub().returns(false); (function() { rs.toFiat(10000, 'USD'); - }).should.throw; + }).should.throw('not available'); }); it('should return current valuation', function() { var rs = new RateService(); @@ -203,7 +203,7 @@ describe('RateService model', function() { }); }); }); - it.only('should return error', function() { + it('should return error', function() { var yesterday = moment().subtract(1, 'day'); var reqStub = sinon.stub(); reqStub.get = sinon.stub().yields(null, { @@ -222,13 +222,75 @@ describe('RateService model', function() { }); }); + describe('#getHistoricRates', function() { + it('should return historic rates for many dates', function() { + var yesterday = moment().subtract(1, 'day'); + var lastWeekday = moment().subtract(7, 'day'); + var reqStub = sinon.stub(); + reqStub.get = sinon.stub().yields(null, { + statusCode: 200 + }, [{ + ts: lastWeekday, + rate: 90, + }, { + ts: yesterday, + rate: 100, + }, ]); + + var rs = new RateService({ + request: reqStub + }); + rs.isAvailable = sinon.stub().returns(true); + + var params = [{ + code: 'USD', + date: lastWeekday, + expected: '90.00' + }, { + code: 'USD', + date: yesterday, + expected: '100.00' + }]; + + var dates = [lastWeekday, yesterday]; + rs.getHistoricRates('USD', dates, function(err, r) { + r.length.should.equal(2); + }); + + //same input dates should return only two values + dates.push(lastWeekday); + dates.push(yesterday); + rs.getHistoricRates('USD', dates, function(err, r) { + r.length.should.equal(2); + }); + }); + + it('should return error', function() { + var yesterday = moment().subtract(1, 'day'); + var reqStub = sinon.stub(); + reqStub.get = sinon.stub().yields(null, { + statusCode: 500 + }); + + var rs = new RateService({ + request: reqStub + }); + rs.isAvailable = sinon.stub().returns(true); + + var dates = [yesterday, yesterday]; + rs.getHistoricRates('USD', dates, function(err, rate) { + err.statusCode.should.equal(500); + }); + }); + }); + describe('#fromFiat', function() { it('should throw error when unavailable', function() { var rs = new RateService(); rs.isAvailable = sinon.stub().returns(false); (function() { rs.fromFiat(300, 'USD'); - }).should.throw; + }).should.throw('not available'); }); it('should return current valuation', function() { var rs = new RateService(); @@ -265,7 +327,9 @@ describe('RateService model', function() { rs.isAvailable = sinon.stub().returns(false); (function() { rs.listAlternatives(); - }).should.throw; + }).should.throw('not available');; + + rs.getAlternatives(); }); it('should return list of available currencies', function() { @@ -284,6 +348,32 @@ describe('RateService model', function() { var list = rs.listAlternatives(); list.should.exist; list.length.should.equal(2); + + list = rs.getAlternatives(); + list.should.exist; + list.length.should.equal(2); + }); + + it('should return an error when trying to get the list of available currencies', function() { + var rs = new RateService(); + rs.isAvailable = sinon.stub().returns(true); + rs.whenAvailable(function() {}); + rs.isAvailable = sinon.stub().returns(false); + rs.whenAvailable(function() {}); + + rs.getAlternatives().length.should.equal(0); + + (function() { + rs.listAlternatives(); + }).should.throw; + }); + }); + describe('#singleton', function() { + it('should create only one instance', function() { + var rs = RateService.singleton(); + rs.should.be.not.null; + rs = RateService.singleton(); + rs.should.be.not.null; }); }); }); From d10a644d3fd6b33c8d613edccfad20feaf59be31 Mon Sep 17 00:00:00 2001 From: Matias Pando Date: Tue, 16 Dec 2014 18:25:34 -0300 Subject: [PATCH 3/7] Added test for Wallet.js --- test/Wallet.js | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/test/Wallet.js b/test/Wallet.js index 2393973fc..c483e0262 100644 --- a/test/Wallet.js +++ b/test/Wallet.js @@ -173,6 +173,57 @@ describe('Wallet model', function() { (new bitcore.Address(w.generateAddress(true))).isValid().should.equal(true); }); + it('should check sizes', function() { + var opts = {}; + var w = cachedCreateW(); + var s = w.sizes(); + s.total.should.be.equal(1634); + }); + + it('should check pending proposals', function() { + var opts = {}; + var w = cachedCreateW(); + + var p = w.getPendingTxProposalsCount(); + console.log('p', p); + p.pending.should.be.equal(0); + p.pendingForUs.should.be.equal(0); + }); + + it('should set version', function() { + var opts = {}; + var w = cachedCreateW(); + w.setVersion('0.123') + w.version.should.be.equal('0.123'); + }); + + it('should check pending proposals', function(done) { + var w = createW2(null, 1); + var utxo = createUTXO(w); + w.blockchain.fixUnspent(utxo); + + sinon.spy(w, 'sendIndexes'); + sinon.spy(w, 'sendTxProposal'); + w.spend({ + toAddress: toAddress, + amountSat: amountSatStr, + }, function(err, id, status) { + should.not.exist(err); + should.exist(id); + status.should.equal(Wallet.TX_PROPOSAL_SENT); + w.sendTxProposal.calledOnce.should.equal(true); + w.sendIndexes.calledOnce.should.equal(false); + + var p = w.getPendingTxProposalsCount(); + p.pending.should.be.equal(1); + p.pendingForUs.should.be.equal(0); + + var p2 = w.getPendingTxProposals(); + p2.length.should.be.equal(1); + done(); + }); + }); + var unspentTest = [{ "address": "dummy", "scriptPubKey": "dummy", @@ -708,6 +759,25 @@ describe('Wallet model', function() { }); + it('should exportEncrypted', function() { + var w = createW2(); + var enc = w.exportEncrypted('', {}); + enc.length.should.equal(2405); + }); + + it('should close wallet', function(done) { + var w = createW2(); + w.removeAllListeners = sinon.stub(); + w.network.removeAllListeners = sinon.stub(); + w.network.cleanUp = sinon.stub(); + w.blockchain.removeAllListeners = sinon.stub(); + w.blockchain.destroy = sinon.stub(); + + w.close(function() { + done(); + }); + }); + // tx handling var createUTXO = function(w) { From f9e8ce2be36e7c277a810cd8a388da13f6f81917 Mon Sep 17 00:00:00 2001 From: Matias Pando Date: Wed, 17 Dec 2014 11:54:47 -0300 Subject: [PATCH 4/7] Delete console.log --- test/Wallet.js | 1 - 1 file changed, 1 deletion(-) diff --git a/test/Wallet.js b/test/Wallet.js index c483e0262..6f8715043 100644 --- a/test/Wallet.js +++ b/test/Wallet.js @@ -185,7 +185,6 @@ describe('Wallet model', function() { var w = cachedCreateW(); var p = w.getPendingTxProposalsCount(); - console.log('p', p); p.pending.should.be.equal(0); p.pendingForUs.should.be.equal(0); }); From 8992fe2ea5cde39b66e5e9ad3aa3a394410d384f Mon Sep 17 00:00:00 2001 From: Matias Pando Date: Wed, 17 Dec 2014 12:10:06 -0300 Subject: [PATCH 5/7] Fixes karma test --- test/RateService.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/RateService.js b/test/RateService.js index eb98c089a..36bbc3ee0 100644 --- a/test/RateService.js +++ b/test/RateService.js @@ -1,6 +1,6 @@ 'use strict'; -var moment = require('moment'); +var moment = moment || require('moment'); var RateService = copay.RateService; describe('RateService model', function() { From f245ba80228b0b40d9928618e4430f680e75c383 Mon Sep 17 00:00:00 2001 From: Matias Pando Date: Thu, 18 Dec 2014 13:02:02 -0300 Subject: [PATCH 6/7] Tests improvements --- test/RateService.js | 27 ++++++++------------------- test/Wallet.js | 9 ++++++++- 2 files changed, 16 insertions(+), 20 deletions(-) diff --git a/test/RateService.js b/test/RateService.js index 36bbc3ee0..fa68e2961 100644 --- a/test/RateService.js +++ b/test/RateService.js @@ -327,11 +327,8 @@ describe('RateService model', function() { rs.isAvailable = sinon.stub().returns(false); (function() { rs.listAlternatives(); - }).should.throw('not available');; - - rs.getAlternatives(); + }).should.throw('not available'); }); - it('should return list of available currencies', function() { var rs = new RateService(); rs.isAvailable = sinon.stub().returns(true); @@ -348,32 +345,24 @@ describe('RateService model', function() { var list = rs.listAlternatives(); list.should.exist; list.length.should.equal(2); - - list = rs.getAlternatives(); - list.should.exist; - list.length.should.equal(2); }); - - it('should return an error when trying to get the list of available currencies', function() { + }); + describe('#getAlternatives', function() { + it('should return nothing when trying to get the list of available currencies', function() { var rs = new RateService(); - rs.isAvailable = sinon.stub().returns(true); - rs.whenAvailable(function() {}); rs.isAvailable = sinon.stub().returns(false); rs.whenAvailable(function() {}); - rs.getAlternatives().length.should.equal(0); - - (function() { - rs.listAlternatives(); - }).should.throw; }); + }); describe('#singleton', function() { it('should create only one instance', function() { var rs = RateService.singleton(); rs.should.be.not.null; - rs = RateService.singleton(); - rs.should.be.not.null; + var rs2 = RateService.singleton(); + rs2.should.be.not.null; + rs.should.deep.equal(rs2); }); }); }); diff --git a/test/Wallet.js b/test/Wallet.js index 6f8715043..4c296dde9 100644 --- a/test/Wallet.js +++ b/test/Wallet.js @@ -203,6 +203,7 @@ describe('Wallet model', function() { sinon.spy(w, 'sendIndexes'); sinon.spy(w, 'sendTxProposal'); + w.spend({ toAddress: toAddress, amountSat: amountSatStr, @@ -213,6 +214,9 @@ describe('Wallet model', function() { w.sendTxProposal.calledOnce.should.equal(true); w.sendIndexes.calledOnce.should.equal(false); + w.network.send.calledOnce.should.equal(true); + + var p = w.getPendingTxProposalsCount(); p.pending.should.be.equal(1); p.pendingForUs.should.be.equal(0); @@ -766,13 +770,16 @@ describe('Wallet model', function() { it('should close wallet', function(done) { var w = createW2(); - w.removeAllListeners = sinon.stub(); w.network.removeAllListeners = sinon.stub(); w.network.cleanUp = sinon.stub(); w.blockchain.removeAllListeners = sinon.stub(); w.blockchain.destroy = sinon.stub(); w.close(function() { + w.network.removeAllListeners.calledOnce.should.equal(true); + w.network.cleanUp.calledOnce.should.equal(true); + w.blockchain.removeAllListeners.calledOnce.should.equal(true); + w.network.cleanUp.calledOnce.should.equal(true); done(); }); }); From 0055b11dd25d0fd66ed05ecb2163682b0abdbe24 Mon Sep 17 00:00:00 2001 From: Matias Pando Date: Thu, 18 Dec 2014 14:44:32 -0300 Subject: [PATCH 7/7] Improving test on Wallet --- test/Wallet.js | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/test/Wallet.js b/test/Wallet.js index 4c296dde9..972b547ff 100644 --- a/test/Wallet.js +++ b/test/Wallet.js @@ -196,14 +196,11 @@ describe('Wallet model', function() { w.version.should.be.equal('0.123'); }); - it('should check pending proposals', function(done) { + it('should check pending proposals case 2', function(done) { var w = createW2(null, 1); var utxo = createUTXO(w); w.blockchain.fixUnspent(utxo); - sinon.spy(w, 'sendIndexes'); - sinon.spy(w, 'sendTxProposal'); - w.spend({ toAddress: toAddress, amountSat: amountSatStr, @@ -211,11 +208,12 @@ describe('Wallet model', function() { should.not.exist(err); should.exist(id); status.should.equal(Wallet.TX_PROPOSAL_SENT); - w.sendTxProposal.calledOnce.should.equal(true); - w.sendIndexes.calledOnce.should.equal(false); w.network.send.calledOnce.should.equal(true); - + console.log(w.network.send.getCall(0).args[1]); + w.network.send.getCall(0).args[1].type.should.equal("txProposal"); + should.exist(w.network.send.getCall(0).args[1].indexes); + should.exist(w.network.send.getCall(0).args[1].txProposal); var p = w.getPendingTxProposalsCount(); p.pending.should.be.equal(1);