AllElemMatchOp_test.js 3.9 KB

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