gfunc.test.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. import gfunc from '../gfunc';
  2. describe('when creating func instance from func names', () => {
  3. it('should return func instance', () => {
  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', () => {
  12. const func = gfunc.createFuncInstance('sum');
  13. expect(func).toBeTruthy();
  14. });
  15. it('should return func instance from funcDef', () => {
  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', () => {
  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. function replaceVariablesDummy(str: string) {
  29. // important that this does replace
  30. return str.replace('asdasdas', 'asdsad');
  31. }
  32. describe('when rendering func instance', () => {
  33. it('should handle single metric param', () => {
  34. const func = gfunc.createFuncInstance('sumSeries');
  35. expect(func.render('hello.metric', replaceVariablesDummy)).toEqual('sumSeries(hello.metric)');
  36. });
  37. it('should include default params if options enable it', () => {
  38. const func = gfunc.createFuncInstance('scaleToSeconds', {
  39. withDefaultParams: true,
  40. });
  41. expect(func.render('hello', replaceVariablesDummy)).toEqual('scaleToSeconds(hello, 1)');
  42. });
  43. it('should handle int or interval params with number', () => {
  44. const func = gfunc.createFuncInstance('movingMedian');
  45. func.params[0] = '5';
  46. expect(func.render('hello', replaceVariablesDummy)).toEqual('movingMedian(hello, 5)');
  47. });
  48. it('should handle int or interval params with interval string', () => {
  49. const func = gfunc.createFuncInstance('movingMedian');
  50. func.params[0] = '5min';
  51. expect(func.render('hello', replaceVariablesDummy)).toEqual("movingMedian(hello, '5min')");
  52. });
  53. it('should never quote boolean paramater', () => {
  54. const func = gfunc.createFuncInstance('sortByName');
  55. func.params[0] = '$natural';
  56. expect(func.render('hello', replaceVariablesDummy)).toEqual('sortByName(hello, $natural)');
  57. });
  58. it('should never quote int paramater', () => {
  59. const func = gfunc.createFuncInstance('maximumAbove');
  60. func.params[0] = '$value';
  61. expect(func.render('hello', replaceVariablesDummy)).toEqual('maximumAbove(hello, $value)');
  62. });
  63. it('should never quote node paramater', () => {
  64. const func = gfunc.createFuncInstance('aliasByNode');
  65. func.params[0] = '$node';
  66. expect(func.render('hello', replaceVariablesDummy)).toEqual('aliasByNode(hello, $node)');
  67. });
  68. it('should handle metric param and int param and string param', () => {
  69. const func = gfunc.createFuncInstance('groupByNode');
  70. func.params[0] = 5;
  71. func.params[1] = 'avg';
  72. expect(func.render('hello.metric', replaceVariablesDummy)).toEqual("groupByNode(hello.metric, 5, 'avg')");
  73. });
  74. it('should handle function with no metric param', () => {
  75. const func = gfunc.createFuncInstance('randomWalk');
  76. func.params[0] = 'test';
  77. expect(func.render(undefined as unknown as string, replaceVariablesDummy)).toEqual("randomWalk('test')");
  78. });
  79. it('should handle function multiple series params', () => {
  80. const func = gfunc.createFuncInstance('asPercent');
  81. func.params[0] = '#B';
  82. expect(func.render('#A', replaceVariablesDummy)).toEqual('asPercent(#A, #B)');
  83. });
  84. it('should not quote variables that have numeric value', () => {
  85. const func = gfunc.createFuncInstance('movingAverage');
  86. func.params[0] = '$variable';
  87. const replaceVariables = (str: string) => {
  88. return str.replace('$variable', '60');
  89. };
  90. expect(func.render('metric', replaceVariables)).toBe('movingAverage(metric, $variable)');
  91. });
  92. it('should quote variables that have string value', () => {
  93. const func = gfunc.createFuncInstance('movingAverage');
  94. func.params[0] = '$variable';
  95. const replaceVariables = (str: string) => {
  96. return str.replace('$variable', '10min');
  97. };
  98. expect(func.render('metric', replaceVariables)).toBe("movingAverage(metric, '$variable')");
  99. });
  100. });
  101. describe('when requesting function definitions', () => {
  102. it('should return function definitions', () => {
  103. const funcIndex = gfunc.getFuncDefs('1.0');
  104. expect(Object.keys(funcIndex).length).toBeGreaterThan(8);
  105. });
  106. });
  107. describe('when updating func param', () => {
  108. it('should update param value and update text representation', () => {
  109. const func = gfunc.createFuncInstance('summarize', {
  110. withDefaultParams: true,
  111. });
  112. func.updateParam('1h', 0);
  113. expect(func.params[0]).toBe('1h');
  114. expect(func.text).toBe('summarize(1h, sum, false)');
  115. });
  116. it('should parse numbers as float', () => {
  117. const func = gfunc.createFuncInstance('scale');
  118. func.updateParam('0.001', 0);
  119. expect(func.params[0]).toBe('0.001');
  120. });
  121. });
  122. describe('when updating func param with optional second parameter', () => {
  123. it('should update value and text', () => {
  124. const func = gfunc.createFuncInstance('aliasByNode');
  125. func.updateParam('1', 0);
  126. expect(func.params[0]).toBe('1');
  127. });
  128. it('should slit text and put value in second param', () => {
  129. const func = gfunc.createFuncInstance('aliasByNode');
  130. func.updateParam('4,-5', 0);
  131. expect(func.params[0]).toBe('4');
  132. expect(func.params[1]).toBe('-5');
  133. expect(func.text).toBe('aliasByNode(4, -5)');
  134. });
  135. it('should remove second param when empty string is set', () => {
  136. const func = gfunc.createFuncInstance('aliasByNode');
  137. func.updateParam('4,-5', 0);
  138. func.updateParam('', 1);
  139. expect(func.params[0]).toBe('4');
  140. expect(func.params[1]).toBe(undefined);
  141. expect(func.text).toBe('aliasByNode(4)');
  142. });
  143. });