<commit_before>var Module = require('../lib/Module'),
    Datasource = require('../lib/Datasource'),
    Q = require('q');

describe('Module', function () {
  var mod,
      moduleConfig,
      moduleData,
      deferred;

  beforeEach(function () {
    moduleConfig = {
      'title': 'test',
      'format': {
        'type': 'number'
      },
      'module-type': 'kpi',
      'value-attribute': 'specific_data',
      'data-source': {
        'data-group': 'transactional-services',
        'data-type': 'summaries',
        'query-params': {
          'sort_by': '_timestamp:descending',
          'filter_by': [
            'service_id:bis-accounts-filing',
            'type:seasonally-adjusted'
          ]
        }
      }
    };

    moduleData = [
      {
        '_quarter_start_at': '2013-07-01T00:00:00+00:00',
        '_timestamp': '2013-07-01T00:00:00+00:00',
        'end_at': '2014-07-01T00:00:00+00:00',
        'specific_data': 1
      },
      {
        '_quarter_start_at': '2013-04-01T00:00:00+00:00',
        '_timestamp': '2013-07-01T00:00:00+00:00',
        'end_at': '2014-07-01T00:00:00+00:00',
        'specific_data': 2
      },
      {
        '_quarter_start_at': '2013-01-01T00:00:00+00:00',
        '_timestamp': '2013-07-01T00:00:00+00:00',
        'end_at': '2014-07-01T00:00:00+00:00',
        'specific_data': 1
      }
    ];

    sinon.spy(Datasource);

    mod = new Module(moduleConfig);

    deferred = Q.defer();

    sinon.stub(Datasource.prototype, 'getData').returns(deferred.promise);
  });

  afterEach(function () {
    Datasource.prototype.getData.restore();
  });

  describe('init', function () {

    it('sets the moduleConfig to the moduleConfig property', function () {
      mod.moduleConfig.should.eql(moduleConfig);
    });

    it('sets a datasource for the module', function () {
      mod.dataSource.should.be.instanceOf(Datasource);
    });

    it('sets a module axes', function () {
      mod.axes.should.eql({
        x: {
          format: 'date',
          key: [
            '_quarter_start_at',
            'end_at'
          ],
          label: 'Quarter'
        },
        y: [{
          format: {
            type: 'number'
          },
          key: 'specific_data',
          label: 'test'
        }]
      });
    });

  });

  describe('isSupported', function () {

    it('retuns true for supported modules', function () {
      mod.isSupported('realtime').should.equal(true);
      mod.isSupported('kpi').should.equal(true);
      mod.isSupported('single_timeseries').should.equal(true);
      mod.isSupported('user_satisfaction_graph').should.equal(true);
    });

    it('returns false for unsupported modules', function () {
      mod.isSupported('something_unsupported').should.equal(false);
    });

  });

  describe('getData', function () {

    it('calls getData on the dataSource', function () {<commit_msg>Added extra tests to cover module axes<commit_after>var Module = require('../lib/Module'),
    Datasource = require('../lib/Datasource'),
    Q = require('q');

describe('Module', function () {
  var mod,
      moduleConfig,
      moduleData,
      deferred;

  beforeEach(function () {
    moduleConfig = {
      'title': 'test',
      'format': {
        'type': 'number'
      },
      'module-type': 'kpi',
      'value-attribute': 'specific_data',
      'data-source': {
        'data-group': 'transactional-services',
        'data-type': 'summaries',
        'query-params': {
          'sort_by': '_timestamp:descending',
          'filter_by': [
            'service_id:bis-accounts-filing',
            'type:seasonally-adjusted'
          ]
        }
      }
    };

    moduleData = [
      {
        '_quarter_start_at': '2013-07-01T00:00:00+00:00',
        '_timestamp': '2013-07-01T00:00:00+00:00',
        'end_at': '2014-07-01T00:00:00+00:00',
        'specific_data': 1
      },
      {
        '_quarter_start_at': '2013-04-01T00:00:00+00:00',
        '_timestamp': '2013-07-01T00:00:00+00:00',
        'end_at': '2014-07-01T00:00:00+00:00',
        'specific_data': 2
      },
      {
        '_quarter_start_at': '2013-01-01T00:00:00+00:00',
        '_timestamp': '2013-07-01T00:00:00+00:00',
        'end_at': '2014-07-01T00:00:00+00:00',
        'specific_data': 1
      }
    ];

    sinon.spy(Datasource);

    mod = new Module(moduleConfig);

    deferred = Q.defer();

    sinon.stub(Datasource.prototype, 'getData').returns(deferred.promise);
  });

  afterEach(function () {
    Datasource.prototype.getData.restore();
  });

  describe('init', function () {

    it('sets the moduleConfig to the moduleConfig property', function () {
      mod.moduleConfig.should.eql(moduleConfig);
    });

    it('sets a datasource for the module', function () {
      mod.dataSource.should.be.instanceOf(Datasource);
    });

    describe('module axes', function () {

      it('should return with axes data for the KPI module', function () {
        mod.axes.x.should.eql({
          'label': 'Quarter',
          'key': ['_quarter_start_at', 'end_at'],
          'format': 'date'
        });

        mod.axes.y.should.eql([{
          label: 'test',
          key: 'specific_data',
          format: {
            type: 'number'
          }
        }]);
      });

      it('should return with axes data for the SINLE_TIMESERIES module', function () {
        moduleConfig.axes = {
          x: {
            label: 'Date',
            key: ['_start_at', '_end_at'],
            format: 'date'
          }
        };
        moduleConfig['module-type'] = 'single_timeseries';
        moduleConfig['format-options'] = {
          'type': 'number'
        };

        mod = new Module(moduleConfig);

        mod.axes.x.should.eql({
          'label': 'Date',
          'key': ['_start_at', '_end_at'],
          'format': 'date'
        });

        mod.axes.y.should.eql([{
          key: 'specific_data',
          format: {
            type: 'number'
          }
        }]);
      });

      it('should assume axis unit is integer if not specified', function () {
        moduleConfig.axes = {
          'y': [{
            'label': 'User satisfaction',
            'key': 'satisfaction:sum',
            'format': 'percent'
          }, {'key': 'respondents', 'label': 'Number of respondents'}],
          'x': {'label': 'Date', 'key': ['_start_at', '_end_at'], 'format': 'date'}
        };
        moduleConfig['module-type'] = 'single_timeseries';

        mod = new Module(moduleConfig);

        mod.axes.y.should.eql([
          {
            'label': 'User satisfaction',
            'key': 'specific_data',
            'format': 'percent'
          },
          {
            'key': 'respondents',
            'label': 'Number of respondents',
            'format': {
              type: 'integer'
            }
          }
        ]);
      });

      it('should return with axes data for the REALTIME module', function () {
        moduleConfig['module-type'] = 'realtime';

        mod = new Module(moduleConfig);

        mod.axes.x.should.eql({
          'label': 'Time',
          'key': '_timestamp',
          'format': 'time'
        });

        mod.axes.y.should.eql([{
          key: 'unique_visitors',
          format: 'integer',
          label: 'Number of unique visitors'
        }]);
      });

    });

  });

  describe('isSupported', function () {

    it('retuns true for supported modules', function () {
      mod.isSupported('realtime').should.equal(true);
      mod.isSupported('kpi').should.equal(true);
      mod.isSupported('single_timeseries').should.equal(true);
      mod.isSupported('user_satisfaction_graph').should.equal(true);
    });

    it('returns false for unsupported modules', function () {
      mod.isSupported('something_unsupported').should.equal(false);
    });

  });

  describe('getData', function () {

    it('calls getData on the dataSource', function () {
{'fraction_non_alphanumeric': 0.16790527774278877, 'fraction_numerical': 0.0415669479783348, 'mean_word_length': 2.0703789636504255, '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': '5353858', 'n_tokens_mistral': 2744, 'n_tokens_neox': 2481, 'n_words': 419}