PushAccumulator.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. "use strict";
  2. var assert = require("assert"),
  3. PushAccumulator = require("../../../../lib/pipeline/accumulators/PushAccumulator"),
  4. FieldPathExpression = require("../../../../lib/pipeline/expressions/FieldPathExpression");
  5. function createAccumulator(){
  6. var accumulator = new PushAccumulator();
  7. accumulator.addOperand(new FieldPathExpression("b") );
  8. return accumulator;
  9. }
  10. module.exports = {
  11. "PushAccumulator": {
  12. "constructor()": {
  13. "should not throw Error when constructing without args": function testConstructor(){
  14. assert.doesNotThrow(function(){
  15. new PushAccumulator();
  16. });
  17. }
  18. },
  19. "#getOpName()": {
  20. "should return the correct op name; $push": function testOpName(){
  21. assert.strictEqual(new PushAccumulator().getOpName(), "$push");
  22. }
  23. },
  24. "#evaluate()": {
  25. "should evaluate no documents and return []": function testEvaluate_None(){
  26. var accumulator = createAccumulator();
  27. assert.deepEqual(accumulator.getValue(), []);
  28. },
  29. "should evaluate a 1 and return [1]": function testEvaluate_One(){
  30. var accumulator = createAccumulator();
  31. accumulator.evaluate({b:1});
  32. assert.deepEqual(accumulator.getValue(), [1]);
  33. },
  34. "should evaluate a 1 and a 2 and return [1,2]": function testEvaluate_OneTwo(){
  35. var accumulator = createAccumulator();
  36. accumulator.evaluate({b:1});
  37. accumulator.evaluate({b:2});
  38. assert.deepEqual(accumulator.getValue(), [1,2]);
  39. },
  40. "should evaluate a 1 and a null and return [1,null]": function testEvaluate_OneNull(){
  41. var accumulator = createAccumulator();
  42. accumulator.evaluate({b:1});
  43. accumulator.evaluate({b:null});
  44. assert.deepEqual(accumulator.getValue(), [1, null]);
  45. },
  46. "should evaluate a 1 and an undefined and return [1]": function testEvaluate_OneUndefined(){
  47. var accumulator = createAccumulator();
  48. accumulator.evaluate({b:1});
  49. accumulator.evaluate({b:undefined});
  50. assert.deepEqual(accumulator.getValue(), [1]);
  51. },
  52. "should evaluate a 1 and a 0 and return [1,0]": function testEvaluate_OneZero(){
  53. var accumulator = createAccumulator();
  54. accumulator.evaluate({b:1});
  55. accumulator.evaluate({b:0});
  56. assert.deepEqual(accumulator.getValue(), [1, 0]);
  57. }
  58. }
  59. }
  60. };
  61. if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);