<commit_before>/* 
 * == BSD2 LICENSE ==
 * Copyright (c) 2014, Tidepool Project
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the associated License, which is identical to the BSD 2-Clause
 * License as published by the Open Source Initiative at opensource.org.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the License for more details.
 * 
 * You should have received a copy of the License along with this program; if
 * not, you can obtain one from Tidepool Project at tidepool.org.
 * == BSD2 LICENSE ==
 */

/*jshint expr: true */
/*global describe, it */

var chai = require('chai');
var assert = chai.assert;
var expect = chai.expect;

var _ = require('lodash');

try {
  global.window = {
    tideline: require('../js/')
  };
  global.window.tideline.watson = require('../plugins/data/watson/');
}
catch (TypeError) {}

var data = require('../example/data/device-data.json');

var Preprocess = require('../plugins/data/preprocess/');

var settings = require('./fixtures/settings');

describe('Preprocess', function() {
  describe('REQUIRED_TYPES', function() {
    it('should be an array', function() {
      assert.isArray(Preprocess.REQUIRED_TYPES);
    });
  });

  describe('OPTIONAL_TYPES', function() {
    it('should be an array', function() {
      assert.isArray(Preprocess.OPTIONAL_TYPES);
    });
  });

  describe('MMOL_STRING', function() {
    it('should be a string', function() {
      assert.isString(Preprocess.MMOL_STRING);
    });
  });

  describe('MGDL_STRING', function() {
    it('should be a string', function() {
      assert.isString(Preprocess.MGDL_STRING);
    });
  });

  describe('MMOL_TO_MGDL', function() {
    it('should be 18', function() {
      expect(Preprocess.MMOL_TO_MGDL).to.equal(18);
    });
  });

  describe('mungeBasals', function() {
    it('should be a function', function() {
      assert.isFunction(Preprocess.mungeBasals);
    });

    it('should return an array', function() {
      assert.isArray(Preprocess.mungeBasals(data));
    });

    it('should add a vizType key to each `basal-rate-segment` object', function() {
      var basal = _.findWhere(data, {'type': 'basal-rate-segment'});

      expect(Preprocess.mungeBasals([basal])[0].vizType).to.exist;
    });
  });

  describe('editBoluses', function() {
    it('should be a function', function() {
      assert.isFunction(Preprocess.editBoluses);
    });

    it('should add a recommended field to a bolus when a matching wizard event exists', function() {
      var data =
        [{
          'deviceTime': '2013-10-24T11:54:14',
          'source': 'carelink',
          'programmed': 5.4,
          'value': 4,<commit_msg>Add preprocess test that tests the processing of the new format<commit_after>/* 
 * == BSD2 LICENSE ==
 * Copyright (c) 2014, Tidepool Project
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the associated License, which is identical to the BSD 2-Clause
 * License as published by the Open Source Initiative at opensource.org.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the License for more details.
 * 
 * You should have received a copy of the License along with this program; if
 * not, you can obtain one from Tidepool Project at tidepool.org.
 * == BSD2 LICENSE ==
 */

/*jshint expr: true */
/*global describe, it */

var chai = require('chai');
var assert = chai.assert;
var expect = chai.expect;

var _ = require('lodash');

try {
  global.window = {
    tideline: require('../js/')
  };
  global.window.tideline.watson = require('../plugins/data/watson/');
}
catch (TypeError) {}

var data = require('../example/data/device-data.json');

var Preprocess = require('../plugins/data/preprocess/');

var settings = require('./fixtures/settings');

describe('Preprocess', function() {
  describe('REQUIRED_TYPES', function() {
    it('should be an array', function() {
      assert.isArray(Preprocess.REQUIRED_TYPES);
    });
  });

  describe('OPTIONAL_TYPES', function() {
    it('should be an array', function() {
      assert.isArray(Preprocess.OPTIONAL_TYPES);
    });
  });

  describe('MMOL_STRING', function() {
    it('should be a string', function() {
      assert.isString(Preprocess.MMOL_STRING);
    });
  });

  describe('MGDL_STRING', function() {
    it('should be a string', function() {
      assert.isString(Preprocess.MGDL_STRING);
    });
  });

  describe('MMOL_TO_MGDL', function() {
    it('should be 18', function() {
      expect(Preprocess.MMOL_TO_MGDL).to.equal(18);
    });
  });

  describe('mungeBasals', function() {
    it('should be a function', function() {
      assert.isFunction(Preprocess.mungeBasals);
    });

    it('should return an array', function() {
      assert.isArray(Preprocess.mungeBasals(data));
    });

    it('should add a vizType key to each `basal-rate-segment` object', function() {
      var basal = _.findWhere(data, {'type': 'basal-rate-segment'});

      expect(Preprocess.mungeBasals([basal])[0].vizType).to.exist;
    });

    it('should process the new data format', function(){
      var input = [
        { type: 'basal', rate: 0.5, deviceTime: '2014-01-01T01:00:00', duration: 60 * 60 * 1000 },
        { id: 'abcd', type: 'basal', deliveryType: 'temp', rate: 0.5,
          deviceTime: '2014-01-01T02:00:00', duration: 60 * 60 * 1000,
          suppressed: { type: 'basal', deliveryType: 'scheduled', rate: 1.0,
            deviceTime: '2014-01-01T02:00:00', duration: 4 * 60 * 60 * 1000 }
        },
        { type: 'basal', deliveryType: 'scheduled', rate: 1.0,
          deviceTime: '2014-01-01T03:00:00', duration: 3 * 60 * 60 * 1000 }
      ];

      expect(Preprocess.mungeBasals(input)).deep.equals(
        [
          { type: 'basal-rate-segment', rate: 0.5, deviceTime: '2014-01-01T01:00:00', duration: 60 * 60 * 1000,
            value: 0.5, start: '2014-01-01T01:00:00', end: '2014-01-01T02:00:00', vizType: 'actual'  },
          { type: 'basal-rate-segment', deliveryType: 'scheduled', value: 1.0,
            deviceTime: '2014-01-01T02:00:00', duration: 4 * 60 * 60 * 1000,
            id: 'abcd_suppressed', start: '2014-01-01T02:00:00', end: '2014-01-01T03:00:00', rate: 1.0, vizType: 'undelivered' },
          { id: 'abcd', type: 'basal-rate-segment', deliveryType: 'temp', rate: 0.5,
            deviceTime: '2014-01-01T02:00:00', duration: 60 * 60 * 1000,
            suppressed: { type: 'basal', deliveryType: 'scheduled', rate: 1.0,
              deviceTime: '2014-01-01T02:00:00', duration: 4 * 60 * 60 * 1000 },
            start: '2014-01-01T02:00:00', end: '2014-01-01T03:00:00', value: 0.5, vizType: 'actual'
          },
          { type: 'basal-rate-segment', deliveryType: 'scheduled', rate: 1.0,
            deviceTime: '2014-01-01T03:00:00', duration: 3 * 60 * 60 * 1000,
            start: '2014-01-01T03:00:00', end: '2014-01-01T06:00:00', value: 1.0, vizType: 'actual'}
        ]
      )
    });
  });

  describe('editBoluses', function() {
    it('should be a function', function() {
      assert.isFunction(Preprocess.editBoluses);
    });

    it('should add a recommended field to a bolus when a matching wizard event exists', function() {
      var data =
        [{
          'deviceTime': '2013-10-24T11:54:14',
          'source': 'carelink',
          'programmed': 5.4,
          'value': 4,
{'fraction_non_alphanumeric': 0.1560015639254529, 'fraction_numerical': 0.052000521308484295, 'mean_word_length': 3.116952789699571, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4857761', 'n_tokens_mistral': 2733, 'n_tokens_neox': 2441, 'n_words': 654}