From c8dfb38378ffe5d4907b04bbb963f39f2389481b Mon Sep 17 00:00:00 2001 From: Joe Fleming Date: Thu, 4 Jun 2015 16:55:23 -0700 Subject: [PATCH 1/3] refactor data class test suite remove unused data sets only one beforeEach for to inject the Data class don't inject d3 (not used) shuffle a little bit of code --- test/unit/specs/vislib/lib/data.js | 202 +++++------------------------ 1 file changed, 35 insertions(+), 167 deletions(-) diff --git a/test/unit/specs/vislib/lib/data.js b/test/unit/specs/vislib/lib/data.js index c8d5aef8771e4..c02d9d892183b 100644 --- a/test/unit/specs/vislib/lib/data.js +++ b/test/unit/specs/vislib/lib/data.js @@ -2,6 +2,10 @@ define(function (require) { var angular = require('angular'); var _ = require('lodash'); + var Data; + var dataSeries = require('vislib_fixtures/mock_data/date_histogram/_series'); + var stackedDataSeries = require('vislib_fixtures/mock_data/stacked/_stacked'); + var seriesData = { 'label': '', 'series': [ @@ -94,118 +98,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: [ @@ -215,19 +132,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 () { @@ -237,7 +145,6 @@ define(function (require) { }); describe('Data.flatten', function () { - var DataFactory; var serIn; var rowIn; var colIn; @@ -246,16 +153,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(); @@ -265,9 +165,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; @@ -277,39 +181,17 @@ 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 minValue = 4; + var stackedMinValue = 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, {}); + stackedVisData = new Data(stackedDataSeries, { type: 'histogram' }); }); // The first value in the time series is less than the min date in the @@ -321,6 +203,8 @@ define(function (require) { }); 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(stackedVisData.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); }); @@ -333,32 +217,14 @@ 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 maxValue = 41; + var stackedMaxValue = 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, {}); + stackedVisData = new Data(stackedDataSeries, { type: 'histogram' }); }); // The first value in the time series is less than the min date in the @@ -370,6 +236,8 @@ define(function (require) { }); 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(stackedVisData.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); }); From d84bd861910500ee3b6389ac1eaa2309bf05e593 Mon Sep 17 00:00:00 2001 From: Joe Fleming Date: Thu, 4 Jun 2015 17:05:23 -0700 Subject: [PATCH 2/3] add negative values to the tests also make var naming more consistent --- test/unit/specs/vislib/lib/data.js | 35 +++++++++++++++++++----------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/test/unit/specs/vislib/lib/data.js b/test/unit/specs/vislib/lib/data.js index c02d9d892183b..10e4e70377035 100644 --- a/test/unit/specs/vislib/lib/data.js +++ b/test/unit/specs/vislib/lib/data.js @@ -4,7 +4,8 @@ define(function (require) { var Data; var dataSeries = require('vislib_fixtures/mock_data/date_histogram/_series'); - var stackedDataSeries = require('vislib_fixtures/mock_data/stacked/_stacked'); + var dataSeriesNeg = require('vislib_fixtures/mock_data/date_histogram/_series_neg'); + var dataStacked = require('vislib_fixtures/mock_data/stacked/_stacked'); var seriesData = { 'label': '', @@ -185,13 +186,16 @@ define(function (require) { describe('getYMin method', function () { var visData; - var stackedVisData; + var visDataNeg; + var visDataStacked; var minValue = 4; - var stackedMinValue = 15; + var minValueNeg = -41; + var minValueStacked = 15; beforeEach(function () { visData = new Data(dataSeries, {}); - stackedVisData = new Data(stackedDataSeries, { type: 'histogram' }); + 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 @@ -199,14 +203,15 @@ define(function (require) { // 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(stackedVisData.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 () { @@ -218,13 +223,16 @@ define(function (require) { describe('getYMax method', function () { var visData; - var stackedVisData; + var visDataNeg; + var visDataStacked; var maxValue = 41; - var stackedMaxValue = 115; + var maxValueNeg = -4; + var maxValueStacked = 115; beforeEach(function () { visData = new Data(dataSeries, {}); - stackedVisData = new Data(stackedDataSeries, { type: 'histogram' }); + 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 @@ -232,14 +240,15 @@ define(function (require) { // 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(stackedVisData.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 () { From 3794bf1dd930c5b66d8830e8183fe8c3155f3418 Mon Sep 17 00:00:00 2001 From: Joe Fleming Date: Thu, 4 Jun 2015 17:29:08 -0700 Subject: [PATCH 3/3] undoing the test changes from c451224cc these failures are important, and verification that data is being mutated --- test/unit/specs/vislib/visualizations/column_chart.js | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/test/unit/specs/vislib/visualizations/column_chart.js b/test/unit/specs/vislib/visualizations/column_chart.js index 08ad563e61712..f2264071d0645 100644 --- a/test/unit/specs/vislib/visualizations/column_chart.js +++ b/test/unit/specs/vislib/visualizations/column_chart.js @@ -36,9 +36,6 @@ define(function (require) { 'stacked' ]; - var min; - var max; - angular.module('ColumnChartFactory', ['kibana']); dataArray.forEach(function (data, i) { @@ -62,9 +59,6 @@ define(function (require) { vis.on('brush', _.noop); vis.render(data); - - min = vis.handler.data.getYMin(); - max = vis.handler.data.getYMax(); }); }); @@ -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);