datasource.test.ts 7.5 KB

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