datasource.test.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. import { of } from 'rxjs';
  2. import { createFetchResponse } from 'test/helpers/createFetchResponse';
  3. import { dataFrameToJSON, dateTime, MetricFindValue, MutableDataFrame } from '@grafana/data';
  4. import { backendSrv } from 'app/core/services/backend_srv';
  5. import { TemplateSrv } from 'app/features/templating/template_srv';
  6. import { initialCustomVariableModelState } from '../../../../features/variables/custom/reducer';
  7. import { MssqlDatasource } from '../datasource';
  8. jest.mock('@grafana/runtime', () => ({
  9. ...(jest.requireActual('@grafana/runtime') as unknown as object),
  10. getBackendSrv: () => backendSrv,
  11. }));
  12. describe('MSSQLDatasource', () => {
  13. const templateSrv: TemplateSrv = new TemplateSrv();
  14. const fetchMock = jest.spyOn(backendSrv, 'fetch');
  15. const ctx: any = {};
  16. beforeEach(() => {
  17. jest.clearAllMocks();
  18. ctx.instanceSettings = { name: 'mssql' };
  19. ctx.ds = new MssqlDatasource(ctx.instanceSettings, templateSrv);
  20. });
  21. describe('When performing annotationQuery', () => {
  22. let results: any;
  23. const annotationName = 'MyAnno';
  24. const options = {
  25. annotation: {
  26. name: annotationName,
  27. rawQuery: 'select time, text, tags from table;',
  28. },
  29. range: {
  30. from: dateTime(1432288354),
  31. to: dateTime(1432288401),
  32. },
  33. };
  34. const response = {
  35. results: {
  36. MyAnno: {
  37. frames: [
  38. dataFrameToJSON(
  39. new MutableDataFrame({
  40. fields: [
  41. { name: 'time', values: [1521545610656, 1521546251185, 1521546501378] },
  42. { name: 'text', values: ['some text', 'some text2', 'some text3'] },
  43. { name: 'tags', values: ['TagA,TagB', ' TagB , TagC', null] },
  44. ],
  45. })
  46. ),
  47. ],
  48. },
  49. },
  50. };
  51. beforeEach(() => {
  52. fetchMock.mockImplementation(() => of(createFetchResponse(response)));
  53. return ctx.ds.annotationQuery(options).then((data: any) => {
  54. results = data;
  55. });
  56. });
  57. it('should return annotation list', () => {
  58. expect(results.length).toBe(3);
  59. expect(results[0].text).toBe('some text');
  60. expect(results[0].tags[0]).toBe('TagA');
  61. expect(results[0].tags[1]).toBe('TagB');
  62. expect(results[1].tags[0]).toBe('TagB');
  63. expect(results[1].tags[1]).toBe('TagC');
  64. expect(results[2].tags.length).toBe(0);
  65. });
  66. });
  67. describe('When performing metricFindQuery that returns multiple string fields', () => {
  68. let results: MetricFindValue[];
  69. const query = 'select * from atable';
  70. const response = {
  71. results: {
  72. tempvar: {
  73. frames: [
  74. dataFrameToJSON(
  75. new MutableDataFrame({
  76. fields: [
  77. { name: 'title', values: ['aTitle', 'aTitle2', 'aTitle3'] },
  78. { name: 'text', values: ['some text', 'some text2', 'some text3'] },
  79. ],
  80. })
  81. ),
  82. ],
  83. },
  84. },
  85. };
  86. beforeEach(() => {
  87. fetchMock.mockImplementation(() => of(createFetchResponse(response)));
  88. return ctx.ds.metricFindQuery(query).then((data: MetricFindValue[]) => {
  89. results = data;
  90. });
  91. });
  92. it('should return list of all column values', () => {
  93. expect(results.length).toBe(6);
  94. expect(results[0].text).toBe('aTitle');
  95. expect(results[5].text).toBe('some text3');
  96. });
  97. });
  98. describe('When performing metricFindQuery with key, value columns', () => {
  99. let results: any;
  100. const query = 'select * from atable';
  101. const response = {
  102. results: {
  103. tempvar: {
  104. frames: [
  105. dataFrameToJSON(
  106. new MutableDataFrame({
  107. fields: [
  108. { name: '__value', values: ['value1', 'value2', 'value3'] },
  109. { name: '__text', values: ['aTitle', 'aTitle2', 'aTitle3'] },
  110. ],
  111. })
  112. ),
  113. ],
  114. },
  115. },
  116. };
  117. beforeEach(() => {
  118. fetchMock.mockImplementation(() => of(createFetchResponse(response)));
  119. return ctx.ds.metricFindQuery(query).then((data: any) => {
  120. results = data;
  121. });
  122. });
  123. it('should return list of as text, value', () => {
  124. expect(results.length).toBe(3);
  125. expect(results[0].text).toBe('aTitle');
  126. expect(results[0].value).toBe('value1');
  127. expect(results[2].text).toBe('aTitle3');
  128. expect(results[2].value).toBe('value3');
  129. });
  130. });
  131. describe('When performing metricFindQuery without key, value columns', () => {
  132. let results: any;
  133. const query = 'select id, values from atable';
  134. const response = {
  135. results: {
  136. tempvar: {
  137. refId: 'tempvar',
  138. frames: [
  139. dataFrameToJSON(
  140. new MutableDataFrame({
  141. fields: [
  142. { name: 'id', values: [1, 2, 3] },
  143. { name: 'values', values: ['test1', 'test2', 'test3'] },
  144. ],
  145. meta: {
  146. executedQueryString: 'select id, values from atable',
  147. },
  148. })
  149. ),
  150. ],
  151. },
  152. },
  153. };
  154. beforeEach(() => {
  155. fetchMock.mockImplementation(() => of(createFetchResponse(response)));
  156. return ctx.ds.metricFindQuery(query).then((data: any) => {
  157. results = data;
  158. });
  159. });
  160. it('should return list of all field values as text', () => {
  161. expect(results).toEqual([
  162. { text: 1 },
  163. { text: 2 },
  164. { text: 3 },
  165. { text: 'test1' },
  166. { text: 'test2' },
  167. { text: 'test3' },
  168. ]);
  169. });
  170. });
  171. describe('When performing metricFindQuery with key, value columns and with duplicate keys', () => {
  172. let results: any;
  173. const query = 'select * from atable';
  174. const response = {
  175. results: {
  176. tempvar: {
  177. frames: [
  178. dataFrameToJSON(
  179. new MutableDataFrame({
  180. fields: [
  181. { name: '__text', values: ['aTitle', 'aTitle', 'aTitle'] },
  182. { name: '__value', values: ['same', 'same', 'diff'] },
  183. ],
  184. })
  185. ),
  186. ],
  187. },
  188. },
  189. };
  190. beforeEach(() => {
  191. fetchMock.mockImplementation(() => of(createFetchResponse(response)));
  192. return ctx.ds.metricFindQuery(query).then((data: any) => {
  193. results = data;
  194. });
  195. });
  196. it('should return list of unique keys', () => {
  197. expect(results.length).toBe(1);
  198. expect(results[0].text).toBe('aTitle');
  199. expect(results[0].value).toBe('same');
  200. });
  201. });
  202. describe('When performing metricFindQuery', () => {
  203. const query = 'select * from atable';
  204. const response = {
  205. results: {
  206. tempvar: {
  207. frames: [
  208. dataFrameToJSON(
  209. new MutableDataFrame({
  210. fields: [{ name: 'test', values: ['aTitle'] }],
  211. })
  212. ),
  213. ],
  214. },
  215. },
  216. };
  217. const time = {
  218. from: dateTime(1521545610656),
  219. to: dateTime(1521546251185),
  220. };
  221. beforeEach(() => {
  222. fetchMock.mockImplementation(() => of(createFetchResponse(response)));
  223. return ctx.ds.metricFindQuery(query, { range: time });
  224. });
  225. it('should pass timerange to datasourceRequest', () => {
  226. expect(fetchMock).toBeCalledTimes(1);
  227. expect(fetchMock.mock.calls[0][0].data.from).toBe(time.from.valueOf().toString());
  228. expect(fetchMock.mock.calls[0][0].data.to).toBe(time.to.valueOf().toString());
  229. expect(fetchMock.mock.calls[0][0].data.queries.length).toBe(1);
  230. expect(fetchMock.mock.calls[0][0].data.queries[0].rawSql).toBe(query);
  231. });
  232. });
  233. describe('When interpolating variables', () => {
  234. beforeEach(() => {
  235. ctx.variable = { ...initialCustomVariableModelState };
  236. });
  237. describe('and value is a string', () => {
  238. it('should return an unquoted value', () => {
  239. expect(ctx.ds.interpolateVariable('abc', ctx.variable)).toEqual('abc');
  240. });
  241. });
  242. describe('and value is a number', () => {
  243. it('should return an unquoted value', () => {
  244. expect(ctx.ds.interpolateVariable(1000, ctx.variable)).toEqual(1000);
  245. });
  246. });
  247. describe('and value is an array of strings', () => {
  248. it('should return comma separated quoted values', () => {
  249. expect(ctx.ds.interpolateVariable(['a', 'b', 'c'], ctx.variable)).toEqual("'a','b','c'");
  250. });
  251. });
  252. describe('and variable allows multi-value and value is a string', () => {
  253. it('should return a quoted value', () => {
  254. ctx.variable.multi = true;
  255. expect(ctx.ds.interpolateVariable('abc', ctx.variable)).toEqual("'abc'");
  256. });
  257. });
  258. describe('and variable contains single quote', () => {
  259. it('should return a quoted value', () => {
  260. ctx.variable.multi = true;
  261. expect(ctx.ds.interpolateVariable("a'bc", ctx.variable)).toEqual("'a''bc'");
  262. });
  263. });
  264. describe('and variable allows all and value is a string', () => {
  265. it('should return a quoted value', () => {
  266. ctx.variable.includeAll = true;
  267. expect(ctx.ds.interpolateVariable('abc', ctx.variable)).toEqual("'abc'");
  268. });
  269. });
  270. });
  271. describe('targetContainsTemplate', () => {
  272. it('given query that contains template variable it should return true', () => {
  273. const rawSql = `SELECT
  274. $__timeGroup(createdAt,'$summarize') as time,
  275. avg(value) as value,
  276. hostname as metric
  277. FROM
  278. grafana_metric
  279. WHERE
  280. $__timeFilter(createdAt) AND
  281. measurement = 'logins.count' AND
  282. hostname IN($host)
  283. GROUP BY $__timeGroup(createdAt,'$summarize'), hostname
  284. ORDER BY 1`;
  285. const query = {
  286. rawSql,
  287. };
  288. templateSrv.init([
  289. { type: 'query', name: 'summarize', current: { value: '1m' } },
  290. { type: 'query', name: 'host', current: { value: 'a' } },
  291. ]);
  292. expect(ctx.ds.targetContainsTemplate(query)).toBeTruthy();
  293. });
  294. it('given query that only contains global template variable it should return false', () => {
  295. const rawSql = `SELECT
  296. $__timeGroup(createdAt,'$__interval') as time,
  297. avg(value) as value,
  298. hostname as metric
  299. FROM
  300. grafana_metric
  301. WHERE
  302. $__timeFilter(createdAt) AND
  303. measurement = 'logins.count'
  304. GROUP BY $__timeGroup(createdAt,'$summarize'), hostname
  305. ORDER BY 1`;
  306. const query = {
  307. rawSql,
  308. };
  309. templateSrv.init([
  310. { type: 'query', name: 'summarize', current: { value: '1m' } },
  311. { type: 'query', name: 'host', current: { value: 'a' } },
  312. ]);
  313. expect(ctx.ds.targetContainsTemplate(query)).toBeFalsy();
  314. });
  315. });
  316. });