added missing checks to QueryTest

This commit is contained in:
Sascha Kuehl 2024-03-29 11:41:37 +01:00
parent 6f3e589e10
commit 31e9c7e9ac
1 changed files with 45 additions and 11 deletions

View File

@ -456,43 +456,77 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with many to many relationship"
} }
auto result = db auto result = db
.query(schema) .query(schema)
.select({"r.id", "r.name", "ri.recipe_id"}) .select({"r.id", "r.name", "ri.ingredient_id"})
.from({"recipes", "r"}) .from({"recipes", "r"})
.join_left({"recipe_ingredients", "ri"}) .join_left({"recipe_ingredients", "ri"})
.on("r.id"_col == "ri.recipe_id"_col) .on("r.id"_col == "ri.recipe_id"_col)
.fetch_all(); .fetch_all();
std::vector<std::tuple<unsigned long, std::string, unsigned long>> expected_result_one_join {
{7, "Apple Crumble", 1},
{7, "Apple Crumble", 4},
{7, "Apple Crumble", 5},
{8, "Beans Chili", 6},
{8, "Beans Chili", 7},
{9, "Fruit Salad", 1},
{9, "Fruit Salad", 2},
{9, "Fruit Salad", 3}
};
size_t index{0};
for (const auto &r: result) { for (const auto &r: result) {
REQUIRE(r.size() == 3); REQUIRE(r.size() == 3);
std::cout << "record r.id " << r.at(0).as<unsigned long>().value() << " r.name " << r.at(1).as<std::string>().value() << " ri.id " << r.at(2).as<unsigned long>().value() << "\n"; REQUIRE(r.at(0).as<unsigned long>().value() == std::get<0>(expected_result_one_join[index]));
REQUIRE(r.at(1).as<std::string>().value() == std::get<1>(expected_result_one_join[index]));
REQUIRE(r.at(2).as<unsigned long>().value() == std::get<2>(expected_result_one_join[index]));
++index;
} }
result = db result = db
.query(schema) .query(schema)
.select({"r.id", "r.name", "ri.recipe_id", "i.name"}) .select({"r.id", "r.name", "ri.ingredient_id", "i.name"})
.from({"recipes", "r"}) .from({"recipes", "r"})
.join_left({"recipe_ingredients", "ri"}).on("r.id"_col == "ri.recipe_id"_col) .join_left({"recipe_ingredients", "ri"}).on("r.id"_col == "ri.recipe_id"_col)
.join_left({"ingredients", "i"}).on("ri.ingredient_id"_col == "i.id"_col) .join_left({"ingredients", "i"}).on("ri.ingredient_id"_col == "i.id"_col)
.fetch_all(); .fetch_all();
std::vector<std::tuple<unsigned long, std::string, unsigned long, std::string>> expected_result_two_joins {
{7, "Apple Crumble", 1, "Apple"},
{7, "Apple Crumble", 4, "Sugar"},
{7, "Apple Crumble", 5, "Flour"},
{8, "Beans Chili", 6, "Butter"},
{8, "Beans Chili", 7, "Beans"},
{9, "Fruit Salad", 1, "Apple"},
{9, "Fruit Salad", 2, "Strawberry"},
{9, "Fruit Salad", 3, "Pineapple"}
};
index = 0;
for (const auto &r: result) { for (const auto &r: result) {
REQUIRE(r.size() == 4); REQUIRE(r.size() == 4);
std::cout << "record r.id " << r.at(0).as<unsigned long>().value() << " r.name " << r.at(1).as<std::string>().value() << " ri.id " << r.at(2).as<unsigned long>().value() << " i.name " << r.at(3).as<std::string>().value() << "\n"; REQUIRE(r.at(0).as<unsigned long>().value() == std::get<0>(expected_result_two_joins[index]));
REQUIRE(r.at(1).as<std::string>().value() == std::get<1>(expected_result_two_joins[index]));
REQUIRE(r.at(2).as<unsigned long>().value() == std::get<2>(expected_result_two_joins[index]));
REQUIRE(r.at(3).as<std::string>().value() == std::get<3>(expected_result_two_joins[index]));
++index;
} }
result = db result = db
.query(schema) .query(schema)
.select({"r.id", "r.name", "ri.recipe_id", "i.name"}) .select({"r.id", "r.name", "ri.ingredient_id", "i.name"})
.from({"recipes", "r"}) .from({"recipes", "r"})
.join_left({"recipe_ingredients", "ri"}).on("r.id"_col == "ri.recipe_id"_col) .join_left({"recipe_ingredients", "ri"}).on("r.id"_col == "ri.recipe_id"_col)
.join_left({"ingredients", "i"}).on("ri.ingredient_id"_col == "i.id"_col) .join_left({"ingredients", "i"}).on("ri.ingredient_id"_col == "i.id"_col)
.where("r.id"_col == 8) .where("r.id"_col == 8)
.fetch_all(); .fetch_all();
index = 3;
for (const auto &r: result) { for (const auto &r: result) {
REQUIRE(r.size() == 4); REQUIRE(r.size() == 4);
std::cout << "record r.id " << r.at(0).as<unsigned long>().value() << " r.name " << r.at(1).as<std::string>().value() << " ri.id " << r.at(2).as<unsigned long>().value() << " i.name " << r.at(3).as<std::string>().value() << "\n"; REQUIRE(r.at(0).as<unsigned long>().value() == std::get<0>(expected_result_two_joins[index]));
REQUIRE(r.at(1).as<std::string>().value() == std::get<1>(expected_result_two_joins[index]));
REQUIRE(r.at(2).as<unsigned long>().value() == std::get<2>(expected_result_two_joins[index]));
REQUIRE(r.at(3).as<std::string>().value() == std::get<3>(expected_result_two_joins[index]));
++index;
} }
db.query(schema).drop().table("recipe_ingredients").execute(); db.query(schema).drop().table("recipe_ingredients").execute();