AddExpression_test.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. Expression = require("../../../../lib/pipeline/expressions/Expression"),
  5. AddExpression = require("../../../../lib/pipeline/expressions/AddExpression"),
  6. VariablesParseState = require("../../../../lib/pipeline/expressions/VariablesParseState"),
  7. VariablesIdGenerator = require("../../../../lib/pipeline/expressions/VariablesIdGenerator"),
  8. FieldPathExpression = require("../../../../lib/pipeline/expressions/FieldPathExpression"),
  9. ConstantExpression = require("../../../../lib/pipeline/expressions/ConstantExpression");
  10. var TestBase = function TestBase(overrides) {
  11. //NOTE: DEVIATION FROM MONGO: using this base class to make things easier to initialize
  12. for (var key in overrides) //jshint ignore:line
  13. this[key] = overrides[key];
  14. },
  15. ExpectedResultBase = (function() {
  16. var klass = function ExpectedResultBase() {
  17. base.apply(this, arguments);
  18. }, base = TestBase, proto = klass.prototype = Object.create(base.prototype);
  19. proto.run = function() {
  20. var expr = new AddExpression();
  21. this.populateOperands(expr);
  22. var expectedResult = this.expectedResult instanceof Function ? this.expectedResult() : this.expectedResult;
  23. if (expectedResult instanceof Date) //NOTE: DEVIATION FROM MONGO: special case for Date
  24. return assert.strictEqual(Date(expectedResult), Date(expr.evaluate({})));
  25. assert.strictEqual(expectedResult, expr.evaluate({}));
  26. };
  27. return klass;
  28. })(),
  29. SingleOperandBase = (function() {
  30. var klass = function SingleOperandBase() {
  31. base.apply(this, arguments);
  32. }, base = ExpectedResultBase, proto = klass.prototype = Object.create(base.prototype);
  33. proto.populateOperands = function(expr) {
  34. var operand = this.operand instanceof Function ? this.operand() : this.operand;
  35. expr.addOperand(ConstantExpression.create(operand));
  36. };
  37. proto.expectedResult = function() {
  38. var operand = this.operand instanceof Function ? this.operand() : this.operand;
  39. return operand;
  40. };
  41. return klass;
  42. })(),
  43. TwoOperandBase = (function() {
  44. var klass = function TwoOperandBase() {
  45. base.apply(this, arguments);
  46. }, base = ExpectedResultBase, proto = klass.prototype = Object.create(base.prototype);
  47. proto.run = function() {
  48. base.prototype.run.call(this);
  49. // Now add the operands in the reverse direction.
  50. this._reverse = true;
  51. base.prototype.run.call(this);
  52. };
  53. proto.populateOperands = function(expr) {
  54. var operand1 = this.operand1 instanceof Function ? this.operand1() : this.operand1,
  55. operand2 = this.operand1 instanceof Function ? this.operand2() : this.operand2;
  56. expr.addOperand(ConstantExpression.create(this._reverse ? operand2 : operand1));
  57. expr.addOperand(ConstantExpression.create(this._reverse ? operand1 : operand2));
  58. };
  59. proto._reverse = false;
  60. return klass;
  61. })();
  62. exports.AddExpression = {
  63. "constructor()": {
  64. "should construct instance": function() {
  65. assert(new AddExpression() instanceof AddExpression);
  66. assert(new AddExpression() instanceof Expression);
  67. },
  68. "should error if given args": function() {
  69. assert.throws(function() {
  70. new AddExpression("bad stuff");
  71. });
  72. },
  73. },
  74. "#getOpName()": {
  75. "should return the correct op name; $add": function() {
  76. assert.equal(new AddExpression().getOpName(), "$add");
  77. }
  78. },
  79. "#evaluate()": {
  80. "should return the operand if null document is given": function testNullDocument() {
  81. /** $add with a NULL Document pointer, as called by ExpressionNary::optimize(). */
  82. var expr = new AddExpression();
  83. expr.addOperand(ConstantExpression.create(2));
  84. assert.strictEqual(expr.evaluate({}), 2);
  85. },
  86. "should return 0 if no operands were given": function testNoOperands() {
  87. /** $add without operands. */
  88. var expr = new AddExpression();
  89. assert.strictEqual(expr.evaluate({}), 0);
  90. },
  91. "should throw Error if a String operand was given": function testString() {
  92. /** String type unsupported. */
  93. var expr = new AddExpression();
  94. expr.addOperand(ConstantExpression.create("a"));
  95. assert.throws(function () {
  96. expr.evaluate({});
  97. });
  98. },
  99. "should throw Error if a Boolean operand was given": function testBool() {
  100. var expr = new AddExpression();
  101. expr.addOperand(ConstantExpression.create(true));
  102. assert.throws(function () {
  103. expr.evaluate({});
  104. });
  105. },
  106. "w/ 1 operand": {
  107. "should pass through a single int": function testInt() {
  108. /** Single int argument. */
  109. new SingleOperandBase({
  110. operand: 1,
  111. }).run();
  112. },
  113. //SKIPPED: Long -- would be same as Int above
  114. "should pass through a single float": function testDouble() {
  115. /** Single double argument. */
  116. new SingleOperandBase({
  117. operand: 99.99,
  118. }).run();
  119. },
  120. "should pass through a single date": function testDate() {
  121. /** Single Date argument. */
  122. new SingleOperandBase({
  123. operand: new Date(12345),
  124. }).run();
  125. },
  126. "should pass through a single null": function testNull() {
  127. /** Single null argument. */
  128. new SingleOperandBase({
  129. operand: null,
  130. }).run();
  131. },
  132. "should pass through a single undefined": function testUndefined() {
  133. /** Single undefined argument. */
  134. new SingleOperandBase({
  135. operand: undefined,
  136. expectedResult: null,
  137. }).run();
  138. },
  139. },
  140. "w/ 2 operands": {
  141. "should add two ints": function testIntInt() {
  142. /** Add two ints. */
  143. new TwoOperandBase({
  144. operand1: 1,
  145. operand2: 5,
  146. expectedResult: 6,
  147. }).run();
  148. },
  149. //SKIPPED: IntIntNoOverflow
  150. //SKIPPED: IntLong
  151. //SKIPPED: IntLongOverflow
  152. "should add int and double": function testIntDouble() {
  153. /** Adding an int and a double produces a double. */
  154. new TwoOperandBase({
  155. operand1: 9,
  156. operand2: 1.1,
  157. expectedResult: 10.1,
  158. }).run();
  159. },
  160. "should add int and date": function testIntDate() {
  161. /** Adding an int and a Date produces a Date. */
  162. new TwoOperandBase({
  163. operand1: 6,
  164. operand2: new Date(123450),
  165. expectedResult: new Date(123456),
  166. }).run();
  167. },
  168. //SKIPPED: LongDouble
  169. //SKIPPED: LongDoubleNoOverflow
  170. "should add int and null": function testIntNull() {
  171. /** Adding an int and null. */
  172. new TwoOperandBase({
  173. operand1: 1,
  174. operand2: null,
  175. expectedResult: null,
  176. }).run();
  177. },
  178. "should add long and undefined": function testLongUndefined() {
  179. /** Adding a long and undefined. */
  180. new TwoOperandBase({
  181. operand1: 5e11,
  182. operand2: undefined,
  183. expectedResult: null,
  184. }).run();
  185. },
  186. }
  187. },
  188. "optimize": {
  189. "should understand a single number": function() {
  190. var vps = new VariablesParseState(new VariablesIdGenerator()),
  191. expr = Expression.parseOperand({$add:[123]}, vps).optimize();
  192. assert.strictEqual(expr.operands.length, 0, "should optimize operands away");
  193. assert(expr instanceof ConstantExpression);
  194. assert.strictEqual(expr.evaluate(), 123);
  195. },
  196. "should optimize strings of numbers without regard to their order": function() {
  197. var vps = new VariablesParseState(new VariablesIdGenerator()),
  198. expr = Expression.parseOperand({$add:[1,2,3,"$a",4,5,6]}, vps).optimize();
  199. assert.strictEqual(expr.operands.length, 2, "should optimize operands away");
  200. assert(expr.operands[0] instanceof FieldPathExpression);
  201. assert(expr.operands[1] instanceof ConstantExpression);
  202. assert.strictEqual(expr.operands[1].evaluate(), 1 + 2 + 3 + 4 + 5 + 6);
  203. },
  204. },
  205. };