query_ctrl.jest.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. import { uiSegmentSrv } from 'app/core/services/segment_srv';
  2. // import { describe, beforeEach, it, sinon, expect, angularMocks } from 'test/lib/common';
  3. import gfunc from '../gfunc';
  4. // import helpers from 'test/specs/helpers';
  5. import { GraphiteQueryCtrl } from '../query_ctrl';
  6. describe('GraphiteQueryCtrl', () => {
  7. let ctx = <any>{
  8. datasource: {
  9. metricFindQuery: jest.fn(() => Promise.resolve([])),
  10. getFuncDefs: jest.fn(() => Promise.resolve(gfunc.getFuncDefs('1.0'))),
  11. getFuncDef: gfunc.getFuncDef,
  12. waitForFuncDefsLoaded: jest.fn(() => Promise.resolve(null)),
  13. createFuncInstance: gfunc.createFuncInstance,
  14. },
  15. target: { target: 'aliasByNode(scaleToSeconds(test.prod.*,1),2)' },
  16. panelCtrl: {
  17. refresh: jest.fn(),
  18. },
  19. };
  20. ctx.panelCtrl.panel = {
  21. targets: [ctx.target],
  22. };
  23. // beforeEach(angularMocks.module('grafana.core'));
  24. // beforeEach(angularMocks.module('grafana.controllers'));
  25. // beforeEach(angularMocks.module('grafana.services'));
  26. // beforeEach(
  27. // angularMocks.module(function($compileProvider) {
  28. // $compileProvider.preAssignBindingsEnabled(true);
  29. // })
  30. // );
  31. //beforeEach(ctx.providePhase());
  32. // beforeEach(
  33. // angularMocks.inject(($rootScope, $controller, $q) => {
  34. // ctx.$q = $q;
  35. // ctx.scope = $rootScope.$new();
  36. // ctx.target = { target: 'aliasByNode(scaleToSeconds(test.prod.*,1),2)' };
  37. // ctx.datasource.metricFindQuery = sinon.stub().returns(ctx.$q.when([]));
  38. // ctx.datasource.getFuncDefs = sinon.stub().returns(ctx.$q.when(gfunc.getFuncDefs('1.0')));
  39. // ctx.datasource.getFuncDef = gfunc.getFuncDef;
  40. // ctx.datasource.waitForFuncDefsLoaded = sinon.stub().returns(ctx.$q.when(null));
  41. // ctx.datasource.createFuncInstance = gfunc.createFuncInstance;
  42. // ctx.panelCtrl = { panel: {} };
  43. // ctx.panelCtrl = {
  44. // panel: {
  45. // targets: [ctx.target],
  46. // },
  47. // };
  48. // ctx.panelCtrl.refresh = sinon.spy();
  49. // ctx.ctrl = $controller(
  50. // GraphiteQueryCtrl,
  51. // { $scope: ctx.scope },
  52. // {
  53. // panelCtrl: ctx.panelCtrl,
  54. // datasource: ctx.datasource,
  55. // target: ctx.target,
  56. // }
  57. // );
  58. // ctx.scope.$digest();
  59. // })
  60. // );
  61. beforeEach(() => {
  62. GraphiteQueryCtrl.prototype.target = ctx.target;
  63. GraphiteQueryCtrl.prototype.datasource = ctx.datasource;
  64. GraphiteQueryCtrl.prototype.panelCtrl = ctx.panelCtrl;
  65. ctx.ctrl = new GraphiteQueryCtrl(
  66. {},
  67. {},
  68. new uiSegmentSrv({ trustAsHtml: html => html }, { highlightVariablesAsHtml: () => {} }),
  69. {},
  70. {}
  71. );
  72. });
  73. describe('init', () => {
  74. it('should validate metric key exists', () => {
  75. expect(ctx.datasource.metricFindQuery.mock.calls[0][0]).toBe('test.prod.*');
  76. });
  77. it('should delete last segment if no metrics are found', () => {
  78. expect(ctx.ctrl.segments[2].value).toBe('select metric');
  79. });
  80. it('should parse expression and build function model', () => {
  81. expect(ctx.ctrl.queryModel.functions.length).toBe(2);
  82. });
  83. });
  84. describe('when adding function', () => {
  85. beforeEach(() => {
  86. ctx.ctrl.target.target = 'test.prod.*.count';
  87. ctx.ctrl.datasource.metricFindQuery = () => Promise.resolve([{ expandable: false }]);
  88. ctx.ctrl.parseTarget();
  89. ctx.ctrl.addFunction(gfunc.getFuncDef('aliasByNode'));
  90. });
  91. it('should add function with correct node number', () => {
  92. expect(ctx.ctrl.queryModel.functions[0].params[0]).toBe(2);
  93. });
  94. it('should update target', () => {
  95. expect(ctx.ctrl.target.target).toBe('aliasByNode(test.prod.*.count, 2)');
  96. });
  97. it('should call refresh', () => {
  98. expect(ctx.panelCtrl.refresh).toHaveBeenCalled();
  99. });
  100. });
  101. describe('when adding function before any metric segment', () => {
  102. beforeEach(() => {
  103. ctx.ctrl.target.target = '';
  104. ctx.ctrl.datasource.metricFindQuery = () => Promise.resolve([{ expandable: true }]);
  105. ctx.ctrl.parseTarget();
  106. ctx.ctrl.addFunction(gfunc.getFuncDef('asPercent'));
  107. });
  108. it('should add function and remove select metric link', () => {
  109. expect(ctx.ctrl.segments.length).toBe(0);
  110. });
  111. });
  112. describe('when initializing target without metric expression and only function', () => {
  113. beforeEach(() => {
  114. ctx.ctrl.target.target = 'asPercent(#A, #B)';
  115. ctx.ctrl.datasource.metricFindQuery = () => Promise.resolve([]);
  116. ctx.ctrl.parseTarget();
  117. });
  118. it('should not add select metric segment', () => {
  119. expect(ctx.ctrl.segments.length).toBe(1);
  120. });
  121. it('should add second series ref as param', () => {
  122. expect(ctx.ctrl.queryModel.functions[0].params.length).toBe(1);
  123. });
  124. });
  125. describe('when initializing a target with single param func using variable', () => {
  126. beforeEach(() => {
  127. ctx.ctrl.target.target = 'movingAverage(prod.count, $var)';
  128. ctx.ctrl.datasource.metricFindQuery = () => Promise.resolve([]);
  129. ctx.ctrl.parseTarget();
  130. });
  131. it('should add 2 segments', () => {
  132. expect(ctx.ctrl.segments.length).toBe(2);
  133. });
  134. it('should add function param', () => {
  135. expect(ctx.ctrl.queryModel.functions[0].params.length).toBe(1);
  136. });
  137. });
  138. describe('when initializing target without metric expression and function with series-ref', () => {
  139. beforeEach(() => {
  140. ctx.ctrl.target.target = 'asPercent(metric.node.count, #A)';
  141. ctx.ctrl.datasource.metricFindQuery = () => Promise.resolve([]);
  142. ctx.ctrl.parseTarget();
  143. });
  144. it('should add segments', () => {
  145. expect(ctx.ctrl.segments.length).toBe(3);
  146. });
  147. it('should have correct func params', () => {
  148. expect(ctx.ctrl.queryModel.functions[0].params.length).toBe(1);
  149. });
  150. });
  151. describe('when getting altSegments and metricFindQuery returns empty array', () => {
  152. beforeEach(() => {
  153. ctx.ctrl.target.target = 'test.count';
  154. ctx.ctrl.datasource.metricFindQuery = () => Promise.resolve([]);
  155. ctx.ctrl.parseTarget();
  156. ctx.ctrl.getAltSegments(1).then(function(results) {
  157. ctx.altSegments = results;
  158. });
  159. });
  160. it('should have no segments', () => {
  161. expect(ctx.altSegments.length).toBe(0);
  162. });
  163. });
  164. describe('targetChanged', () => {
  165. beforeEach(() => {
  166. ctx.ctrl.target.target = 'aliasByNode(scaleToSeconds(test.prod.*, 1), 2)';
  167. ctx.ctrl.datasource.metricFindQuery = () => Promise.resolve([{ expandable: false }]);
  168. ctx.ctrl.parseTarget();
  169. ctx.ctrl.target.target = '';
  170. ctx.ctrl.targetChanged();
  171. });
  172. it('should rebuild target after expression model', () => {
  173. expect(ctx.ctrl.target.target).toBe('aliasByNode(scaleToSeconds(test.prod.*, 1), 2)');
  174. });
  175. it('should call panelCtrl.refresh', () => {
  176. expect(ctx.panelCtrl.refresh).toHaveBeenCalled();
  177. });
  178. });
  179. describe('when updating targets with nested query', () => {
  180. beforeEach(() => {
  181. ctx.ctrl.target.target = 'scaleToSeconds(#A, 60)';
  182. ctx.ctrl.datasource.metricFindQuery = () => Promise.resolve([{ expandable: false }]);
  183. ctx.ctrl.parseTarget();
  184. });
  185. it('should add function params', () => {
  186. expect(ctx.ctrl.queryModel.segments.length).toBe(1);
  187. expect(ctx.ctrl.queryModel.segments[0].value).toBe('#A');
  188. expect(ctx.ctrl.queryModel.functions[0].params.length).toBe(1);
  189. expect(ctx.ctrl.queryModel.functions[0].params[0]).toBe(60);
  190. });
  191. it('target should remain the same', () => {
  192. expect(ctx.ctrl.target.target).toBe('scaleToSeconds(#A, 60)');
  193. });
  194. it('targetFull should include nested queries', () => {
  195. ctx.ctrl.panelCtrl.panel.targets = [
  196. {
  197. target: 'nested.query.count',
  198. refId: 'A',
  199. },
  200. ];
  201. ctx.ctrl.updateModelTarget();
  202. expect(ctx.ctrl.target.target).toBe('scaleToSeconds(#A, 60)');
  203. expect(ctx.ctrl.target.targetFull).toBe('scaleToSeconds(nested.query.count, 60)');
  204. });
  205. });
  206. describe('when updating target used in other query', () => {
  207. beforeEach(() => {
  208. ctx.ctrl.target.target = 'metrics.a.count';
  209. ctx.ctrl.target.refId = 'A';
  210. ctx.ctrl.datasource.metricFindQuery = () => Promise.resolve([{ expandable: false }]);
  211. ctx.ctrl.parseTarget();
  212. ctx.ctrl.panelCtrl.panel.targets = [ctx.ctrl.target, { target: 'sumSeries(#A)', refId: 'B' }];
  213. ctx.ctrl.updateModelTarget();
  214. });
  215. it('targetFull of other query should update', () => {
  216. expect(ctx.ctrl.panel.targets[1].targetFull).toBe('sumSeries(metrics.a.count)');
  217. });
  218. });
  219. describe('when adding seriesByTag function', () => {
  220. beforeEach(() => {
  221. ctx.ctrl.target.target = '';
  222. ctx.ctrl.datasource.metricFindQuery = () => Promise.resolve([{ expandable: false }]);
  223. ctx.ctrl.parseTarget();
  224. ctx.ctrl.addFunction(gfunc.getFuncDef('seriesByTag'));
  225. });
  226. it('should update functions', () => {
  227. expect(ctx.ctrl.queryModel.getSeriesByTagFuncIndex()).toBe(0);
  228. });
  229. it('should update seriesByTagUsed flag', () => {
  230. expect(ctx.ctrl.queryModel.seriesByTagUsed).toBe(true);
  231. });
  232. it('should update target', () => {
  233. expect(ctx.ctrl.target.target).toBe('seriesByTag()');
  234. });
  235. it('should call refresh', () => {
  236. expect(ctx.panelCtrl.refresh).toHaveBeenCalled();
  237. });
  238. });
  239. describe('when parsing seriesByTag function', () => {
  240. beforeEach(() => {
  241. ctx.ctrl.target.target = "seriesByTag('tag1=value1', 'tag2!=~value2')";
  242. ctx.ctrl.datasource.metricFindQuery = () => Promise.resolve([{ expandable: false }]);
  243. ctx.ctrl.parseTarget();
  244. });
  245. it('should add tags', () => {
  246. const expected = [
  247. { key: 'tag1', operator: '=', value: 'value1' },
  248. { key: 'tag2', operator: '!=~', value: 'value2' },
  249. ];
  250. expect(ctx.ctrl.queryModel.tags).toEqual(expected);
  251. });
  252. it('should add plus button', () => {
  253. expect(ctx.ctrl.addTagSegments.length).toBe(1);
  254. });
  255. });
  256. describe('when tag added', () => {
  257. beforeEach(() => {
  258. ctx.ctrl.target.target = 'seriesByTag()';
  259. ctx.ctrl.datasource.metricFindQuery = () => Promise.resolve([{ expandable: false }]);
  260. ctx.ctrl.parseTarget();
  261. ctx.ctrl.addNewTag({ value: 'tag1' });
  262. });
  263. it('should update tags with default value', () => {
  264. const expected = [{ key: 'tag1', operator: '=', value: '' }];
  265. expect(ctx.ctrl.queryModel.tags).toEqual(expected);
  266. });
  267. it('should update target', () => {
  268. const expected = "seriesByTag('tag1=')";
  269. expect(ctx.ctrl.target.target).toEqual(expected);
  270. });
  271. });
  272. describe('when tag changed', () => {
  273. beforeEach(() => {
  274. ctx.ctrl.target.target = "seriesByTag('tag1=value1', 'tag2!=~value2')";
  275. ctx.ctrl.datasource.metricFindQuery = () => Promise.resolve([{ expandable: false }]);
  276. ctx.ctrl.parseTarget();
  277. ctx.ctrl.tagChanged({ key: 'tag1', operator: '=', value: 'new_value' }, 0);
  278. });
  279. it('should update tags', () => {
  280. const expected = [
  281. { key: 'tag1', operator: '=', value: 'new_value' },
  282. { key: 'tag2', operator: '!=~', value: 'value2' },
  283. ];
  284. expect(ctx.ctrl.queryModel.tags).toEqual(expected);
  285. });
  286. it('should update target', () => {
  287. const expected = "seriesByTag('tag1=new_value', 'tag2!=~value2')";
  288. expect(ctx.ctrl.target.target).toEqual(expected);
  289. });
  290. });
  291. describe('when tag removed', () => {
  292. beforeEach(() => {
  293. ctx.ctrl.target.target = "seriesByTag('tag1=value1', 'tag2!=~value2')";
  294. ctx.ctrl.datasource.metricFindQuery = () => Promise.resolve([{ expandable: false }]);
  295. ctx.ctrl.parseTarget();
  296. ctx.ctrl.removeTag(0);
  297. });
  298. it('should update tags', () => {
  299. const expected = [{ key: 'tag2', operator: '!=~', value: 'value2' }];
  300. expect(ctx.ctrl.queryModel.tags).toEqual(expected);
  301. });
  302. it('should update target', () => {
  303. const expected = "seriesByTag('tag2!=~value2')";
  304. expect(ctx.ctrl.target.target).toEqual(expected);
  305. });
  306. });
  307. });