datasource.test.ts 6.4 KB

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