LastAccumulator.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. var assert = require("assert"),
  2. LastAccumulator = require("../../../../lib/pipeline/accumulators/LastAccumulator"),
  3. FieldPathExpression = require("../../../../lib/pipeline/expressions/FieldPathExpression");
  4. function createAccumulator(){
  5. var lastAccumulator = new LastAccumulator();
  6. lastAccumulator.addOperand(new FieldPathExpression("b") );
  7. return lastAccumulator;
  8. }
  9. module.exports = {
  10. "LastAccumulator": {
  11. "constructor()": {
  12. "should not throw Error when constructing without args": function testConstructor(){
  13. assert.doesNotThrow(function(){
  14. new LastAccumulator();
  15. });
  16. }
  17. },
  18. "#getOpName()": {
  19. "should return the correct op name; $last": function testOpName(){
  20. assert.strictEqual(new LastAccumulator().getOpName(), "$last");
  21. }
  22. },
  23. "#evaluate()": {
  24. "should evaluate no documents": function testStuff(){
  25. var lastAccumulator = createAccumulator();
  26. assert.strictEqual(lastAccumulator.getValue(), undefined);
  27. },
  28. "should evaluate one document and retains its value": function testStuff(){
  29. var lastAccumulator = createAccumulator();
  30. lastAccumulator.evaluate({b:5});
  31. assert.strictEqual(lastAccumulator.getValue(), 5);
  32. },
  33. "should evaluate one document with the field missing retains undefined": function testStuff(){
  34. var lastAccumulator = createAccumulator();
  35. lastAccumulator.evaluate({});
  36. assert.strictEqual(lastAccumulator.getValue(), undefined);
  37. },
  38. "should evaluate two documents and retains the value in the last": function testStuff(){
  39. var lastAccumulator = createAccumulator();
  40. lastAccumulator.evaluate({b:5});
  41. lastAccumulator.evaluate({b:7});
  42. assert.strictEqual(lastAccumulator.getValue(), 7);
  43. },
  44. "should evaluate two documents and retains the undefined value in the last": function testStuff(){
  45. var lastAccumulator = createAccumulator();
  46. lastAccumulator.evaluate({b:5});
  47. lastAccumulator.evaluate({});
  48. assert.strictEqual(lastAccumulator.getValue(), undefined);
  49. }
  50. }
  51. }
  52. };
  53. if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);