SumAccumulator.js 3.5 KB

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