LastAccumulator.js 2.1 KB

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