ElemMatchValueMatchExpression_test.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. ElemMatchObjectMatchExpression = require("../../../../lib/pipeline/matcher/ElemMatchObjectMatchExpression.js"),
  6. ElemMatchValueMatchExpression = require("../../../../lib/pipeline/matcher/ElemMatchValueMatchExpression.js"),
  7. MatchDetails = require("../../../../lib/pipeline/matcher/MatchDetails.js"),
  8. LTMatchExpression = require("../../../../lib/pipeline/matcher/LTMatchExpression.js"),
  9. GTMatchExpression = require("../../../../lib/pipeline/matcher/GTMatchExpression.js");
  10. module.exports = {
  11. "ElemMatchValueMatchExpression": {
  12. "Should match a single element": function() {
  13. var baseOperand={"$gt":5},
  14. match={"a":[6]},
  15. notMatch={"a":[4]},
  16. gt = new GTMatchExpression(),
  17. op = new ElemMatchValueMatchExpression();
  18. assert.strictEqual(gt.init("", baseOperand.$gt).code, ErrorCodes.OK);
  19. assert.strictEqual(op.init("a", gt).code, ErrorCodes.OK);
  20. assert.ok(op.matchesSingleElement(match.a));
  21. assert.ok(!op.matchesSingleElement(notMatch.a));
  22. },
  23. "Should match multiple elements": function() {
  24. var baseOperand1={"$gt":1},
  25. baseOperand2={"$lt":10},
  26. notMatch1={"a":[0,1]},
  27. notMatch2={"a":[10,11]},
  28. match={"a":[0,5,11]},
  29. gt = new GTMatchExpression(),
  30. lt = new LTMatchExpression(),
  31. op = new ElemMatchValueMatchExpression();
  32. assert.strictEqual(gt.init("", baseOperand1.$gt).code, ErrorCodes.OK);
  33. assert.strictEqual(lt.init("", baseOperand2.$lt).code, ErrorCodes.OK);
  34. assert.strictEqual(op.init("a").code, ErrorCodes.OK);
  35. op.add(gt);
  36. op.add(lt);
  37. assert.ok(!op.matchesSingleElement(notMatch1.a));
  38. assert.ok(!op.matchesSingleElement(notMatch2.a));
  39. assert.ok(op.matchesSingleElement(match.a));
  40. },
  41. "Should match a non array": function() {
  42. var baseOperand={"$gt":5},
  43. gt = new GTMatchExpression(),
  44. op = new ElemMatchObjectMatchExpression();
  45. assert.strictEqual(gt.init("", baseOperand.$gt).code, ErrorCodes.OK);
  46. assert.strictEqual(op.init("a", gt).code, ErrorCodes.OK);
  47. // Directly nested objects are not matched with $elemMatch. An intervening array is
  48. // required.
  49. assert.ok(!op.matches({"a":6},null));
  50. assert.ok(!op.matches({"a":{"0":6}},null));
  51. },
  52. "Should match an array scalar": function() {
  53. var baseOperand={"$gt":5},
  54. gt = new GTMatchExpression(),
  55. op = new ElemMatchValueMatchExpression();
  56. assert.strictEqual(gt.init("", baseOperand.$gt).code, ErrorCodes.OK);
  57. assert.strictEqual(op.init("a", gt).code, ErrorCodes.OK);
  58. assert.ok(op.matches({"a":[6]},null));
  59. assert.ok(op.matches({"a":[4,6]},null));
  60. assert.ok(op.matches({"a":[{},7]},null));
  61. },
  62. "Should match multiple named values": function() {
  63. var baseOperand={"$gt":5},
  64. gt = new GTMatchExpression(),
  65. op = new ElemMatchValueMatchExpression();
  66. assert.strictEqual(gt.init("", baseOperand.$gt).code, ErrorCodes.OK);
  67. assert.strictEqual(op.init("a.b", gt).code, ErrorCodes.OK);
  68. assert.ok(op.matches({"a":[{"b":[6]}]}, null));
  69. assert.ok(op.matches({"a":[{"b":[4]}, {"b":[4,6]}]}, null));
  70. },
  71. "ElemMatchKey should return the appropriate values": function() {
  72. var baseOperand={"$gt":6},
  73. gt = new GTMatchExpression(),
  74. op = new ElemMatchValueMatchExpression(),
  75. details = new MatchDetails();
  76. assert.strictEqual(gt.init("", baseOperand.$gt).code, ErrorCodes.OK);
  77. assert.strictEqual(op.init("a.b", gt).code, ErrorCodes.OK);
  78. details.requestElemMatchKey();
  79. assert.ok(!op.matches({}, details));
  80. assert.ok(!details.hasElemMatchKey());
  81. assert.ok(!op.matches({"a":{"b":[2]}}, details));
  82. assert.ok(!details.hasElemMatchKey());
  83. assert.ok(op.matches({"a":{"b":[3,7]}}, details));
  84. assert.ok(details.hasElemMatchKey());
  85. // The entry within the $elemMatch array is reported.
  86. assert.strictEqual("1", details.elemMatchKey());
  87. assert.ok(op.matches({"a":[1, 2, {"b":[3,7]}]}, details));
  88. assert.ok(details.hasElemMatchKey());
  89. // The entry within a parent of the $elemMatch array is reported.
  90. assert.strictEqual("2", details.elemMatchKey());
  91. }
  92. }
  93. };