gfunc.jest.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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("groupByNode(hello.metric, 5, 'avg')");
  53. });
  54. it('should handle function with no metric param', function() {
  55. var func = gfunc.createFuncInstance('randomWalk');
  56. func.params[0] = 'test';
  57. expect(func.render(undefined)).toEqual("randomWalk('test')");
  58. });
  59. it('should handle function multiple series params', function() {
  60. var func = gfunc.createFuncInstance('asPercent');
  61. func.params[0] = '#B';
  62. expect(func.render('#A')).toEqual('asPercent(#A, #B)');
  63. });
  64. });
  65. describe('when requesting function categories', function() {
  66. it('should return function categories', function() {
  67. var catIndex = gfunc.getCategories('1.0');
  68. expect(catIndex.Special.length).toBeGreaterThan(8);
  69. });
  70. });
  71. describe('when updating func param', function() {
  72. it('should update param value and update text representation', function() {
  73. var func = gfunc.createFuncInstance('summarize', {
  74. withDefaultParams: true,
  75. });
  76. func.updateParam('1h', 0);
  77. expect(func.params[0]).toBe('1h');
  78. expect(func.text).toBe('summarize(1h, sum, false)');
  79. });
  80. it('should parse numbers as float', function() {
  81. var func = gfunc.createFuncInstance('scale');
  82. func.updateParam('0.001', 0);
  83. expect(func.params[0]).toBe('0.001');
  84. });
  85. });
  86. describe('when updating func param with optional second parameter', function() {
  87. it('should update value and text', function() {
  88. var func = gfunc.createFuncInstance('aliasByNode');
  89. func.updateParam('1', 0);
  90. expect(func.params[0]).toBe('1');
  91. });
  92. it('should slit text and put value in second param', function() {
  93. var func = gfunc.createFuncInstance('aliasByNode');
  94. func.updateParam('4,-5', 0);
  95. expect(func.params[0]).toBe('4');
  96. expect(func.params[1]).toBe('-5');
  97. expect(func.text).toBe('aliasByNode(4, -5)');
  98. });
  99. it('should remove second param when empty string is set', function() {
  100. var func = gfunc.createFuncInstance('aliasByNode');
  101. func.updateParam('4,-5', 0);
  102. func.updateParam('', 1);
  103. expect(func.params[0]).toBe('4');
  104. expect(func.params[1]).toBe(undefined);
  105. expect(func.text).toBe('aliasByNode(4)');
  106. });
  107. });