#include #include #include #include #include "models/author.hpp" #include "models/book.hpp" #include "models/recipe.hpp" #include "models/order.hpp" using namespace matador::sql; TEST_CASE("Create sql query data for entity with eager belongs to", "[query][entity][builder]") { using namespace matador::test; connection db("noop://noop.db"); schema scm("noop"); scm.attach("authors"); scm.attach("books"); entity_query_builder eqb(17, scm); auto data = eqb.build(); REQUIRE(data.has_value()); REQUIRE(data->root_table_name == "books"); REQUIRE(data->joins.size() == 1); REQUIRE(data->columns.size() == 9); REQUIRE(data->where_clause); std::vector> expected_join_data { { "books", "books.author_id = authors.id"} }; query_context qc; size_t index{0}; for (const auto &jd : data->joins) { REQUIRE(jd.join_table.name == expected_join_data[index].first); REQUIRE(jd.condition->evaluate(db.dialect(), qc) == expected_join_data[index].second); ++index; } auto cond = data->where_clause->evaluate(db.dialect(), qc); REQUIRE(cond == "books.id = 17"); auto &jd = data->joins.front(); auto context = db.query(scm) .select(data->columns) .from(data->root_table_name) .join_left(jd.join_table).on(std::move(jd.condition)) .where(std::move(data->where_clause)) .build(); } TEST_CASE("Create sql query data for entity with eager has many belongs to", "[query][entity][builder]") { using namespace matador::test; connection db("noop://noop.db"); schema scm("noop"); scm.attach("products"); scm.attach("order_details"); scm.attach("orders"); entity_query_builder eqb(17, scm); auto data = eqb.build(); REQUIRE(data.has_value()); REQUIRE(data->root_table_name == "orders"); REQUIRE(data->joins.size() == 1); REQUIRE(data->columns.size() == 15); REQUIRE(data->where_clause); std::vector> expected_join_data { { "orders", "orders.order_id = order_details.order_id"} }; query_context qc; size_t index{0}; for (const auto &jd : data->joins) { REQUIRE(jd.join_table.name == expected_join_data[index].first); REQUIRE(jd.condition->evaluate(db.dialect(), qc) == expected_join_data[index].second); ++index; } auto cond = data->where_clause->evaluate(db.dialect(), qc); REQUIRE(cond == "orders.order_id = 17"); } TEST_CASE("Create sql query data for entity with eager many to many", "[query][entity][builder]") { using namespace matador::test; connection db("noop://noop.db"); schema scm("noop"); scm.attach("recipes"); scm.attach("ingredients"); scm.attach("recipe_ingredients"); entity_query_builder eqb(17, scm); auto data = eqb.build(); REQUIRE(data.has_value()); REQUIRE(data->root_table_name == "ingredients"); REQUIRE(data->joins.size() == 2); REQUIRE(data->columns.size() == 4); REQUIRE(data->where_clause); std::vector> expected_join_data { { "ingredients", "ingredients.id = recipe_ingredients.ingredient_id"}, { "recipes", "recipes.id = recipe_ingredients.recipe_id"} }; query_context qc; size_t index{0}; for (const auto &jd : data->joins) { REQUIRE(jd.join_table.name == expected_join_data[index].first); REQUIRE(jd.condition->evaluate(db.dialect(), qc) == expected_join_data[index].second); ++index; } auto cond = data->where_clause->evaluate(db.dialect(), qc); REQUIRE(cond == "ingredients.id = 17"); }