From 92f50ca38497978fc3eb29f886484b3741ed4aae Mon Sep 17 00:00:00 2001 From: Erik Sundell Date: Thu, 27 Sep 2018 14:24:28 +0200 Subject: [PATCH] stackdriver: add unit tests to resolve unit function --- .../datasource/stackdriver/datasource.ts | 4 +- .../stackdriver/specs/datasource.test.ts | 52 +++++++++++++++++++ 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/public/app/plugins/datasource/stackdriver/datasource.ts b/public/app/plugins/datasource/stackdriver/datasource.ts index 82cca4fcfb0..30499866b3f 100644 --- a/public/app/plugins/datasource/stackdriver/datasource.ts +++ b/public/app/plugins/datasource/stackdriver/datasource.ts @@ -86,7 +86,7 @@ export default class StackdriverDatasource { return interpolatedGroupBys; } - resolveUnit(targets: any[]) { + resolvePanelUnitFromTargets(targets: any[]) { let unit = 'none'; if (targets.length > 0 && targets.every(t => t.unit === targets[0].unit)) { if (stackdriverUnitMappings.hasOwnProperty(targets[0].unit)) { @@ -105,7 +105,7 @@ export default class StackdriverDatasource { return; } - const unit = this.resolveUnit(options.targets); + const unit = this.resolvePanelUnitFromTargets(options.targets); queryRes.series.forEach(series => { result.push({ target: series.name, diff --git a/public/app/plugins/datasource/stackdriver/specs/datasource.test.ts b/public/app/plugins/datasource/stackdriver/specs/datasource.test.ts index 58575b577b9..80830fd4d68 100644 --- a/public/app/plugins/datasource/stackdriver/specs/datasource.test.ts +++ b/public/app/plugins/datasource/stackdriver/specs/datasource.test.ts @@ -219,4 +219,56 @@ describe('StackdriverDataSource', () => { }); }); }); + + describe('unit parsing', () => { + let ds, res; + beforeEach(() => { + ds = new StackdriverDataSource(instanceSettings, {}, templateSrv, timeSrv); + }); + describe('when theres only one target', () => { + describe('and the stackdriver unit doesnt have a corresponding grafana unit', () => { + beforeEach(() => { + res = ds.resolvePanelUnitFromTargets([{ unit: 'megaseconds' }]); + }); + it('should return none', () => { + expect(res).toEqual('none'); + }); + }); + describe('and the stackdriver unit has a corresponding grafana unit', () => { + beforeEach(() => { + res = ds.resolvePanelUnitFromTargets([{ unit: 'bit' }]); + }); + it('should return bits', () => { + expect(res).toEqual('bits'); + }); + }); + }); + + describe('when theres more than one target', () => { + describe('and all target units are the same', () => { + beforeEach(() => { + res = ds.resolvePanelUnitFromTargets([{ unit: 'bit' }, { unit: 'bit' }]); + }); + it('should return bits', () => { + expect(res).toEqual('bits'); + }); + }); + describe('and all target units are the same but doesnt have grafana mappings', () => { + beforeEach(() => { + res = ds.resolvePanelUnitFromTargets([{ unit: 'megaseconds' }, { unit: 'megaseconds' }]); + }); + it('should return the default value - none', () => { + expect(res).toEqual('none'); + }); + }); + describe('and all target units are not the same', () => { + beforeEach(() => { + res = ds.resolvePanelUnitFromTargets([{ unit: 'bit' }, { unit: 'min' }]); + }); + it('should return the default value - none', () => { + expect(res).toEqual('none'); + }); + }); + }); + }); });