OrExpression_test.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. "use strict";
  2. var assert = require("assert"),
  3. OrExpression = require("../../../../lib/pipeline/expressions/OrExpression"),
  4. Expression = require("../../../../lib/pipeline/expressions/Expression");
  5. module.exports = {
  6. "OrExpression": {
  7. "constructor()": {
  8. "should not throw Error when constructing without args": function testConstructor(){
  9. assert.doesNotThrow(function(){
  10. new OrExpression();
  11. });
  12. },
  13. "should throw Error when constructing with args": function testConstructor(){
  14. assert.throws(function(){
  15. new OrExpression(1);
  16. });
  17. }
  18. },
  19. "#getOpName()": {
  20. "should return the correct op name; $or": function testOpName(){
  21. assert.equal(new OrExpression().getOpName(), "$or");
  22. }
  23. },
  24. "#getFactory()": {
  25. "should return the constructor for this class": function factoryIsConstructor(){
  26. assert.equal(new OrExpression().getFactory(), OrExpression);
  27. }
  28. },
  29. "#evaluateInternalInternal()": {
  30. "should return false if no operors were given; {$or:[]}": function testEmpty(){
  31. assert.equal(Expression.parseOperand({$or:[]}).evaluateInternal(), false);
  32. },
  33. "should return true if operors is one true; {$or:[true]}": function testTrue(){
  34. assert.equal(Expression.parseOperand({$or:[true]}).evaluateInternal(), true);
  35. },
  36. "should return false if operors is one false; {$or:[false]}": function testFalse(){
  37. assert.equal(Expression.parseOperand({$or:[false]}).evaluateInternal(), false);
  38. },
  39. "should return true if operors are true or true; {$or:[true,true]}": function testTrueTrue(){
  40. assert.equal(Expression.parseOperand({$or:[true,true]}).evaluateInternal(), true);
  41. },
  42. "should return true if operors are true or false; {$or:[true,false]}": function testTrueFalse(){
  43. assert.equal(Expression.parseOperand({$or:[true,false]}).evaluateInternal(), true);
  44. },
  45. "should return true if operors are false or true; {$or:[false,true]}": function testFalseTrue(){
  46. assert.equal(Expression.parseOperand({$or:[false,true]}).evaluateInternal(), true);
  47. },
  48. "should return false if operors are false or false; {$or:[false,false]}": function testFalseFalse(){
  49. assert.equal(Expression.parseOperand({$or:[false,false]}).evaluateInternal(), false);
  50. },
  51. "should return false if operors are false, false, or false; {$or:[false,false,false]}": function testFalseFalseFalse(){
  52. assert.equal(Expression.parseOperand({$or:[false,false,false]}).evaluateInternal(), false);
  53. },
  54. "should return false if operors are false, false, or false; {$or:[false,false,true]}": function testFalseFalseTrue(){
  55. assert.equal(Expression.parseOperand({$or:[false,false,true]}).evaluateInternal(), true);
  56. },
  57. "should return true if operors are 0 or 1; {$or:[0,1]}": function testZeroOne(){
  58. assert.equal(Expression.parseOperand({$or:[0,1]}).evaluateInternal(), true);
  59. },
  60. "should return false if operors are 0 or false; {$or:[0,false]}": function testZeroFalse(){
  61. assert.equal(Expression.parseOperand({$or:[0,false]}).evaluateInternal(), false);
  62. },
  63. "should return true if operor is a path String to a truthy value; {$or:['$a']}": function testFieldPath(){
  64. assert.equal(Expression.parseOperand({$or:['$a']}).evaluateInternal({a:1}), true);
  65. }
  66. },
  67. "#optimize()": {
  68. "should optimize a constant expression to a constant; {$or:[1]} == true": function testOptimizeConstantExpression(){
  69. assert.deepEqual(Expression.parseOperand({$or:[1]}).optimize().toJSON(true), {$const:true});
  70. },
  71. "should not optimize a non-constant expression; {$or:['$a']}; SERVER-6192": function testNonConstant(){
  72. assert.deepEqual(Expression.parseOperand({$or:['$a']}).optimize().toJSON(), {$or:['$a']});
  73. },
  74. "should optimize an expression with a path or a '1' (is entirely constant); {$or:['$a',1]}": function testNonConstantOne(){
  75. assert.deepEqual(Expression.parseOperand({$or:['$a',1]}).optimize().toJSON(true), {$const:true});
  76. },
  77. "should optimize an expression with a field path or a '0'; {$or:['$a',0]}": function testNonConstantZero(){
  78. assert.deepEqual(Expression.parseOperand({$or:['$a',0]}).optimize().toJSON(), {$and:['$a']});
  79. },
  80. "should optimize an expression with two field paths or '1' (is entirely constant); {$or:['$a','$b',1]}": function testNonConstantNonConstantOne(){
  81. assert.deepEqual(Expression.parseOperand({$or:['$a','$b',1]}).optimize().toJSON(true), {$const:true});
  82. },
  83. "should optimize an expression with two field paths or '0'; {$or:['$a','$b',0]}": function testNonConstantNonConstantZero(){
  84. assert.deepEqual(Expression.parseOperand({$or:['$a','$b',0]}).optimize().toJSON(), {$or:['$a','$b']});
  85. },
  86. "should optimize an expression with '0', '1', or a field path; {$or:[0,1,'$a']}": function testZeroOneNonConstant(){
  87. assert.deepEqual(Expression.parseOperand({$or:[0,1,'$a']}).optimize().toJSON(true), {$const:true});
  88. },
  89. "should optimize an expression with '0', '0', or a field path; {$or:[0,0,'$a']}": function testZeroZeroNonConstant(){
  90. assert.deepEqual(Expression.parseOperand({$or:[0,0,'$a']}).optimize().toJSON(), {$and:['$a']});
  91. },
  92. "should optimize nested $or expressions properly or optimize out values evaluating to false; {$or:[0,{$or:[0]},'$a','$b']}": function testNested(){
  93. assert.deepEqual(Expression.parseOperand({$or:[0,{$or:[0]},'$a','$b']}).optimize().toJSON(), {$or:['$a','$b']});
  94. },
  95. "should optimize nested $or expressions containing a nested value evaluating to false; {$or:[0,{$or:[{$or:[1]}]},'$a','$b']}": function testNestedOne(){
  96. assert.deepEqual(Expression.parseOperand({$or:[0,{$or:[{$or:[1]}]},'$a','$b']}).optimize().toJSON(true), {$const:true});
  97. }
  98. }
  99. }
  100. };
  101. if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);