Browse Source

add marketFetcher unit test

develop
Mike van Rossum 5 years ago
parent
commit
e6408be14b
5 changed files with 112 additions and 82 deletions
  1. +2
    -1
      core/budfox/marketFetcher.js
  2. +1
    -0
      package.json
  3. +96
    -0
      test/marketFetcher.js
  4. +13
    -13
      test/tradeBatcher.js
  5. +0
    -68
      test/tradeFetcher.js

+ 2
- 1
core/budfox/marketFetcher.js View File

@ -18,6 +18,8 @@ var exchangeChecker = require(util.dirs().core + 'exchangeChecker');
var TradeBatcher = require(util.dirs().budfox + 'tradeBatcher');
var Fetcher = function(config) {
if(!_.isObject(config))
throw 'TradeFetcher expects a config';
var provider = config.watch.exchange.toLowerCase();
var DataProvider = require(util.dirs().gekko + 'exchanges/' + provider);
@ -92,7 +94,6 @@ Fetcher.prototype.processTrades = function(err, trades) {
setTimeout(this._fetch, +moment.duration('s', 1));
return;
}
this.batcher.write(trades);
}

+ 1
- 0
package.json View File

@ -37,6 +37,7 @@
"devDependencies": {
"chai": "^2.0.0",
"mocha": "^2.1.1",
"proxyquire": "^1.7.10",
"sinon": "^1.12.2"
},
"engines": {

+ 96
- 0
test/marketFetcher.js View File

@ -0,0 +1,96 @@
var chai = require('chai');
var expect = chai.expect;
var should = chai.should;
var assert = chai.assert;
var sinon = require('sinon');
var proxyquire = require('proxyquire');
var _ = require('lodash');
var moment = require('moment');
var util = require(__dirname + '/../core/util');
var config = util.getConfig();
var dirs = util.dirs();
var providerName = config.watch.exchange.toLowerCase();
var providerPath = util.dirs().gekko + 'exchanges/' + providerName;
var mf;
var spoofer = {};
var TRADES = [
{ tid: 1, amount: 1, price: 100, date: 1475837937 },
{ tid: 2, amount: 1, price: 100, date: 1475837938 }
];
// stub the exchange
var FakeProvider = function() {};
var getTrades = function(since, handler, descending) {
handler(
null,
TRADES
);
}
FakeProvider.prototype = {
getTrades: getTrades
}
spoofer[providerPath] = FakeProvider;
var getTradesSpy = sinon.spy(FakeProvider.prototype, 'getTrades');
// stub the tradebatcher
var TradeBatcher = require(util.dirs().budfox + 'tradeBatcher');
var tradeBatcherSpy = sinon.spy(TradeBatcher.prototype, 'write');
spoofer[util.dirs().budfox + 'tradeBatcher'] = TradeBatcher;
var MarketFetcher = proxyquire(dirs.budfox + 'MarketFetcher', spoofer);
describe('marketFetcher', function() {
it('should throw when not passed a config', function() {
expect(function() {
new MarketFetcher();
}).to.throw('TradeFetcher expects a config');
});
it('should instantiate', function() {
mf = new MarketFetcher(config);
});
it('should fetch with correct arguments', function() {
// mf.fetch should call the DataProvider like:
// provider.getTrades(since, callback, descending)
mf.fetch();
expect(getTradesSpy.callCount).to.equal(1);
var args = getTradesSpy.firstCall.args;
// test-config uses NO `tradingAdvisor`
var since = args[0];
expect(since).to.equal(undefined);
var handler = args[1];
assert.isFunction(handler);
var descending = args[2];
expect(descending).to.equal(false);
});
xit('should retry on error', function() {
// todo
});
it('should pass the data to the tradebatcher', function() {
mf.fetch();
expect(getTradesSpy.callCount).to.equal(2);
expect(tradeBatcherSpy.lastCall.args[0]).to.deep.equal(TRADES);
});
xit('should relay trades', function() {
// todo
});
});

+ 13
- 13
test/tradeBatcher.js View File

@ -97,23 +97,23 @@ describe('tradeBatcher', function() {
});
});
it('should correctly filter trades', function() {
tb = new TradeBatcher('tid');
it('should correctly filter trades', function() {
tb = new TradeBatcher('tid');
var spy = sinon.spy();
tb.on('new batch', spy);
var spy = sinon.spy();
tb.on('new batch', spy);
tb.write( trades_tid_1 );
tb.write( trades_tid_2 );
tb.write( trades_tid_1 );
tb.write( trades_tid_2 );
expect(spy.callCount).to.equal(2);
expect(spy.callCount).to.equal(2);
var tbResult = _.first(_.last(spy.args));
var tbResult = _.first(_.last(spy.args));
expect(tbResult.amount).to.equal(2);
expect(tbResult.start.unix()).to.equal(1466115796);
expect(tbResult.end.unix()).to.equal(1466115797);
expect(tbResult.data.length).to.equal(2);
});
expect(tbResult.amount).to.equal(2);
expect(tbResult.start.unix()).to.equal(1466115796);
expect(tbResult.end.unix()).to.equal(1466115797);
expect(tbResult.data.length).to.equal(2);
});
});

+ 0
- 68
test/tradeFetcher.js View File

@ -1,68 +0,0 @@
// TODO: rewrite tests for mocha once we use Days.
// var _ = require('lodash');
// var moment = require('moment');
// var util = require('../core/util');
// var Fetcher = require('../core/tradeFetcher');
// var TRADES = [
// { tid: 4, amount: 1.4, price: 99, date: 1381356250 },
// { tid: 5, amount: 1.5, price: 98, date: 1381356310 },
// { tid: 6, amount: 1.6, price: 97, date: 1381356370 },
// { tid: 7, amount: 1.7, price: 96, date: 1381356430 },
// { tid: 8, amount: 1.8, price: 95, date: 1381356490 },
// { tid: 9, amount: 1.9, price: 94, date: 1381356550 },
// { tid: 10, amount: 2, price: 93, date: 1381356610 },
// { tid: 11, amount: 2.1, price: 92, date: 1381356670 },
// { tid: 12, amount: 2.2, price: 91, date: 1381356730 },
// { tid: 13, amount: 2.3, price: 90, date: 1381356790 }
// ];
// var FakeFetcher = function(cb) {
// this.exchange = {
// providesHistory: true
// }
// }
// FakeFetcher.prototype = {
// calculateNextFetch: function() {}
// }
// // the methods we want to test
// FakeFetcher.prototype.processTrades = Fetcher.prototype.processTrades;
// FakeFetcher.prototype.setFetchMeta = Fetcher.prototype.setFetchMeta;
// module.exports = {
// processTrades: function(test) {
// var checker = function(what, data) {
// // should broadcast the `new trades`
// // event
// test.equal(what, 'new trades');
// // every trade should be in there in
// // the same order
// _.each(TRADES, function(t, i) {
// test.deepEqual(t, data.all[i]);
// });
// // meta should be correct
// test.deepEqual(_.first(TRADES), data.first);
// test.deepEqual(_.last(TRADES), data.last);
// test.ok(util.equals(moment.unix(1381356250), data.start));
// test.ok(util.equals(moment.unix(1381356790), data.end));
// test.equal(
// data.timespan,
// 1381356790 * 1000 - 1381356250 * 1000
// );
// test.done();
// }
// var fetcher = new FakeFetcher;
// fetcher.emit = checker;
// fetcher.processTrades(false, TRADES);
// }
// }

Loading…
Cancel
Save