PushAccumulator.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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": function testStuff(){
  26. var accumulator = createAccumulator();
  27. assert.deepEqual(accumulator.getValue(), []);
  28. },
  29. "should evaluate one document and return an array of 1": function testStuff(){
  30. var accumulator = createAccumulator();
  31. accumulator.evaluate({b:1});
  32. assert.deepEqual(accumulator.getValue(), [1]);
  33. },
  34. "should evaluate two documents and return an array of 2": function testStuff(){
  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 one document with null and one document with a value and return an array of 1": function testStuff(){
  41. var accumulator = createAccumulator();
  42. accumulator.evaluate({b:1});
  43. accumulator.evaluate({b:null});
  44. assert.deepEqual(accumulator.getValue(), [1]);
  45. }
  46. }
  47. }
  48. };
  49. if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);