Skip to content

Commit f92dece

Browse files
committed
CXX-844 Refactor existing aggregation integration tests
Simplify existing aggregation tests, and gather them into a single "aggregation" section. This will make it easy to add new integration test coverage for more pipeline methods.
1 parent 06dc612 commit f92dece

File tree

1 file changed

+42
-79
lines changed

1 file changed

+42
-79
lines changed

src/mongocxx/test/collection.cpp

Lines changed: 42 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -990,89 +990,52 @@ TEST_CASE("CRUD functionality", "[driver::collection]") {
990990
}
991991
}
992992

993-
SECTION("aggregate some things", "[collection]") {
994-
document b1;
995-
b1 << "x" << 1;
996-
997-
document b2;
998-
b2 << "x" << 2;
993+
SECTION("aggregation", "[collection]") {
994+
pipeline pipeline;
995+
996+
auto get_results = [](cursor&& cursor) {
997+
std::vector<bsoncxx::document::value> results;
998+
std::transform(cursor.begin(), cursor.end(), std::back_inserter(results),
999+
[](bsoncxx::document::view v) { return bsoncxx::document::value{v}; });
1000+
return results;
1001+
};
9991002

1000-
coll.insert_one(b1.view());
1001-
coll.insert_one(b2.view());
1002-
coll.insert_one(b2.view());
1003+
SECTION("lookup") {
1004+
coll.insert_one(document{} << "x" << 0 << finalize);
1005+
coll.insert_one(document{} << "x" << 1 << "y" << 0 << finalize);
1006+
1007+
pipeline.lookup(document{} << "from" << coll.name() << "localField"
1008+
<< "x"
1009+
<< "foreignField"
1010+
<< "y"
1011+
<< "as"
1012+
<< "z" << finalize);
1013+
// Add a sort to the pipeline, so below tests can make assumptions about result order.
1014+
pipeline.sort(document{} << "x" << 1 << finalize);
1015+
auto cursor = coll.aggregate(pipeline);
1016+
1017+
if (test_util::get_max_wire_version(mongodb_client) >= 4) {
1018+
// The server supports lookup().
1019+
auto results = get_results(std::move(cursor));
1020+
REQUIRE(results.size() == 2);
1021+
REQUIRE(!results[0].view()["z"].get_array().value.empty());
1022+
REQUIRE(results[1].view()["z"].get_array().value.empty());
1023+
} else {
1024+
// The server does not support lookup().
1025+
REQUIRE_THROWS_AS(get_results(std::move(cursor)), operation_exception);
1026+
}
1027+
}
10031028

1004-
pipeline p;
1005-
p.match(b1.view());
1029+
SECTION("match") {
1030+
coll.insert_one(document{} << "x" << 1 << finalize);
1031+
coll.insert_one(document{} << "x" << 1 << finalize);
1032+
coll.insert_one(document{} << "x" << 2 << finalize);
10061033

1007-
auto results = coll.aggregate(p);
1008-
}
1034+
pipeline.match(document{} << "x" << 1 << finalize);
1035+
auto cursor = coll.aggregate(pipeline);
10091036

1010-
SECTION("aggregation $lookup operator", "[collection]") {
1011-
auto people_coll_name = "people_on_the_block";
1012-
auto people_coll = db[people_coll_name];
1013-
auto houses_coll_name = "houses_on_the_block";
1014-
auto houses_coll = db[houses_coll_name];
1015-
houses_coll.drop();
1016-
people_coll.drop();
1017-
1018-
// populate one collection with names
1019-
document name1;
1020-
name1 << "firstname"
1021-
<< "Tasha"
1022-
<< "lastname"
1023-
<< "Brown";
1024-
document name2;
1025-
name2 << "firstname"
1026-
<< "Logan"
1027-
<< "lastname"
1028-
<< "Brown";
1029-
document name3;
1030-
name3 << "firstname"
1031-
<< "Tasha"
1032-
<< "lastname"
1033-
<< "Johnson";
1034-
1035-
people_coll.insert_one(name1.view());
1036-
people_coll.insert_one(name2.view());
1037-
people_coll.insert_one(name3.view());
1038-
1039-
// populate the other with addresses
1040-
document address1;
1041-
address1 << "household"
1042-
<< "Brown"
1043-
<< "address"
1044-
<< "23 Prince St";
1045-
document address2;
1046-
address2 << "household"
1047-
<< "Johnson"
1048-
<< "address"
1049-
<< "15 Prince St";
1050-
1051-
houses_coll.insert_one(address1.view());
1052-
houses_coll.insert_one(address2.view());
1053-
1054-
// perform a $lookup
1055-
document lookup_doc;
1056-
lookup_doc << "from" << people_coll_name << "localField"
1057-
<< "household"
1058-
<< "foreignField"
1059-
<< "lastname"
1060-
<< "as"
1061-
<< "residents";
1062-
1063-
pipeline stages;
1064-
stages.lookup(lookup_doc.view());
1065-
1066-
auto results = houses_coll.aggregate(stages);
1067-
1068-
if (test_util::get_max_wire_version(mongodb_client) >= 4) {
1069-
// The server supports $lookup.
1070-
//
1071-
// Should have two result documents, one per household.
1072-
REQUIRE(std::distance(results.begin(), results.end()) == 2);
1073-
} else {
1074-
// The server does not support $lookup.
1075-
REQUIRE_THROWS_AS(std::distance(results.begin(), results.end()), operation_exception);
1037+
auto results = get_results(std::move(cursor));
1038+
REQUIRE(results.size() == 2);
10761039
}
10771040
}
10781041

0 commit comments

Comments
 (0)