AndExpression.js 5.6 KB

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