Skip to content

Commit

Permalink
instead of using the data-loaded event, just go back to db directly
Browse files Browse the repository at this point in the history
  • Loading branch information
jasoncalabrese committed Mar 15, 2017
1 parent ca08db4 commit b888050
Showing 1 changed file with 32 additions and 53 deletions.
85 changes: 32 additions & 53 deletions tests/api.treatments.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

var _ = require('lodash');
var request = require('supertest');
var should = require('should');
var language = require('../lib/language')();
Expand Down Expand Up @@ -30,36 +31,8 @@ describe('Treatment API', function ( ) {
});

it('post single treatments', function (done) {
var doneCalled = false;

self.ctx.bus.on('data-loaded', function dataWasLoaded ( ) {
self.ctx.ddata.treatments.length.should.equal(3);
self.ctx.ddata.treatments[0].mgdl.should.equal(100);
should.not.exist(self.ctx.ddata.treatments[0].eventTime);
should.not.exist(self.ctx.ddata.treatments[0].notes);

should.not.exist(self.ctx.ddata.treatments[1].eventTime);
self.ctx.ddata.treatments[1].insulin.should.equal(2);
self.ctx.ddata.treatments[2].carbs.should.equal(30);

//if travis is slow the 2 posts take long enough that 2 data-loaded events are emitted
if (!doneCalled) { done(); }

doneCalled = true;
});

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

request(self.app)
.post('/api/treatments/')
.set('api-secret', self.env.api_secret || '')
Expand All @@ -68,26 +41,26 @@ describe('Treatment API', function ( ) {
.end(function (err) {
if (err) {
done(err);
} else {
self.ctx.treatments.list({}, function (err, list) {
var sorted = _.sortBy(list, function (treatment) {
return treatment.created_at;
});
sorted.length.should.equal(2);
sorted[0].glucose.should.equal(100);
should.not.exist(sorted[0].eventTime);
sorted[0].insulin.should.equal(2);
sorted[1].carbs.should.equal(30);

done();
});
}
});

});
});

it('post a treatment array', function (done) {
var doneCalled = false;

self.ctx.bus.on('data-loaded', function dataWasLoaded ( ) {
self.ctx.ddata.treatments.length.should.equal(3);
should.not.exist(self.ctx.ddata.treatments[0].eventTime);
should.not.exist(self.ctx.ddata.treatments[1].eventTime);

//if travis is slow the 2 posts take long enough that 2 data-loaded events are emitted
if (!doneCalled) { done(); }

doneCalled = true;
});

self.ctx.treatments().remove({ }, function ( ) {
request(self.app)
.post('/api/treatments/')
Expand All @@ -100,24 +73,20 @@ describe('Treatment API', function ( ) {
.end(function (err) {
if (err) {
done(err);
} else {
self.ctx.treatments.list({}, function (err, list) {
list.length.should.equal(3);
should.not.exist(list[0].eventTime);
should.not.exist(list[1].eventTime);

done();
});
}
});
});
});

it('post a treatment array and dedupe', function (done) {
var doneCalled = false;

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

//if travis is slow the 2 posts take long enough that 2 data-loaded events are emitted
if (!doneCalled) { done(); }

doneCalled = true;
});

self.ctx.treatments().remove({ }, function ( ) {
var now = (new Date()).toISOString();
request(self.app)
Expand All @@ -138,6 +107,16 @@ describe('Treatment API', function ( ) {
.end(function (err) {
if (err) {
done(err);
} else {
self.ctx.treatments.list({}, function (err, list) {
var sorted = _.sortBy(list, function (treatment) {
return treatment.created_at;
});
sorted.length.should.equal(3);
sorted[0].glucose.should.equal(100);

done();
});
}
});
});
Expand Down

0 comments on commit b888050

Please sign in to comment.