AddToSetAccumulator.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. "use strict";
  2. var assert = require("assert"),
  3. AddToSetAccumulator = require("../../../../lib/pipeline/accumulators/AddToSetAccumulator"),
  4. ConstantExpression = require("../../../../lib/pipeline/expressions/ConstantExpression"),
  5. FieldPathExpression = require("../../../../lib/pipeline/expressions/FieldPathExpression");
  6. var createAccumulator = function createAccumulator() {
  7. var myAccumulator = new AddToSetAccumulator();
  8. myAccumulator.addOperand(new FieldPathExpression("b"));
  9. return myAccumulator;
  10. };
  11. //TODO: refactor these test cases using Expression.parseOperand() or something because these could be a whole lot cleaner...
  12. module.exports = {
  13. "AddToSetAccumulator": {
  14. "constructor()": {
  15. "should error if called with args": function testArgsGivenToCtor() {
  16. assert.throws(function() {
  17. new AddToSetAccumulator('arg');
  18. });
  19. },
  20. "should construct object with set property": function testCtorAssignsSet() {
  21. var acc = new AddToSetAccumulator();
  22. assert.notEqual(acc.set, null);
  23. assert.notEqual(acc.set, undefined);
  24. }
  25. },
  26. "#evaluate()" : {
  27. "should error if evaluate is called with no args": function testNoArgs() {
  28. assert.throws(function() {
  29. var acc = new createAccumulator();
  30. acc.evaluate();
  31. });
  32. },
  33. "should error if evaluate is called with more than one arg": function testTooManyArgs() {
  34. assert.throws(function() {
  35. var acc = new createAccumulator();
  36. acc.evaluate({}, {});
  37. });
  38. }
  39. },
  40. "#getValue()": {
  41. "should return empty array": function testEmptySet() {
  42. var acc = new createAccumulator();
  43. var value = acc.getValue();
  44. assert.equal((value instanceof Array), true);
  45. assert.equal(value.length, 0);
  46. },
  47. "should return array with one element that equals 5": function test5InSet() {
  48. var acc = createAccumulator();
  49. acc.evaluate({b:5});
  50. acc.evaluate({b:5});
  51. var value = acc.getValue();
  52. assert.deepEqual(JSON.stringify(value), JSON.stringify([5]));
  53. },
  54. "should produce value that is an array of multiple elements": function testMultipleItems() {
  55. var acc = createAccumulator();
  56. acc.evaluate({b:5});
  57. acc.evaluate({b:{key: "value"}});
  58. var value = acc.getValue();
  59. assert.deepEqual(JSON.stringify(value), JSON.stringify([5, {key: "value"}]));
  60. },
  61. "should return array with one element that is an object containing a key/value pair": function testKeyValue() {
  62. var acc = createAccumulator();
  63. acc.evaluate({b:{key: "value"}});
  64. var value = acc.getValue();
  65. assert.deepEqual(JSON.stringify(value), JSON.stringify([{key: "value"}]));
  66. },
  67. "should ignore undefined values": function testKeyValue() {
  68. var acc = createAccumulator();
  69. acc.evaluate({b:{key: "value"}});
  70. acc.evaluate({a:5});
  71. var value = acc.getValue();
  72. assert.deepEqual(JSON.stringify(value), JSON.stringify([{key: "value"}]));
  73. },
  74. "should coalesce different instances of equivalent objects": function testGetValue_() {
  75. var acc = createAccumulator();
  76. acc.evaluate({b:{key: "value"}});
  77. acc.evaluate({b:{key: "value"}});
  78. var value = acc.getValue();
  79. assert.deepEqual(JSON.stringify(value), JSON.stringify([{key: "value"}]));
  80. }
  81. }
  82. }
  83. };
  84. if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);