AllElemMatchOp.js 4.1 KB

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