From 52d748baa54d0918af8dd7b730090cf140bb56b3 Mon Sep 17 00:00:00 2001 From: Spencer Alger Date: Fri, 30 Jan 2015 20:20:13 -0700 Subject: [PATCH] [filterBar/createFilter/dateHistogram] update tests --- test/unit/fixtures/agg_resp/date_histogram.js | 252 ++++++++++++++++++ .../buckets/create_filter/date_histogram.js | 123 ++++++--- 2 files changed, 337 insertions(+), 38 deletions(-) create mode 100644 test/unit/fixtures/agg_resp/date_histogram.js diff --git a/test/unit/fixtures/agg_resp/date_histogram.js b/test/unit/fixtures/agg_resp/date_histogram.js new file mode 100644 index 000000000000..3c66a87dd4be --- /dev/null +++ b/test/unit/fixtures/agg_resp/date_histogram.js @@ -0,0 +1,252 @@ +define(function () { + return { + 'took': 35, + 'timed_out': false, + '_shards': { + 'total': 2, + 'successful': 2, + 'failed': 0 + }, + 'hits': { + 'total': 32899, + 'max_score': 0, + 'hits': [] + }, + 'aggregations': { + '1': { + 'buckets': [ + { + 'key_as_string': '2015-01-30T01:00:00.000Z', + 'key': 1422579600000, + 'doc_count': 18 + }, + { + 'key_as_string': '2015-01-30T02:00:00.000Z', + 'key': 1422583200000, + 'doc_count': 68 + }, + { + 'key_as_string': '2015-01-30T03:00:00.000Z', + 'key': 1422586800000, + 'doc_count': 146 + }, + { + 'key_as_string': '2015-01-30T04:00:00.000Z', + 'key': 1422590400000, + 'doc_count': 149 + }, + { + 'key_as_string': '2015-01-30T05:00:00.000Z', + 'key': 1422594000000, + 'doc_count': 363 + }, + { + 'key_as_string': '2015-01-30T06:00:00.000Z', + 'key': 1422597600000, + 'doc_count': 555 + }, + { + 'key_as_string': '2015-01-30T07:00:00.000Z', + 'key': 1422601200000, + 'doc_count': 878 + }, + { + 'key_as_string': '2015-01-30T08:00:00.000Z', + 'key': 1422604800000, + 'doc_count': 1133 + }, + { + 'key_as_string': '2015-01-30T09:00:00.000Z', + 'key': 1422608400000, + 'doc_count': 1438 + }, + { + 'key_as_string': '2015-01-30T10:00:00.000Z', + 'key': 1422612000000, + 'doc_count': 1719 + }, + { + 'key_as_string': '2015-01-30T11:00:00.000Z', + 'key': 1422615600000, + 'doc_count': 1813 + }, + { + 'key_as_string': '2015-01-30T12:00:00.000Z', + 'key': 1422619200000, + 'doc_count': 1790 + }, + { + 'key_as_string': '2015-01-30T13:00:00.000Z', + 'key': 1422622800000, + 'doc_count': 1582 + }, + { + 'key_as_string': '2015-01-30T14:00:00.000Z', + 'key': 1422626400000, + 'doc_count': 1439 + }, + { + 'key_as_string': '2015-01-30T15:00:00.000Z', + 'key': 1422630000000, + 'doc_count': 1154 + }, + { + 'key_as_string': '2015-01-30T16:00:00.000Z', + 'key': 1422633600000, + 'doc_count': 847 + }, + { + 'key_as_string': '2015-01-30T17:00:00.000Z', + 'key': 1422637200000, + 'doc_count': 588 + }, + { + 'key_as_string': '2015-01-30T18:00:00.000Z', + 'key': 1422640800000, + 'doc_count': 374 + }, + { + 'key_as_string': '2015-01-30T19:00:00.000Z', + 'key': 1422644400000, + 'doc_count': 152 + }, + { + 'key_as_string': '2015-01-30T20:00:00.000Z', + 'key': 1422648000000, + 'doc_count': 140 + }, + { + 'key_as_string': '2015-01-30T21:00:00.000Z', + 'key': 1422651600000, + 'doc_count': 73 + }, + { + 'key_as_string': '2015-01-30T22:00:00.000Z', + 'key': 1422655200000, + 'doc_count': 28 + }, + { + 'key_as_string': '2015-01-30T23:00:00.000Z', + 'key': 1422658800000, + 'doc_count': 9 + }, + { + 'key_as_string': '2015-01-31T00:00:00.000Z', + 'key': 1422662400000, + 'doc_count': 29 + }, + { + 'key_as_string': '2015-01-31T01:00:00.000Z', + 'key': 1422666000000, + 'doc_count': 38 + }, + { + 'key_as_string': '2015-01-31T02:00:00.000Z', + 'key': 1422669600000, + 'doc_count': 70 + }, + { + 'key_as_string': '2015-01-31T03:00:00.000Z', + 'key': 1422673200000, + 'doc_count': 136 + }, + { + 'key_as_string': '2015-01-31T04:00:00.000Z', + 'key': 1422676800000, + 'doc_count': 173 + }, + { + 'key_as_string': '2015-01-31T05:00:00.000Z', + 'key': 1422680400000, + 'doc_count': 370 + }, + { + 'key_as_string': '2015-01-31T06:00:00.000Z', + 'key': 1422684000000, + 'doc_count': 545 + }, + { + 'key_as_string': '2015-01-31T07:00:00.000Z', + 'key': 1422687600000, + 'doc_count': 845 + }, + { + 'key_as_string': '2015-01-31T08:00:00.000Z', + 'key': 1422691200000, + 'doc_count': 1070 + }, + { + 'key_as_string': '2015-01-31T09:00:00.000Z', + 'key': 1422694800000, + 'doc_count': 1419 + }, + { + 'key_as_string': '2015-01-31T10:00:00.000Z', + 'key': 1422698400000, + 'doc_count': 1725 + }, + { + 'key_as_string': '2015-01-31T11:00:00.000Z', + 'key': 1422702000000, + 'doc_count': 1801 + }, + { + 'key_as_string': '2015-01-31T12:00:00.000Z', + 'key': 1422705600000, + 'doc_count': 1823 + }, + { + 'key_as_string': '2015-01-31T13:00:00.000Z', + 'key': 1422709200000, + 'doc_count': 1657 + }, + { + 'key_as_string': '2015-01-31T14:00:00.000Z', + 'key': 1422712800000, + 'doc_count': 1454 + }, + { + 'key_as_string': '2015-01-31T15:00:00.000Z', + 'key': 1422716400000, + 'doc_count': 1131 + }, + { + 'key_as_string': '2015-01-31T16:00:00.000Z', + 'key': 1422720000000, + 'doc_count': 810 + }, + { + 'key_as_string': '2015-01-31T17:00:00.000Z', + 'key': 1422723600000, + 'doc_count': 583 + }, + { + 'key_as_string': '2015-01-31T18:00:00.000Z', + 'key': 1422727200000, + 'doc_count': 384 + }, + { + 'key_as_string': '2015-01-31T19:00:00.000Z', + 'key': 1422730800000, + 'doc_count': 165 + }, + { + 'key_as_string': '2015-01-31T20:00:00.000Z', + 'key': 1422734400000, + 'doc_count': 135 + }, + { + 'key_as_string': '2015-01-31T21:00:00.000Z', + 'key': 1422738000000, + 'doc_count': 72 + }, + { + 'key_as_string': '2015-01-31T22:00:00.000Z', + 'key': 1422741600000, + 'doc_count': 8 + } + ] + } + } + }; +}); \ No newline at end of file diff --git a/test/unit/specs/components/agg_types/buckets/create_filter/date_histogram.js b/test/unit/specs/components/agg_types/buckets/create_filter/date_histogram.js index dca71f349ab1..4cbe9fec4666 100644 --- a/test/unit/specs/components/agg_types/buckets/create_filter/date_histogram.js +++ b/test/unit/specs/components/agg_types/buckets/create_filter/date_histogram.js @@ -1,55 +1,102 @@ define(function (require) { - var _ = require('lodash'); - var moment = require('moment'); describe('AggConfig Filters', function () { - describe('date_historgram', function () { - var AggConfig; - var indexPattern; - var Vis; - var createFilter; + describe('date_histogram', function () { + var _ = require('lodash'); + var moment = require('moment'); + var sinon = require('test_utils/auto_release_sinon'); + var aggResp = require('fixtures/agg_resp/date_histogram'); + + var vis; + var agg; + var field; + var filter; + var bucketKey; + var bucketStart; + var getIntervalStub; + var intervalOptions; + + var init; beforeEach(module('kibana')); - beforeEach(inject(function (Private) { - Vis = Private(require('components/vis/vis')); - AggConfig = Private(require('components/vis/_agg_config')); - indexPattern = Private(require('fixtures/stubbed_logstash_index_pattern')); - createFilter = Private(require('components/agg_types/buckets/create_filter/date_histogram')); - })); + beforeEach(inject(function (Private, $injector) { + var Vis = Private(require('components/vis/vis')); + var indexPattern = Private(require('fixtures/stubbed_logstash_index_pattern')); + var createFilter = Private(require('components/agg_types/buckets/create_filter/date_histogram')); + var TimeBuckets = Private(require('components/time_buckets/time_buckets')); + intervalOptions = Private(require('components/agg_types/buckets/_interval_options')); - var runTest = function (intervalString, interval) { - it('should return a range filter for ' + intervalString, function () { - var vis = new Vis(indexPattern, { + init = function (interval, duration) { + interval = interval || 'auto'; + duration = duration || moment.duration(15, 'minutes'); + + bucketKey = _.sample(aggResp.aggregations['1'].buckets).key; + bucketStart = moment(bucketKey); + + var timePad = moment.duration(duration / 2); + var timeBuckets = new TimeBuckets(); + timeBuckets.setBounds({ + min: bucketStart.clone().subtract(timePad), + max: bucketStart.clone().add(timePad), + }); + timeBuckets.setInterval(interval); + + field = _.sample(indexPattern.fields.byType.date); + vis = new Vis(indexPattern, { type: 'histogram', aggs: [ { type: 'date_histogram', schema: 'segment', - params: { field: '@timestamp', interval: intervalString } + params: { field: field.name, interval: interval, buckets: timeBuckets } } ] }); - var aggConfig = vis.aggs.byTypeName.date_histogram[0]; - var date = moment('2014-01-01 12:00'); - var max = date.clone().add(interval, 'ms'); - var filter = createFilter(aggConfig, date.valueOf()); - expect(filter).to.have.property('range'); - expect(filter).to.have.property('meta'); - expect(filter.range).to.have.property('@timestamp'); - expect(filter.range['@timestamp']).to.have.property('gte', date.valueOf()); - expect(filter.range['@timestamp']).to.have.property('lte', max.valueOf()); - expect(filter.meta).to.have.property('index', indexPattern.id); + + agg = vis.aggs[0]; + filter = createFilter(agg, bucketKey); + }; + })); + + it('creates a valid range filter', function () { + init(); + + expect(filter).to.have.property('range'); + expect(filter.range).to.have.property(field.name); + + var fieldParams = filter.range[field.name]; + expect(fieldParams).to.have.property('gte'); + expect(fieldParams.gte).to.be.a('number'); + + expect(fieldParams).to.have.property('lte'); + expect(fieldParams.lte).to.be.a('number'); + + expect(fieldParams.gte).to.be.lessThan(fieldParams.lte); + + expect(filter).to.have.property('meta'); + expect(filter.meta).to.have.property('index', vis.indexPattern.id); + }); + + + it('extends the filter edge to 1ms before the next bucket for all interval options', function () { + intervalOptions.forEach(function (option) { + var duration; + if (moment(1, option.val).isValid()) { + duration = moment.duration(10, option.val); + + if (+duration < 10) { + throw new Error('unable to create interval for ' + option.val); + } + } + + init(option.val, duration); + + var interval = agg.params.buckets.getInterval(); + var params = filter.range[field.name]; + + expect(params.gte).to.be(+bucketStart); + expect(params.lte).to.be(+bucketStart.clone().add(interval).subtract(1, 'ms')); }); - }; - - runTest('auto', 30000); - runTest('second', 10000); - runTest('minute', 60000); - runTest('hour', 3600000); - runTest('day', 86400000); - runTest('week', 604800000); - runTest('month', 2592000000); - runTest('year', 31536000000); - + }); }); }); });