InMatchExpression.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. "use strict";
  2. var assert = require("assert"),
  3. MatchDetails = require('../../../../lib/pipeline/matcher/MatchDetails'),
  4. InMatchExpression = require("../../../../lib/pipeline/matcher/InMatchExpression"),
  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. "InMatchExpression": {
  10. "should match a single element": function (){
  11. var e = new InMatchExpression();
  12. var s = e.init('a');
  13. assert.strictEqual( s.code,'OK' );
  14. e.getArrayFilterEntries().addEquality(1);
  15. assert.ok( e.matchesSingleElement(1) );
  16. assert.ok( ! e.matchesSingleElement(2) );
  17. },
  18. "should not match with an empty array": function() {
  19. var e = new InMatchExpression();
  20. var s = e.init('a');
  21. assert.strictEqual( s.code,'OK' );
  22. assert.ok( ! e.matchesSingleElement(2) );
  23. assert.ok( ! e.matches({'a':null}) );
  24. assert.ok( ! e.matches({'a':1}) );
  25. },
  26. "should match with multiple elements": function() {
  27. var e = new InMatchExpression();
  28. var s = e.init('a');
  29. assert.strictEqual( s.code,'OK' );
  30. e.getArrayFilterEntries().addEquality(1);
  31. e.getArrayFilterEntries().addEquality('r');
  32. e.getArrayFilterEntries().addEquality(true);
  33. e.getArrayFilterEntries().addEquality(1);
  34. assert.ok( e.matchesSingleElement( 1 ) );
  35. assert.ok( e.matchesSingleElement( 'r' ) );
  36. assert.ok( e.matchesSingleElement( true ) );
  37. assert.ok( !e.matchesSingleElement( false ) );
  38. },
  39. "should match a scalar":function() {
  40. var e = new InMatchExpression();
  41. var s = e.init('a');
  42. assert.strictEqual( s.code,'OK' );
  43. e.getArrayFilterEntries().addEquality(5);
  44. assert.ok( e.matches({'a':5}) );
  45. assert.ok( ! e.matches({'a':4}) );
  46. },
  47. "should match an array":function() {
  48. var e = new InMatchExpression();
  49. var s = e.init('a');
  50. assert.strictEqual( s.code,'OK' );
  51. e.getArrayFilterEntries().addEquality(5);
  52. assert.ok( e.matches({'a':[5,6]}) );
  53. assert.ok( ! e.matches({'a':[6,7]}) );
  54. assert.ok( ! e.matches({'a':[[5]]}) );
  55. },
  56. "should match null": function() {
  57. var e = new InMatchExpression();
  58. var s = e.init('a');
  59. assert.strictEqual( s.code,'OK' );
  60. e.getArrayFilterEntries().addEquality(null);
  61. assert.ok( e.matches({}) );
  62. assert.ok( e.matches({'a':null}) );
  63. assert.ok( ! e.matches({'a':4}) );
  64. // A non-existent field is treated same way as an empty bson object
  65. assert.ok( e.matches({'b':4}) );
  66. },
  67. "should match undefined": function() {
  68. var e = new InMatchExpression();
  69. var s = e.init('a');
  70. assert.strictEqual( s.code,'OK' );
  71. assert( e.getArrayFilterEntries().addEquality(undefined) !== 'OK' );
  72. },
  73. "should match MinKey": function() {
  74. var e = new InMatchExpression();
  75. var s = e.init('a');
  76. assert.strictEqual( s.code,'OK' );
  77. e._arrayEntries._equalities = [MinKey];
  78. assert.ok( e.matches({'a':MinKey}) );
  79. assert.ok( ! e.matches({'a':MaxKey}) );
  80. assert.ok( ! e.matches({'a':4}) );
  81. },
  82. "should match MaxKey": function() {
  83. var e = new InMatchExpression();
  84. var s = e.init('a');
  85. assert.strictEqual( s.code,'OK' );
  86. e._arrayEntries._equalities = [MaxKey];
  87. assert.ok( ! e.matches({'a':MinKey}) );
  88. assert.ok( e.matches({'a':MaxKey}) );
  89. assert.ok( ! e.matches({'a':4}) );
  90. },
  91. "should match a full array":function() {
  92. var e = new InMatchExpression();
  93. var s = e.init('a');
  94. assert.strictEqual( s.code,'OK' );
  95. e.getArrayFilterEntries().addEquality([1,2]);
  96. e.getArrayFilterEntries().addEquality(4);
  97. e.getArrayFilterEntries().addEquality(5);
  98. assert.ok( e.matches({'a':[1,2]}) );
  99. assert.ok( ! e.matches({'a':[1,2,3]}) );
  100. assert.ok( ! e.matches({'a':[1]}) );
  101. assert.ok( ! e.matches({'a':1}) );
  102. },
  103. "should match elemmatchKey": function() {
  104. var e = new InMatchExpression();
  105. var s = e.init('a');
  106. var m = new MatchDetails();
  107. assert.strictEqual( s.code,'OK' );
  108. e.getArrayFilterEntries().addEquality(5);
  109. e.getArrayFilterEntries().addEquality(2);
  110. m.requestElemMatchKey();
  111. assert.ok( !e.matches({'a':4}, m) );
  112. assert.ok( !m.hasElemMatchKey() );
  113. assert.ok( e.matches({'a':5}, m) );
  114. assert.ok( !m.hasElemMatchKey() );
  115. assert.ok( e.matches({'a':[1,2,5]}, m ));
  116. assert.ok( m.hasElemMatchKey() );
  117. assert.strictEqual( m.elemMatchKey(), '1' );
  118. }
  119. }
  120. };
  121. if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);