FirstAccumulator.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. acc.evaluate({a:7});
  48. assert.strictEqual(acc.getValue(), 5);
  49. },
  50. "The accumulator evaluates two documents and retains the undefined value in the first": function firstMissing() {
  51. var acc = createAccumulator();
  52. acc.evaluate({});
  53. acc.evaluate({a:7});
  54. assert.strictEqual(acc.getValue(), undefined);
  55. }
  56. }
  57. }
  58. };
  59. if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);