EqualityMatchExpression.js 5.2 KB

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