AvgAccumulator.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. "use strict";
  2. var assert = require("assert"),
  3. AvgAccumulator = require("../../../../lib/pipeline/accumulators/AvgAccumulator");
  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.AvgAccumulator = {
  7. ".constructor()": {
  8. "should not throw Error when constructing without args": function() {
  9. new AvgAccumulator();
  10. },
  11. },
  12. "#process()": {
  13. "should allow numbers": function() {
  14. assert.doesNotThrow(function() {
  15. var acc = AvgAccumulator.create();
  16. acc.process(1);
  17. });
  18. },
  19. "should ignore non-numbers": function() {
  20. assert.doesNotThrow(function() {
  21. var acc = AvgAccumulator.create();
  22. acc.process(true);
  23. acc.process("Foo");
  24. acc.process(new Date());
  25. acc.process({});
  26. acc.process([]);
  27. });
  28. },
  29. "router": {
  30. "should handle result from one shard": function testOneShard() {
  31. var acc = AvgAccumulator.create();
  32. acc.process({subTotal:3.0, count:2}, true);
  33. assert.deepEqual(acc.getValue(), 3.0 / 2);
  34. },
  35. "should handle result from two shards": function testTwoShards() {
  36. var acc = AvgAccumulator.create();
  37. acc.process({subTotal:6.0, count:1}, true);
  38. acc.process({subTotal:5.0, count:2}, true);
  39. assert.deepEqual(acc.getValue(), 11.0 / 3);
  40. },
  41. },
  42. },
  43. ".create()": {
  44. "should create an instance": function() {
  45. assert(AvgAccumulator.create() instanceof AvgAccumulator);
  46. },
  47. },
  48. "#getValue()": {
  49. "should return 0 if no inputs evaluated": function testNoDocsEvaluated() {
  50. var acc = AvgAccumulator.create();
  51. assert.equal(acc.getValue(), 0);
  52. },
  53. "should return one int": function testOneInt() {
  54. var acc = AvgAccumulator.create();
  55. acc.process(3);
  56. assert.equal(acc.getValue(), 3);
  57. },
  58. "should return one long": function testOneLong() {
  59. var acc = AvgAccumulator.create();
  60. acc.process(-4e24);
  61. assert.equal(acc.getValue(), -4e24);
  62. },
  63. "should return one double": function testOneDouble() {
  64. var acc = AvgAccumulator.create();
  65. acc.process(22.6);
  66. assert.equal(acc.getValue(), 22.6);
  67. },
  68. "should return avg for two ints": function testIntInt() {
  69. var acc = AvgAccumulator.create();
  70. acc.process(10);
  71. acc.process(11);
  72. assert.equal(acc.getValue(), 10.5);
  73. },
  74. "should return avg for int and double": function testIntDouble() {
  75. var acc = AvgAccumulator.create();
  76. acc.process(10);
  77. acc.process(11.0);
  78. assert.equal(acc.getValue(), 10.5);
  79. },
  80. "should return avg for two ints w/o overflow": function testIntIntNoOverflow() {
  81. var acc = AvgAccumulator.create();
  82. acc.process(32767);
  83. acc.process(32767);
  84. assert.equal(acc.getValue(), 32767);
  85. },
  86. "should return avg for two longs w/o overflow": function testLongLongOverflow() {
  87. var acc = AvgAccumulator.create();
  88. acc.process(2147483647);
  89. acc.process(2147483647);
  90. assert.equal(acc.getValue(), (2147483647 + 2147483647) / 2);
  91. },
  92. "shard": {
  93. "should return avg info for int": function testShardInt() {
  94. var acc = AvgAccumulator.create();
  95. acc.process(3);
  96. assert.deepEqual(acc.getValue(true), {subTotal:3.0, count:1});
  97. },
  98. "should return avg info for long": function testShardLong() {
  99. var acc = AvgAccumulator.create();
  100. acc.process(5);
  101. assert.deepEqual(acc.getValue(true), {subTotal:5.0, count:1});
  102. },
  103. "should return avg info for double": function testShardDouble() {
  104. var acc = AvgAccumulator.create();
  105. acc.process(116.0);
  106. assert.deepEqual(acc.getValue(true), {subTotal:116.0, count:1});
  107. },
  108. beforeEach: function() { // used in the tests below
  109. this.getAvgValueFor = function(a, b) { // kind of like TwoOperandBase
  110. var acc = AvgAccumulator.create();
  111. for (var i = 0, l = arguments.length; i < l; i++) {
  112. acc.process(arguments[i]);
  113. }
  114. return acc.getValue(true);
  115. };
  116. },
  117. "should return avg info for two ints w/ overflow": function testShardIntIntOverflow() {
  118. var operand1 = 32767,
  119. operand2 = 3,
  120. expected = {subTotal: 32767 + 3.0, count: 2};
  121. assert.deepEqual(this.getAvgValueFor(operand1, operand2), expected);
  122. assert.deepEqual(this.getAvgValueFor(operand2, operand1), expected);
  123. },
  124. "should return avg info for int and long": function testShardIntLong() {
  125. var operand1 = 5,
  126. operand2 = 3e24,
  127. expected = {subTotal: 5 + 3e24, count: 2};
  128. assert.deepEqual(this.getAvgValueFor(operand1, operand2), expected);
  129. assert.deepEqual(this.getAvgValueFor(operand2, operand1), expected);
  130. },
  131. "should return avg info for int and double": function testShardIntDouble() {
  132. var operand1 = 5,
  133. operand2 = 6.2,
  134. expected = {subTotal: 5 + 6.2, count: 2};
  135. assert.deepEqual(this.getAvgValueFor(operand1, operand2), expected);
  136. assert.deepEqual(this.getAvgValueFor(operand2, operand1), expected);
  137. },
  138. "should return avg info for long and double": function testShardLongDouble() {
  139. var operand1 = 5e24,
  140. operand2 = 1.0,
  141. expected = {subTotal: 5e24 + 1.0, count: 2};
  142. assert.deepEqual(this.getAvgValueFor(operand1, operand2), expected);
  143. assert.deepEqual(this.getAvgValueFor(operand2, operand1), expected);
  144. },
  145. "should return avg info for int and long and double": function testShardIntLongDouble() {
  146. var operand1 = 1,
  147. operand2 = 2e24,
  148. operand3 = 4.0,
  149. expected = {subTotal: 1 + 2e24 + 4.0, count: 3};
  150. assert.deepEqual(this.getAvgValueFor(operand1, operand2, operand3), expected);
  151. },
  152. },
  153. "should handle NaN": function() {
  154. var acc = AvgAccumulator.create();
  155. acc.process(NaN);
  156. acc.process(1);
  157. assert(isNaN(acc.getValue()));
  158. acc = AvgAccumulator.create();
  159. acc.process(1);
  160. acc.process(NaN);
  161. assert(isNaN(acc.getValue()));
  162. },
  163. "should handle null as 0": function() {
  164. var acc = AvgAccumulator.create();
  165. acc.process(null);
  166. assert.equal(acc.getValue(), 0);
  167. }
  168. },
  169. "#reset()": {
  170. "should reset to zero": function() {
  171. var acc = AvgAccumulator.create();
  172. assert.equal(acc.getValue(), 0);
  173. acc.process(123);
  174. assert.notEqual(acc.getValue(), 0);
  175. acc.reset();
  176. assert.equal(acc.getValue(), 0);
  177. assert.deepEqual(acc.getValue(true), {subTotal:0, count:0});
  178. }
  179. },
  180. "#getOpName()": {
  181. "should return the correct op name; $avg": function() {
  182. assert.equal(new AvgAccumulator().getOpName(), "$avg");
  183. }
  184. },
  185. };