From 53312852e9941ab3e99f7cd22896612971c394c5 Mon Sep 17 00:00:00 2001 From: bergquist Date: Tue, 22 Mar 2016 20:23:27 +0100 Subject: [PATCH] tests(influxdb): adds tests for influxdb response --- .../plugins/datasource/influxdb/datasource.ts | 23 ++------ .../datasource/influxdb/response_parser.ts | 23 ++++++++ .../influxdb/specs/response_parser_specs.ts | 58 +++++++++++++++++++ 3 files changed, 86 insertions(+), 18 deletions(-) create mode 100644 public/app/plugins/datasource/influxdb/response_parser.ts create mode 100644 public/app/plugins/datasource/influxdb/specs/response_parser_specs.ts diff --git a/public/app/plugins/datasource/influxdb/datasource.ts b/public/app/plugins/datasource/influxdb/datasource.ts index 1136c9709bd..7052201b422 100644 --- a/public/app/plugins/datasource/influxdb/datasource.ts +++ b/public/app/plugins/datasource/influxdb/datasource.ts @@ -6,6 +6,7 @@ import _ from 'lodash'; import * as dateMath from 'app/core/utils/datemath'; import InfluxSeries from './influx_series'; import InfluxQuery from './influx_query'; +import ResponseParser from './response_parser'; /** @ngInject */ export function InfluxDatasource(instanceSettings, $q, backendSrv, templateSrv) { @@ -22,6 +23,7 @@ export function InfluxDatasource(instanceSettings, $q, backendSrv, templateSrv) this.interval = (instanceSettings.jsonData || {}).timeInterval; this.supportAnnotations = true; this.supportMetrics = true; + this.responseParser = new ResponseParser(); this.query = function(options) { var timeFilter = getTimeFilter(options); @@ -101,7 +103,7 @@ export function InfluxDatasource(instanceSettings, $q, backendSrv, templateSrv) }); }; - this.metricFindQuery = function (query) { + this.metricFindQuery = function (query, queryType) { var interpolated; try { interpolated = templateSrv.replace(query, null, 'regex'); @@ -109,23 +111,8 @@ export function InfluxDatasource(instanceSettings, $q, backendSrv, templateSrv) return $q.reject(err); } - return this._seriesQuery(interpolated).then(function (results) { - if (!results || results.results.length === 0) { return []; } - - var influxResults = results.results[0]; - if (!influxResults.series) { - return []; - } - - var series = influxResults.series[0]; - return _.map(series.values, function(value) { - if (_.isArray(value)) { - return { text: value[0] }; - } else { - return { text: value }; - } - }); - }); + return this._seriesQuery(interpolated) + .then(_.curry(this.responseParser.parse)(queryType)); }; this._seriesQuery = function(query) { diff --git a/public/app/plugins/datasource/influxdb/response_parser.ts b/public/app/plugins/datasource/influxdb/response_parser.ts new file mode 100644 index 00000000000..8d2cdff0e54 --- /dev/null +++ b/public/app/plugins/datasource/influxdb/response_parser.ts @@ -0,0 +1,23 @@ +/// + +import _ from 'lodash'; + +export default class ResponseParser { + parse(queryType, results) { + if (!results || results.results.length === 0) { return []; } + + var influxResults = results.results[0]; + if (!influxResults.series) { + return []; + } + + var series = influxResults.series[0]; + return _.map(series.values, function(value) { + if (_.isArray(value)) { + return { text: value[0] }; + } else { + return { text: value }; + } + }); + } +} diff --git a/public/app/plugins/datasource/influxdb/specs/response_parser_specs.ts b/public/app/plugins/datasource/influxdb/specs/response_parser_specs.ts new file mode 100644 index 00000000000..a52c98f0e05 --- /dev/null +++ b/public/app/plugins/datasource/influxdb/specs/response_parser_specs.ts @@ -0,0 +1,58 @@ +import _ from 'lodash'; +import {describe, beforeEach, it, sinon, expect} from 'test/lib/common'; +import ResponseParser from '../response_parser'; + +describe.only("influxdb response parser", () => { + describe("SHOW_TAGS response", () => { + this.parser = new ResponseParser(); + + describe("response from 0.10.0", () => { + var response = { + "results": [ + { + "series": [ + { + "name": "hostnameTagValues", + "columns": ["hostname"], + "values": [ ["server1"], ["server2"] ] + } + ] + } + ] + }; + + var result = this.parser.parse('SHOW_TAGS', response); + + it("should get two responses", () => { + expect(_.size(result)).to.be(2); + expect(result[0].text).to.be("server1"); + expect(result[1].text).to.be("server2"); + }); + }); + }); + + describe("SHOW_FIELDS response", () => { + describe("response from 0.10.0", () => { + var response = { + "results": [ + { + "series": [ + { + "name": "measurements", + "columns": ["name"], + "values": [ + ["cpu"], ["derivative"], ["logins.count"], ["logs"], ["payment.ended"], ["payment.started"] + ] + } + ] + } + ] + }; + + var result = this.parser.parse('SHOW_FIELDS', response); + it("should get two responses", () => { + expect(_.size(result)).to.be(6); + }); + }); + }); +});