PushAccumulator.js 1.8 KB

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