From db8bbe3cad26a3bb37cba7ec8269cf594c06c8a1 Mon Sep 17 00:00:00 2001 From: Erik Sundell Date: Fri, 28 Sep 2018 17:06:24 +0200 Subject: [PATCH] stackdriver: unit test group by and aggregation dropdown changes --- .../stackdriver/query_aggregation_ctrl.ts | 11 ++++--- .../specs/query_aggregation_ctrl.test.ts | 31 +++++++++++++++++-- 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/public/app/plugins/datasource/stackdriver/query_aggregation_ctrl.ts b/public/app/plugins/datasource/stackdriver/query_aggregation_ctrl.ts index 236adda6053..de144071b93 100644 --- a/public/app/plugins/datasource/stackdriver/query_aggregation_ctrl.ts +++ b/public/app/plugins/datasource/stackdriver/query_aggregation_ctrl.ts @@ -62,14 +62,12 @@ export class StackdriverAggregationCtrl { }); if (!this.aggOptions.find(o => o.value === this.target.aggregation.crossSeriesReducer)) { - const newValue = this.aggOptions.find(o => o.value !== 'REDUCE_NONE'); - this.target.aggregation.crossSeriesReducer = newValue ? newValue.value : ''; + this.deselectAggregationOption('REDUCE_NONE'); } if (this.target.aggregation.groupBys.length > 0) { this.aggOptions = this.aggOptions.filter(o => o.value !== 'REDUCE_NONE'); - const newValue = this.aggOptions.find(o => o.value !== 'REDUCE_NONE'); - this.target.aggregation.crossSeriesReducer = newValue ? newValue.value : ''; + this.deselectAggregationOption('REDUCE_NONE'); } } @@ -77,6 +75,11 @@ export class StackdriverAggregationCtrl { const selectedAlignment = this.alignOptions.find(ap => ap.value === this.target.aggregation.perSeriesAligner); return `${kbn.secondsToHms(this.$scope.alignmentPeriod)} interval (${selectedAlignment.text})`; } + + deselectAggregationOption(notValidOptionValue: string) { + const newValue = this.aggOptions.find(o => o.value !== notValidOptionValue); + this.target.aggregation.crossSeriesReducer = newValue ? newValue.value : ''; + } } angular.module('grafana.controllers').directive('stackdriverAggregation', StackdriverAggregation); diff --git a/public/app/plugins/datasource/stackdriver/specs/query_aggregation_ctrl.test.ts b/public/app/plugins/datasource/stackdriver/specs/query_aggregation_ctrl.test.ts index 3887381c9a8..ac9ea2ac6bc 100644 --- a/public/app/plugins/datasource/stackdriver/specs/query_aggregation_ctrl.test.ts +++ b/public/app/plugins/datasource/stackdriver/specs/query_aggregation_ctrl.test.ts @@ -4,11 +4,11 @@ describe('StackdriverAggregationCtrl', () => { let ctrl; describe('aggregation and alignment options', () => { describe('when new query result is returned from the server', () => { - describe('and result is double and gauge', () => { + describe('and result is double and gauge and no group by is used', () => { beforeEach(async () => { ctrl = new StackdriverAggregationCtrl({ $on: () => {}, - target: { valueType: 'DOUBLE', metricKind: 'GAUGE', aggregation: { crossSeriesReducer: '' } }, + target: { valueType: 'DOUBLE', metricKind: 'GAUGE', aggregation: { crossSeriesReducer: '', groupBys: [] } }, }); }); @@ -28,6 +28,33 @@ describe('StackdriverAggregationCtrl', () => { ); }); }); + + describe('and result is double and gauge and a group by is used', () => { + beforeEach(async () => { + ctrl = new StackdriverAggregationCtrl({ + $on: () => {}, + target: { + valueType: 'DOUBLE', + metricKind: 'GAUGE', + aggregation: { crossSeriesReducer: 'REDUCE_NONE', groupBys: ['resource.label.projectid'] }, + }, + }); + }); + + it('should populate all aggregate options except three', () => { + ctrl.setAggOptions(); + expect(ctrl.aggOptions.length).toBe(10); + expect(ctrl.aggOptions.map(o => o.value)).toEqual( + expect['not'].arrayContaining(['REDUCE_COUNT_TRUE', 'REDUCE_COUNT_FALSE', 'REDUCE_NONE']) + ); + }); + + it('should select some other reducer than REDUCE_NONE', () => { + ctrl.setAggOptions(); + expect(ctrl.target.aggregation.crossSeriesReducer).not.toBe(''); + expect(ctrl.target.aggregation.crossSeriesReducer).not.toBe('REDUCE_NONE'); + }); + }); }); }); });