graphite_query.test.ts 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import gfunc from '../gfunc';
  2. import GraphiteQuery from '../graphite_query';
  3. import { TemplateSrvStub } from 'test/specs/helpers';
  4. describe('Graphite query model', () => {
  5. const ctx: any = {
  6. datasource: {
  7. getFuncDef: gfunc.getFuncDef,
  8. getFuncDefs: jest.fn().mockReturnValue(Promise.resolve(gfunc.getFuncDefs('1.0'))),
  9. waitForFuncDefsLoaded: jest.fn().mockReturnValue(Promise.resolve(null)),
  10. createFuncInstance: gfunc.createFuncInstance,
  11. },
  12. // @ts-ignore
  13. templateSrv: new TemplateSrvStub(),
  14. targets: [],
  15. };
  16. beforeEach(() => {
  17. ctx.target = { refId: 'A', target: 'scaleToSeconds(#A, 60)' };
  18. ctx.queryModel = new GraphiteQuery(ctx.datasource, ctx.target, ctx.templateSrv);
  19. });
  20. describe('when updating targets with nested queries', () => {
  21. beforeEach(() => {
  22. ctx.target = { refId: 'D', target: 'asPercent(#A, #C)' };
  23. ctx.targets = [
  24. { refId: 'A', target: 'first.query.count' },
  25. { refId: 'B', target: 'second.query.count' },
  26. { refId: 'C', target: 'diffSeries(#A, #B)' },
  27. { refId: 'D', target: 'asPercent(#A, #C)' },
  28. ];
  29. ctx.queryModel = new GraphiteQuery(ctx.datasource, ctx.target, ctx.templateSrv);
  30. });
  31. it('targetFull should include nested queries', () => {
  32. ctx.queryModel.updateRenderedTarget(ctx.target, ctx.targets);
  33. const targetFullExpected = 'asPercent(first.query.count, diffSeries(first.query.count, second.query.count))';
  34. expect(ctx.queryModel.target.targetFull).toBe(targetFullExpected);
  35. });
  36. it('should not hang on circular references', () => {
  37. ctx.target.target = 'asPercent(#A, #B)';
  38. ctx.targets = [{ refId: 'A', target: 'asPercent(#B, #C)' }, { refId: 'B', target: 'asPercent(#A, #C)' }];
  39. ctx.queryModel.updateRenderedTarget(ctx.target, ctx.targets);
  40. // Just ensure updateRenderedTarget() is completed and doesn't hang
  41. expect(ctx.queryModel.target.targetFull).toBeDefined();
  42. });
  43. });
  44. describe('when query seriesByTag and series ref', () => {
  45. beforeEach(() => {
  46. ctx.target = { refId: 'A', target: `group(seriesByTag('namespace=asd'), #A)` };
  47. ctx.targets = [ctx.target];
  48. ctx.queryModel = new GraphiteQuery(ctx.datasource, ctx.target, ctx.templateSrv);
  49. });
  50. it('should keep group function series ref', () => {
  51. expect(ctx.queryModel.functions[1].params[0]).toBe('#A');
  52. });
  53. });
  54. describe('when query has seriesByTag and highestMax with variable param', () => {
  55. beforeEach(() => {
  56. ctx.target = { refId: 'A', target: `highestMax(seriesByTag('namespace=asd'), $limit)` };
  57. ctx.targets = [ctx.target];
  58. ctx.queryModel = new GraphiteQuery(ctx.datasource, ctx.target, ctx.templateSrv);
  59. });
  60. it('should add $limit to highestMax function param', () => {
  61. expect(ctx.queryModel.segments.length).toBe(0);
  62. expect(ctx.queryModel.functions[1].params[0]).toBe('$limit');
  63. });
  64. });
  65. });