datasource.test.ts 6.8 KB

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