gfunc.test.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import gfunc from '../gfunc';
  2. describe('when creating func instance from func names', function() {
  3. it('should return func instance', function() {
  4. const func = gfunc.createFuncInstance('sumSeries');
  5. expect(func).toBeTruthy();
  6. expect(func.def.name).toEqual('sumSeries');
  7. expect(func.def.params.length).toEqual(1);
  8. expect(func.def.params[0].multiple).toEqual(true);
  9. expect(func.def.defaultParams.length).toEqual(1);
  10. });
  11. it('should return func instance with shortName', function() {
  12. const func = gfunc.createFuncInstance('sum');
  13. expect(func).toBeTruthy();
  14. });
  15. it('should return func instance from funcDef', function() {
  16. const func = gfunc.createFuncInstance('sum');
  17. const func2 = gfunc.createFuncInstance(func.def);
  18. expect(func2).toBeTruthy();
  19. });
  20. it('func instance should have text representation', function() {
  21. const func = gfunc.createFuncInstance('groupByNode');
  22. func.params[0] = 5;
  23. func.params[1] = 'avg';
  24. func.updateText();
  25. expect(func.text).toEqual('groupByNode(5, avg)');
  26. });
  27. });
  28. describe('when rendering func instance', function() {
  29. it('should handle single metric param', function() {
  30. const func = gfunc.createFuncInstance('sumSeries');
  31. expect(func.render('hello.metric')).toEqual('sumSeries(hello.metric)');
  32. });
  33. it('should include default params if options enable it', function() {
  34. const func = gfunc.createFuncInstance('scaleToSeconds', {
  35. withDefaultParams: true,
  36. });
  37. expect(func.render('hello')).toEqual('scaleToSeconds(hello, 1)');
  38. });
  39. it('should handle int or interval params with number', function() {
  40. const func = gfunc.createFuncInstance('movingMedian');
  41. func.params[0] = '5';
  42. expect(func.render('hello')).toEqual('movingMedian(hello, 5)');
  43. });
  44. it('should handle int or interval params with interval string', function() {
  45. const func = gfunc.createFuncInstance('movingMedian');
  46. func.params[0] = '5min';
  47. expect(func.render('hello')).toEqual("movingMedian(hello, '5min')");
  48. });
  49. it('should never quote boolean paramater', function() {
  50. const func = gfunc.createFuncInstance('sortByName');
  51. func.params[0] = '$natural';
  52. expect(func.render('hello')).toEqual('sortByName(hello, $natural)');
  53. });
  54. it('should never quote int paramater', function() {
  55. const func = gfunc.createFuncInstance('maximumAbove');
  56. func.params[0] = '$value';
  57. expect(func.render('hello')).toEqual('maximumAbove(hello, $value)');
  58. });
  59. it('should never quote node paramater', function() {
  60. const func = gfunc.createFuncInstance('aliasByNode');
  61. func.params[0] = '$node';
  62. expect(func.render('hello')).toEqual('aliasByNode(hello, $node)');
  63. });
  64. it('should handle metric param and int param and string param', function() {
  65. const func = gfunc.createFuncInstance('groupByNode');
  66. func.params[0] = 5;
  67. func.params[1] = 'avg';
  68. expect(func.render('hello.metric')).toEqual("groupByNode(hello.metric, 5, 'avg')");
  69. });
  70. it('should handle function with no metric param', function() {
  71. const func = gfunc.createFuncInstance('randomWalk');
  72. func.params[0] = 'test';
  73. expect(func.render(undefined)).toEqual("randomWalk('test')");
  74. });
  75. it('should handle function multiple series params', function() {
  76. const func = gfunc.createFuncInstance('asPercent');
  77. func.params[0] = '#B';
  78. expect(func.render('#A')).toEqual('asPercent(#A, #B)');
  79. });
  80. });
  81. describe('when requesting function definitions', function() {
  82. it('should return function definitions', function() {
  83. const funcIndex = gfunc.getFuncDefs('1.0');
  84. expect(Object.keys(funcIndex).length).toBeGreaterThan(8);
  85. });
  86. });
  87. describe('when updating func param', function() {
  88. it('should update param value and update text representation', function() {
  89. const func = gfunc.createFuncInstance('summarize', {
  90. withDefaultParams: true,
  91. });
  92. func.updateParam('1h', 0);
  93. expect(func.params[0]).toBe('1h');
  94. expect(func.text).toBe('summarize(1h, sum, false)');
  95. });
  96. it('should parse numbers as float', function() {
  97. const func = gfunc.createFuncInstance('scale');
  98. func.updateParam('0.001', 0);
  99. expect(func.params[0]).toBe('0.001');
  100. });
  101. });
  102. describe('when updating func param with optional second parameter', function() {
  103. it('should update value and text', function() {
  104. const func = gfunc.createFuncInstance('aliasByNode');
  105. func.updateParam('1', 0);
  106. expect(func.params[0]).toBe('1');
  107. });
  108. it('should slit text and put value in second param', function() {
  109. const func = gfunc.createFuncInstance('aliasByNode');
  110. func.updateParam('4,-5', 0);
  111. expect(func.params[0]).toBe('4');
  112. expect(func.params[1]).toBe('-5');
  113. expect(func.text).toBe('aliasByNode(4, -5)');
  114. });
  115. it('should remove second param when empty string is set', function() {
  116. const func = gfunc.createFuncInstance('aliasByNode');
  117. func.updateParam('4,-5', 0);
  118. func.updateParam('', 1);
  119. expect(func.params[0]).toBe('4');
  120. expect(func.params[1]).toBe(undefined);
  121. expect(func.text).toBe('aliasByNode(4)');
  122. });
  123. });