MinAccumulator.js 2.2 KB

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