ComparisonMatchExpression_test.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. bson = require("bson"),
  5. MinKey = bson.BSONPure.MinKey,
  6. MaxKey = bson.BSONPure.MaxKey,
  7. ErrorCodes = require("../../../lib/errors").ErrorCodes,
  8. MatchDetails = require("../../../lib/matcher/MatchDetails"),
  9. ComparisonMatchExpression = require("../../../lib/matcher/ComparisonMatchExpression");
  10. exports.ComparisonMatchExpression = {
  11. "should properly initialize with an empty path and a number": function () {
  12. var e = new ComparisonMatchExpression("LT");
  13. assert.strictEqual(e.init("",5).code, ErrorCodes.OK);
  14. },
  15. "should not initialize when given an invalid operand": function() {
  16. var e = new ComparisonMatchExpression("");
  17. assert.strictEqual(e.init("",5).code, ErrorCodes.BAD_VALUE);
  18. },
  19. "should not initialize when given an undefined rhs": function() {
  20. var e = new ComparisonMatchExpression();
  21. assert.strictEqual(e.init("",5).code,ErrorCodes.BAD_VALUE);
  22. e._matchType = "LT";
  23. assert.strictEqual(e.init("",{}).code,ErrorCodes.BAD_VALUE);
  24. assert.strictEqual(e.init("",undefined).code,ErrorCodes.BAD_VALUE);
  25. assert.strictEqual(e.init("",{}).code,ErrorCodes.BAD_VALUE);
  26. },
  27. "should match numbers with GTE": function () {
  28. var e = new ComparisonMatchExpression("GTE");
  29. assert.strictEqual(e.init("",5).code, ErrorCodes.OK);
  30. assert(e.matchesSingleElement(6),"6 ≥ 5");
  31. assert(e.matchesSingleElement(5),"5 ≥ 5");
  32. assert(!e.matchesSingleElement(4),"4 !≥ 5");
  33. assert(!e.matchesSingleElement("foo"),"'foo' !≥ 5");
  34. },
  35. "should match with simple paths and GTE": function() {
  36. var e = new ComparisonMatchExpression("GTE");
  37. assert.strictEqual(e.init("a",5).code, ErrorCodes.OK);
  38. assert(e.matches({a:6}));
  39. },
  40. "should match array values with GTE": function () {
  41. var e = new ComparisonMatchExpression("GTE");
  42. assert.strictEqual(e.init("a",5).code, ErrorCodes.OK);
  43. assert(e.matches({a:[6,10]}),"[6,10] ≥ 5");
  44. assert(e.matches({a:[4,5.5]}),"[4,5.5] ≥ 5");
  45. assert(!e.matches({a:[1,2]}),"[1,2] !≥ 5");
  46. assert(e.matches({a:[1,10]}),"[1,10] ≥ 5");
  47. },
  48. "should match entire arrays with GTE": function() {
  49. var e = new ComparisonMatchExpression("GTE");
  50. assert.strictEqual(e.init("a",[5]).code, ErrorCodes.OK);
  51. assert(!e.matches({a:[4]}),"[4] !≥ [5]");
  52. assert(e.matches({a:[5]}),"[5] !≥ [5]");
  53. assert(e.matches({a:[6]}),"[6] !≥ [5]");
  54. // documents current behavior
  55. assert(e.matches({a:[[6]]}),"[[4]] ≥ [5]");
  56. assert(e.matches({a:[[6]]}),"[[5]] ≥ [5]");
  57. assert(e.matches({a:[[6]]}),"[[6]] ≥ [5]");
  58. },
  59. "should match null with GTE": function() {
  60. var e = new ComparisonMatchExpression("GTE");
  61. e._matchType = "GTE";
  62. assert.strictEqual(e.init("a",null).code, ErrorCodes.OK);
  63. assert(e.matches({}),"{} ≥ null");
  64. assert(e.matches({a:null}),"null ≥ null");
  65. assert(!e.matches({a:4}),"4 !≥ null");
  66. assert(e.matches({b:null}),"non-existent field ≥ null");
  67. },
  68. "should match null in dotted paths with GTE": function() {
  69. var e = new ComparisonMatchExpression("GTE");
  70. assert.strictEqual(e.init("a.b",null).code, ErrorCodes.OK);
  71. assert.ok(e.matches({}),"{} ≥ null");
  72. assert.ok(e.matches({a:null}),"{a:null} ≥ {a.b:null}");
  73. assert.ok(e.matches({a:4}),"{a:4} ≥ {a.b:null}");
  74. assert.ok(e.matches({a:{}}),"{a:{}} ≥ {a.b:null}");
  75. assert.ok(e.matches({a:[{b:null}]}),"{a:[{b:null}]} ≥ {a.b:null}");
  76. assert.ok(e.matches({a:[{a:4},{b:4}]}),"{a:[{a:4},{b:4}]} ≥ {a.b:null}");
  77. assert.ok(!e.matches({a:[4]}),"{a:[4]} !≥ {a.b:null}");
  78. assert.ok(!e.matches({a:[{b:4}]}),"{a:[{b:4}]} !≥ {a.b:null}");
  79. },
  80. "should match MinKeys": function() {
  81. var e = new ComparisonMatchExpression("GTE");
  82. assert.strictEqual(e.init("a",new MinKey()).code, ErrorCodes.OK);
  83. assert.ok(e.matches({a:new MinKey()}),"minKey ≥ minKey");
  84. assert.ok(e.matches({a:new MaxKey()}),"maxKey ≥ minKey");
  85. assert.ok(e.matches({a:4}),"4 ≥ minKey");
  86. },
  87. "should match MaxKeys": function() {
  88. var e = new ComparisonMatchExpression("GTE");
  89. assert.strictEqual(e.init("a",new MaxKey()).code, ErrorCodes.OK);
  90. assert.ok(e.matches({a:new MaxKey()}),"maxKey ≥ maxKey");
  91. assert.ok(!e.matches({a:new MinKey()}),"minKey !≥ maxKey");
  92. assert.ok(!e.matches({a:4},null),"4 !≥ maxKey");
  93. },
  94. "should properly set match keys": function() {
  95. var e = new ComparisonMatchExpression("GTE"),
  96. d = new MatchDetails();
  97. d.requestElemMatchKey();
  98. assert.strictEqual(e.init("a",5).code, ErrorCodes.OK);
  99. assert.ok(!e.matchesJSON({a:4},d),"4 !≥ 5");
  100. assert(!d.hasElemMatchKey());
  101. assert.ok(e.matchesJSON({a:6},d),"6 ≥ 5");
  102. assert(!d.hasElemMatchKey());
  103. assert.ok(e.matchesJSON({a:[2,6,5]},d),"[2,6,5] ≥ 5");
  104. assert(d.hasElemMatchKey());
  105. assert.strictEqual("1",d.elemMatchKey());
  106. }
  107. };