123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281 |
- import {Datasource} from "../module";
- import Q from "q";
- describe('GenericDatasource', function() {
- var ctx = {};
- beforeEach(function() {
- ctx.$q = Q;
- ctx.backendSrv = {};
- ctx.templateSrv = {};
- ctx.ds = new Datasource({}, ctx.$q, ctx.backendSrv, ctx.templateSrv);
- });
- it('should return an empty array when no targets are set', function(done) {
- ctx.ds.query({targets: []}).then(function(result) {
- expect(result.data).to.have.length(0);
- done();
- });
- });
- it('should return the server results when a target is set', function(done) {
- ctx.backendSrv.datasourceRequest = function(request) {
- return ctx.$q.when({
- _request: request,
- data: [
- {
- target: 'X',
- datapoints: [1, 2, 3]
- }
- ]
- });
- };
- ctx.templateSrv.replace = function(data) {
- return data;
- }
- ctx.ds.query({targets: ['hits']}).then(function(result) {
- expect(result._request.data.targets).to.have.length(1);
- var series = result.data[0];
- expect(series.target).to.equal('X');
- expect(series.datapoints).to.have.length(3);
- done();
- });
- });
- it ('should return the metric results when a target is null', function(done) {
- ctx.backendSrv.datasourceRequest = function(request) {
- return ctx.$q.when({
- _request: request,
- data: [
- "metric_0",
- "metric_1",
- "metric_2",
- ]
- });
- };
- ctx.templateSrv.replace = function(data) {
- return data;
- }
- ctx.ds.metricFindQuery({target: null}).then(function(result) {
- expect(result).to.have.length(3);
- expect(result[0].text).to.equal('metric_0');
- expect(result[0].value).to.equal('metric_0');
- expect(result[1].text).to.equal('metric_1');
- expect(result[1].value).to.equal('metric_1');
- expect(result[2].text).to.equal('metric_2');
- expect(result[2].value).to.equal('metric_2');
- done();
- });
- });
- it ('should return the metric target results when a target is set', function(done) {
- ctx.backendSrv.datasourceRequest = function(request) {
- var target = request.data.target;
- var result = [target + "_0", target + "_1", target + "_2"];
- return ctx.$q.when({
- _request: request,
- data: result
- });
- };
- ctx.templateSrv.replace = function(data) {
- return data;
- }
- ctx.ds.metricFindQuery('search').then(function(result) {
- expect(result).to.have.length(3);
- expect(result[0].text).to.equal('search_0');
- expect(result[0].value).to.equal('search_0');
- expect(result[1].text).to.equal('search_1');
- expect(result[1].value).to.equal('search_1');
- expect(result[2].text).to.equal('search_2');
- expect(result[2].value).to.equal('search_2');
- done();
- });
- });
- it ('should return the metric results when the target is an empty string', function(done) {
- ctx.backendSrv.datasourceRequest = function(request) {
- return ctx.$q.when({
- _request: request,
- data: [
- "metric_0",
- "metric_1",
- "metric_2",
- ]
- });
- };
- ctx.templateSrv.replace = function(data) {
- return data;
- }
- ctx.ds.metricFindQuery('').then(function(result) {
- expect(result).to.have.length(3);
- expect(result[0].text).to.equal('metric_0');
- expect(result[0].value).to.equal('metric_0');
- expect(result[1].text).to.equal('metric_1');
- expect(result[1].value).to.equal('metric_1');
- expect(result[2].text).to.equal('metric_2');
- expect(result[2].value).to.equal('metric_2');
- done();
- });
- });
- it ('should return the metric results when the args are an empty object', function(done) {
- ctx.backendSrv.datasourceRequest = function(request) {
- return ctx.$q.when({
- _request: request,
- data: [
- "metric_0",
- "metric_1",
- "metric_2",
- ]
- });
- };
- ctx.templateSrv.replace = function(data) {
- return data;
- }
- ctx.ds.metricFindQuery().then(function(result) {
- expect(result).to.have.length(3);
- expect(result[0].text).to.equal('metric_0');
- expect(result[0].value).to.equal('metric_0');
- expect(result[1].text).to.equal('metric_1');
- expect(result[1].value).to.equal('metric_1');
- expect(result[2].text).to.equal('metric_2');
- expect(result[2].value).to.equal('metric_2');
- done();
- });
- });
- it ('should return the metric target results when the args are a string', function(done) {
- ctx.backendSrv.datasourceRequest = function(request) {
- var target = request.data.target;
- var result = [target + "_0", target + "_1", target + "_2"];
- return ctx.$q.when({
- _request: request,
- data: result
- });
- };
- ctx.templateSrv.replace = function(data) {
- return data;
- }
- ctx.ds.metricFindQuery('search').then(function(result) {
- expect(result).to.have.length(3);
- expect(result[0].text).to.equal('search_0');
- expect(result[0].value).to.equal('search_0');
- expect(result[1].text).to.equal('search_1');
- expect(result[1].value).to.equal('search_1');
- expect(result[2].text).to.equal('search_2');
- expect(result[2].value).to.equal('search_2');
- done();
- });
- });
- it ('should return data as text and as value', function(done) {
- var result = ctx.ds.mapToTextValue({data: ["zero", "one", "two"]});
- expect(result).to.have.length(3);
- expect(result[0].text).to.equal('zero');
- expect(result[0].value).to.equal('zero');
- expect(result[1].text).to.equal('one');
- expect(result[1].value).to.equal('one');
- expect(result[2].text).to.equal('two');
- expect(result[2].value).to.equal('two');
- done();
- });
- it ('should return text as text and value as value', function(done) {
- var data = [
- {text: "zero", value: "value_0"},
- {text: "one", value: "value_1"},
- {text: "two", value: "value_2"},
- ];
- var result = ctx.ds.mapToTextValue({data: data});
- expect(result).to.have.length(3);
- expect(result[0].text).to.equal('zero');
- expect(result[0].value).to.equal('value_0');
- expect(result[1].text).to.equal('one');
- expect(result[1].value).to.equal('value_1');
- expect(result[2].text).to.equal('two');
- expect(result[2].value).to.equal('value_2');
- done();
- });
- it ('should return data as text and index as value', function(done) {
- var data = [
- {a: "zero", b: "value_0"},
- {a: "one", b: "value_1"},
- {a: "two", b: "value_2"},
- ];
- var result = ctx.ds.mapToTextValue({data: data});
- expect(result).to.have.length(3);
- expect(result[0].text).to.equal(data[0]);
- expect(result[0].value).to.equal(0);
- expect(result[1].text).to.equal(data[1]);
- expect(result[1].value).to.equal(1);
- expect(result[2].text).to.equal(data[2]);
- expect(result[2].value).to.equal(2);
- done();
- });
- it('should support tag keys', function(done) {
- var data = [{'type': 'string', 'text': 'One', 'key': 'one'}, {'type': 'string', 'text': 'two', 'key': 'Two'}];
- ctx.backendSrv.datasourceRequest = function(request) {
- return ctx.$q.when({
- _request: request,
- data: data
- });
- };
- ctx.ds.getTagKeys().then(function(result) {
- expect(result).to.have.length(2);
- expect(result[0].type).to.equal(data[0].type);
- expect(result[0].text).to.equal(data[0].text);
- expect(result[0].key).to.equal(data[0].key);
- expect(result[1].type).to.equal(data[1].type);
- expect(result[1].text).to.equal(data[1].text);
- expect(result[1].key).to.equal(data[1].key);
- done();
- });
- });
- it('should support tag values', function(done) {
- var data = [{'key': 'eins', 'text': 'Eins!'}, {'key': 'zwei', 'text': 'Zwei'}, {'key': 'drei', 'text': 'Drei!'}];
- ctx.backendSrv.datasourceRequest = function(request) {
- return ctx.$q.when({
- _request: request,
- data: data
- });
- };
- ctx.ds.getTagValues().then(function(result) {
- expect(result).to.have.length(3);
- expect(result[0].text).to.equal(data[0].text);
- expect(result[0].key).to.equal(data[0].key);
- expect(result[1].text).to.equal(data[1].text);
- expect(result[1].key).to.equal(data[1].key);
- expect(result[2].text).to.equal(data[2].text);
- expect(result[2].key).to.equal(data[2].key);
- done();
- });
- });
- });
|