Browse Source

stackdriver: unit test group by and aggregation dropdown changes

Erik Sundell 7 years ago
parent
commit
db8bbe3cad

+ 7 - 4
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);

+ 29 - 2
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');
+        });
+      });
     });
   });
 });