Commit 45eadae6 by Tobias Skarhed

Convert datasource

parent 6f1b125c
import { describe, beforeEach, it, expect, angularMocks } from 'test/lib/common';
import helpers from 'test/specs/helpers';
import OpenTsDatasource from '../datasource';
describe('opentsdb', function() {
var ctx = new helpers.ServiceTestContext();
var instanceSettings = { url: '', jsonData: { tsdbVersion: 1 } };
beforeEach(angularMocks.module('grafana.core'));
beforeEach(angularMocks.module('grafana.services'));
beforeEach(ctx.providePhase(['backendSrv']));
beforeEach(
angularMocks.inject(function($q, $rootScope, $httpBackend, $injector) {
ctx.$q = $q;
ctx.$httpBackend = $httpBackend;
ctx.$rootScope = $rootScope;
ctx.ds = $injector.instantiate(OpenTsDatasource, {
instanceSettings: instanceSettings,
});
$httpBackend.when('GET', /\.html$/).respond('');
})
);
describe('When performing metricFindQuery', function() {
var results;
var requestOptions;
beforeEach(function() {
ctx.backendSrv.datasourceRequest = function(options) {
requestOptions = options;
return ctx.$q.when({
data: [{ target: 'prod1.count', datapoints: [[10, 1], [12, 1]] }],
});
};
});
it('metrics() should generate api suggest query', function() {
ctx.ds.metricFindQuery('metrics(pew)').then(function(data) {
results = data;
});
ctx.$rootScope.$apply();
expect(requestOptions.url).to.be('/api/suggest');
expect(requestOptions.params.type).to.be('metrics');
expect(requestOptions.params.q).to.be('pew');
expect(results).not.to.be(null);
});
it('tag_names(cpu) should generate lookup query', function() {
ctx.ds.metricFindQuery('tag_names(cpu)').then(function(data) {
results = data;
});
ctx.$rootScope.$apply();
expect(requestOptions.url).to.be('/api/search/lookup');
expect(requestOptions.params.m).to.be('cpu');
});
it('tag_values(cpu, test) should generate lookup query', function() {
ctx.ds.metricFindQuery('tag_values(cpu, hostname)').then(function(data) {
results = data;
});
ctx.$rootScope.$apply();
expect(requestOptions.url).to.be('/api/search/lookup');
expect(requestOptions.params.m).to.be('cpu{hostname=*}');
});
it('tag_values(cpu, test) should generate lookup query', function() {
ctx.ds.metricFindQuery('tag_values(cpu, hostname, env=$env)').then(function(data) {
results = data;
});
ctx.$rootScope.$apply();
expect(requestOptions.url).to.be('/api/search/lookup');
expect(requestOptions.params.m).to.be('cpu{hostname=*,env=$env}');
});
it('tag_values(cpu, test) should generate lookup query', function() {
ctx.ds.metricFindQuery('tag_values(cpu, hostname, env=$env, region=$region)').then(function(data) {
results = data;
});
ctx.$rootScope.$apply();
expect(requestOptions.url).to.be('/api/search/lookup');
expect(requestOptions.params.m).to.be('cpu{hostname=*,env=$env,region=$region}');
});
it('suggest_tagk() should generate api suggest query', function() {
ctx.ds.metricFindQuery('suggest_tagk(foo)').then(function(data) {
results = data;
});
ctx.$rootScope.$apply();
expect(requestOptions.url).to.be('/api/suggest');
expect(requestOptions.params.type).to.be('tagk');
expect(requestOptions.params.q).to.be('foo');
});
it('suggest_tagv() should generate api suggest query', function() {
ctx.ds.metricFindQuery('suggest_tagv(bar)').then(function(data) {
results = data;
});
ctx.$rootScope.$apply();
expect(requestOptions.url).to.be('/api/suggest');
expect(requestOptions.params.type).to.be('tagv');
expect(requestOptions.params.q).to.be('bar');
});
});
});
import OpenTsDatasource from '../datasource';
import $q from 'q';
describe('opentsdb', () => {
let ctx = <any>{
backendSrv: {},
ds: {},
templateSrv: {
replace: str => str,
},
};
let instanceSettings = { url: '', jsonData: { tsdbVersion: 1 } };
beforeEach(() => {
ctx.ctrl = new OpenTsDatasource(instanceSettings, $q, ctx.backendSrv, ctx.templateSrv);
});
describe('When performing metricFindQuery', () => {
var results;
var requestOptions;
beforeEach(async () => {
ctx.backendSrv.datasourceRequest = await function(options) {
requestOptions = options;
return Promise.resolve({
data: [{ target: 'prod1.count', datapoints: [[10, 1], [12, 1]] }],
});
};
});
it('metrics() should generate api suggest query', () => {
ctx.ctrl.metricFindQuery('metrics(pew)').then(function(data) {
results = data;
});
expect(requestOptions.url).toBe('/api/suggest');
expect(requestOptions.params.type).toBe('metrics');
expect(requestOptions.params.q).toBe('pew');
expect(results).not.toBe(null);
});
it('tag_names(cpu) should generate lookup query', () => {
ctx.ctrl.metricFindQuery('tag_names(cpu)').then(function(data) {
results = data;
});
expect(requestOptions.url).toBe('/api/search/lookup');
expect(requestOptions.params.m).toBe('cpu');
});
it('tag_values(cpu, test) should generate lookup query', () => {
ctx.ctrl.metricFindQuery('tag_values(cpu, hostname)').then(function(data) {
results = data;
});
expect(requestOptions.url).toBe('/api/search/lookup');
expect(requestOptions.params.m).toBe('cpu{hostname=*}');
});
it('tag_values(cpu, test) should generate lookup query', () => {
ctx.ctrl.metricFindQuery('tag_values(cpu, hostname, env=$env)').then(function(data) {
results = data;
});
expect(requestOptions.url).toBe('/api/search/lookup');
expect(requestOptions.params.m).toBe('cpu{hostname=*,env=$env}');
});
it('tag_values(cpu, test) should generate lookup query', () => {
ctx.ctrl.metricFindQuery('tag_values(cpu, hostname, env=$env, region=$region)').then(function(data) {
results = data;
});
expect(requestOptions.url).toBe('/api/search/lookup');
expect(requestOptions.params.m).toBe('cpu{hostname=*,env=$env,region=$region}');
});
it('suggest_tagk() should generate api suggest query', () => {
ctx.ctrl.metricFindQuery('suggest_tagk(foo)').then(function(data) {
results = data;
});
expect(requestOptions.url).toBe('/api/suggest');
expect(requestOptions.params.type).toBe('tagk');
expect(requestOptions.params.q).toBe('foo');
});
it('suggest_tagv() should generate api suggest query', () => {
ctx.ctrl.metricFindQuery('suggest_tagv(bar)').then(function(data) {
results = data;
});
expect(requestOptions.url).toBe('/api/suggest');
expect(requestOptions.params.type).toBe('tagv');
expect(requestOptions.params.q).toBe('bar');
});
});
});
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