EqualityMatchExpression_test.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. MatchDetails = require("../../../../lib/pipeline/matcher/MatchDetails"),
  6. EqualityMatchExpression = require("../../../../lib/pipeline/matcher/EqualityMatchExpression"),
  7. // TODO: replace the following with a real BSONTypes at some point
  8. MinKey = new (function MinKey(){/*matcher does weird stuff with empty objects*/this.foo = 'bar';})(), // jshint ignore:line
  9. MaxKey = new (function MaxKey(){/*matcher does weird stuff with empty objects*/this.foo = 'bar';})(); // jshint ignore:line
  10. module.exports = {
  11. "EqualityMatchExpression": {
  12. "should match elements": function testMatchesElement(){
  13. var operand = {a:5},
  14. match = {a:5.0},
  15. notMatch = {a:6};
  16. var eq = new EqualityMatchExpression();
  17. eq.init("a", operand.a);
  18. assert.ok(eq.matches(match));
  19. assert.ok(!eq.matches(notMatch));
  20. assert.ok(eq.equivalent(eq));
  21. },
  22. "should handle invalid End of Object Operand": function testInvalidEooOperand(){
  23. var e = new EqualityMatchExpression();
  24. var s = e.init("",{});
  25. assert.ok(s.code !== ErrorCodes.OK);
  26. },
  27. "should match a pathed number":function() {
  28. var e = new EqualityMatchExpression();
  29. var s = e.init("a",5);
  30. assert.strictEqual(s.code, ErrorCodes.OK);
  31. assert.ok( e.matches({"a":5}) );
  32. assert.ok( ! e.matches({"a":4}) );
  33. },
  34. "should match stuff in an array": function() {
  35. var e = new EqualityMatchExpression();
  36. var s = e.init("a",5);
  37. assert.strictEqual(s.code, ErrorCodes.OK);
  38. assert.ok( e.matches({"a":[5,6]}) );
  39. assert.ok( ! e.matches({"a":[6,7]}) );
  40. },
  41. "should match on a longer path": function() {
  42. var e = new EqualityMatchExpression();
  43. var s = e.init("a.b",5);
  44. assert.strictEqual(s.code, ErrorCodes.OK);
  45. assert.ok( e.matches({"a":{"b":5}}) );
  46. assert.ok( e.matches({"a":{"b":[5]}}) );
  47. assert.ok( e.matches({"a":[{"b":5}]}) );
  48. },
  49. "should match in an array": function() {
  50. var e = new EqualityMatchExpression();
  51. var s = e.init("a.0",5);
  52. assert.strictEqual(s.code, ErrorCodes.OK);
  53. assert.ok( e.matches({"a":[5]}) );
  54. assert.ok( ! e.matches({"a":[[5]]}) );
  55. },
  56. "should match null" : function() {
  57. var e = new EqualityMatchExpression();
  58. var s = e.init("a",null);
  59. assert.strictEqual(s.code, ErrorCodes.OK);
  60. assert.ok( e.matches({}) );
  61. assert.ok( e.matches({"a":null}) );
  62. assert.ok( ! e.matches({"a":4}) );
  63. assert.ok( e.matches({"b":4}) );
  64. },
  65. //// This test documents how the matcher currently works,
  66. //// not necessarily how it should work ideally.
  67. "should match nested nulls" : function(){
  68. var e = new EqualityMatchExpression();
  69. var s = e.init("a.b",null);
  70. assert.strictEqual(s.code, ErrorCodes.OK);
  71. // // null matches any empty object that is on a subpath of a.b
  72. assert.ok( e.matches({}) );
  73. assert.ok( e.matches({"a":{}}) );
  74. assert.ok( e.matches({"a":[{}]}) );
  75. assert.ok( e.matches({"a":{"b":null}} ) );
  76. // b does not exist as an element in array under a.
  77. assert.ok( !e.matches({"a":[]}) );
  78. assert.ok( !e.matches({"a":[null]}) );
  79. assert.ok( !e.matches({"a":[1,2]}) );
  80. // a.b exists but is not null.
  81. assert.ok( !e.matches({"a":{"b":4}} ) );
  82. assert.ok( !e.matches({"a":{"b":{}}} ) );
  83. // A non-existent field is treated same way as an empty bson object
  84. assert.ok( e.matches({"b":4} ) );
  85. assert.ok( e.matches({"b":"stuff"}) );
  86. },
  87. "should match MinKey" : function(){
  88. var e = new EqualityMatchExpression();
  89. e.init("a",MinKey);
  90. assert.ok( e.matches({"a": MinKey}) );
  91. assert.ok( !e.matches({"a":MaxKey}) );
  92. assert.ok( !e.matches({"a":4}) );
  93. },
  94. "should match MaxKey" : function(){
  95. var e = new EqualityMatchExpression();
  96. e.init("a",MaxKey);
  97. assert.ok( e.matches({"a":MaxKey}) );
  98. assert.ok( !e.matches({"a": MinKey}) );
  99. assert.ok( !e.matches({"a":4}) );
  100. },
  101. "should match full array" : function() {
  102. var e = new EqualityMatchExpression();
  103. var s = e.init("a",[1,2]);
  104. assert.strictEqual(s.code, ErrorCodes.OK);
  105. assert.ok( e.matches({"a":[1,2]}) );
  106. assert.ok( ! e.matches({"a":[1,2,3]}) );
  107. assert.ok( ! e.matches({"a":[1]}) );
  108. assert.ok( ! e.matches({"a":1}) );
  109. },
  110. "should match a nested array": function() {
  111. var e = new EqualityMatchExpression();
  112. var s = e.init("a.b.c.d",3);
  113. assert.strictEqual(s.code, ErrorCodes.OK);
  114. assert.ok( e.matches({a:{b:[{c:[{d:1},{d:2}]},{c:[{d:3}]}]}}) );
  115. },
  116. "should handle elemMatchKey":function() {
  117. var e = new EqualityMatchExpression();
  118. var s = e.init("a", 5);
  119. var m = new MatchDetails();
  120. m.requestElemMatchKey();
  121. assert.strictEqual(s.code, ErrorCodes.OK);
  122. assert.ok( ! e.matches({"a":4}, m) );
  123. assert.ok( ! m.hasElemMatchKey() );
  124. assert.ok( e.matches({"a":5}, m) );
  125. assert.ok( ! m.hasElemMatchKey() );
  126. assert.ok( e.matches({"a":[1,2,5]}, m));
  127. assert.ok( m.hasElemMatchKey());
  128. assert.strictEqual("2", m.elemMatchKey());
  129. },
  130. "should handle equivalence":function() {
  131. var a = new EqualityMatchExpression();
  132. var b = new EqualityMatchExpression();
  133. var c = new EqualityMatchExpression();
  134. assert.strictEqual(a.init("a",5).code, ErrorCodes.OK);
  135. assert.strictEqual(b.init("a",5).code, ErrorCodes.OK);
  136. assert.strictEqual(c.init("c",4).code, ErrorCodes.OK);
  137. assert.ok( a.equivalent(a) );
  138. assert.ok( a.equivalent(b) );
  139. assert.ok( ! a.equivalent(c) );
  140. }
  141. }
  142. };