ElemMatchValueMatchExpression.js 4.0 KB

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