AvgAccumulator.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. var assert = require("assert"),
  2. AvgAccumulator = require("../../../../lib/pipeline/accumulators/AvgAccumulator"),
  3. FieldPathExpression = require("../../../../lib/pipeline/expressions/FieldPathExpression");
  4. function createAccumulator(){
  5. var avgAccumulator = new AvgAccumulator();
  6. avgAccumulator.addOperand(new FieldPathExpression("b") );
  7. return avgAccumulator;
  8. }
  9. module.exports = {
  10. "AvgAccumulator": {
  11. "constructor()": {
  12. "should not throw Error when constructing without args": function testConstructor(){
  13. assert.doesNotThrow(function(){
  14. new AvgAccumulator();
  15. });
  16. }
  17. },
  18. "#getOpName()": {
  19. "should return the correct op name; $avg": function testOpName(){
  20. assert.strictEqual(new AvgAccumulator().getOpName(), "$avg");
  21. }
  22. },
  23. "#evaluate()": {
  24. "should evaluate no documents": function testStuff(){
  25. var avgAccumulator = createAccumulator();
  26. avgAccumulator.evaluate();
  27. assert.strictEqual(avgAccumulator.getValue(), 0);
  28. },
  29. "should evaluate one document with a field that is NaN": function testStuff(){
  30. var avgAccumulator = createAccumulator();
  31. avgAccumulator.evaluate({b:Number("foo")});
  32. // NaN is unequal to itself
  33. assert.notStrictEqual(avgAccumulator.getValue(), avgAccumulator.getValue());
  34. },
  35. "should evaluate one document and avg it's value": function testStuff(){
  36. var avgAccumulator = createAccumulator();
  37. avgAccumulator.evaluate({b:5});
  38. assert.strictEqual(avgAccumulator.getValue(), 5);
  39. },
  40. "should evaluate and avg two ints": function testStuff(){
  41. var avgAccumulator = createAccumulator();
  42. avgAccumulator.evaluate({b:5});
  43. avgAccumulator.evaluate({b:7});
  44. assert.strictEqual(avgAccumulator.getValue(), 6);
  45. },
  46. "should evaluate and avg two ints overflow": function testStuff(){
  47. var avgAccumulator = createAccumulator();
  48. avgAccumulator.evaluate({b:Number.MAX_VALUE});
  49. avgAccumulator.evaluate({b:Number.MAX_VALUE});
  50. assert.strictEqual(Number.isFinite(avgAccumulator.getValue()), false);
  51. },
  52. "should evaluate and avg two negative ints": function testStuff(){
  53. var avgAccumulator = createAccumulator();
  54. avgAccumulator.evaluate({b:-5});
  55. avgAccumulator.evaluate({b:-7});
  56. assert.strictEqual(avgAccumulator.getValue(), -6);
  57. },
  58. //TODO Not sure how to do this in Javascript
  59. // "should evaluate and avg two negative ints overflow": function testStuff(){
  60. // var avgAccumulator = createAccumulator();
  61. // avgAccumulator.evaluate({b:Number.MIN_VALUE});
  62. // avgAccumulator.evaluate({b:7});
  63. // assert.strictEqual(avgAccumulator.getValue(), Number.MAX_VALUE);
  64. // },
  65. //
  66. "should evaluate and avg int and float": function testStuff(){
  67. var avgAccumulator = createAccumulator();
  68. avgAccumulator.evaluate({b:8.5});
  69. avgAccumulator.evaluate({b:7});
  70. assert.strictEqual(avgAccumulator.getValue(), 7.75);
  71. },
  72. "should evaluate and avg one Number and a NaN sum to NaN": function testStuff(){
  73. var avgAccumulator = createAccumulator();
  74. avgAccumulator.evaluate({b:8});
  75. avgAccumulator.evaluate({b:Number("bar")});
  76. // NaN is unequal to itself
  77. assert.notStrictEqual(avgAccumulator.getValue(), avgAccumulator.getValue());
  78. },
  79. "should evaluate and avg a null value to 0": function testStuff(){
  80. var avgAccumulator = createAccumulator();
  81. avgAccumulator.evaluate({b:null});
  82. assert.strictEqual(avgAccumulator.getValue(), 0);
  83. }
  84. }
  85. }
  86. };
  87. if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);