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