datasource.test.ts 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. import { MssqlDatasource } from '../datasource';
  2. import { TemplateSrvStub, TimeSrvStub } from 'test/specs/helpers';
  3. import { CustomVariable } from 'app/features/templating/custom_variable';
  4. // @ts-ignore
  5. import q from 'q';
  6. import { dateTime } from '@grafana/data';
  7. describe('MSSQLDatasource', () => {
  8. const ctx: any = {
  9. backendSrv: {},
  10. // @ts-ignore
  11. templateSrv: new TemplateSrvStub(),
  12. timeSrv: new TimeSrvStub(),
  13. };
  14. beforeEach(() => {
  15. ctx.$q = q;
  16. ctx.instanceSettings = { name: 'mssql' };
  17. ctx.ds = new MssqlDatasource(ctx.instanceSettings, ctx.backendSrv, ctx.$q, ctx.templateSrv, ctx.timeSrv);
  18. });
  19. describe('When performing annotationQuery', () => {
  20. let results: any;
  21. const annotationName = 'MyAnno';
  22. const options = {
  23. annotation: {
  24. name: annotationName,
  25. rawQuery: 'select time, text, tags from table;',
  26. },
  27. range: {
  28. from: dateTime(1432288354),
  29. to: dateTime(1432288401),
  30. },
  31. };
  32. const response = {
  33. results: {
  34. MyAnno: {
  35. refId: annotationName,
  36. tables: [
  37. {
  38. columns: [{ text: 'time' }, { text: 'text' }, { text: 'tags' }],
  39. rows: [
  40. [1521545610656, 'some text', 'TagA,TagB'],
  41. [1521546251185, 'some text2', ' TagB , TagC'],
  42. [1521546501378, 'some text3'],
  43. ],
  44. },
  45. ],
  46. },
  47. },
  48. };
  49. beforeEach(() => {
  50. ctx.backendSrv.datasourceRequest = (options: any) => {
  51. return ctx.$q.when({ data: response, status: 200 });
  52. };
  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', () => {
  68. let results: any;
  69. const query = 'select * from atable';
  70. const response = {
  71. results: {
  72. tempvar: {
  73. meta: {
  74. rowCount: 3,
  75. },
  76. refId: 'tempvar',
  77. tables: [
  78. {
  79. columns: [{ text: 'title' }, { text: 'text' }],
  80. rows: [['aTitle', 'some text'], ['aTitle2', 'some text2'], ['aTitle3', 'some text3']],
  81. },
  82. ],
  83. },
  84. },
  85. };
  86. beforeEach(() => {
  87. ctx.backendSrv.datasourceRequest = (options: any) => {
  88. return ctx.$q.when({ data: response, status: 200 });
  89. };
  90. return ctx.ds.metricFindQuery(query).then((data: any) => {
  91. results = data;
  92. });
  93. });
  94. it('should return list of all column values', () => {
  95. expect(results.length).toBe(6);
  96. expect(results[0].text).toBe('aTitle');
  97. expect(results[5].text).toBe('some text3');
  98. });
  99. });
  100. describe('When performing metricFindQuery with key, value columns', () => {
  101. let results: any;
  102. const query = 'select * from atable';
  103. const response = {
  104. results: {
  105. tempvar: {
  106. meta: {
  107. rowCount: 3,
  108. },
  109. refId: 'tempvar',
  110. tables: [
  111. {
  112. columns: [{ text: '__value' }, { text: '__text' }],
  113. rows: [['value1', 'aTitle'], ['value2', 'aTitle2'], ['value3', 'aTitle3']],
  114. },
  115. ],
  116. },
  117. },
  118. };
  119. beforeEach(() => {
  120. ctx.backendSrv.datasourceRequest = (options: any) => {
  121. return ctx.$q.when({ data: response, status: 200 });
  122. };
  123. return ctx.ds.metricFindQuery(query).then((data: any) => {
  124. results = data;
  125. });
  126. });
  127. it('should return list of as text, value', () => {
  128. expect(results.length).toBe(3);
  129. expect(results[0].text).toBe('aTitle');
  130. expect(results[0].value).toBe('value1');
  131. expect(results[2].text).toBe('aTitle3');
  132. expect(results[2].value).toBe('value3');
  133. });
  134. });
  135. describe('When performing metricFindQuery with key, value columns and with duplicate keys', () => {
  136. let results: any;
  137. const query = 'select * from atable';
  138. const response = {
  139. results: {
  140. tempvar: {
  141. meta: {
  142. rowCount: 3,
  143. },
  144. refId: 'tempvar',
  145. tables: [
  146. {
  147. columns: [{ text: '__text' }, { text: '__value' }],
  148. rows: [['aTitle', 'same'], ['aTitle', 'same'], ['aTitle', 'diff']],
  149. },
  150. ],
  151. },
  152. },
  153. };
  154. beforeEach(() => {
  155. ctx.backendSrv.datasourceRequest = (options: any) => {
  156. return ctx.$q.when({ data: response, status: 200 });
  157. };
  158. return ctx.ds.metricFindQuery(query).then((data: any) => {
  159. results = data;
  160. });
  161. });
  162. it('should return list of unique keys', () => {
  163. expect(results.length).toBe(1);
  164. expect(results[0].text).toBe('aTitle');
  165. expect(results[0].value).toBe('same');
  166. });
  167. });
  168. describe('When performing metricFindQuery', () => {
  169. let results: any;
  170. const query = 'select * from atable';
  171. const response = {
  172. results: {
  173. tempvar: {
  174. meta: {
  175. rowCount: 1,
  176. },
  177. refId: 'tempvar',
  178. tables: [
  179. {
  180. columns: [{ text: 'title' }],
  181. rows: [['aTitle']],
  182. },
  183. ],
  184. },
  185. },
  186. };
  187. const time = {
  188. from: dateTime(1521545610656),
  189. to: dateTime(1521546251185),
  190. };
  191. beforeEach(() => {
  192. ctx.timeSrv.setTime(time);
  193. ctx.backendSrv.datasourceRequest = (options: any) => {
  194. results = options.data;
  195. return ctx.$q.when({ data: response, status: 200 });
  196. };
  197. return ctx.ds.metricFindQuery(query);
  198. });
  199. it('should pass timerange to datasourceRequest', () => {
  200. expect(results.from).toBe(time.from.valueOf().toString());
  201. expect(results.to).toBe(time.to.valueOf().toString());
  202. expect(results.queries.length).toBe(1);
  203. expect(results.queries[0].rawSql).toBe(query);
  204. });
  205. });
  206. describe('When interpolating variables', () => {
  207. beforeEach(() => {
  208. ctx.variable = new CustomVariable({}, {} as any);
  209. });
  210. describe('and value is a string', () => {
  211. it('should return an unquoted value', () => {
  212. expect(ctx.ds.interpolateVariable('abc', ctx.variable)).toEqual('abc');
  213. });
  214. });
  215. describe('and value is a number', () => {
  216. it('should return an unquoted value', () => {
  217. expect(ctx.ds.interpolateVariable(1000, ctx.variable)).toEqual(1000);
  218. });
  219. });
  220. describe('and value is an array of strings', () => {
  221. it('should return comma separated quoted values', () => {
  222. expect(ctx.ds.interpolateVariable(['a', 'b', 'c'], ctx.variable)).toEqual("'a','b','c'");
  223. });
  224. });
  225. describe('and variable allows multi-value and value is a string', () => {
  226. it('should return a quoted value', () => {
  227. ctx.variable.multi = true;
  228. expect(ctx.ds.interpolateVariable('abc', ctx.variable)).toEqual("'abc'");
  229. });
  230. });
  231. describe('and variable contains single quote', () => {
  232. it('should return a quoted value', () => {
  233. ctx.variable.multi = true;
  234. expect(ctx.ds.interpolateVariable("a'bc", ctx.variable)).toEqual("'a''bc'");
  235. });
  236. });
  237. describe('and variable allows all and value is a string', () => {
  238. it('should return a quoted value', () => {
  239. ctx.variable.includeAll = true;
  240. expect(ctx.ds.interpolateVariable('abc', ctx.variable)).toEqual("'abc'");
  241. });
  242. });
  243. });
  244. });