gfunc.jest.ts 4.2 KB

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