datasource.test.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. import moment from 'moment';
  2. import { MysqlDatasource } from '../datasource';
  3. import { CustomVariable } from 'app/features/templating/custom_variable';
  4. describe('MySQLDatasource', () => {
  5. const instanceSettings = { name: 'mysql' };
  6. const backendSrv = {};
  7. const templateSrv = {
  8. replace: jest.fn(text => text),
  9. };
  10. const raw = {
  11. from: moment.utc('2018-04-25 10:00'),
  12. to: moment.utc('2018-04-25 11:00'),
  13. };
  14. const ctx = {
  15. backendSrv,
  16. timeSrvMock: {
  17. timeRange: () => ({
  18. from: raw.from,
  19. to: raw.to,
  20. raw: raw,
  21. }),
  22. },
  23. } as any;
  24. beforeEach(() => {
  25. ctx.ds = new MysqlDatasource(instanceSettings, backendSrv, {}, templateSrv, ctx.timeSrvMock);
  26. });
  27. describe('When performing annotationQuery', () => {
  28. let results;
  29. const annotationName = 'MyAnno';
  30. const options = {
  31. annotation: {
  32. name: annotationName,
  33. rawQuery: 'select time_sec, text, tags from table;',
  34. },
  35. range: {
  36. from: moment(1432288354),
  37. to: moment(1432288401),
  38. },
  39. };
  40. const response = {
  41. results: {
  42. MyAnno: {
  43. refId: annotationName,
  44. tables: [
  45. {
  46. columns: [{ text: 'time_sec' }, { text: 'text' }, { text: 'tags' }],
  47. rows: [
  48. [1432288355, 'some text', 'TagA,TagB'],
  49. [1432288390, 'some text2', ' TagB , TagC'],
  50. [1432288400, 'some text3'],
  51. ],
  52. },
  53. ],
  54. },
  55. },
  56. };
  57. beforeEach(() => {
  58. ctx.backendSrv.datasourceRequest = jest.fn(options => {
  59. return Promise.resolve({ data: response, status: 200 });
  60. });
  61. ctx.ds.annotationQuery(options).then(data => {
  62. results = data;
  63. });
  64. });
  65. it('should return annotation list', () => {
  66. expect(results.length).toBe(3);
  67. expect(results[0].text).toBe('some text');
  68. expect(results[0].tags[0]).toBe('TagA');
  69. expect(results[0].tags[1]).toBe('TagB');
  70. expect(results[1].tags[0]).toBe('TagB');
  71. expect(results[1].tags[1]).toBe('TagC');
  72. expect(results[2].tags.length).toBe(0);
  73. });
  74. });
  75. describe('When performing metricFindQuery', () => {
  76. let results;
  77. const query = 'select * from atable';
  78. const response = {
  79. results: {
  80. tempvar: {
  81. meta: {
  82. rowCount: 3,
  83. },
  84. refId: 'tempvar',
  85. tables: [
  86. {
  87. columns: [{ text: 'title' }, { text: 'text' }],
  88. rows: [['aTitle', 'some text'], ['aTitle2', 'some text2'], ['aTitle3', 'some text3']],
  89. },
  90. ],
  91. },
  92. },
  93. };
  94. beforeEach(() => {
  95. ctx.backendSrv.datasourceRequest = jest.fn(options => {
  96. return Promise.resolve({ data: response, status: 200 });
  97. });
  98. ctx.ds.metricFindQuery(query).then(data => {
  99. results = data;
  100. });
  101. });
  102. it('should return list of all column values', () => {
  103. expect(results.length).toBe(6);
  104. expect(results[0].text).toBe('aTitle');
  105. expect(results[5].text).toBe('some text3');
  106. });
  107. });
  108. describe('When performing metricFindQuery with key, value columns', () => {
  109. let results;
  110. const query = 'select * from atable';
  111. const response = {
  112. results: {
  113. tempvar: {
  114. meta: {
  115. rowCount: 3,
  116. },
  117. refId: 'tempvar',
  118. tables: [
  119. {
  120. columns: [{ text: '__value' }, { text: '__text' }],
  121. rows: [['value1', 'aTitle'], ['value2', 'aTitle2'], ['value3', 'aTitle3']],
  122. },
  123. ],
  124. },
  125. },
  126. };
  127. beforeEach(() => {
  128. ctx.backendSrv.datasourceRequest = jest.fn(options => {
  129. return Promise.resolve({ data: response, status: 200 });
  130. });
  131. ctx.ds.metricFindQuery(query).then(data => {
  132. results = data;
  133. });
  134. });
  135. it('should return list of as text, value', () => {
  136. expect(results.length).toBe(3);
  137. expect(results[0].text).toBe('aTitle');
  138. expect(results[0].value).toBe('value1');
  139. expect(results[2].text).toBe('aTitle3');
  140. expect(results[2].value).toBe('value3');
  141. });
  142. });
  143. describe('When performing metricFindQuery with key, value columns and with duplicate keys', () => {
  144. let results;
  145. const query = 'select * from atable';
  146. const response = {
  147. results: {
  148. tempvar: {
  149. meta: {
  150. rowCount: 3,
  151. },
  152. refId: 'tempvar',
  153. tables: [
  154. {
  155. columns: [{ text: '__text' }, { text: '__value' }],
  156. rows: [['aTitle', 'same'], ['aTitle', 'same'], ['aTitle', 'diff']],
  157. },
  158. ],
  159. },
  160. },
  161. };
  162. beforeEach(() => {
  163. ctx.backendSrv.datasourceRequest = jest.fn(options => {
  164. return Promise.resolve({ data: response, status: 200 });
  165. });
  166. ctx.ds.metricFindQuery(query).then(data => {
  167. results = data;
  168. });
  169. });
  170. it('should return list of unique keys', () => {
  171. expect(results.length).toBe(1);
  172. expect(results[0].text).toBe('aTitle');
  173. expect(results[0].value).toBe('same');
  174. });
  175. });
  176. describe('When interpolating variables', () => {
  177. beforeEach(() => {
  178. ctx.variable = new CustomVariable({}, {});
  179. });
  180. describe('and value is a string', () => {
  181. it('should return an unquoted value', () => {
  182. expect(ctx.ds.interpolateVariable('abc', ctx.variable)).toEqual('abc');
  183. });
  184. });
  185. describe('and value is a number', () => {
  186. it('should return an unquoted value', () => {
  187. expect(ctx.ds.interpolateVariable(1000, ctx.variable)).toEqual(1000);
  188. });
  189. });
  190. describe('and value is an array of strings', () => {
  191. it('should return comma separated quoted values', () => {
  192. expect(ctx.ds.interpolateVariable(['a', 'b', 'c'], ctx.variable)).toEqual("'a','b','c'");
  193. });
  194. });
  195. describe('and variable allows multi-value and value is a string', () => {
  196. it('should return a quoted value', () => {
  197. ctx.variable.multi = true;
  198. expect(ctx.ds.interpolateVariable('abc', ctx.variable)).toEqual("'abc'");
  199. });
  200. });
  201. describe('and variable contains single quote', () => {
  202. it('should return a quoted value', () => {
  203. ctx.variable.multi = true;
  204. expect(ctx.ds.interpolateVariable("a'bc", ctx.variable)).toEqual("'a''bc'");
  205. });
  206. });
  207. describe('and variable allows all and value is a string', () => {
  208. it('should return a quoted value', () => {
  209. ctx.variable.includeAll = true;
  210. expect(ctx.ds.interpolateVariable('abc', ctx.variable)).toEqual("'abc'");
  211. });
  212. });
  213. });
  214. });