Skip to content

Commit

Permalink
Merge pull request #718 from nightscout/wip/treatment-tests
Browse files Browse the repository at this point in the history
treatment tests
  • Loading branch information
jasoncalabrese committed Jul 19, 2015
2 parents 97add14 + f37da2c commit f50a17e
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 8 deletions.
10 changes: 7 additions & 3 deletions lib/bootevent.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
'use strict';

var _ = require('lodash');

var UPDATE_THROTTLE = 1000;

function boot (env) {

function setupMongo (ctx, next) {
Expand Down Expand Up @@ -45,19 +49,19 @@ function boot (env) {
}

function setupListeners (ctx, next) {
function updateData ( ) {
var updateData = _.debounce(function debouncedUpdateData ( ) {
ctx.data.update(function dataUpdated () {
ctx.bus.emit('data-loaded');
});
}
}, UPDATE_THROTTLE);

ctx.bus.on('tick', function timedReloadData (tick) {
console.info('tick', tick.now);
updateData();
});

ctx.bus.on('data-received', function forceReloadData ( ) {
console.info('got data-received event, reloading now');
console.info('got data-received event, requesting reload');
updateData();
});

Expand Down
8 changes: 3 additions & 5 deletions lib/bus.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,19 @@ var Stream = require('stream');
function init (env) {
var beats = 0;
var started = new Date( );
var id;
var interval = env.HEARTBEAT || 20000;
var interval = env.HEARTBEAT || 60000;

var stream = new Stream;

function ictus ( ) {
var tick = {
return {
now: new Date( )
, type: 'heartbeat'
, sig: 'internal://' + ['heartbeat', beats ].join('/')
, beat: beats++
, interval: interval
, started: started
};
return tick;
}

function repeat ( ) {
Expand All @@ -26,7 +24,7 @@ function init (env) {

stream.readable = true;
stream.uptime = repeat;
id = setInterval(repeat, interval);
setInterval(repeat, interval);
return stream;
}
module.exports = init;
Expand Down
66 changes: 66 additions & 0 deletions tests/api.treatments.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
'use strict';

var request = require('supertest');
require('should');

describe('Treatment API', function ( ) {
var self = this;

var api = require('../lib/api/');
before(function (done) {
process.env.API_SECRET = 'this is my long pass phrase';
self.env = require('../env')();
self.env.enable = 'careportal';
this.wares = require('../lib/middleware/')(self.env);
self.app = require('express')();
self.app.enable('api');
require('../lib/bootevent')(self.env).boot(function booted(ctx) {
self.ctx = ctx;
self.app.use('/api', api(self.env, ctx));
done();
});
});

after(function () {
delete process.env.API_SECRET;
});

it('post a some treatments', function (done) {
self.ctx.bus.on('data-loaded', function dataWasLoaded ( ) {
self.ctx.data.treatments.length.should.equal(3);
self.ctx.data.treatments[0].mgdl.should.equal(100);

self.ctx.data.treatments[1].mgdl.should.equal(100);
self.ctx.data.treatments[1].insulin.should.equal('2.00');
self.ctx.data.treatments[2].carbs.should.equal('30');

done();
});

self.ctx.treatments().remove({ }, function ( ) {
request(self.app)
.post('/api/treatments/')
.set('api-secret', self.env.api_secret || '')
.send({eventType: 'BG Check', glucose: 100, glucoseType: 'Finger', units: 'mg/dl'})
.expect(200)
.end(function (err) {
if (err) {
done(err);
}
});

request(self.app)
.post('/api/treatments/')
.set('api-secret', self.env.api_secret || '')
.send({eventType: 'Meal Bolus', carbs: '30', insulin: '2.00', preBolus: 15, glucose: 100, glucoseType: 'Finger', units: 'mg/dl'})
.expect(200)
.end(function (err) {
if (err) {
done(err);
}
});

});
});

});
51 changes: 51 additions & 0 deletions tests/update-throttle.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
'use strict';

var _ = require('lodash');
var request = require('supertest');
require('should');

describe('Throttle', function ( ) {
var self = this;

var api = require('../lib/api/');
before(function (done) {
process.env.API_SECRET = 'this is my long pass phrase';
self.env = require('../env')();
this.wares = require('../lib/middleware/')(self.env);
self.app = require('express')();
self.app.enable('api');
require('../lib/bootevent')(self.env).boot(function booted(ctx) {
self.ctx = ctx;
self.app.use('/api', api(self.env, ctx));
done();
});
});

after(function () {
delete process.env.API_SECRET;
});

it('only update once when there are multiple posts', function (done) {

//if the data-loaded event is triggered more than once the test will fail
self.ctx.bus.on('data-loaded', function dataWasLoaded ( ) {
done();
});

function post () {
request(self.app)
.post('/api/entries/')
.set('api-secret', self.env.api_secret || '')
.send({type: 'sgv', sgv: 100, date: Date.now()})
.expect(200)
.end(function(err) {
if (err) {
done(err);
}
});
}

_.times(10, post);
});

});

0 comments on commit f50a17e

Please sign in to comment.