graphite_query.test.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { TemplateSrvStub } from 'test/specs/helpers';
  2. import gfunc from '../gfunc';
  3. import GraphiteQuery from '../graphite_query';
  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 = [
  39. { refId: 'A', target: 'asPercent(#B, #C)' },
  40. { refId: 'B', target: 'asPercent(#A, #C)' },
  41. ];
  42. ctx.queryModel.updateRenderedTarget(ctx.target, ctx.targets);
  43. // Just ensure updateRenderedTarget() is completed and doesn't hang
  44. expect(ctx.queryModel.target.targetFull).toBeDefined();
  45. });
  46. });
  47. describe('when query seriesByTag and series ref', () => {
  48. beforeEach(() => {
  49. ctx.target = { refId: 'A', target: `group(seriesByTag('namespace=asd'), #A)` };
  50. ctx.targets = [ctx.target];
  51. ctx.queryModel = new GraphiteQuery(ctx.datasource, ctx.target, ctx.templateSrv);
  52. });
  53. it('should keep group function series ref', () => {
  54. expect(ctx.queryModel.functions[1].params[0]).toBe('#A');
  55. });
  56. });
  57. describe('when query has seriesByTag and highestMax with variable param', () => {
  58. beforeEach(() => {
  59. ctx.target = { refId: 'A', target: `highestMax(seriesByTag('namespace=asd'), $limit)` };
  60. ctx.targets = [ctx.target];
  61. ctx.queryModel = new GraphiteQuery(ctx.datasource, ctx.target, ctx.templateSrv);
  62. });
  63. it('should add $limit to highestMax function param', () => {
  64. expect(ctx.queryModel.segments.length).toBe(0);
  65. expect(ctx.queryModel.functions[1].params[0]).toBe('$limit');
  66. });
  67. });
  68. describe('when query is generated from segments', () => {
  69. beforeEach(() => {
  70. ctx.target = { refId: 'A', target: '' };
  71. ctx.queryModel = new GraphiteQuery(ctx.datasource, ctx.target, ctx.templateSrv);
  72. });
  73. it('and no segments are selected then the query is empty', () => {
  74. ctx.queryModel.segments = [{ value: 'select metric' }];
  75. ctx.queryModel.updateModelTarget(ctx.targets);
  76. expect(ctx.queryModel.target.target).toBe('');
  77. });
  78. it('and some segments are selected then segments without selected value are omitted', () => {
  79. ctx.queryModel.segments = [{ value: 'foo' }, { value: 'bar' }, { value: 'select metric' }];
  80. ctx.queryModel.updateModelTarget(ctx.targets);
  81. expect(ctx.queryModel.target.target).toBe('foo.bar');
  82. });
  83. });
  84. });