AllElemMatchOp_test.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. "use strict";
  2. var assert = require("assert"),
  3. EqualityMatchExpression = require("../../../../lib/pipeline/matcher/EqualityMatchExpression.js"),
  4. ElemMatchObjectMatchExpression = require("../../../../lib/pipeline/matcher/ElemMatchObjectMatchExpression.js"),
  5. ElemMatchValueMatchExpression = require("../../../../lib/pipeline/matcher/ElemMatchValueMatchExpression.js"),
  6. AndMatchExpression = require("../../../../lib/pipeline/matcher/AndMatchExpression.js"),
  7. LTMatchExpression = require("../../../../lib/pipeline/matcher/LTMatchExpression.js"),
  8. GTMatchExpression = require("../../../../lib/pipeline/matcher/GTMatchExpression.js"),
  9. AllElemMatchOp = require("../../../../lib/pipeline/matcher/AllElemMatchOp.js");
  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. exports.AllElemMatchOp = {
  13. "Should match an element": function() {
  14. var baseOperanda1={"a":1},
  15. eqa1 = new EqualityMatchExpression();
  16. assert.strictEqual(eqa1.init("a", baseOperanda1.a).code, 'OK');
  17. var baseOperandb1={"b":1},
  18. eqb1 = new EqualityMatchExpression(),
  19. and1 = new AndMatchExpression(),
  20. elemMatch1 = new ElemMatchObjectMatchExpression();
  21. assert.strictEqual(eqb1.init("b", baseOperandb1.b).code, 'OK');
  22. and1.add(eqa1);
  23. and1.add(eqb1);
  24. // and1 = { a : 1, b : 1 }
  25. elemMatch1.init("x", and1);
  26. // elemMatch1 = { x : { $elemMatch : { a : 1, b : 1 } } }
  27. var baseOperanda2={"a":2},
  28. eqa2 = new EqualityMatchExpression();
  29. assert.strictEqual(eqa2.init("a", baseOperanda2.a).code, 'OK');
  30. var baseOperandb2={"b":2},
  31. eqb2 = new EqualityMatchExpression(),
  32. and2 = new AndMatchExpression(),
  33. elemMatch2 = new ElemMatchObjectMatchExpression(),
  34. op = new AllElemMatchOp();
  35. assert.strictEqual(eqb2.init("b", baseOperandb2.b).code, 'OK');
  36. and2.add(eqa2);
  37. and2.add(eqb2);
  38. elemMatch2.init("x", and2);
  39. // elemMatch2 = { x : { $elemMatch : { a : 2, b : 2 } } }
  40. op.init("");
  41. op.add(elemMatch1);
  42. op.add(elemMatch2);
  43. var nonArray={"x":4},
  44. emptyArray={"x":[]},
  45. nonObjArray={"x":[4]},
  46. singleObjMatch={"x":[{"a":1, "b":1}]},
  47. otherObjMatch={"x":[{"a":2, "b":2}]},
  48. bothObjMatch={"x":[{"a":1, "b":1}, {"a":2, "b":2}]},
  49. noObjMatch={"x":[{"a":1, "b":2}, {"a":2, "b":1}]};
  50. assert.ok(!op.matchesSingleElement(nonArray.x));
  51. assert.ok(!op.matchesSingleElement(emptyArray.x));
  52. assert.ok(!op.matchesSingleElement(nonObjArray.x));
  53. assert.ok(!op.matchesSingleElement(singleObjMatch.x));
  54. assert.ok(!op.matchesSingleElement(otherObjMatch.x));
  55. assert.ok(op.matchesSingleElement(bothObjMatch.x));
  56. assert.ok(!op.matchesSingleElement(noObjMatch.x));
  57. },
  58. "Should match things": function() {
  59. var baseOperandgt1={"$gt":1},
  60. gt1 = new GTMatchExpression();
  61. assert.strictEqual(gt1.init("", baseOperandgt1.$gt).code, 'OK');
  62. var baseOperandlt1={"$lt":10},
  63. lt1 = new LTMatchExpression(),
  64. elemMatch1 = new ElemMatchValueMatchExpression();
  65. assert.strictEqual(lt1.init("", baseOperandlt1.$lt).code, 'OK');
  66. elemMatch1.init("x");
  67. elemMatch1.add(gt1);
  68. elemMatch1.add(lt1);
  69. var baseOperandgt2={"$gt":101},
  70. gt2 = new GTMatchExpression();
  71. assert.strictEqual(gt2.init("", baseOperandgt2.$gt).code, 'OK');
  72. var baseOperandlt2={"$lt":110},
  73. lt2 = new LTMatchExpression(),
  74. elemMatch2 = new ElemMatchValueMatchExpression(),
  75. op = new AllElemMatchOp();
  76. assert.strictEqual(lt2.init("", baseOperandlt2.$lt).code, 'OK');
  77. elemMatch2.init("x");
  78. elemMatch2.add(gt2);
  79. elemMatch2.add(lt2);
  80. op.init("x");
  81. op.add(elemMatch1);
  82. op.add(elemMatch2);
  83. var nonArray={"x":4},
  84. emptyArray={"x":[]},
  85. nonNumberArray={"x":["q"]},
  86. singleMatch={"x":[5]},
  87. otherMatch={"x":[105]},
  88. bothMatch={"x":[5,105]},
  89. neitherMatch={"x":[0,200]};
  90. assert.ok(!op.matches(nonArray, null));
  91. assert.ok(!op.matches(emptyArray, null));
  92. assert.ok(!op.matches(nonNumberArray, null));
  93. assert.ok(!op.matches(singleMatch, null));
  94. assert.ok(!op.matches(otherMatch, null));
  95. assert.ok(op.matches(bothMatch, null));
  96. assert.ok(!op.matches(neitherMatch, null));
  97. }
  98. };