PushAccumulator.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. "use strict";
  2. var assert = require("assert"),
  3. PushAccumulator = require("../../../../lib/pipeline/accumulators/PushAccumulator");
  4. // Mocha one-liner to make these tests self-hosted
  5. if(!module.parent)return(require.cache[__filename]=null,(new(require("mocha"))({ui:"exports",reporter:"spec",grep:process.env.TEST_GREP})).addFile(__filename).run(process.exit));
  6. exports.PushAccumulator = {
  7. ".constructor()": {
  8. "should create instance of accumulator": function() {
  9. assert(new PushAccumulator() instanceof PushAccumulator);
  10. },
  11. "should throw error if called with args": function() {
  12. assert.throws(function() {
  13. new PushAccumulator(123);
  14. });
  15. },
  16. },
  17. ".create()": {
  18. "should return an instance of the accumulator": function() {
  19. assert(PushAccumulator.create() instanceof PushAccumulator);
  20. },
  21. },
  22. "#process()": {
  23. "should return empty array if no inputs evaluated": function() {
  24. var acc = PushAccumulator.create();
  25. assert.deepEqual(acc.getValue(), []);
  26. },
  27. "should return array of one value for one input": function() {
  28. var acc = PushAccumulator.create();
  29. acc.process(1);
  30. assert.deepEqual(acc.getValue(), [1]);
  31. },
  32. "should return array of two values for two inputs": function() {
  33. var acc = PushAccumulator.create();
  34. acc.process(1);
  35. acc.process(2);
  36. assert.deepEqual(acc.getValue(), [1,2]);
  37. },
  38. "should return array of two values for two inputs (including null)": function() {
  39. var acc = PushAccumulator.create();
  40. acc.process(1);
  41. acc.process(null);
  42. assert.deepEqual(acc.getValue(), [1, null]);
  43. },
  44. "should return array of one value for two inputs if one is undefined": function() {
  45. var acc = PushAccumulator.create();
  46. acc.process(1);
  47. acc.process(undefined);
  48. assert.deepEqual(acc.getValue(), [1]);
  49. },
  50. "should return array of two values from two separate mergeable inputs": function() {
  51. var acc = PushAccumulator.create();
  52. acc.process([1], true);
  53. acc.process([0], true);
  54. assert.deepEqual(acc.getValue(), [1, 0]);
  55. },
  56. "should throw error if merging non-array": function() {
  57. var acc = PushAccumulator.create();
  58. assert.throws(function() {
  59. acc.process(0, true);
  60. });
  61. assert.throws(function() {
  62. acc.process("foo", true);
  63. });
  64. },
  65. },
  66. "#getValue()": {
  67. "should get value the same for shard and router": function() {
  68. var acc = PushAccumulator.create();
  69. assert.strictEqual(acc.getValue(false), acc.getValue(true));
  70. acc.process(123);
  71. assert.strictEqual(acc.getValue(false), acc.getValue(true));
  72. },
  73. },
  74. "#reset()": {
  75. "should reset to empty array": function() {
  76. var acc = PushAccumulator.create();
  77. assert.deepEqual(acc.getValue(), []);
  78. acc.process(123);
  79. assert.notDeepEqual(acc.getValue(), []);
  80. acc.reset();
  81. assert.deepEqual(acc.getValue(), []);
  82. assert.deepEqual(acc.getValue(true), []);
  83. },
  84. },
  85. "#getOpName()": {
  86. "should return the correct op name; $push": function(){
  87. assert.strictEqual(new PushAccumulator().getOpName(), "$push");
  88. },
  89. },
  90. };
  91. if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);