AddExpression_test.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. "use strict";
  2. var assert = require("assert"),
  3. AddExpression = require("../../../../lib/pipeline/expressions/AddExpression"),
  4. Expression = require("../../../../lib/pipeline/expressions/Expression"),
  5. VariablesParseState = require("../../../../lib/pipeline/expressions/VariablesParseState"),
  6. VariablesIdGenerator = require("../../../../lib/pipeline/expressions/VariablesIdGenerator"),
  7. FieldPathExpression = require("../../../../lib/pipeline/expressions/FieldPathExpression"),
  8. ConstantExpression = require("../../../../lib/pipeline/expressions/ConstantExpression");
  9. //TODO: refactor these test cases using Expression.parseOperand() or something because these could be a whole lot cleaner...
  10. module.exports = {
  11. "AddExpression": {
  12. "constructor()": {
  13. "should not throw Error when constructing without args": function testConstructor() {
  14. assert.doesNotThrow(function () {
  15. new AddExpression();
  16. });
  17. },
  18. "should throw Error when constructing with args": function testConstructor() {
  19. assert.throws(function () {
  20. new AddExpression(1);
  21. });
  22. }
  23. },
  24. "#getOpName()": {
  25. "should return the correct op name; $add": function testOpName() {
  26. assert.equal(new AddExpression().getOpName(), "$add");
  27. }
  28. },
  29. "#evaluateInternal()": {
  30. "should return the operand if null document is given": function nullDocument() {
  31. var expr = new AddExpression();
  32. expr.addOperand(new ConstantExpression(2));
  33. assert.equal(expr.evaluateInternal(null), 2);
  34. },
  35. "should return 0 if no operands were given": function noOperands() {
  36. var expr = new AddExpression();
  37. assert.equal(expr.evaluateInternal({}), 0);
  38. },
  39. "should throw Error if a Date operand was given": function date() {
  40. var expr = new AddExpression();
  41. expr.addOperand(new ConstantExpression(new Date()));
  42. assert.throws(function () {
  43. expr.evaluateInternal({});
  44. });
  45. },
  46. "should throw Error if a String operand was given": function string() {
  47. var expr = new AddExpression();
  48. expr.addOperand(new ConstantExpression(""));
  49. assert.throws(function () {
  50. expr.evaluateInternal({});
  51. });
  52. },
  53. "should throw Error if a Boolean operand was given": function bool() {
  54. var expr = new AddExpression();
  55. expr.addOperand(new ConstantExpression(true));
  56. assert.throws(function () {
  57. expr.evaluateInternal({});
  58. });
  59. },
  60. "singleOperandBase": {
  61. beforeEach: function () {
  62. this.expectedResult = function (_input_, _expected_) {
  63. var expr = new AddExpression();
  64. expr.addOperand(new ConstantExpression(_input_));
  65. assert.equal(expr.evaluateInternal({}), _expected_);
  66. }
  67. },
  68. "should pass through a single number": function number() {
  69. this.expectedResult(123, 123);
  70. },
  71. "should pass through a single null": function nullSupport() {
  72. this.expectedResult(null, 0);
  73. },
  74. "should pass through a single undefined": function undefinedSupport() {
  75. this.expectedResult(undefined, 0);
  76. },
  77. "should pass through a single float": function () {
  78. var v = 123.234;
  79. this.expectedResult(v, v);
  80. },
  81. //NOTE: DEVIATION FROM MONGO: We don't support dates
  82. // "should pass through a single date": function () {
  83. // var v = new Date();
  84. // this.expectedResult(v, v);
  85. // }
  86. },
  87. "TwoOperand": {
  88. beforeEach: function () {
  89. this.reverse = function (array) {
  90. var reversed = [];
  91. array.forEach(function (a) {
  92. reversed.unshift(a);
  93. })
  94. return reversed;
  95. };
  96. this.compareBothWays = function (array, expected) {
  97. this.compare(array, expected);
  98. this.compare(this.reverse(array), expected);
  99. };
  100. this.compare = function (array, expected) {
  101. var expr = new AddExpression();
  102. array.forEach(function (input) {
  103. expr.addOperand(new ConstantExpression(input));
  104. });
  105. assert.equal(expr.evaluateInternal({}), expected);
  106. }
  107. },
  108. "should add two numbers": function numbers() {
  109. this.compareBothWays([1, 5], 6);
  110. },
  111. "should add a number and a null": function numberAndNull() {
  112. this.compareBothWays([1, null], 1);
  113. },
  114. "should add a number and an undefined": function numberAndUndefined() {
  115. this.compareBothWays([1, undefined], 1);
  116. },
  117. "should add several numbers": function () {
  118. this.compareBothWays([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 55);
  119. },
  120. "should add floats": function () {
  121. this.compareBothWays([1.1, 2.2, 3.3], 6.6);
  122. },
  123. //NOTE: DEVIATION FROM MONGO: We don't support dates
  124. // "should add an int and a date and get a date": function () {
  125. // var d = new Date();
  126. // this.compareBothWays([d, 10], d.getTime() + 10);
  127. // },
  128. //NOTE: DEVIATION FROM MONGO: We don't support dates
  129. // "We can't add 2 dates": function () {
  130. // var d = new Date();
  131. // this.compareBothWays([d, d], 0);
  132. // }
  133. }
  134. },
  135. "optimize": {
  136. beforeEach: function(){
  137. this.vps = new VariablesParseState(new VariablesIdGenerator());
  138. },
  139. "should understand a single number": function() {
  140. var a = Expression.parseOperand({$add:[123]}, this.vps).optimize();
  141. assert.equal(a.operands.length, 0, "The operands should have been optimized away");
  142. assert(a instanceof ConstantExpression);
  143. assert.equal(a.evaluateInternal(), 123);
  144. },
  145. "should optimize strings of numbers without regard to their order": function() {
  146. var a = Expression.parseOperand({$add:[1,2,3,'$a',4,5,6]}, this.vps).optimize();
  147. assert.equal(a.operands.length, 2, "The operands should have been optimized away");
  148. assert(a.operands[0] instanceof FieldPathExpression);
  149. assert(a.operands[1] instanceof ConstantExpression);
  150. assert(a.operands[1].evaluateInternal(), 1+2+3+4+5+6);
  151. }
  152. }
  153. }
  154. };
  155. if (!module.parent)(new (require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);