diff --git a/test/backends/QueryTest.cpp b/test/backends/QueryTest.cpp index 131a8af..6bc2155 100644 --- a/test/backends/QueryTest.cpp +++ b/test/backends/QueryTest.cpp @@ -482,10 +482,8 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with many to many relationship" select_result = query::select({r.id, r.name, ri.ingredient_id, i.name}) .from(r) - .join_left(ri) - .on(r.id == ri.recipe_id) - .join_left(i) - .on(ri.ingredient_id == i.id) + .join_left(ri).on(r.id == ri.recipe_id) + .join_left(i).on(ri.ingredient_id == i.id) .fetch_all(db); REQUIRE(result.is_ok()); diff --git a/test/backends/SessionTest.cpp b/test/backends/SessionTest.cpp index 2152acd..c769dab 100644 --- a/test/backends/SessionTest.cpp +++ b/test/backends/SessionTest.cpp @@ -17,9 +17,7 @@ using namespace matador::test; TEST_CASE_METHOD(SessionFixture, "Session insert test", "[session][insert]") { const auto result = schema.attach("airplanes") - .and_then([this] { - return schema.create(db); - } ); + .and_then([this] { return schema.create(db); } ); REQUIRE(result.is_ok()); auto plane = ses.insert(make_object(1, "Boeing", "A380")); @@ -35,9 +33,7 @@ TEST_CASE_METHOD(SessionFixture, "Session insert test", "[session][insert]") { TEST_CASE_METHOD(SessionFixture, "Session update test", "[session][update]") { const auto res = schema.attach("airplanes") - .and_then([this] { - return schema.create(db); - } ); + .and_then([this] { return schema.create(db); } ); REQUIRE(res.is_ok()); auto result = ses.insert(make_object(1, "Boeing", "A380")); @@ -131,9 +127,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]") { const auto result = schema.attach("airplanes") - .and_then([this] { - return schema.create(db); - } ); + .and_then([this] { return schema.create(db); } ); REQUIRE(result.is_ok()); std::vector planes { @@ -187,7 +181,7 @@ TEST_CASE_METHOD(SessionFixture, "Use session to find all objects with one-to-ma auto all_authors = find_result.release(); std::vector> author_repo; for (auto it = all_authors.begin(); it != all_authors.end(); ++it) { - REQUIRE(it->books.size() == 0); + REQUIRE(it->books.empty()); author_repo.emplace_back(it.optr()); } REQUIRE(author_repo.size() == 2); @@ -240,7 +234,7 @@ TEST_CASE_METHOD(SessionFixture, "Use session to find all objects with one-to-ma auto all_departments = find_result.release(); std::vector> departments_repo; for (auto it = all_departments.begin(); it != all_departments.end(); ++it) { - std::cout << "department: " << it->name << " (employees: " << it->employees.size() << ")\n"; + REQUIRE(it->employees.empty()); departments_repo.emplace_back(it.optr()); } REQUIRE(departments_repo.size() == 2); @@ -268,10 +262,10 @@ TEST_CASE_METHOD(SessionFixture, "Use session to find all objects with one-to-ma all_departments = find_result.release(); for (auto it = all_departments.begin(); it != all_departments.end(); ++it) { - std::cout << "department: " << it->name << " (id: " << it->id << ", employees: " << it->employees.size() << ")\n"; - for (const auto& emp : it->employees) { - std::cout << "\temployee: " << emp->first_name << " " << emp->last_name << "( " << emp->id << ")\n"; - } + REQUIRE(it->employees.size() == 5); + // for (const auto& emp : it->employees) { + // REQUIRE(emp->dep->id == it->id); + // } } } @@ -303,6 +297,40 @@ TEST_CASE_METHOD(SessionFixture, "Use session to find all objects with many-to-m make_object(3, "Pineapple Pie", std::vector{ingredients[0], ingredients[1], ingredients[2]}) }; + for (auto &r: recipes) { + auto res = ses.insert(r); + REQUIRE(res.is_ok()); + } +} + +TEST_CASE_METHOD(SessionFixture, "Use session to find all objects with many-to-many lazy relation", "[session][find][many-to-many][lazy]") { + auto result = schema.attach("recipes") + .and_then( [this] { return schema.attach("ingredients"); } ) + .and_then([this] { + return schema.create(db); + } ); + + std::vector ingredients { + make_object(1, "Apple"), + make_object(2, "Strawberry"), + make_object(3, "Pineapple"), + make_object(4, "Sugar"), + make_object(5, "Flour"), + make_object(6, "Butter"), + make_object(7, "Beans") + }; + + for (auto &i: ingredients) { + auto res = ses.insert(i); + REQUIRE(res.is_ok()); + } + + std::vector recipes { + make_object(1, "Apple Pie", std::vector{ingredients[0], ingredients[3], ingredients[4]}), + make_object(2, "Strawberry Cake", std::vector{ingredients[5], ingredients[6]}), + make_object(3, "Pineapple Pie", std::vector{ingredients[0], ingredients[1], ingredients[2]}) + }; + for (auto &r: recipes) { auto res = ses.insert(r); REQUIRE(res.is_ok());