gfunc.jest.ts 4.3 KB

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