AvgAccumulator_test.js 6.2 KB

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