PushAccumulator.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. var assert = require("assert"),
  2. PushAccumulator = require("../../../../lib/pipeline/accumulators/PushAccumulator"),
  3. FieldPathExpression = require("../../../../lib/pipeline/expressions/FieldPathExpression");
  4. function createAccumulator(){
  5. var accumulator = new PushAccumulator();
  6. accumulator.addOperand(new FieldPathExpression("b") );
  7. return accumulator;
  8. }
  9. module.exports = {
  10. "PushAccumulator": {
  11. "constructor()": {
  12. "should not throw Error when constructing without args": function testConstructor(){
  13. assert.doesNotThrow(function(){
  14. new PushAccumulator();
  15. });
  16. }
  17. },
  18. "#getOpName()": {
  19. "should return the correct op name; $push": function testOpName(){
  20. assert.strictEqual(new PushAccumulator().getOpName(), "$push");
  21. }
  22. },
  23. "#evaluate()": {
  24. "should evaluate no documents": function testStuff(){
  25. var accumulator = createAccumulator();
  26. accumulator.evaluate();
  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);