gfunc.test.ts 5.9 KB

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