ComparisonMatchExpression_test.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. "use strict";
  2. var assert = require("assert"),
  3. bson = require("bson"),
  4. MinKey = bson.BSONPure.MinKey,
  5. MaxKey = bson.BSONPure.MaxKey,
  6. MatchDetails = require("../../../../lib/pipeline/matcher/MatchDetails"),
  7. ComparisonMatchExpression = require("../../../../lib/pipeline/matcher/ComparisonMatchExpression");
  8. // Mocha one-liner to make these tests self-hosted
  9. if(!module.parent)return(require.cache[__filename]=null,(new(require("mocha"))({ui:"exports",reporter:"spec",grep:process.env.TEST_GREP})).addFile(__filename).run(process.exit));
  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,'OK');
  14. },
  15. "should not initialize when given an invalid operand": function() {
  16. var e = new ComparisonMatchExpression('');
  17. assert.strictEqual(e.init('',5).code, '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,'BAD_VALUE');
  22. e._matchType = 'LT';
  23. assert.strictEqual(e.init('',{}).code,'BAD_VALUE');
  24. assert.strictEqual(e.init('',undefined).code,'BAD_VALUE');
  25. assert.strictEqual(e.init('',{}).code,'BAD_VALUE');
  26. },
  27. "should match numbers with GTE": function () {
  28. var e = new ComparisonMatchExpression('GTE');
  29. assert.strictEqual(e.init('',5).code,'OK');
  30. assert.ok(e.matchesSingleElement(6),'6 ≥ 5');
  31. assert.ok(e.matchesSingleElement(5),'5 ≥ 5');
  32. assert.ok(!e.matchesSingleElement(4),'4 !≥ 5');
  33. assert.ok(!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,'OK');
  38. assert.ok(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,'OK');
  43. assert.ok(e.matches({'a':[6,10]}),'[6,10] ≥ 5');
  44. assert.ok(e.matches({'a':[4,5.5]}),'[4,5.5] ≥ 5');
  45. assert.ok(!e.matches({'a':[1,2]}),'[1,2] !≥ 5');
  46. assert.ok(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,'OK');
  51. assert.ok(!e.matches({'a':[4]}),'[4] !≥ [5]');
  52. assert.ok(e.matches({'a':[5]}),'[5] !≥ [5]');
  53. assert.ok(e.matches({'a':[6]}),'[6] !≥ [5]');
  54. // documents current behavior
  55. assert.ok(e.matches({'a':[[6]]}),'[[4]] ≥ [5]');
  56. assert.ok(e.matches({'a':[[6]]}),'[[5]] ≥ [5]');
  57. assert.ok(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,'OK');
  63. assert.ok(e.matches({}),'{} ≥ null');
  64. assert.ok(e.matches({'a':null}),'null ≥ null');
  65. assert.ok(!e.matches({'a':4}),'4 !≥ null');
  66. assert.ok(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,'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,'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,'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,'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. };