TEST( AndOp, ElemMatchKey ) { BSONObj baseOperand1 = BSON( "a" << 1 ); BSONObj baseOperand2 = BSON( "b" << 2 ); auto_ptr<ComparisonMatchExpression> sub1( new ComparisonMatchExpression() ); ASSERT( sub1->init( "a", ComparisonMatchExpression::EQ, baseOperand1[ "a" ] ).isOK() ); auto_ptr<ComparisonMatchExpression> sub2( new ComparisonMatchExpression() ); ASSERT( sub2->init( "b", ComparisonMatchExpression::EQ, baseOperand2[ "b" ] ).isOK() ); AndMatchExpression andOp; andOp.add( sub1.release() ); andOp.add( sub2.release() ); MatchDetails details; details.requestElemMatchKey(); ASSERT( !andOp.matches( BSON( "a" << BSON_ARRAY( 1 ) ), &details ) ); ASSERT( !details.hasElemMatchKey() ); ASSERT( !andOp.matches( BSON( "b" << BSON_ARRAY( 2 ) ), &details ) ); ASSERT( !details.hasElemMatchKey() ); ASSERT( andOp.matches( BSON( "a" << BSON_ARRAY( 1 ) << "b" << BSON_ARRAY( 1 << 2 ) ), &details ) ); ASSERT( details.hasElemMatchKey() ); // The elem match key for the second $and clause is recorded. ASSERT_EQUALS( "1", details.elemMatchKey() ); }
TEST( AndOp, MatchesThreeClauses ) { BSONObj baseOperand1 = BSON( "$gt" << 1 ); BSONObj baseOperand2 = BSON( "$lt" << 10 ); BSONObj baseOperand3 = BSON( "$lt" << 100 ); auto_ptr<ComparisonMatchExpression> sub1( new ComparisonMatchExpression() ); ASSERT( sub1->init( "a", ComparisonMatchExpression::GT, baseOperand1[ "$gt" ] ).isOK() ); auto_ptr<ComparisonMatchExpression> sub2( new ComparisonMatchExpression() ); ASSERT( sub2->init( "a", ComparisonMatchExpression::LT, baseOperand2[ "$lt" ] ).isOK() ); auto_ptr<ComparisonMatchExpression> sub3( new ComparisonMatchExpression() ); ASSERT( sub3->init( "b", ComparisonMatchExpression::LT, baseOperand3[ "$lt" ] ).isOK() ); AndMatchExpression andOp; andOp.add( sub1.release() ); andOp.add( sub2.release() ); andOp.add( sub3.release() ); ASSERT( andOp.matches( BSON( "a" << 5 << "b" << 6 ), NULL ) ); ASSERT( !andOp.matches( BSON( "a" << 5 ), NULL ) ); ASSERT( !andOp.matches( BSON( "b" << 6 ), NULL ) ); ASSERT( !andOp.matches( BSON( "a" << 1 << "b" << 6 ), NULL ) ); ASSERT( !andOp.matches( BSON( "a" << 10 << "b" << 6 ), NULL ) ); }
TEST( AndOp, MatchesElementThreeClauses ) { BSONObj baseOperand1 = BSON( "$lt" << "z1" ); BSONObj baseOperand2 = BSON( "$gt" << "a1" ); BSONObj match = BSON( "a" << "r1" ); BSONObj notMatch1 = BSON( "a" << "z1" ); BSONObj notMatch2 = BSON( "a" << "a1" ); BSONObj notMatch3 = BSON( "a" << "r" ); auto_ptr<ComparisonMatchExpression> sub1( new ComparisonMatchExpression() ); ASSERT( sub1->init( "a", ComparisonMatchExpression::LT, baseOperand1[ "$lt" ] ).isOK() ); auto_ptr<ComparisonMatchExpression> sub2( new ComparisonMatchExpression() ); ASSERT( sub2->init( "a", ComparisonMatchExpression::GT, baseOperand2[ "$gt" ] ).isOK() ); auto_ptr<RegexMatchExpression> sub3( new RegexMatchExpression() ); ASSERT( sub3->init( "a", "1", "" ).isOK() ); AndMatchExpression andOp; andOp.add( sub1.release() ); andOp.add( sub2.release() ); andOp.add( sub3.release() ); ASSERT( andOp.matches( match ) ); ASSERT( !andOp.matches( notMatch1 ) ); ASSERT( !andOp.matches( notMatch2 ) ); ASSERT( !andOp.matches( notMatch3 ) ); }
TEST( AndOp, MatchesSingleClause ) { BSONObj baseOperand = BSON( "$ne" << 5 ); auto_ptr<ComparisonMatchExpression> ne( new ComparisonMatchExpression() ); ASSERT( ne->init( "a", ComparisonMatchExpression::NE, baseOperand[ "$ne" ] ).isOK() ); AndMatchExpression andOp; andOp.add( ne.release() ); ASSERT( andOp.matches( BSON( "a" << 4 ), NULL ) ); ASSERT( andOp.matches( BSON( "a" << BSON_ARRAY( 4 << 6 ) ), NULL ) ); ASSERT( !andOp.matches( BSON( "a" << 5 ), NULL ) ); ASSERT( !andOp.matches( BSON( "a" << BSON_ARRAY( 4 << 5 ) ), NULL ) ); }
TEST( AndOp, NoClauses ) { AndMatchExpression andMatchExpression; ASSERT( andMatchExpression.matches( BSONObj(), NULL ) ); }