Skip to content

Commit

Permalink
Merge pull request elastic#16 from w33ble/fix/3959-tests
Browse files Browse the repository at this point in the history
Data mutation tests
  • Loading branch information
stormpython committed Jun 5, 2015
2 parents db33d63 + 3794bf1 commit 398a805
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 179 deletions.
223 changes: 50 additions & 173 deletions test/unit/specs/vislib/lib/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ define(function (require) {
var angular = require('angular');
var _ = require('lodash');

var Data;
var dataSeries = require('vislib_fixtures/mock_data/date_histogram/_series');
var dataSeriesNeg = require('vislib_fixtures/mock_data/date_histogram/_series_neg');
var dataStacked = require('vislib_fixtures/mock_data/stacked/_stacked');

var seriesData = {
'label': '',
'series': [
Expand Down Expand Up @@ -94,118 +99,31 @@ define(function (require) {
]
};

var seriesData2 = {
'label': '',
'series': [
{
'label': '100',
'values': [{x: 0, y: 1}, {x: 1, y: 2}, {x: 2, y: 3}]
},
{
'label': '200',
'values': [{x: 0, y: 1}, {x: 1, y: 2}, {x: 2, y: 3}]
}
]
};

var rowsData2 = {
'rows': [
{
'label': 'a',
'series': [
{
'label': '100',
'values': [{x: 0, y: 1}, {x: 1, y: 2}, {x: 2, y: 3}]
},
{
'label': '200',
'values': [{x: 0, y: 1}, {x: 1, y: 2}, {x: 2, y: 3}]
}
]
},
{
'label': 'b',
'series': [
{
'label': '100',
'values': [{x: 0, y: 1}, {x: 1, y: 2}, {x: 2, y: 3}]
},
{
'label': '200',
'values': [{x: 0, y: 1}, {x: 1, y: 2}, {x: 2, y: 3}]
}
]
}
]
};

var colsData2 = {
'columns': [
{
'label': 'a',
'series': [
{
'label': '100',
'values': [{x: 0, y: 1}, {x: 1, y: 2}, {x: 2, y: 3}]
},
{
'label': '200',
'values': [{x: 0, y: 1}, {x: 1, y: 2}, {x: 2, y: 3}]
}
]
},
{
'label': 'b',
'series': [
{
'label': '100',
'values': [{x: 0, y: 1}, {x: 1, y: 2}, {x: 2, y: 3}]
},
{
'label': '200',
'values': [{x: 0, y: 1}, {x: 1, y: 2}, {x: 2, y: 3}]
}
]
}
]
};

var flattenedData = [
[{x: 0, y: 0}, {x: 1, y: 2}, {x: 2, y: 4}, {x: 3, y: 6}, {x: 4, y: 8}],
[{x: 0, y: 0}, {x: 1, y: 2}, {x: 2, y: 4}, {x: 3, y: 6}, {x: 4, y: 8}],
[{x: 0, y: 0}, {x: 1, y: 2}, {x: 2, y: 4}, {x: 3, y: 6}, {x: 4, y: 8}]
];

angular.module('DataFactory', ['kibana']);

describe('Vislib Data Class Test Suite', function () {

describe('Data Class (main)', function () {
var DataFactory;
var rowIn;

beforeEach(function () {
module('DataFactory');
});
beforeEach(function () {
module('DataFactory');

beforeEach(function () {
inject(function (d3, Private) {
DataFactory = Private(require('components/vislib/lib/data'));
});
rowIn = new DataFactory(rowsData, {});
inject(function (Private) {
Data = Private(require('components/vislib/lib/data'));
});
});

describe('Data Class (main)', function () {
it('should be a function', function () {
expect(_.isFunction(DataFactory)).to.be(true);
expect(_.isFunction(Data)).to.be(true);
});

it('should return an object', function () {
var rowIn = new Data(rowsData, {});
expect(_.isObject(rowIn)).to.be(true);
});

});

describe('_removeZeroSlices', function () {
var data;
var pieData = {
slices: {
children: [
Expand All @@ -215,19 +133,10 @@ define(function (require) {
]
}
};
var DataFactory;
var data;

beforeEach(function () {
module('DataFactory');
});

beforeEach(function () {
inject(function (Private) {
DataFactory = Private(require('components/vislib/lib/data'));
data = new DataFactory(pieData, {});
data._removeZeroSlices(pieData.slices);
});
data = new Data(pieData, {});
data._removeZeroSlices(pieData.slices);
});

it('should remove zero values', function () {
Expand All @@ -237,7 +146,6 @@ define(function (require) {
});

describe('Data.flatten', function () {
var DataFactory;
var serIn;
var rowIn;
var colIn;
Expand All @@ -246,16 +154,9 @@ define(function (require) {
var colOut;

beforeEach(function () {
module('DataFactory');
});

beforeEach(function () {
inject(function (d3, Private) {
DataFactory = Private(require('components/vislib/lib/data'));
});
serIn = new DataFactory(seriesData, {});
rowIn = new DataFactory(rowsData, {});
colIn = new DataFactory(colsData, {});
serIn = new Data(seriesData, {});
rowIn = new Data(rowsData, {});
colIn = new Data(colsData, {});
serOut = serIn.flatten();
rowOut = rowIn.flatten();
colOut = colIn.flatten();
Expand All @@ -265,9 +166,13 @@ define(function (require) {
expect(serOut.every(_.isObject)).to.be(true);
});

it('should return all points from every series', testLength(seriesData));
it('should return all points from every series', testLength(rowsData));
it('should return all points from every series', testLength(colsData));

function testLength(inputData) {
return function () {
var data = new DataFactory(inputData, {});
var data = new Data(inputData, {});
var len = _.reduce(data.chartData(), function (sum, chart) {
return sum + chart.series.reduce(function (sum, series) {
return sum + series.values.length;
Expand All @@ -277,52 +182,36 @@ define(function (require) {
expect(data.flatten()).to.have.length(len);
};
}

it('should return all points from every series', testLength(seriesData));
it('should return all points from every series', testLength(rowsData));
it('should return all points from every series', testLength(colsData));
});

describe('getYMin method', function () {
var Data;
var dataSeries;
var stackedDataSeries;
var visData;
var stackedVisData;
var series;
var stackedSeries;
var minValue;
var stackedMinValue;
var visDataNeg;
var visDataStacked;
var minValue = 4;
var minValueNeg = -41;
var minValueStacked = 15;

beforeEach(function () {
module('DataFactory');
});

beforeEach(function () {
inject(function (d3, Private) {
Data = Private(require('components/vislib/lib/data'));
dataSeries = require('vislib_fixtures/mock_data/date_histogram/_series');
stackedDataSeries = require('vislib_fixtures/mock_data/stacked/_stacked');
visData = new Data(dataSeries, {});
stackedVisData = new Data(stackedDataSeries, { type: 'histogram' });
series = _.pluck(visData.chartData(), 'series');
stackedSeries = _.pluck(stackedVisData.chartData(), 'series');
minValue = 4;
stackedMinValue = 15;
});
visData = new Data(dataSeries, {});
visDataNeg = new Data(dataSeriesNeg, {});
visDataStacked = new Data(dataStacked, { type: 'histogram' });
});

// The first value in the time series is less than the min date in the
// date range. It also has the largest y value. This value should be excluded
// when calculating the Y max value since it falls outside of the range.
it('should return the Y domain min value', function () {
expect(visData.getYMin()).to.be(minValue);
expect(stackedVisData.getYMin()).to.be(stackedMinValue);
expect(visDataNeg.getYMin()).to.be(minValueNeg);
expect(visDataStacked.getYMin()).to.be(minValueStacked);
});

it('should have a minimum date value that is greater than the max value within the date range', function () {
var series = _.pluck(visData.chartData(), 'series');
var stackedSeries = _.pluck(visDataStacked.chartData(), 'series');
expect(_.min(series.values, function (d) { return d.x; })).to.be.greaterThan(minValue);
expect(_.min(stackedSeries.values, function (d) { return d.x; })).to.be.greaterThan(stackedMinValue);
expect(_.min(stackedSeries.values, function (d) { return d.x; })).to.be.greaterThan(minValueStacked);
});

it('allows passing a value getter for manipulating the values considered', function () {
Expand All @@ -333,45 +222,33 @@ define(function (require) {
});

describe('getYMax method', function () {
var Data;
var dataSeries;
var stackedDataSeries;
var visData;
var stackedVisData;
var series;
var stackedSeries;
var maxValue;
var stackedMaxValue;

beforeEach(function () {
module('DataFactory');
});
var visDataNeg;
var visDataStacked;
var maxValue = 41;
var maxValueNeg = -4;
var maxValueStacked = 115;

beforeEach(function () {
inject(function (d3, Private) {
Data = Private(require('components/vislib/lib/data'));
dataSeries = require('vislib_fixtures/mock_data/date_histogram/_series');
stackedDataSeries = require('vislib_fixtures/mock_data/stacked/_stacked');
visData = new Data(dataSeries, {});
stackedVisData = new Data(stackedDataSeries, { type: 'histogram' });
series = _.pluck(visData.chartData(), 'series');
stackedSeries = _.pluck(stackedVisData.chartData(), 'series');
maxValue = 41;
stackedMaxValue = 115;
});
visData = new Data(dataSeries, {});
visDataNeg = new Data(dataSeriesNeg, {});
visDataStacked = new Data(dataStacked, { type: 'histogram' });
});

// The first value in the time series is less than the min date in the
// date range. It also has the largest y value. This value should be excluded
// when calculating the Y max value since it falls outside of the range.
it('should return the Y domain min value', function () {
expect(visData.getYMax()).to.be(maxValue);
expect(stackedVisData.getYMax()).to.be(stackedMaxValue);
expect(visDataNeg.getYMax()).to.be(maxValueNeg);
expect(visDataStacked.getYMax()).to.be(maxValueStacked);
});

it('should have a minimum date value that is greater than the max value within the date range', function () {
var series = _.pluck(visData.chartData(), 'series');
var stackedSeries = _.pluck(visDataStacked.chartData(), 'series');
expect(_.min(series, function (d) { return d.x; })).to.be.greaterThan(maxValue);
expect(_.min(stackedSeries, function (d) { return d.x; })).to.be.greaterThan(stackedMaxValue);
expect(_.min(stackedSeries, function (d) { return d.x; })).to.be.greaterThan(maxValueStacked);
});

it('allows passing a value getter for manipulating the values considered', function () {
Expand Down
8 changes: 2 additions & 6 deletions test/unit/specs/vislib/visualizations/column_chart.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ define(function (require) {
'stacked'
];

var min;
var max;

angular.module('ColumnChartFactory', ['kibana']);

dataArray.forEach(function (data, i) {
Expand All @@ -62,9 +59,6 @@ define(function (require) {

vis.on('brush', _.noop);
vis.render(data);

min = vis.handler.data.getYMin();
max = vis.handler.data.getYMax();
});
});

Expand Down Expand Up @@ -212,6 +206,8 @@ define(function (require) {
it('should return yAxis extents equal to data extents', function () {
vis.handler.charts.forEach(function (chart) {
var yAxis = chart.handler.yAxis;
var min = vis.handler.data.getYMin();
var max = vis.handler.data.getYMax();

expect(yAxis.domain[0]).to.equal(min);
expect(yAxis.domain[1]).to.equal(max);
Expand Down

0 comments on commit 398a805

Please sign in to comment.