PushAccumulator_test.js 2.9 KB

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