summaryrefslogtreecommitdiff
path: root/src/mongo/db/matcher/expression_parser_array_test.cpp
diff options
context:
space:
mode:
authorDavid Storch <david.storch@10gen.com>2014-11-21 11:40:19 -0500
committerDavid Storch <david.storch@10gen.com>2014-12-01 14:15:08 -0500
commit430f62da7380a6864058c2f75a64c5d662a95176 (patch)
treecd8de113d6de21d4c5bb3b11d404c547fcc4187f /src/mongo/db/matcher/expression_parser_array_test.cpp
parent2684c73fc835681b00f9822009a5621fb12d26bc (diff)
downloadmongo-430f62da7380a6864058c2f75a64c5d662a95176.tar.gz
SERVER-16256 get rid of ALL match expressions
Every $all should parse to an AND match expression. This simplifies the implementation, and fixes a bug related to bounds building for $all-$elemMatch constructions.
Diffstat (limited to 'src/mongo/db/matcher/expression_parser_array_test.cpp')
-rw-r--r--src/mongo/db/matcher/expression_parser_array_test.cpp85
1 files changed, 85 insertions, 0 deletions
diff --git a/src/mongo/db/matcher/expression_parser_array_test.cpp b/src/mongo/db/matcher/expression_parser_array_test.cpp
index e2987efb74c..1d4bdf54ee8 100644
--- a/src/mongo/db/matcher/expression_parser_array_test.cpp
+++ b/src/mongo/db/matcher/expression_parser_array_test.cpp
@@ -324,6 +324,9 @@ namespace mongo {
StatusWithMatchExpression result = MatchExpressionParser::parse( query );
ASSERT_TRUE( result.isOK() );
+ // Verify that the $all got parsed to AND.
+ ASSERT_EQUALS( MatchExpression::AND, result.getValue()->matchType() );
+
ASSERT( !result.getValue()->matchesBSON( BSON( "x" << 1 ) ) );
ASSERT( !result.getValue()->matchesBSON( BSON( "x" << BSON_ARRAY( 1 ) ) ) );
ASSERT( !result.getValue()->matchesBSON( BSON( "x" << BSON_ARRAY( 2 ) ) ) );
@@ -338,6 +341,9 @@ namespace mongo {
StatusWithMatchExpression result = MatchExpressionParser::parse( query );
ASSERT_TRUE( result.isOK() );
+ // Verify that the $all got parsed to AND.
+ ASSERT_EQUALS( MatchExpression::AND, result.getValue()->matchType() );
+
ASSERT( !result.getValue()->matchesBSON( BSON( "x" << 1 ) ) );
ASSERT( !result.getValue()->matchesBSON( BSON( "x" << BSON_ARRAY( 1 ) ) ) );
ASSERT( result.getValue()->matchesBSON( BSON( "x" << BSONNULL ) ) );
@@ -376,6 +382,9 @@ namespace mongo {
StatusWithMatchExpression result = MatchExpressionParser::parse( query );
ASSERT_TRUE( result.isOK() );
+ // Verify that the $all got parsed to AND.
+ ASSERT_EQUALS( MatchExpression::AND, result.getValue()->matchType() );
+
BSONObj notMatchFirst = BSON( "a" << "ax" );
BSONObj notMatchSecond = BSON( "a" << "qqb" );
BSONObj matchesBoth = BSON( "a" << "ab" );
@@ -397,6 +406,9 @@ namespace mongo {
StatusWithMatchExpression result = MatchExpressionParser::parse( query );
ASSERT_TRUE( result.isOK() );
+ // Verify that the $all got parsed to AND.
+ ASSERT_EQUALS( MatchExpression::AND, result.getValue()->matchType() );
+
BSONObj notMatchFirst = BSON( "a" << "ax" );
BSONObj matchesBoth = BSON( "a" << "abc" );
@@ -410,6 +422,9 @@ namespace mongo {
StatusWithMatchExpression result = MatchExpressionParser::parse( query );
ASSERT_TRUE( result.isOK() );
+ // Verify that the $all got parsed to AND.
+ ASSERT_EQUALS( MatchExpression::AND, result.getValue()->matchType() );
+
ASSERT( result.getValue()->matchesBSON( BSON( "x" << 5 ) ) );
ASSERT( result.getValue()->matchesBSON( BSON( "x" << BSON_ARRAY( 5 ) ) ) );
ASSERT( !result.getValue()->matchesBSON( BSON( "x" << 4 ) ) );
@@ -424,6 +439,12 @@ namespace mongo {
StatusWithMatchExpression result = MatchExpressionParser::parse( query );
ASSERT_TRUE( result.isOK() );
+ // Verify that the $all got parsed to an AND with a single ELEM_MATCH_OBJECT child.
+ ASSERT_EQUALS( MatchExpression::AND, result.getValue()->matchType() );
+ ASSERT_EQUALS( 1U, result.getValue()->numChildren() );
+ MatchExpression* child = result.getValue()->getChild( 0 );
+ ASSERT_EQUALS( MatchExpression::ELEM_MATCH_OBJECT, child->matchType() );
+
ASSERT( !result.getValue()->matchesBSON( BSON( "x" << 1 ) ) );
ASSERT( !result.getValue()->matchesBSON( BSON( "x" << BSON_ARRAY( 1 << 2 ) ) ) );
ASSERT( !result.getValue()->matchesBSON( BSON( "x" << BSON_ARRAY( BSON( "x" << 1 ) ) ) ) );
@@ -442,6 +463,12 @@ namespace mongo {
StatusWithMatchExpression result = MatchExpressionParser::parse( query );
ASSERT_TRUE( result.isOK() );
+ // Verify that the $all got parsed to an AND with a single ELEM_MATCH_OBJECT child.
+ ASSERT_EQUALS( MatchExpression::AND, result.getValue()->matchType() );
+ ASSERT_EQUALS( 1U, result.getValue()->numChildren() );
+ MatchExpression* child = result.getValue()->getChild( 0 );
+ ASSERT_EQUALS( MatchExpression::ELEM_MATCH_OBJECT, child->matchType() );
+
ASSERT( !result.getValue()->matchesBSON( BSON( "x" << BSON( "y" << 1 ) ) ) );
ASSERT( !result.getValue()->matchesBSON( BSON( "x" << BSON( "y" <<
BSON_ARRAY( 1 << 2 ) ) ) ) );
@@ -482,6 +509,41 @@ namespace mongo {
delete result.getValue();
}
+ // Check the structure of the resulting MatchExpression, and make sure that the paths
+ // are correct.
+ TEST( MatchExpressionParserArrayTest, AllElemMatch3 ) {
+ BSONObj query = fromjson( "{x: {$all: [{$elemMatch: {y: 1, z: 1}}]}}" );
+ StatusWithMatchExpression result = MatchExpressionParser::parse( query );
+ ASSERT_TRUE( result.isOK() );
+
+ boost::scoped_ptr<MatchExpression> expr( result.getValue() );
+
+ // Root node should be an AND with one child.
+ ASSERT_EQUALS( MatchExpression::AND, expr->matchType() );
+ ASSERT_EQUALS( 1U, expr->numChildren() );
+
+ // Child should be an ELEM_MATCH_OBJECT with one child and path "x".
+ MatchExpression* emObject = expr->getChild( 0 );
+ ASSERT_EQUALS( MatchExpression::ELEM_MATCH_OBJECT, emObject->matchType() );
+ ASSERT_EQUALS( 1U, emObject->numChildren() );
+ ASSERT_EQUALS( "x", emObject->path().toString() );
+
+ // Child should be another AND with two children.
+ MatchExpression* and2 = emObject->getChild( 0 );
+ ASSERT_EQUALS( MatchExpression::AND, and2->matchType() );
+ ASSERT_EQUALS( 2U, and2->numChildren() );
+
+ // Both children should be equalites, with paths "y" and "z".
+ MatchExpression* leaf1 = and2->getChild( 0 );
+ ASSERT_EQUALS( MatchExpression::EQ, leaf1->matchType() );
+ ASSERT_EQUALS( 0U, leaf1->numChildren() );
+ ASSERT_EQUALS( "y", leaf1->path().toString() );
+ MatchExpression* leaf2 = and2->getChild( 1 );
+ ASSERT_EQUALS( MatchExpression::EQ, leaf2->matchType() );
+ ASSERT_EQUALS( 0U, leaf2->numChildren() );
+ ASSERT_EQUALS( "z", leaf2->path().toString() );
+ }
+
TEST( MatchExpressionParserArrayTest, AllElemMatchBad ) {
BSONObj internal = BSON( "x" << 1 << "y" << 2 );
@@ -494,6 +556,29 @@ namespace mongo {
ASSERT_FALSE( result.isOK() );
}
+ // You can't mix $elemMatch and regular equality inside $all.
+ TEST( MatchExpressionParserArrayTest, AllElemMatchBadMixed ) {
+ // $elemMatch first, equality second.
+ BSONObj bad1 = fromjson( "{x: {$all: [{$elemMatch: {y: 1}}, 3]}}" );
+ StatusWithMatchExpression result1 = MatchExpressionParser::parse( bad1 );
+ ASSERT_FALSE( result1.isOK() );
+
+ // equality first, $elemMatch second
+ BSONObj bad2 = fromjson( "{x: {$all: [3, {$elemMatch: {y: 1}}]}}" );
+ StatusWithMatchExpression result2 = MatchExpressionParser::parse( bad2 );
+ ASSERT_FALSE( result1.isOK() );
+
+ // $elemMatch first, object second
+ BSONObj bad3 = fromjson( "{x: {$all: [{$elemMatch: {y: 1}}, {z: 1}]}}" );
+ StatusWithMatchExpression result3 = MatchExpressionParser::parse( bad3 );
+ ASSERT_FALSE( result3.isOK() );
+
+ // object first, $elemMatch second
+ BSONObj bad4 = fromjson( "{x: {$all: [{z: 1}, {$elemMatch: {y: 1}}]}}" );
+ StatusWithMatchExpression result4 = MatchExpressionParser::parse( bad4 );
+ ASSERT_FALSE( result4.isOK() );
+ }
+
// $all with empty string.
TEST( MatchExpressionParserArrayTest, AllEmptyString ) {
BSONObj query = BSON( "x" << BSON( "$all" << BSON_ARRAY( "" ) ) );