MinMaxAccumulator.js 5.0 KB

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