ElemMatchObjectMatchExpression.js 4.9 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. AndMatchExpression = require("../../../../lib/pipeline/matcher/AndMatchExpression.js"),
  6. MatchDetails = require("../../../../lib/pipeline/matcher/MatchDetails.js");
  7. module.exports = {
  8. "ElemMatchObjectMatchExpression": {
  9. "Should match a single element": function(){
  10. var baseOperand = {"b":5},
  11. match= {"a":[{"b":5.0}]},
  12. notMatch= {"a":[{"b":6}]},
  13. eq = new EqualityMatchExpression(),
  14. op = new ElemMatchObjectMatchExpression();
  15. assert.strictEqual(eq.init("b", baseOperand.b).code, 'OK');
  16. assert.strictEqual(op.init("a", eq).code, 'OK');
  17. assert.ok(op.matchesSingleElement(match.a));
  18. assert.ok(!op.matchesSingleElement(notMatch.a));
  19. },
  20. "Should match an array of elements inside the array": function() {
  21. var baseOperand= {"1":5},
  22. match= {"a":[['s',5.0]]},
  23. notMatch= {"a":[[5,6]]},
  24. eq = new EqualityMatchExpression(),
  25. op = new ElemMatchObjectMatchExpression();
  26. assert.strictEqual(eq.init("1", baseOperand["1"]).code, 'OK');
  27. assert.strictEqual(op.init("a", eq).code, 'OK');
  28. assert.ok(op.matchesSingleElement(match.a));
  29. assert.ok(!op.matchesSingleElement(notMatch.a));
  30. },
  31. "Should match multiple elements in an array": function() {
  32. var baseOperand1 = {"b":5},
  33. baseOperand2 = {"b":6},
  34. baseOperand3 = {"c":7},
  35. notMatch1 = {"a":[{"b":5,"c":7}]},
  36. notMatch2 = {"a":[{"b":6,"c":7}]},
  37. notMatch3 = {"a":[{"b":[5,6]}]},
  38. match = {"a":[{"b":[5,6],"c":7}]},
  39. eq1 = new EqualityMatchExpression(),
  40. eq2 = new EqualityMatchExpression(),
  41. andOp = new AndMatchExpression(),
  42. eq3 = new EqualityMatchExpression(),
  43. op = new ElemMatchObjectMatchExpression();
  44. assert.strictEqual(eq1.init("b", baseOperand1.b).code, 'OK');
  45. assert.strictEqual(eq2.init("b", baseOperand2.b).code, 'OK');
  46. assert.strictEqual(eq3.init("c", baseOperand3.c).code, 'OK');
  47. andOp.add(eq1);
  48. andOp.add(eq2);
  49. andOp.add(eq3);
  50. assert.strictEqual(op.init("a", andOp).code, 'OK');
  51. assert.ok(!op.matchesSingleElement(notMatch1.a));
  52. assert.ok(!op.matchesSingleElement(notMatch2.a));
  53. assert.ok(!op.matchesSingleElement(notMatch3.a));
  54. assert.ok(op.matchesSingleElement(match.a));
  55. },
  56. "Should not match a nested non array": function() {
  57. var baseOperand={"b":5},
  58. eq = new EqualityMatchExpression(),
  59. op = new ElemMatchObjectMatchExpression();
  60. assert.strictEqual(eq.init("b", baseOperand.b).code, 'OK');
  61. assert.strictEqual(op.init("a", eq).code, 'OK');
  62. // Directly nested objects are not matched with $elemMatch. An intervening array is
  63. // required.
  64. assert.ok(!op.matches({"a":{"b":5}},null));
  65. assert.ok(!op.matches({"a":{"0":{"b":5}}},null));
  66. assert.ok(!op.matches({"a":4},null));
  67. },
  68. "Should match an object in an array": function() {
  69. var baseOperand={"b":5},
  70. eq = new EqualityMatchExpression(),
  71. op = new ElemMatchObjectMatchExpression();
  72. assert.strictEqual(eq.init("b", baseOperand.b).code, 'OK');
  73. assert.strictEqual(op.init("a", eq).code, 'OK');
  74. assert.ok(op.matches({"a":[{"b":5}]}, null));
  75. assert.ok(op.matches({"a":[4,{"b":5}]}, null));
  76. assert.ok(op.matches({"a":[{},{"b":5}]}, null));
  77. assert.ok(op.matches({"a":[{"b":6},{"b":5}]}, null));
  78. },
  79. "Should match a path inside an array": function() {
  80. var baseOperand={"c":5},
  81. eq = new EqualityMatchExpression(),
  82. op = new ElemMatchObjectMatchExpression();
  83. assert.strictEqual(eq.init("c", baseOperand.c).code, 'OK');
  84. assert.strictEqual(op.init("a.b", eq).code, 'OK');
  85. assert.ok(op.matches({"a":[{"b":[{"c":5}]}]},null));
  86. assert.ok(op.matches({"a":[{"b":[{"c":1}]}, {"b":[{"c":5}]}]},null));
  87. },
  88. "ElemMatchKey should return the appropriate values": function() {
  89. var baseOperand={"c":6},
  90. eq = new EqualityMatchExpression(),
  91. op = new ElemMatchObjectMatchExpression(),
  92. details = new MatchDetails();
  93. assert.strictEqual(eq.init("c", baseOperand.c).code, 'OK');
  94. assert.strictEqual(op.init("a.b", eq).code, 'OK');
  95. details.requestElemMatchKey();
  96. assert.ok(!op.matches({}, details));
  97. assert.ok(!details.hasElemMatchKey());
  98. assert.ok(!op.matches({"a":{"b":[{"c":7}]}}, details));
  99. assert.ok(!details.hasElemMatchKey());
  100. assert.ok(op.matches({"a":{"b":[3, {"c":6}]}}, details));
  101. assert.ok(details.hasElemMatchKey());
  102. // The entry within the $elemMatch array is reported.
  103. assert.strictEqual("1", details.elemMatchKey());
  104. assert.ok(op.matches({"a":[1, 2, {"b":[3, 5, {"c":6}]}]}, details));
  105. assert.ok(details.hasElemMatchKey());
  106. // The entry within a parent of the $elemMatch array is reported.
  107. assert.strictEqual("2", details.elemMatchKey());
  108. },
  109. }
  110. };
  111. if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);