EqualityMatchExpression.js 5.3 KB

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