More progress on influxdb query editor, templating, refactoring, unit tests, #740, #507, #586

This commit is contained in:
Torkel Ödegaard
2014-09-02 07:05:24 +02:00
parent 141ea7ba91
commit 2da04e72f5
14 changed files with 246 additions and 112 deletions

View File

@@ -1,6 +1,7 @@
define([
'kbn'
], function(kbn) {
'kbn',
'lodash'
], function(kbn, _) {
'use strict';
function ControllerTestContext() {
@@ -47,10 +48,17 @@ define([
function ServiceTestContext() {
var self = this;
self.templateSrv = new TemplateSrvStub();
this.providePhase = function() {
return module(function($provide) {
$provide.value('templateSrv', self.templateSrv);
});
};
this.createService = function(name) {
return inject([name, '$q', '$rootScope', '$httpBackend', function(InfluxDatasource, $q, $rootScope, $httpBackend) {
self.service = InfluxDatasource;
return inject([name, '$q', '$rootScope', '$httpBackend', function(service, $q, $rootScope, $httpBackend) {
self.service = service;
self.$q = $q;
self.$rootScope = $rootScope;
self.$httpBackend = $httpBackend;
@@ -82,11 +90,16 @@ define([
function TemplateSrvStub() {
this.variables = [];
this.replace = function() {};
this.templateSettings = { interpolate : /\[\[([\s\S]+?)\]\]/g };
this.data = {};
this.replace = function(text) {
return _.template(text, this.data, this.templateSettings);
};
this.setGrafanaVariable = function(name, value) {
this.data[name] = value;
};
}
return {
ControllerTestContext: ControllerTestContext,
TimeSrvStub: TimeSrvStub,

View File

@@ -0,0 +1,54 @@
define([
'services/influxdb/influxQueryBuilder'
], function(InfluxQueryBuilder) {
'use strict';
describe('InfluxQueryBuilder', function() {
describe('series with conditon and group by', function() {
var builder = new InfluxQueryBuilder({
series: 'google.test',
column: 'value',
function: 'mean',
condition_filter: true,
condition_expression: "code=1",
groupby_field_add: true,
groupby_field: 'code'
});
var query = builder.build();
it('should generate correct query', function() {
expect(query).to.be('select code, mean(value) from "google.test" where [[$timeFilter]] and code=1 ' +
'group by time([[$interval]]), code order asc');
});
it('should expose groupByFiled', function() {
expect(builder.groupByField).to.be('code');
});
});
describe('old style raw query', function() {
var builder = new InfluxQueryBuilder({
query: 'select host, mean(value) from asd.asd where time > now() - 1h group by time(1s), code order asc',
rawQuery: true
});
var query = builder.build();
it('should generate correct query', function() {
expect(query).to.be('select host, mean(value) from asd.asd where [[$timeFilter]] and time > now() - 1h ' +
' group by time(1s), code order asc');
});
it('should expose groupByFiled', function() {
expect(builder.groupByField).to.be('host');
});
});
});
});

View File

@@ -8,6 +8,7 @@ define([
var ctx = new helpers.ServiceTestContext();
beforeEach(module('grafana.services'));
beforeEach(ctx.providePhase());
beforeEach(ctx.createService('InfluxDatasource'));
describe('When querying influxdb with one target using query editor target spec', function() {

View File

@@ -140,6 +140,18 @@ define([
});
});
describeUpdateVariable('regex pattern remove duplicates', function(ctx) {
ctx.setup(function() {
ctx.variable = { type: 'query', query: 'apps.*', name: 'test' };
ctx.variable.regex = 'backend_01';
ctx.queryResult = [{text: 'apps.backend.backend_01.counters.req'}, {text: 'apps.backend.backend_01.counters.req'}];
});
it('should return matches options', function() {
expect(ctx.variable.options.length).to.be(1);
});
});
describeUpdateVariable('and existing value still exists in options', function(ctx) {
ctx.setup(function() {
ctx.variable = { type: 'query', query: 'apps.*', name: 'test' };
@@ -163,7 +175,29 @@ define([
});
});
describeUpdateVariable('with include all regex wildcard', function(ctx) {
describeUpdateVariable('with include all wildcard', function(ctx) {
ctx.setup(function() {
ctx.variable = { type: 'query', query: 'apps.*', name: 'test', includeAll: true, allFormat: 'wildcard' };
ctx.queryResult = [{text: 'backend1'}, {text: 'backend2'}, { text: 'backend3'}];
});
it('should add All wildcard option', function() {
expect(ctx.variable.options[0].value).to.be('*');
});
});
describeUpdateVariable('with include all wildcard', function(ctx) {
ctx.setup(function() {
ctx.variable = { type: 'query', query: 'apps.*', name: 'test', includeAll: true, allFormat: 'regex wildcard' };
ctx.queryResult = [{text: 'backend1'}, {text: 'backend2'}, { text: 'backend3'}];
});
it('should add All wildcard option', function() {
expect(ctx.variable.options[0].value).to.be('.*');
});
});
describeUpdateVariable('with include all regex values', function(ctx) {
ctx.setup(function() {
ctx.variable = { type: 'query', query: 'apps.*', name: 'test', includeAll: true, allFormat: 'wildcard' };
ctx.queryResult = [{text: 'backend1'}, {text: 'backend2'}, { text: 'backend3'}];

View File

@@ -121,6 +121,8 @@ require([
'specs/timeSeries-specs',
'specs/row-ctrl-specs',
'specs/graphiteTargetCtrl-specs',
'specs/influxSeries-specs',
'specs/influxQueryBuilder-specs',
'specs/influxdb-datasource-specs',
'specs/graph-ctrl-specs',
'specs/grafanaGraph-specs',
@@ -130,8 +132,7 @@ require([
'specs/templateValuesSrv-specs',
'specs/kbn-format-specs',
'specs/dashboardSrv-specs',
'specs/dashboardViewStateSrv-specs',
'specs/influxSeries-specs'
'specs/dashboardViewStateSrv-specs'
], function () {
window.__karma__.start();
});