InMatchExpression_test.js 4.7 KB

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