FirstAccumulator.js 2.2 KB

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