diff --git a/test/orm/orm/SessionInsertBuilderTest.cpp b/test/orm/orm/SessionInsertBuilderTest.cpp new file mode 100644 index 0000000..024f56b --- /dev/null +++ b/test/orm/orm/SessionInsertBuilderTest.cpp @@ -0,0 +1,306 @@ +#include +#include + +#include "matador/sql/backend_provider.hpp" +#include "matador/sql/connection.hpp" +#include "matador/sql/column.hpp" +#include "matador/sql/table.hpp" + +#include "matador/query/query.hpp" + +#include "matador/orm/session_query_builder.hpp" + +#include "../backend/test_connection.hpp" +#include "../backend/test_backend_service.hpp" + +#include "../../models/airplane.hpp" +#include "../../models/author.hpp" +#include "../../models/department.hpp" +#include "../../models/book.hpp" +#include "../../models/flight.hpp" +#include "../../models/recipe.hpp" +#include "../../models/order.hpp" +#include "../../models/student.hpp" + +using namespace matador::object; +using namespace matador::orm; +using namespace matador::query; +using namespace matador::sql; +using namespace matador::test; + +TEST_CASE("Create sql query data for entity with eager has one", "[query][entity][builder]") { + using namespace matador::test; + backend_provider::instance().register_backend("noop", std::make_unique()); + connection db("noop://noop.db"); + schema scm("noop"); + auto result = scm.attach("airplanes") + .and_then( [&scm] { return scm.attach("flights"); } ); + REQUIRE(result); + + session_query_builder eqb(scm); + + auto data = eqb.build(17U); + + REQUIRE(data.is_ok()); + REQUIRE(data->root_table->name == "flights"); + REQUIRE(data->joins.size() == 1); + const std::vector expected_columns { + { "flights", "id", "c01" }, + { "airplanes", "id", "c02" }, + { "airplanes", "brand", "c03" }, + { "airplanes", "model", "c04" }, + { "flights", "pilot_name", "c05" }, + }; + REQUIRE(data->columns.size() == expected_columns.size()); + for (size_t i = 0; i != expected_columns.size(); ++i) { + REQUIRE(expected_columns[i].equals(data->columns[i])); + } + + std::vector> expected_join_data { + { "airplanes", R"("t01"."airplane_id" = "t02"."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; + } + + REQUIRE(data->where_clause); + auto cond = data->where_clause->evaluate(db.dialect(), qc); + REQUIRE(cond == R"("t01"."id" = 17)"); +} + +TEST_CASE("Create sql query data for entity with eager belongs to", "[query][entity][builder]") { + using namespace matador::test; + backend_provider::instance().register_backend("noop", std::make_unique()); + connection db("noop://noop.db"); + schema scm("noop"); + auto result = scm.attach("authors") + .and_then( [&scm] { return scm.attach("books"); } ); + REQUIRE(result); + + session_query_builder eqb(scm); + + auto data = eqb.build(17); + + REQUIRE(data.is_ok()); + REQUIRE(data->root_table->name == "books"); + REQUIRE(data->joins.size() == 1); + const std::vector expected_columns { + { "books", "id", "c01" }, + { "books", "title", "c02" }, + { "authors", "id", "c03" }, + { "authors", "first_name", "c04" }, + { "authors", "last_name", "c05" }, + { "authors", "date_of_birth", "c06" }, + { "authors", "year_of_birth", "c07" }, + { "authors", "distinguished", "c08" }, + { "books", "published_in", "c09" } + }; + REQUIRE(data->columns.size() == expected_columns.size()); + for (size_t i = 0; i != expected_columns.size(); ++i) { + REQUIRE(expected_columns[i].equals(data->columns[i])); + } + + std::vector> expected_join_data { + { "authors", R"("t01"."author_id" = "t02"."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; + } + + REQUIRE(data->where_clause); + auto cond = data->where_clause->evaluate(db.dialect(), qc); + REQUIRE(cond == R"("t01"."id" = 17)"); + + auto q = matador::query::query::select(data->columns) + .from(data->root_table->name); + + for (auto &jd : data->joins) { + q.join_left(*jd.join_table) + .on(std::move(jd.condition)); + } + auto context = q + .where(std::move(data->where_clause)) + .str(db); +} + +TEST_CASE("Create sql query data for entity with eager has many belongs to", "[query][entity][builder]") { + using namespace matador::test; + backend_provider::instance().register_backend("noop", std::make_unique()); + connection db("noop://noop.db"); + schema scm("noop"); + auto result = scm.attach("products") + .and_then( [&scm] { return scm.attach("order_details"); } ) + .and_then( [&scm] { return scm.attach("suppliers"); } ) + .and_then( [&scm] { return scm.attach("categories"); } ) + .and_then( [&scm] { return scm.attach("orders"); } ); + REQUIRE(result); + + session_query_builder eqb(scm); + + auto data = eqb.build(17); + + REQUIRE(data.is_ok()); + REQUIRE(data->root_table->name == "orders"); + REQUIRE(data->joins.size() == 1); + const std::vector expected_columns = { + { "orders", "order_id", "c01" }, + { "orders", "order_date", "c02" }, + { "orders", "required_date", "c03" }, + { "orders", "shipped_date", "c04" }, + { "orders", "ship_via", "c05" }, + { "orders", "freight", "c06" }, + { "orders", "ship_name", "c07" }, + { "orders", "ship_address", "c08" }, + { "orders", "ship_city", "c09" }, + { "orders", "ship_region", "c10" }, + { "orders", "ship_postal_code", "c11" }, + { "orders", "ship_country", "c12" }, + { "order_details", "order_details_id", "c13" }, + { "order_details", "order_id", "c14" }, + { "order_details", "product_id", "c15" } + }; + REQUIRE(data->columns.size() == expected_columns.size()); + for (size_t i = 0; i != expected_columns.size(); ++i) { + REQUIRE(expected_columns[i].equals(data->columns[i])); + } + + std::vector> expected_join_data { + { "order_details", R"("t01"."order_id" = "t02"."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; + } + + REQUIRE(data->where_clause); + auto cond = data->where_clause->evaluate(db.dialect(), qc); + REQUIRE(cond == R"("t01"."order_id" = 17)"); +} + +TEST_CASE("Create sql query data for entity with eager many to many", "[query][entity][builder]") { + using namespace matador::test; + backend_provider::instance().register_backend("noop", std::make_unique()); + connection db("noop://noop.db"); + schema scm("noop"); + auto result = scm.attach("recipes") + .and_then( [&scm] { return scm.attach("ingredients"); } ) + .and_then( [&scm] { return scm.attach("recipe_ingredients"); } ); + + session_query_builder eqb(scm); + + auto data = eqb.build(17); + + REQUIRE(data.is_ok()); + REQUIRE(data->root_table->name == "ingredients"); + REQUIRE(data->joins.size() == 2); + const std::vector expected_columns { + { "ingredients", "id", "c01" }, + { "ingredients", "name", "c02" }, + { "recipes", "id", "c03" }, + { "recipes", "name", "c04" } + }; + REQUIRE(data->columns.size() == expected_columns.size()); + for (size_t i = 0; i != expected_columns.size(); ++i) { + REQUIRE(expected_columns[i].equals(data->columns[i])); + } + + std::vector> expected_join_data { + { "recipe_ingredients", R"("t01"."id" = "t02"."ingredient_id")"}, + { "recipes", R"("t02"."recipe_id" = "t03"."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; + } + + REQUIRE(data->where_clause); + auto cond = data->where_clause->evaluate(db.dialect(), qc); + REQUIRE(cond == R"("t01"."id" = 17)"); +} + +TEST_CASE("Create sql query data for entity with eager many to many (inverse part)", "[query][entity][builder]") { + using namespace matador::test; + backend_provider::instance().register_backend("noop", std::make_unique()); + connection db("noop://noop.db"); + schema scm("noop"); + auto result = scm.attach("students") + .and_then( [&scm] { return scm.attach("courses"); } ) + .and_then( [&scm] { return scm.attach("student_courses"); } ); + + session_query_builder eqb(scm); + + auto data = eqb.build(17); + + REQUIRE(data.is_ok()); + REQUIRE(data->root_table->name == "courses"); + REQUIRE(data->joins.size() == 2); + const std::vector expected_columns { + { "courses", "id", "c01" }, + { "courses", "title", "c02" }, + { "students", "id", "c03" }, + { "students", "name", "c04" } + }; + REQUIRE(data->columns.size() == expected_columns.size()); + for (size_t i = 0; i != expected_columns.size(); ++i) { + REQUIRE(expected_columns[i].equals(data->columns[i])); + } + + std::vector> expected_join_data { + { "student_courses", R"("t01"."id" = "t02"."course_id")"}, + { "students", R"("t02"."student_id" = "t03"."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; + } + + REQUIRE(data->where_clause); + auto cond = data->where_clause->evaluate(db.dialect(), qc); + REQUIRE(cond == R"("t01"."id" = 17)"); +} + +TEST_CASE("Test eager relationship", "[session][eager]") { + using namespace matador::test; + backend_provider::instance().register_backend("noop", std::make_unique()); + connection db("noop://noop.db"); + schema scm("noop"); + auto result = scm.attach("departments") + .and_then( [&scm] { return scm.attach("employees"); } ); + + session_query_builder eqb(scm); + + auto data = eqb.build(); + REQUIRE(data.is_ok()); + + auto ctx = query::select(data->columns) + .from(*data->root_table) + .join_left(data->joins) + .where(std::move(data->where_clause)) + .order_by(column{data->root_table, data->pk_column_name}) + .asc() + .str(db); + + std::cout << ctx << std::endl; +} \ No newline at end of file