| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 |
- import { MssqlDatasource } from '../datasource';
- import { TemplateSrvStub, TimeSrvStub } from 'test/specs/helpers';
- import { CustomVariable } from 'app/features/templating/custom_variable';
- import q from 'q';
- import { dateTime } from '@grafana/ui/src/utils/moment_wrapper';
- describe('MSSQLDatasource', () => {
- const ctx: any = {
- backendSrv: {},
- templateSrv: new TemplateSrvStub(),
- timeSrv: new TimeSrvStub(),
- };
- beforeEach(() => {
- ctx.$q = q;
- ctx.instanceSettings = { name: 'mssql' };
- ctx.ds = new MssqlDatasource(ctx.instanceSettings, ctx.backendSrv, ctx.$q, ctx.templateSrv, ctx.timeSrv);
- });
- describe('When performing annotationQuery', () => {
- let results;
- const annotationName = 'MyAnno';
- const options = {
- annotation: {
- name: annotationName,
- rawQuery: 'select time, text, tags from table;',
- },
- range: {
- from: dateTime(1432288354),
- to: dateTime(1432288401),
- },
- };
- const response = {
- results: {
- MyAnno: {
- refId: annotationName,
- tables: [
- {
- columns: [{ text: 'time' }, { text: 'text' }, { text: 'tags' }],
- rows: [
- [1521545610656, 'some text', 'TagA,TagB'],
- [1521546251185, 'some text2', ' TagB , TagC'],
- [1521546501378, 'some text3'],
- ],
- },
- ],
- },
- },
- };
- beforeEach(() => {
- ctx.backendSrv.datasourceRequest = options => {
- return ctx.$q.when({ data: response, status: 200 });
- };
- return ctx.ds.annotationQuery(options).then(data => {
- results = data;
- });
- });
- it('should return annotation list', () => {
- expect(results.length).toBe(3);
- expect(results[0].text).toBe('some text');
- expect(results[0].tags[0]).toBe('TagA');
- expect(results[0].tags[1]).toBe('TagB');
- expect(results[1].tags[0]).toBe('TagB');
- expect(results[1].tags[1]).toBe('TagC');
- expect(results[2].tags.length).toBe(0);
- });
- });
- describe('When performing metricFindQuery', () => {
- let results;
- const query = 'select * from atable';
- const response = {
- results: {
- tempvar: {
- meta: {
- rowCount: 3,
- },
- refId: 'tempvar',
- tables: [
- {
- columns: [{ text: 'title' }, { text: 'text' }],
- rows: [['aTitle', 'some text'], ['aTitle2', 'some text2'], ['aTitle3', 'some text3']],
- },
- ],
- },
- },
- };
- beforeEach(() => {
- ctx.backendSrv.datasourceRequest = options => {
- return ctx.$q.when({ data: response, status: 200 });
- };
- return ctx.ds.metricFindQuery(query).then(data => {
- results = data;
- });
- });
- it('should return list of all column values', () => {
- expect(results.length).toBe(6);
- expect(results[0].text).toBe('aTitle');
- expect(results[5].text).toBe('some text3');
- });
- });
- describe('When performing metricFindQuery with key, value columns', () => {
- let results;
- const query = 'select * from atable';
- const response = {
- results: {
- tempvar: {
- meta: {
- rowCount: 3,
- },
- refId: 'tempvar',
- tables: [
- {
- columns: [{ text: '__value' }, { text: '__text' }],
- rows: [['value1', 'aTitle'], ['value2', 'aTitle2'], ['value3', 'aTitle3']],
- },
- ],
- },
- },
- };
- beforeEach(() => {
- ctx.backendSrv.datasourceRequest = options => {
- return ctx.$q.when({ data: response, status: 200 });
- };
- return ctx.ds.metricFindQuery(query).then(data => {
- results = data;
- });
- });
- it('should return list of as text, value', () => {
- expect(results.length).toBe(3);
- expect(results[0].text).toBe('aTitle');
- expect(results[0].value).toBe('value1');
- expect(results[2].text).toBe('aTitle3');
- expect(results[2].value).toBe('value3');
- });
- });
- describe('When performing metricFindQuery with key, value columns and with duplicate keys', () => {
- let results;
- const query = 'select * from atable';
- const response = {
- results: {
- tempvar: {
- meta: {
- rowCount: 3,
- },
- refId: 'tempvar',
- tables: [
- {
- columns: [{ text: '__text' }, { text: '__value' }],
- rows: [['aTitle', 'same'], ['aTitle', 'same'], ['aTitle', 'diff']],
- },
- ],
- },
- },
- };
- beforeEach(() => {
- ctx.backendSrv.datasourceRequest = options => {
- return ctx.$q.when({ data: response, status: 200 });
- };
- return ctx.ds.metricFindQuery(query).then(data => {
- results = data;
- });
- });
- it('should return list of unique keys', () => {
- expect(results.length).toBe(1);
- expect(results[0].text).toBe('aTitle');
- expect(results[0].value).toBe('same');
- });
- });
- describe('When performing metricFindQuery', () => {
- let results;
- const query = 'select * from atable';
- const response = {
- results: {
- tempvar: {
- meta: {
- rowCount: 1,
- },
- refId: 'tempvar',
- tables: [
- {
- columns: [{ text: 'title' }],
- rows: [['aTitle']],
- },
- ],
- },
- },
- };
- const time = {
- from: dateTime(1521545610656),
- to: dateTime(1521546251185),
- };
- beforeEach(() => {
- ctx.timeSrv.setTime(time);
- ctx.backendSrv.datasourceRequest = options => {
- results = options.data;
- return ctx.$q.when({ data: response, status: 200 });
- };
- return ctx.ds.metricFindQuery(query);
- });
- it('should pass timerange to datasourceRequest', () => {
- expect(results.from).toBe(time.from.valueOf().toString());
- expect(results.to).toBe(time.to.valueOf().toString());
- expect(results.queries.length).toBe(1);
- expect(results.queries[0].rawSql).toBe(query);
- });
- });
- describe('When interpolating variables', () => {
- beforeEach(() => {
- ctx.variable = new CustomVariable({}, {});
- });
- describe('and value is a string', () => {
- it('should return an unquoted value', () => {
- expect(ctx.ds.interpolateVariable('abc', ctx.variable)).toEqual('abc');
- });
- });
- describe('and value is a number', () => {
- it('should return an unquoted value', () => {
- expect(ctx.ds.interpolateVariable(1000, ctx.variable)).toEqual(1000);
- });
- });
- describe('and value is an array of strings', () => {
- it('should return comma separated quoted values', () => {
- expect(ctx.ds.interpolateVariable(['a', 'b', 'c'], ctx.variable)).toEqual("'a','b','c'");
- });
- });
- describe('and variable allows multi-value and value is a string', () => {
- it('should return a quoted value', () => {
- ctx.variable.multi = true;
- expect(ctx.ds.interpolateVariable('abc', ctx.variable)).toEqual("'abc'");
- });
- });
- describe('and variable contains single quote', () => {
- it('should return a quoted value', () => {
- ctx.variable.multi = true;
- expect(ctx.ds.interpolateVariable("a'bc", ctx.variable)).toEqual("'a''bc'");
- });
- });
- describe('and variable allows all and value is a string', () => {
- it('should return a quoted value', () => {
- ctx.variable.includeAll = true;
- expect(ctx.ds.interpolateVariable('abc', ctx.variable)).toEqual("'abc'");
- });
- });
- });
- });
|