Browse Source

test fetch

activeAddress
Ivan Socolsky 9 years ago
parent
commit
aa415268fc
  1. 1
      lib/fiatrateproviders/bitpay.js
  2. 1
      lib/fiatrateproviders/bitstamp.js
  3. 5
      lib/fiatrateservice.js
  4. 49
      test/integration/fiatrateservice.js

1
lib/fiatrateproviders/bitpay.js

@ -1,6 +1,7 @@
var _ = require('lodash');
var provider = {
name: 'BitPay',
url: 'https://bitpay.com/api/rates/',
parseFn: function(raw) {
var rates = _.compact(_.map(raw, function(d) {

1
lib/fiatrateproviders/bitstamp.js

@ -1,4 +1,5 @@
var provider = {
name: 'Bitstamp',
url: 'https://www.bitstamp.net/api/ticker/',
parseFn: function(raw) {
return [{

5
lib/fiatrateservice.js

@ -26,7 +26,7 @@ FiatRateService.prototype.start = function(opts, cb) {
if (_.isArray(opts.providers)) {
self.providers = opts.providers;
} else {
self.providers = require('./fiatrateproviders');
self.providers = _.values(require('./fiatrateproviders'));
}
self.request = opts.request || request;
self.defaultProvider = opts.defaultProvider || DEFAULT_PROVIDER;
@ -65,13 +65,14 @@ FiatRateService.prototype._fetch = function(cb) {
cb = cb || function() {};
async.each(_.values(self.providers), function(provider, next) {
async.each(self.providers, function(provider, next) {
self._retrieve(provider, function(err, res) {
if (err) {
log.warn(err);
return next();
}
self.storage.storeFiatRate(provider.name, res, function(err) {
if (err) log.warn(err);
return next();
});
});

49
test/integration/fiatrateservice.js

@ -14,7 +14,7 @@ var helpers = require('./helpers');
var FiatRateService = require('../../lib/fiatrateservice');
describe.only('Fiat rate service', function() {
describe('Fiat rate service', function() {
var service, request;
before(function(done) {
@ -163,4 +163,51 @@ describe.only('Fiat rate service', function() {
});
});
});
describe('#fetch', function() {
it('should fetch rates from all providers', function(done) {
var clock = sinon.useFakeTimers(100, 'Date');
var bitpay = [{
code: 'USD',
rate: 123.45,
}, {
code: 'EUR',
rate: 234.56,
}];
var bitstamp = {
last: 120.00,
};
request.get.withArgs({
url: 'https://bitpay.com/api/rates/',
json: true
}).yields(null, null, bitpay);
request.get.withArgs({
url: 'https://www.bitstamp.net/api/ticker/',
json: true
}).yields(null, null, bitstamp);
service._fetch(function(err) {
should.not.exist(err);
service.getRate('USD', {}, function(err, res) {
should.not.exist(err);
res.fetchedOn.should.equal(100);
res.rate.should.equal(123.45);
service.getRate('USD', {
provider: 'Bitstamp'
}, function(err, res) {
should.not.exist(err);
res.fetchedOn.should.equal(100);
res.rate.should.equal(120.00);
service.getRate('EUR', {}, function(err, res) {
should.not.exist(err);
res.fetchedOn.should.equal(100);
res.rate.should.equal(234.56);
clock.restore();
done();
});
});
});
});
});
});
});

Loading…
Cancel
Save