GTMatchExpression.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. "use strict";
  2. var assert = require("assert"),
  3. BSON = require("bson"),
  4. MatchDetails = require("../../../../lib/pipeline/matcher/MatchDetails"),
  5. GTMatchExpression = require("../../../../lib/pipeline/matcher/GTMatchExpression");
  6. module.exports = {
  7. "GTMatchExpression": {
  8. "should match scalars properly": function (){
  9. var operand = {$gt:5},
  10. gt = new GTMatchExpression();
  11. var s = gt.init("a",operand.$gt);
  12. assert.strictEqual(s.code, "OK");
  13. assert.ok(gt.matchesJSON({"a":5.5}, null));
  14. assert.ok(!gt.matchesJSON({"a":4}), null);
  15. },
  16. "should match array values": function (){
  17. var operand = {$gt:5},
  18. gt = new GTMatchExpression();
  19. var s = gt.init("a",operand.$gt);
  20. assert.strictEqual(s.code, "OK");
  21. assert.ok(gt.matchesJSON({"a":[3,5.5]}, null));
  22. assert.ok(!gt.matchesJSON({"a":[2,4]}), null);
  23. },
  24. "should match whole arrays": function (){
  25. var operand = {$gt:5},
  26. gt = new GTMatchExpression();
  27. var s = gt.init("a",operand.$gt);
  28. assert.strictEqual(s.code, "OK");
  29. assert.ok(!gt.matchesJSON({"a":[4]}, null));
  30. assert.ok(!gt.matchesJSON({"a":[5]}, null));
  31. assert.ok(gt.matchesJSON({"a":[6]}, null));
  32. // Nested array.
  33. // XXX: The following assertion documents current behavior.
  34. assert.ok(gt.matchesJSON({"a":[[4]]}, null));
  35. // XXX: The following assertion documents current behavior.
  36. assert.ok(gt.matchesJSON({"a":[[5]]}, null));
  37. assert.ok(gt.matchesJSON({"a":[[6]]}, null));
  38. },
  39. "should match null values": function (){
  40. var operand = {$gt:null},
  41. gt = new GTMatchExpression();
  42. var s = gt.init("a",operand.$gt);
  43. assert.strictEqual(s.code, "OK");
  44. assert.ok(!gt.matchesJSON({}, null));
  45. assert.ok(!gt.matchesJSON({"a":null}, null));
  46. assert.ok(!gt.matchesJSON({"a":4}), null);
  47. // A non-existent field is treated same way as an empty bson object
  48. assert.ok(!gt.matchesJSON({"b":4}), null);
  49. },
  50. "should match dot notation when null": function (){
  51. var operand = {$gt:null},
  52. gt = new GTMatchExpression();
  53. var s = gt.init("a.b",operand.$gt);
  54. assert.strictEqual(s.code, "OK");
  55. assert.ok(!gt.matchesJSON({}, null));
  56. assert.ok(!gt.matchesJSON({"a":null}, null));
  57. assert.ok(!gt.matchesJSON({"a":4}), null);
  58. assert.ok(!gt.matchesJSON({"a":{}}), null);
  59. assert.ok(!gt.matchesJSON({"a":[{b:null}]}), null);
  60. assert.ok(!gt.matchesJSON({"a":[{a:4},{b:4}]}), null);
  61. assert.ok(!gt.matchesJSON({"a":[4]}), null);
  62. assert.ok(!gt.matchesJSON({"a":[{b:4}]}), null);
  63. },
  64. "should match MinKey": function (){
  65. var operand = {a:new BSON.MinKey()},
  66. gt = new GTMatchExpression();
  67. var s = gt.init("a",operand.a);
  68. assert.strictEqual(s.code, "OK");
  69. assert.ok(!gt.matchesJSON({"a":new BSON.MinKey()}, null));
  70. assert.ok(gt.matchesJSON({"a":new BSON.MaxKey()}, null));
  71. assert.ok(gt.matchesJSON({"a":4}), null);
  72. },
  73. "should match MaxKey": function (){
  74. var operand = {a:new BSON.MaxKey()},
  75. gt = new GTMatchExpression();
  76. var s = gt.init("a",operand.a);
  77. assert.strictEqual(s.code, "OK");
  78. assert.ok(!gt.matchesJSON({"a":new BSON.MaxKey()}, null));
  79. assert.ok(!gt.matchesJSON({"a":new BSON.MinKey()}, null));
  80. assert.ok(!gt.matchesJSON({"a":4}), null);
  81. },
  82. "should use ElemMatchKey": function(){
  83. var operand = {$gt:5},
  84. gt = new GTMatchExpression(),
  85. s = gt.init("a",operand.$gt);
  86. assert.strictEqual(s.code, "OK");
  87. var details = new MatchDetails();
  88. details.requestElemMatchKey();
  89. assert(!gt.matchesJSON({a:4}, details));
  90. assert(!details.hasElemMatchKey());
  91. assert(gt.matchesJSON({a:6}, details));
  92. assert(!details.hasElemMatchKey());
  93. assert(gt.matchesJSON({a:[2,6,5]}, details));
  94. assert(details.hasElemMatchKey());
  95. assert.strictEqual(details.elemMatchKey(), "1");
  96. }
  97. }
  98. };
  99. if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);