MaxAccumulator.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. var assert = require("assert"),
  2. MaxAccumulator = require("../../../../lib/pipeline/accumulators/MinMaxAccumulator"),
  3. FieldPathExpression = require("../../../../lib/pipeline/expressions/FieldPathExpression");
  4. function createAccumulator(){
  5. var maxAccumulator = MaxAccumulator.createMax();
  6. maxAccumulator.addOperand(new FieldPathExpression("a") );
  7. return maxAccumulator;
  8. }
  9. module.exports = {
  10. "MaxAccumulator": {
  11. "constructor()": {
  12. "should not throw Error when constructing without args using createMax": function testConstructor(){
  13. assert.doesNotThrow(function(){
  14. MaxAccumulator.createMax();
  15. });
  16. },
  17. "should throw Error when constructing without args using default constructor": function testConstructor(){
  18. assert.throws(function(){
  19. new MaxAccumulator();
  20. });
  21. }
  22. },
  23. "#getOpName()": {
  24. "should return the correct op name; $max": function testOpName(){
  25. var acc = createAccumulator();
  26. assert.equal(acc.getOpName(), "$max");
  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 maximum": function two() {
  46. var acc = createAccumulator();
  47. acc.evaluate({a:5});
  48. acc.evaluate({a:7});
  49. assert.strictEqual(acc.getValue(), 7);
  50. },
  51. "The accumulator evaluates two documents and retains the defined value in the first": function lastMissing() {
  52. var acc = createAccumulator();
  53. acc.evaluate({a:7});
  54. acc.evaluate({});
  55. assert.strictEqual(acc.getValue(), 7);
  56. }
  57. }
  58. }
  59. };
  60. if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);