gfunc-specs.js 4.5 KB

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