InMatchExpression_test.js 4.6 KB

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