gfunc_specs.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. ///<amd-dependency path="app/plugins/datasource/graphite/gfunc" name="gfunc" />
  2. import {describe, beforeEach, it, sinon, expect} from 'test/lib/common';
  3. declare var gfunc: any;
  4. describe('when creating func instance from func names', function() {
  5. it('should return func instance', function() {
  6. var func = gfunc.createFuncInstance('sumSeries');
  7. expect(func).to.be.ok();
  8. expect(func.def.name).to.equal('sumSeries');
  9. expect(func.def.params.length).to.equal(5);
  10. expect(func.def.defaultParams.length).to.equal(1);
  11. });
  12. it('should return func instance with shortName', function() {
  13. var func = gfunc.createFuncInstance('sum');
  14. expect(func).to.be.ok();
  15. });
  16. it('should return func instance from funcDef', function() {
  17. var func = gfunc.createFuncInstance('sum');
  18. var func2 = gfunc.createFuncInstance(func.def);
  19. expect(func2).to.be.ok();
  20. });
  21. it('func instance should have text representation', function() {
  22. var func = gfunc.createFuncInstance('groupByNode');
  23. func.params[0] = 5;
  24. func.params[1] = 'avg';
  25. func.updateText();
  26. expect(func.text).to.equal("groupByNode(5, avg)");
  27. });
  28. });
  29. describe('when rendering func instance', function() {
  30. it('should handle single metric param', function() {
  31. var func = gfunc.createFuncInstance('sumSeries');
  32. expect(func.render('hello.metric')).to.equal("sumSeries(hello.metric)");
  33. });
  34. it('should include default params if options enable it', function() {
  35. var func = gfunc.createFuncInstance('scaleToSeconds', { withDefaultParams: true });
  36. expect(func.render('hello')).to.equal("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')).to.equal("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')).to.equal("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')).to.equal("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)).to.equal("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')).to.equal("asPercent(#A, #B)");
  63. });
  64. });
  65. describe('when requesting function categories', function() {
  66. it('should return function categories', function() {
  67. var catIndex = gfunc.getCategories();
  68. expect(catIndex.Special.length).to.be.greaterThan(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', { withDefaultParams: true });
  74. func.updateParam('1h', 0);
  75. expect(func.params[0]).to.be('1h');
  76. expect(func.text).to.be('summarize(1h, sum, false)');
  77. });
  78. it('should parse numbers as float', function() {
  79. var func = gfunc.createFuncInstance('scale');
  80. func.updateParam('0.001', 0);
  81. expect(func.params[0]).to.be('0.001');
  82. });
  83. });
  84. describe('when updating func param with optional second parameter', function() {
  85. it('should update value and text', function() {
  86. var func = gfunc.createFuncInstance('aliasByNode');
  87. func.updateParam('1', 0);
  88. expect(func.params[0]).to.be('1');
  89. });
  90. it('should slit text and put value in second param', function() {
  91. var func = gfunc.createFuncInstance('aliasByNode');
  92. func.updateParam('4,-5', 0);
  93. expect(func.params[0]).to.be('4');
  94. expect(func.params[1]).to.be('-5');
  95. expect(func.text).to.be('aliasByNode(4, -5)');
  96. });
  97. it('should remove second param when empty string is set', function() {
  98. var func = gfunc.createFuncInstance('aliasByNode');
  99. func.updateParam('4,-5', 0);
  100. func.updateParam('', 1);
  101. expect(func.params[0]).to.be('4');
  102. expect(func.params[1]).to.be(undefined);
  103. expect(func.text).to.be('aliasByNode(4)');
  104. });
  105. });