AndExpression_test.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. "use strict";
  2. var assert = require("assert"),
  3. Expression = require("../../../../lib/pipeline/expressions/Expression"),
  4. AndExpression = require("../../../../lib/pipeline/expressions/AndExpression"),
  5. VariablesParseState = require("../../../../lib/pipeline/expressions/VariablesParseState"),
  6. VariablesIdGenerator = require("../../../../lib/pipeline/expressions/VariablesIdGenerator"),
  7. utils = require("./utils"),
  8. constify = utils.constify,
  9. expressionToJson = utils.expressionToJson;
  10. // Mocha one-liner to make these tests self-hosted
  11. 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));
  12. var TestBase = function TestBase(overrides) {
  13. //NOTE: DEVIATION FROM MONGO: using this base class to make things easier to initialize
  14. for (var key in overrides)
  15. this[key] = overrides[key];
  16. },
  17. ExpectedResultBase = (function() {
  18. var klass = function ExpectedResultBase() {
  19. base.apply(this, arguments);
  20. }, base = TestBase, proto = klass.prototype = Object.create(base.prototype);
  21. proto.run = function() {
  22. var specElement = this.spec instanceof Function ? this.spec() : this.spec,
  23. idGenerator = new VariablesIdGenerator(),
  24. vps = new VariablesParseState(idGenerator),
  25. expr = Expression.parseOperand(specElement, vps);
  26. assert.deepEqual(constify(specElement), expressionToJson(expr));
  27. var expectedResult = this.expectedResult instanceof Function ? this.expectedResult() : this.expectedResult;
  28. assert.strictEqual(expectedResult, expr.evaluate({a:1}));
  29. var optimized = expr.optimize();
  30. assert.strictEqual(expectedResult, optimized.evaluate({a:1}));
  31. };
  32. return klass;
  33. })(),
  34. OptimizeBase = (function() {
  35. var klass = function OptimizeBase() {
  36. base.apply(this, arguments);
  37. }, base = TestBase, proto = klass.prototype = Object.create(base.prototype);
  38. proto.run = function() {
  39. var specElement = this.spec instanceof Function ? this.spec() : this.spec,
  40. idGenerator = new VariablesIdGenerator(),
  41. vps = new VariablesParseState(idGenerator),
  42. expr = Expression.parseOperand(specElement, vps);
  43. assert.deepEqual(constify(specElement), expressionToJson(expr));
  44. var optimized = expr.optimize(),
  45. expectedOptimized = this.expectedOptimized instanceof Function ? this.expectedOptimized() : this.expectedOptimized;
  46. assert.deepEqual(expectedOptimized, expressionToJson(optimized));
  47. };
  48. return klass;
  49. })(),
  50. NoOptimizeBase = (function() {
  51. var klass = function NoOptimizeBase() {
  52. base.apply(this, arguments);
  53. }, base = OptimizeBase, proto = klass.prototype = Object.create(base.prototype);
  54. proto.expectedOptimized = function() {
  55. return constify(this.spec instanceof Function ? this.spec() : this.spec);
  56. };
  57. return klass;
  58. })();
  59. exports.AndExpression = {
  60. "constructor()": {
  61. "should construct instance": function() {
  62. assert(new AndExpression() instanceof AndExpression);
  63. assert(new AndExpression() instanceof Expression);
  64. },
  65. "should error if given args": function() {
  66. assert.throws(function() {
  67. new AndExpression("bad stuff");
  68. });
  69. },
  70. },
  71. "#getOpName()": {
  72. "should return the correct op name; $and": function() {
  73. assert.equal(new AndExpression().getOpName(), "$and");
  74. }
  75. },
  76. "#evaluate()": {
  77. "should return true if no operands": function testNoOperands() {
  78. /** $and without operands. */
  79. new ExpectedResultBase({
  80. spec: {$and:[]},
  81. expectedResult: true,
  82. }).run();
  83. },
  84. "should return true if given true": function testTrue() {
  85. /** $and passed 'true'. */
  86. new ExpectedResultBase({
  87. spec: {$and:[true]},
  88. expectedResult: true,
  89. }).run();
  90. },
  91. "should return false if given false": function testFalse() {
  92. /** $and passed 'false'. */
  93. new ExpectedResultBase({
  94. spec: {$and:[false]},
  95. expectedResult: false,
  96. }).run();
  97. },
  98. "should return true if given true and true": function testTrueTrue() {
  99. /** $and passed 'true', 'true'. */
  100. new ExpectedResultBase({
  101. spec: {$and:[true, true]},
  102. expectedResult: true,
  103. }).run();
  104. },
  105. "should return false if given true and false": function testTrueFalse() {
  106. /** $and passed 'true', 'false'. */
  107. new ExpectedResultBase({
  108. spec: {$and:[true, false]},
  109. expectedResult: false,
  110. }).run();
  111. },
  112. "should return false if given false and true": function testFalseTrue() {
  113. /** $and passed 'false', 'true'. */
  114. new ExpectedResultBase({
  115. spec: {$and:[false, true]},
  116. expectedResult: false,
  117. }).run();
  118. },
  119. "should return false if given false and false": function testFalseFalse() {
  120. /** $and passed 'false', 'false'. */
  121. new ExpectedResultBase({
  122. spec: {$and:[false, false]},
  123. expectedResult: false,
  124. }).run();
  125. },
  126. "should return true if given true and true and true": function testTrueTrueTrue() {
  127. /** $and passed 'true', 'true', 'true'. */
  128. new ExpectedResultBase({
  129. spec: {$and:[true, true, true]},
  130. expectedResult: true,
  131. }).run();
  132. },
  133. "should return false if given true and true and false": function testTrueTrueFalse() {
  134. /** $and passed 'true', 'true', 'false'. */
  135. new ExpectedResultBase({
  136. spec: {$and:[true, true, false]},
  137. expectedResult: false,
  138. }).run();
  139. },
  140. "should return false if given 0 and 1": function testZeroOne() {
  141. /** $and passed '0', '1'. */
  142. new ExpectedResultBase({
  143. spec: {$and:[0, 1]},
  144. expectedResult: false,
  145. }).run();
  146. },
  147. "should return true if given 1 and 2": function testOneTwo() {
  148. /** $and passed '1', '2'. */
  149. new ExpectedResultBase({
  150. spec: {$and:[1, 2]},
  151. expectedResult: true,
  152. }).run();
  153. },
  154. "should return true if given a field path to a truthy value": function testFieldPath() {
  155. /** $and passed a field path. */
  156. new ExpectedResultBase({
  157. spec: {$and:["$a"]},
  158. expectedResult: true,
  159. }).run();
  160. },
  161. },
  162. "#optimize()": {
  163. "should optimize a constant expression": function OptimizeConstantExpression() {
  164. /** A constant expression is optimized to a constant. */
  165. new OptimizeBase({
  166. spec: {$and:[1]},
  167. expectedOptimized: {$const:true},
  168. }).run();
  169. },
  170. "should not optimize a non constant": function NonConstant() {
  171. /** A non constant expression is not optimized. */
  172. new NoOptimizeBase({
  173. spec: {$and:["$a"]},
  174. }).run();
  175. },
  176. "should optimize if begins with a single truthy constant": function ConstantNonConstantTrue() {
  177. /** An expression beginning with a single constant is optimized. */
  178. new OptimizeBase({
  179. spec: {$and:[1,"$a"]},
  180. expectedOptimized: {$and:["$a"]},
  181. }).run();
  182. // note: using $and as serialization of ExpressionCoerceToBool rather than ExpressionAnd
  183. },
  184. "should optimize if begins with a single falsey constant": function ConstantNonConstantFalse() {
  185. new OptimizeBase({
  186. spec: {$and:[0,"$a"]},
  187. expectedOptimized: {$const:false},
  188. }).run();
  189. },
  190. "should optimize away any truthy constant expressions": function NonConstantOne() {
  191. /** An expression with a field path and '1'. */
  192. new OptimizeBase({
  193. spec: {$and:["$a",1]},
  194. expectedOptimized: {$and:["$a"]}
  195. }).run();
  196. },
  197. "should optimize to false if contains non-truthy constant expressions": function NonConstantZero() {
  198. /** An expression with a field path and '0'. */
  199. new OptimizeBase({
  200. spec: {$and:["$a",0]},
  201. expectedOptimized: {$const:false},
  202. }).run();
  203. },
  204. "should optimize away any truthy constant expressions (and 2 field paths)": function NonConstantNonConstantOne() {
  205. /** An expression with two field paths and '1'. */
  206. new OptimizeBase({
  207. spec: {$and:["$a","$b",1]},
  208. expectedOptimized: {$and:["$a","$b"]}
  209. }).run();
  210. },
  211. "should optimize to false if contains non-truthy constant expressions (and 2 field paths)": function NonConstantNonConstantZero() {
  212. /** An expression with two field paths and '0'. */
  213. new OptimizeBase({
  214. spec: {$and:["$a","$b",0]},
  215. expectedOptimized: {$const:false},
  216. }).run();
  217. },
  218. "should optimize to false if [0,1,'$a']": function ZeroOneNonConstant() {
  219. /** An expression with '0', '1', and a field path. */
  220. new OptimizeBase({
  221. spec: {$and:[0,1,"$a"]},
  222. expectedOptimized: {$const:false},
  223. }).run();
  224. },
  225. "should optimize to '$a' if [1,1,'$a']": function OneOneNonConstant() {
  226. /** An expression with '1', '1', and a field path. */
  227. new OptimizeBase({
  228. spec: {$and:[1,1,"$a"]},
  229. expectedOptimized: {$and:["$a"]},
  230. }).run();
  231. },
  232. "should optimize away nested truthy $and": function Nested() {
  233. /** Nested $and expressions. */
  234. new OptimizeBase({
  235. spec: {$and:[1, {$and:[1]}, "$a", "$b"]},
  236. expectedOptimized: {$and:["$a","$b"]},
  237. }).run();
  238. },
  239. "should optimize to false if nested falsey $and": function NestedZero() {
  240. /** Nested $and expressions containing a nested value evaluating to false. */
  241. new OptimizeBase({
  242. spec: {$and:[1, {$and:[ {$and:[0]} ]}, "$a", "$b"]},
  243. expectedOptimized: {$const:false},
  244. }).run();
  245. },
  246. },
  247. };