Commit 8a39b32b by Torkel Ödegaard

refactor: moved elasticsearch specs to plugin folder and to typescript

parent 7d2646f6
define([
'angular',
'lodash',
'./queryDef',
'./query_def',
],
function (angular, _, queryDef) {
'use strict';
......
......@@ -3,10 +3,10 @@ define([
'lodash',
'moment',
'kbn',
'./queryBuilder',
'./indexPattern',
'./elasticResponse',
'./queryCtrl',
'./query_builder',
'./index_pattern',
'./elastic_response',
'./query_ctrl',
'./directives'
],
function (angular, _, moment, kbn, ElasticQueryBuilder, IndexPattern, ElasticResponse) {
......
define([
'angular',
'./bucketAgg',
'./metricAgg',
'./bucket_agg',
'./metric_agg',
],
function (angular) {
'use strict';
......
define([
'angular',
'lodash',
'./queryDef'
'./query_def'
],
function (angular, _, queryDef) {
'use strict';
......
define([
'./helpers',
'moment',
'angular',
'app/plugins/datasource/elasticsearch/datasource',
], function(helpers, moment, angular) {
'use strict';
///<amd-dependency path="../datasource" />
///<amd-dependency path="test/specs/helpers" name="helpers" />
describe('ElasticDatasource', function() {
var ctx = new helpers.ServiceTestContext();
import {describe, beforeEach, it, sinon, expect, angularMocks} from 'test/lib/common';
import moment = require('moment');
import angular = require('angular');
beforeEach(module('grafana.services'));
beforeEach(ctx.providePhase(['templateSrv', 'backendSrv']));
beforeEach(ctx.createService('ElasticDatasource'));
beforeEach(function() {
ctx.ds = new ctx.service({jsonData: {}});
});
declare var helpers: any;
describe('When testing datasource with index pattern', function() {
beforeEach(function() {
ctx.ds = new ctx.service({
url: 'http://es.com',
index: '[asd-]YYYY.MM.DD',
jsonData: { interval: 'Daily' }
});
describe('ElasticDatasource', function() {
var ctx = new helpers.ServiceTestContext();
beforeEach(angularMocks.module('grafana.services'));
beforeEach(ctx.providePhase(['templateSrv', 'backendSrv']));
beforeEach(ctx.createService('ElasticDatasource'));
beforeEach(function() {
ctx.ds = new ctx.service({jsonData: {}});
});
describe('When testing datasource with index pattern', function() {
beforeEach(function() {
ctx.ds = new ctx.service({
url: 'http://es.com',
index: '[asd-]YYYY.MM.DD',
jsonData: { interval: 'Daily' }
});
});
it('should translate index pattern to current day', function() {
var requestOptions;
ctx.backendSrv.datasourceRequest = function(options) {
requestOptions = options;
return ctx.$q.when({});
};
it('should translate index pattern to current day', function() {
var requestOptions;
ctx.backendSrv.datasourceRequest = function(options) {
requestOptions = options;
return ctx.$q.when({});
};
ctx.ds.testDatasource();
ctx.$rootScope.$apply();
ctx.ds.testDatasource();
ctx.$rootScope.$apply();
var today = moment().format("YYYY.MM.DD");
expect(requestOptions.url).to.be("http://es.com/asd-" + today + '/_stats');
});
var today = moment().format("YYYY.MM.DD");
expect(requestOptions.url).to.be("http://es.com/asd-" + today + '/_stats');
});
});
describe('When issueing metric query with interval pattern', function() {
beforeEach(function() {
ctx.ds = new ctx.service({
url: 'http://es.com',
index: '[asd-]YYYY.MM.DD',
jsonData: { interval: 'Daily' }
});
describe('When issueing metric query with interval pattern', function() {
beforeEach(function() {
ctx.ds = new ctx.service({
url: 'http://es.com',
index: '[asd-]YYYY.MM.DD',
jsonData: { interval: 'Daily' }
});
});
it('should translate index pattern to current day', function() {
var requestOptions;
ctx.backendSrv.datasourceRequest = function(options) {
requestOptions = options;
return ctx.$q.when({data: {responses: []}});
};
ctx.ds.query({
range: {
from: moment([2015, 4, 30, 10]),
to: moment([2015, 5, 1, 10])
},
targets: [{ bucketAggs: [], metrics: [] }]
});
it('should translate index pattern to current day', function() {
var requestOptions;
ctx.backendSrv.datasourceRequest = function(options) {
requestOptions = options;
return ctx.$q.when({data: {responses: []}});
};
ctx.$rootScope.$apply();
var parts = requestOptions.data.split('\n');
var header = angular.fromJson(parts[0]);
expect(header.index).to.eql(['asd-2015.05.30', 'asd-2015.05.31', 'asd-2015.06.01']);
ctx.ds.query({
range: {
from: moment([2015, 4, 30, 10]),
to: moment([2015, 5, 1, 10])
},
targets: [{ bucketAggs: [], metrics: [] }]
});
ctx.$rootScope.$apply();
var parts = requestOptions.data.split('\n');
var header = angular.fromJson(parts[0]);
expect(header.index).to.eql(['asd-2015.05.30', 'asd-2015.05.31', 'asd-2015.06.01']);
});
});
});
define([
'moment',
'app/plugins/datasource/elasticsearch/indexPattern'
], function(moment, IndexPattern) {
'use strict';
///<amd-dependency path="../index_pattern" name="IndexPattern"/>
///<amd-dependency path="test/specs/helpers" name="helpers" />
describe('IndexPattern', function() {
import {describe, beforeEach, it, sinon, expect} from 'test/lib/common';
import moment = require('moment');
describe('when getting index for today', function() {
it('should return correct index name', function() {
var pattern = new IndexPattern('[asd-]YYYY.MM.DD', 'Daily');
var expected = 'asd-' + moment().format('YYYY.MM.DD');
declare var IndexPattern: any;
expect(pattern.getIndexForToday()).to.be(expected);
});
describe('IndexPattern', function() {
describe('when getting index for today', function() {
it('should return correct index name', function() {
var pattern = new IndexPattern('[asd-]YYYY.MM.DD', 'Daily');
var expected = 'asd-' + moment().format('YYYY.MM.DD');
expect(pattern.getIndexForToday()).to.be(expected);
});
});
describe('when getting index list for time range', function() {
describe('when getting index list for time range', function() {
describe('no interval', function() {
it('should return correct index', function() {
var pattern = new IndexPattern('my-metrics');
var from = new Date(2015, 4, 30, 1, 2, 3);
var to = new Date(2015, 5, 1, 12, 5 , 6);
expect(pattern.getIndexList(from, to)).to.eql('my-metrics');
});
describe('no interval', function() {
it('should return correct index', function() {
var pattern = new IndexPattern('my-metrics');
var from = new Date(2015, 4, 30, 1, 2, 3);
var to = new Date(2015, 5, 1, 12, 5 , 6);
expect(pattern.getIndexList(from, to)).to.eql('my-metrics');
});
});
describe('daily', function() {
it('should return correct index list', function() {
var pattern = new IndexPattern('[asd-]YYYY.MM.DD', 'Daily');
var from = new Date(1432940523000);
var to = new Date(1433153106000);
describe('daily', function() {
var expected = [
'asd-2015.05.29',
'asd-2015.05.30',
'asd-2015.05.31',
'asd-2015.06.01',
];
it('should return correct index list', function() {
var pattern = new IndexPattern('[asd-]YYYY.MM.DD', 'Daily');
var from = new Date(1432940523000);
var to = new Date(1433153106000);
expect(pattern.getIndexList(from, to)).to.eql(expected);
});
var expected = [
'asd-2015.05.29',
'asd-2015.05.30',
'asd-2015.05.31',
'asd-2015.06.01',
];
expect(pattern.getIndexList(from, to)).to.eql(expected);
});
});
});
});
define([
'app/plugins/datasource/elasticsearch/queryBuilder'
], function(ElasticQueryBuilder) {
'use strict';
///<amd-dependency path="../query_builder" name="ElasticQueryBuilder"/>
describe('ElasticQueryBuilder', function() {
var builder;
import {describe, beforeEach, it, sinon, expect} from 'test/lib/common';
beforeEach(function() {
builder = new ElasticQueryBuilder({timeField: '@timestamp'});
});
declare var ElasticQueryBuilder: any;
describe('ElasticQueryBuilder', function() {
var builder;
it('with defaults', function() {
var query = builder.build({
metrics: [{type: 'Count', id: '0'}],
timeField: '@timestamp',
bucketAggs: [{type: 'date_histogram', field: '@timestamp', id: '1'}],
});
beforeEach(function() {
builder = new ElasticQueryBuilder({timeField: '@timestamp'});
});
expect(query.query.filtered.filter.bool.must[0].range["@timestamp"].gte).to.be("$timeFrom");
expect(query.aggs["1"].date_histogram.extended_bounds.min).to.be("$timeFrom");
it('with defaults', function() {
var query = builder.build({
metrics: [{type: 'Count', id: '0'}],
timeField: '@timestamp',
bucketAggs: [{type: 'date_histogram', field: '@timestamp', id: '1'}],
});
it('with raw query', function() {
var query = builder.build({
rawQuery: '{"query": "$lucene_query"}',
});
expect(query.query.filtered.filter.bool.must[0].range["@timestamp"].gte).to.be("$timeFrom");
expect(query.aggs["1"].date_histogram.extended_bounds.min).to.be("$timeFrom");
});
expect(query.query).to.be("$lucene_query");
it('with raw query', function() {
var query = builder.build({
rawQuery: '{"query": "$lucene_query"}',
});
it('with multiple bucket aggs', function() {
var query = builder.build({
metrics: [{type: 'count', id: '1'}],
timeField: '@timestamp',
bucketAggs: [
{type: 'terms', field: '@host', id: '2'},
{type: 'date_histogram', field: '@timestamp', id: '3'}
],
});
expect(query.aggs["2"].terms.field).to.be("@host");
expect(query.aggs["2"].aggs["3"].date_histogram.field).to.be("@timestamp");
expect(query.query).to.be("$lucene_query");
});
it('with multiple bucket aggs', function() {
var query = builder.build({
metrics: [{type: 'count', id: '1'}],
timeField: '@timestamp',
bucketAggs: [
{type: 'terms', field: '@host', id: '2'},
{type: 'date_histogram', field: '@timestamp', id: '3'}
],
});
it('with select field', function() {
var query = builder.build({
metrics: [{type: 'avg', field: '@value', id: '1'}],
bucketAggs: [{type: 'date_histogram', field: '@timestamp', id: '2'}],
}, 100, 1000);
expect(query.aggs["2"].terms.field).to.be("@host");
expect(query.aggs["2"].aggs["3"].date_histogram.field).to.be("@timestamp");
});
var aggs = query.aggs["2"].aggs;
expect(aggs["1"].avg.field).to.be("@value");
});
it('with select field', function() {
var query = builder.build({
metrics: [{type: 'avg', field: '@value', id: '1'}],
bucketAggs: [{type: 'date_histogram', field: '@timestamp', id: '2'}],
}, 100, 1000);
it('with term agg and order by metric agg', function() {
var query = builder.build({
metrics: [
{type: 'count', id: '1'},
{type: 'avg', field: '@value', id: '5'}
],
bucketAggs: [
{type: 'terms', field: '@host', settings: {size: 5, order: 'asc', orderBy: '5'}, id: '2' },
{type: 'date_histogram', field: '@timestamp', id: '3'}
],
}, 100, 1000);
var firstLevel = query.aggs["2"];
var secondLevel = firstLevel.aggs["3"];
expect(firstLevel.aggs["5"].avg.field).to.be("@value");
expect(secondLevel.aggs["5"].avg.field).to.be("@value");
});
var aggs = query.aggs["2"].aggs;
expect(aggs["1"].avg.field).to.be("@value");
});
it('with term agg and order by metric agg', function() {
var query = builder.build({
metrics: [
{type: 'count', id: '1'},
{type: 'avg', field: '@value', id: '5'}
],
bucketAggs: [
{type: 'terms', field: '@host', settings: {size: 5, order: 'asc', orderBy: '5'}, id: '2' },
{type: 'date_histogram', field: '@timestamp', id: '3'}
],
}, 100, 1000);
var firstLevel = query.aggs["2"];
var secondLevel = firstLevel.aggs["3"];
expect(firstLevel.aggs["5"].avg.field).to.be("@value");
expect(secondLevel.aggs["5"].avg.field).to.be("@value");
});
it('with metric percentiles', function() {
var query = builder.build({
metrics: [
{
id: '1',
type: 'percentiles',
field: '@load_time',
settings: {
percents: [1,2,3,4]
}
it('with metric percentiles', function() {
var query = builder.build({
metrics: [
{
id: '1',
type: 'percentiles',
field: '@load_time',
settings: {
percents: [1,2,3,4]
}
],
bucketAggs: [
{type: 'date_histogram', field: '@timestamp', id: '3'}
],
}, 100, 1000);
}
],
bucketAggs: [
{type: 'date_histogram', field: '@timestamp', id: '3'}
],
}, 100, 1000);
var firstLevel = query.aggs["3"];
var firstLevel = query.aggs["3"];
expect(firstLevel.aggs["1"].percentiles.field).to.be("@load_time");
expect(firstLevel.aggs["1"].percentiles.percents).to.eql([1,2,3,4]);
});
expect(firstLevel.aggs["1"].percentiles.field).to.be("@load_time");
expect(firstLevel.aggs["1"].percentiles.percents).to.eql([1,2,3,4]);
});
it('with filters aggs', function() {
var query = builder.build({
metrics: [{type: 'count', id: '1'}],
timeField: '@timestamp',
bucketAggs: [
{
id: '2',
type: 'filters',
settings: {
filters: [
{query: '@metric:cpu' },
{query: '@metric:logins.count' },
]
}
},
{type: 'date_histogram', field: '@timestamp', id: '4'}
],
});
expect(query.aggs["2"].filters.filters["@metric:cpu"].query.query_string.query).to.be("@metric:cpu");
expect(query.aggs["2"].filters.filters["@metric:logins.count"].query.query_string.query).to.be("@metric:logins.count");
expect(query.aggs["2"].aggs["4"].date_histogram.field).to.be("@timestamp");
it('with filters aggs', function() {
var query = builder.build({
metrics: [{type: 'count', id: '1'}],
timeField: '@timestamp',
bucketAggs: [
{
id: '2',
type: 'filters',
settings: {
filters: [
{query: '@metric:cpu' },
{query: '@metric:logins.count' },
]
}
},
{type: 'date_histogram', field: '@timestamp', id: '4'}
],
});
expect(query.aggs["2"].filters.filters["@metric:cpu"].query.query_string.query).to.be("@metric:cpu");
expect(query.aggs["2"].filters.filters["@metric:logins.count"].query.query_string.query).to.be("@metric:logins.count");
expect(query.aggs["2"].aggs["4"].date_histogram.field).to.be("@timestamp");
});
});
define([
'./helpers',
'app/plugins/datasource/elasticsearch/queryCtrl',
'app/services/uiSegmentSrv'
], function(helpers) {
'use strict';
///<amd-dependency path="../query_ctrl" />
///<amd-dependency path="app/services/uiSegmentSrv" />
///<amd-dependency path="test/specs/helpers" name="helpers" />
describe('ElasticQueryCtrl', function() {
var ctx = new helpers.ControllerTestContext();
import {describe, beforeEach, it, sinon, expect, angularMocks} from 'test/lib/common';
beforeEach(module('grafana.controllers'));
beforeEach(module('grafana.services'));
beforeEach(ctx.providePhase());
beforeEach(ctx.createControllerPhase('ElasticQueryCtrl'));
declare var helpers: any;
beforeEach(function() {
ctx.scope.target = {};
ctx.scope.$parent = { get_data: sinon.spy() };
describe('ElasticQueryCtrl', function() {
var ctx = new helpers.ControllerTestContext();
ctx.scope.datasource = ctx.datasource;
ctx.scope.datasource.metricFindQuery = sinon.stub().returns(ctx.$q.when([]));
});
beforeEach(angularMocks.module('grafana.controllers'));
beforeEach(angularMocks.module('grafana.services'));
beforeEach(ctx.providePhase());
beforeEach(ctx.createControllerPhase('ElasticQueryCtrl'));
describe('init', function() {
beforeEach(function() {
ctx.scope.init();
});
beforeEach(function() {
ctx.scope.target = {};
ctx.scope.$parent = { get_data: sinon.spy() };
});
ctx.scope.datasource = ctx.datasource;
ctx.scope.datasource.metricFindQuery = sinon.stub().returns(ctx.$q.when([]));
});
describe('init', function() {
beforeEach(function() {
ctx.scope.init();
});
});
});
///<amd-dependency path="app/plugins/datasource/influxdb/query_builder" name="InfluxQueryBuilder"/>
//
import {describe, beforeEach, it, sinon, expect} from 'test/lib/common';
declare var InfluxQueryBuilder: any;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment