SumAccumulator.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. assert.strictEqual(sumAccumulator.getValue(), 0);
  27. },
  28. "should evaluate one document with a field that is NaN": function testStuff(){
  29. var sumAccumulator = createAccumulator();
  30. sumAccumulator.evaluate({b:Number("foo")});
  31. // NaN is unequal to itself
  32. assert.notStrictEqual(sumAccumulator.getValue(), sumAccumulator.getValue());
  33. },
  34. "should evaluate one document and sum it's value": function testStuff(){
  35. var sumAccumulator = createAccumulator();
  36. sumAccumulator.evaluate({b:5});
  37. assert.strictEqual(sumAccumulator.getValue(), 5);
  38. },
  39. "should evaluate and sum two ints": function testStuff(){
  40. var sumAccumulator = createAccumulator();
  41. sumAccumulator.evaluate({b:5});
  42. sumAccumulator.evaluate({b:7});
  43. assert.strictEqual(sumAccumulator.getValue(), 12);
  44. },
  45. "should evaluate and sum two ints overflow": function testStuff(){
  46. var sumAccumulator = createAccumulator();
  47. sumAccumulator.evaluate({b:Number.MAX_VALUE});
  48. sumAccumulator.evaluate({b:Number.MAX_VALUE});
  49. assert.strictEqual(Number.isFinite(sumAccumulator.getValue()), false);
  50. },
  51. "should evaluate and sum two negative ints": function testStuff(){
  52. var sumAccumulator = createAccumulator();
  53. sumAccumulator.evaluate({b:-5});
  54. sumAccumulator.evaluate({b:-7});
  55. assert.strictEqual(sumAccumulator.getValue(), -12);
  56. },
  57. //TODO Not sure how to do this in Javascript
  58. // "should evaluate and sum two negative ints overflow": function testStuff(){
  59. // var sumAccumulator = createAccumulator();
  60. // sumAccumulator.evaluate({b:Number.MIN_VALUE});
  61. // sumAccumulator.evaluate({b:7});
  62. // assert.strictEqual(sumAccumulator.getValue(), Number.MAX_VALUE);
  63. // },
  64. //
  65. "should evaluate and sum int and float": function testStuff(){
  66. var sumAccumulator = createAccumulator();
  67. sumAccumulator.evaluate({b:8.5});
  68. sumAccumulator.evaluate({b:7});
  69. assert.strictEqual(sumAccumulator.getValue(), 15.5);
  70. },
  71. "should evaluate and sum one Number and a NaN sum to NaN": function testStuff(){
  72. var sumAccumulator = createAccumulator();
  73. sumAccumulator.evaluate({b:8});
  74. sumAccumulator.evaluate({b:Number("bar")});
  75. // NaN is unequal to itself
  76. assert.notStrictEqual(sumAccumulator.getValue(), sumAccumulator.getValue());
  77. },
  78. "should evaluate and sum a null value to 0": function testStuff(){
  79. var sumAccumulator = createAccumulator();
  80. sumAccumulator.evaluate({b:null});
  81. assert.strictEqual(sumAccumulator.getValue(), 0);
  82. }
  83. }
  84. }
  85. };
  86. if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);