SumAccumulator.js 3.3 KB

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