FirstAccumulator.js 2.2 KB

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