gfunc_specs.ts 4.3 KB

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