EqualityMatchExpression_test.js 5.3 KB

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