From 4199c5029cc91e35432811ec0c514c262acbfb19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sascha=20K=C3=BChl?= Date: Mon, 15 Apr 2024 15:54:37 +0200 Subject: [PATCH] added session test with has one relation --- backends/tests/SessionTest.cpp | 19 ++++--- include/matador/sql/entity_query_builder.hpp | 10 ++-- include/matador/sql/session.hpp | 2 +- src/sql/entity_query_builder.cpp | 2 +- test/EntityQueryBuilderTest.cpp | 57 +++++++++++++++++--- 5 files changed, 69 insertions(+), 21 deletions(-) diff --git a/backends/tests/SessionTest.cpp b/backends/tests/SessionTest.cpp index abd9740..4d4830e 100644 --- a/backends/tests/SessionTest.cpp +++ b/backends/tests/SessionTest.cpp @@ -5,6 +5,7 @@ #include "matador/sql/session.hpp" #include "models/airplane.hpp" +#include "models/flight.hpp" class SessionFixture { @@ -16,9 +17,8 @@ public: ~SessionFixture() { - drop_table_if_exists("flight"); - drop_table_if_exists("airplane"); - drop_table_if_exists("person"); + drop_table_if_exists("flights"); + drop_table_if_exists("airplanes"); } protected: @@ -38,16 +38,19 @@ using namespace matador; TEST_CASE_METHOD(SessionFixture, "Session relation test", "[session][relation]") { using namespace matador; - ses.attach("airplane"); + ses.attach("airplanes"); + ses.attach("flights"); ses.create_schema(); - ses.insert(1, "Boeing", "A380"); + auto plane = ses.insert(1, "Boeing", "A380"); + auto f = ses.insert(2, plane, "sully"); - REQUIRE(true); + auto result = ses.find(2); + REQUIRE(result.is_ok()); } TEST_CASE_METHOD(SessionFixture, "Use session to find object with id", "[session][find]") { using namespace matador::test; - ses.attach("airplane"); + ses.attach("airplanes"); ses.create_schema(); auto a380 = ses.insert(1, "Boeing", "A380"); @@ -64,7 +67,7 @@ TEST_CASE_METHOD(SessionFixture, "Use session to find object with id", "[session TEST_CASE_METHOD(SessionFixture, "Use session to find all objects", "[session][find]") { using namespace matador::test; - ses.attach("airplane"); + ses.attach("airplanes"); ses.create_schema(); std::vector> planes; diff --git a/include/matador/sql/entity_query_builder.hpp b/include/matador/sql/entity_query_builder.hpp index 2ae7149..13e4254 100644 --- a/include/matador/sql/entity_query_builder.hpp +++ b/include/matador/sql/entity_query_builder.hpp @@ -48,8 +48,8 @@ public: template void on_has_many_to_many(const char * /*id*/, ContainerType &/*c*/, const char *join_column, const char *inverse_join_column, const utils::foreign_attributes &/*attr*/) { - join_columns_.join_column = inverse_join_column; - join_columns_.inverse_join_column = join_column; + join_columns_.join_column = join_column; + join_columns_.inverse_join_column = inverse_join_column; } template void on_has_many_to_many(const char * /*id*/, ContainerType &/*c*/, const utils::foreign_attributes &/*attr*/) {} @@ -209,7 +209,7 @@ public: } append_join({table_info_stack_.top().name, table_info_stack_.top().prototype.primary_key()->name()}, {id, join_column}); - append_join({info->name, pk->name()}, {id, inverse_join_column}); + append_join({id, inverse_join_column}, {info->name, pk->name()}); } template @@ -234,8 +234,8 @@ public: const auto join_columns = join_column_collector_.collect(); - append_join({table_info_stack_.top().name, table_info_stack_.top().prototype.primary_key()->name()}, {id, join_columns.join_column}); - append_join({info->name, pk->name()}, {id, join_columns.inverse_join_column}); + append_join({table_info_stack_.top().name, table_info_stack_.top().prototype.primary_key()->name()}, {id, join_columns.inverse_join_column}); + append_join({id, join_columns.join_column}, {info->name, pk->name()}); } private: diff --git a/include/matador/sql/session.hpp b/include/matador/sql/session.hpp index a653930..5dfecb0 100644 --- a/include/matador/sql/session.hpp +++ b/include/matador/sql/session.hpp @@ -150,7 +150,7 @@ entity session::insert(Type *obj) } c->query(*schema_) .insert() - .into(info->name, column_generator::generate(*schema_)) + .into(info->name, column_generator::generate(*schema_, true)) .values(*obj) .execute(); diff --git a/src/sql/entity_query_builder.cpp b/src/sql/entity_query_builder.cpp index 6d96555..b1ff7bc 100644 --- a/src/sql/entity_query_builder.cpp +++ b/src/sql/entity_query_builder.cpp @@ -30,7 +30,7 @@ void entity_query_builder::push(const std::string &column_name) void entity_query_builder::append_join(const column &left, const column &right) { entity_query_data_.joins.push_back({ - { left.table }, + { right.table }, make_condition(left == right) }); } diff --git a/test/EntityQueryBuilderTest.cpp b/test/EntityQueryBuilderTest.cpp index 22a748d..235bf19 100644 --- a/test/EntityQueryBuilderTest.cpp +++ b/test/EntityQueryBuilderTest.cpp @@ -3,14 +3,59 @@ #include #include +#include "models/airplane.hpp" #include "models/author.hpp" #include "models/book.hpp" +#include "models/flight.hpp" #include "models/recipe.hpp" #include "models/order.hpp" #include "models/student.hpp" using namespace matador::sql; +TEST_CASE("Create sql query data for entity with eager has one", "[query][entity][builder]") { + using namespace matador::test; + connection db("noop://noop.db"); + schema scm("noop"); + scm.attach("airplanes"); + scm.attach("flights"); + + entity_query_builder eqb(scm); + + auto data = eqb.build(17); + + 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"("flights"."airplane_id" = "airplanes"."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"("flights"."id" = 17)"); +} + TEST_CASE("Create sql query data for entity with eager belongs to", "[query][entity][builder]") { using namespace matador::test; connection db("noop://noop.db"); @@ -42,7 +87,7 @@ TEST_CASE("Create sql query data for entity with eager belongs to", "[query][ent } std::vector> expected_join_data { - { "books", R"("books"."author_id" = "authors"."id")"} + { "authors", R"("books"."author_id" = "authors"."id")"} }; query_context qc; @@ -108,7 +153,7 @@ TEST_CASE("Create sql query data for entity with eager has many belongs to", "[q } std::vector> expected_join_data { - { "orders", R"("orders"."order_id" = "order_details"."order_id")"} + { "order_details", R"("orders"."order_id" = "order_details"."order_id")"} }; query_context qc; @@ -151,8 +196,8 @@ TEST_CASE("Create sql query data for entity with eager many to many", "[query][e } std::vector> expected_join_data { - { "ingredients", R"("ingredients"."id" = "recipe_ingredients"."ingredient_id")"}, - { "recipes", R"("recipes"."id" = "recipe_ingredients"."recipe_id")"} + { "recipe_ingredients", R"("ingredients"."id" = "recipe_ingredients"."ingredient_id")"}, + { "recipes", R"("recipe_ingredients"."recipe_id" = "recipes"."id")"} }; query_context qc; @@ -195,8 +240,8 @@ TEST_CASE("Create sql query data for entity with eager many to many (inverse par } std::vector> expected_join_data { - { "courses", R"("courses"."id" = "student_courses"."course_id")"}, - { "students", R"("students"."id" = "student_courses"."student_id")"} + { "student_courses", R"("courses"."id" = "student_courses"."course_id")"}, + { "students", R"("student_courses"."student_id" = "students"."id")"} }; query_context qc;