datasource.test.ts 7.0 KB

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