LastAccumulator.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. lastAccumulator.evaluate();
  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);