TEST(AndOp, ElemMatchKey) { BSONObj baseOperand1 = BSON("a" << 1); BSONObj baseOperand2 = BSON("b" << 2); unique_ptr<ComparisonMatchExpression> sub1(new EqualityMatchExpression()); ASSERT(sub1->init("a", baseOperand1["a"]).isOK()); unique_ptr<ComparisonMatchExpression> sub2(new EqualityMatchExpression()); ASSERT(sub2->init("b", baseOperand2["b"]).isOK()); AndMatchExpression andOp; andOp.add(sub1.release()); andOp.add(sub2.release()); MatchDetails details; details.requestElemMatchKey(); ASSERT(!andOp.matchesBSON(BSON("a" << BSON_ARRAY(1)), &details)); ASSERT(!details.hasElemMatchKey()); ASSERT(!andOp.matchesBSON(BSON("b" << BSON_ARRAY(2)), &details)); ASSERT(!details.hasElemMatchKey()); ASSERT(andOp.matchesBSON(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); unique_ptr<ComparisonMatchExpression> sub1(new GTMatchExpression()); ASSERT(sub1->init("a", baseOperand1["$gt"]).isOK()); unique_ptr<ComparisonMatchExpression> sub2(new LTMatchExpression()); ASSERT(sub2->init("a", baseOperand2["$lt"]).isOK()); unique_ptr<ComparisonMatchExpression> sub3(new LTMatchExpression()); ASSERT(sub3->init("b", baseOperand3["$lt"]).isOK()); AndMatchExpression andOp; andOp.add(sub1.release()); andOp.add(sub2.release()); andOp.add(sub3.release()); ASSERT(andOp.matchesBSON(BSON("a" << 5 << "b" << 6), NULL)); ASSERT(!andOp.matchesBSON(BSON("a" << 5), NULL)); ASSERT(!andOp.matchesBSON(BSON("b" << 6), NULL)); ASSERT(!andOp.matchesBSON(BSON("a" << 1 << "b" << 6), NULL)); ASSERT(!andOp.matchesBSON(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"); unique_ptr<ComparisonMatchExpression> sub1(new LTMatchExpression()); ASSERT(sub1->init("a", baseOperand1["$lt"]).isOK()); unique_ptr<ComparisonMatchExpression> sub2(new GTMatchExpression()); ASSERT(sub2->init("a", baseOperand2["$gt"]).isOK()); unique_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.matchesBSON(match)); ASSERT(!andOp.matchesBSON(notMatch1)); ASSERT(!andOp.matchesBSON(notMatch2)); ASSERT(!andOp.matchesBSON(notMatch3)); }
TEST(AndOp, MatchesSingleClause) { BSONObj baseOperand = BSON("$ne" << 5); unique_ptr<ComparisonMatchExpression> eq(new EqualityMatchExpression()); ASSERT(eq->init("a", baseOperand["$ne"]).isOK()); unique_ptr<NotMatchExpression> ne(new NotMatchExpression()); ASSERT(ne->init(eq.release()).isOK()); AndMatchExpression andOp; andOp.add(ne.release()); ASSERT(andOp.matchesBSON(BSON("a" << 4), NULL)); ASSERT(andOp.matchesBSON(BSON("a" << BSON_ARRAY(4 << 6)), NULL)); ASSERT(!andOp.matchesBSON(BSON("a" << 5), NULL)); ASSERT(!andOp.matchesBSON(BSON("a" << BSON_ARRAY(4 << 5)), NULL)); }
TEST(AndOp, NoClauses) { AndMatchExpression andMatchExpression; ASSERT(andMatchExpression.matchesBSON(BSONObj(), NULL)); }