MinMaxAccumulator_test.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. MinMaxAccumulator = require("../../../../lib/pipeline/accumulators/MinMaxAccumulator");
  5. exports.MinMaxAccumulator = {
  6. ".constructor()": {
  7. "should create instance of Accumulator": function() {
  8. assert(MinMaxAccumulator.createMax() instanceof MinMaxAccumulator);
  9. },
  10. "should throw error if called without args": function() {
  11. assert.throws(function() {
  12. new MinMaxAccumulator();
  13. });
  14. },
  15. "should create instance of Accumulator if called with valid sense": function() {
  16. new MinMaxAccumulator(-1);
  17. new MinMaxAccumulator(1);
  18. },
  19. "should throw error if called with invalid sense": function() {
  20. assert.throws(function() {
  21. new MinMaxAccumulator(0);
  22. });
  23. },
  24. },
  25. ".createMin()": {
  26. "should return an instance of the accumulator": function() {
  27. var acc = MinMaxAccumulator.createMin();
  28. assert(acc instanceof MinMaxAccumulator);
  29. assert.strictEqual(acc._sense, 1);
  30. },
  31. },
  32. ".createMax()": {
  33. "should return an instance of the accumulator": function() {
  34. var acc = MinMaxAccumulator.createMax();
  35. assert(acc instanceof MinMaxAccumulator);
  36. assert.strictEqual(acc._sense, -1);
  37. },
  38. },
  39. "#process()": {
  40. "Min": {
  41. "should return undefined if no inputs evaluated": function testNone() {
  42. var acc = MinMaxAccumulator.createMin();
  43. assert.strictEqual(acc.getValue(), undefined);
  44. },
  45. "should return value for one input": function testOne() {
  46. var acc = MinMaxAccumulator.createMin();
  47. acc.process(5);
  48. assert.strictEqual(acc.getValue(), 5);
  49. },
  50. "should return missing for one missing input": function testMissing() {
  51. var acc = MinMaxAccumulator.createMin();
  52. acc.process();
  53. assert.strictEqual(acc.getValue(), undefined);
  54. },
  55. "should return minimum of two inputs": function testTwo() {
  56. var acc = MinMaxAccumulator.createMin();
  57. acc.process(5);
  58. acc.process(7);
  59. assert.strictEqual(acc.getValue(), 5);
  60. },
  61. "should return minimum of two inputs (ignoring undefined once found)": function testLastMissing() {
  62. var acc = MinMaxAccumulator.createMin();
  63. acc.process(7);
  64. acc.process(undefined);
  65. assert.strictEqual(acc.getValue(), 7);
  66. },
  67. },
  68. "Max": {
  69. "should return undefined if no inputs evaluated": function testNone() {
  70. var acc = MinMaxAccumulator.createMax();
  71. assert.strictEqual(acc.getValue(), undefined);
  72. },
  73. "should return value for one input": function testOne() {
  74. var acc = MinMaxAccumulator.createMax();
  75. acc.process(5);
  76. assert.strictEqual(acc.getValue(), 5);
  77. },
  78. "should return missing for one missing input": function testMissing() {
  79. var acc = MinMaxAccumulator.createMax();
  80. acc.process();
  81. assert.strictEqual(acc.getValue(), undefined);
  82. },
  83. "should return maximum of two inputs": function testTwo() {
  84. var acc = MinMaxAccumulator.createMax();
  85. acc.process(5);
  86. acc.process(7);
  87. assert.strictEqual(acc.getValue(), 7);
  88. },
  89. "should return maximum of two inputs (ignoring undefined once found)": function testLastMissing() {
  90. var acc = MinMaxAccumulator.createMax();
  91. acc.process(7);
  92. acc.process(undefined);
  93. assert.strictEqual(acc.getValue(), 7);
  94. },
  95. },
  96. },
  97. "#getValue()": {
  98. "Min": {
  99. "should get value the same for shard and router": function() {
  100. var acc = MinMaxAccumulator.createMin();
  101. assert.strictEqual(acc.getValue(false), acc.getValue(true));
  102. acc.process(123);
  103. assert.strictEqual(acc.getValue(false), acc.getValue(true));
  104. },
  105. },
  106. "Max": {
  107. "should get value the same for shard and router": function() {
  108. var acc = MinMaxAccumulator.createMax();
  109. assert.strictEqual(acc.getValue(false), acc.getValue(true));
  110. acc.process(123);
  111. assert.strictEqual(acc.getValue(false), acc.getValue(true));
  112. },
  113. },
  114. },
  115. "#reset()": {
  116. "Min": {
  117. "should reset to missing": function() {
  118. var acc = MinMaxAccumulator.createMin();
  119. assert.strictEqual(acc.getValue(), undefined);
  120. acc.process(123);
  121. assert.notEqual(acc.getValue(), undefined);
  122. acc.reset();
  123. assert.strictEqual(acc.getValue(), undefined);
  124. assert.strictEqual(acc.getValue(true), undefined);
  125. },
  126. },
  127. "Max": {
  128. "should reset to missing": function() {
  129. var acc = MinMaxAccumulator.createMax();
  130. assert.strictEqual(acc.getValue(), undefined);
  131. acc.process(123);
  132. assert.notEqual(acc.getValue(), undefined);
  133. acc.reset();
  134. assert.strictEqual(acc.getValue(), undefined);
  135. assert.strictEqual(acc.getValue(true), undefined);
  136. },
  137. },
  138. },
  139. "#getOpName()": {
  140. "Min": {
  141. "should return the correct op name; $min": function() {
  142. assert.equal(MinMaxAccumulator.createMin().getOpName(), "$min");
  143. },
  144. },
  145. "Max":{
  146. "should return the correct op name; $max": function() {
  147. assert.equal(MinMaxAccumulator.createMax().getOpName(), "$max");
  148. },
  149. },
  150. },
  151. };