#include "catch2/catch_test_macros.hpp" #include "SessionFixture.hpp" #include "models/airplane.hpp" #include "models/author.hpp" #include "models/book.hpp" #include "models/department.hpp" #include "models/flight.hpp" #include "models/recipe.hpp" #include using namespace matador; using namespace matador::object; using namespace matador::test; TEST_CASE_METHOD(SessionFixture, "Session insert test", "[session][insert]") { const auto result = ses.attach("airplanes") .and_then([this] { return ses.create_schema(); } ); REQUIRE(result.is_ok()); tables_to_drop.emplace("airplanes"); auto plane = ses.insert(1, "Boeing", "A380"); REQUIRE(plane.is_ok()); const auto res = ses.find(1); REQUIRE(res.is_ok()); const auto& read_airplane = *res; REQUIRE(read_airplane->id == (*plane)->id); REQUIRE(read_airplane->brand == (*plane)->brand); REQUIRE(read_airplane->model == (*plane)->model); } TEST_CASE_METHOD(SessionFixture, "Session update test", "[session][update]") { const auto res = ses.attach("airplanes") .and_then([this] { return ses.create_schema(); } ); REQUIRE(res.is_ok()); tables_to_drop.emplace("airplanes"); auto result = ses.insert(1, "Boeing", "747"); REQUIRE(result.is_ok()); const auto plane = result.value(); result = ses.find(1); REQUIRE(result.is_ok()); const auto& read_airplane = *result; REQUIRE(read_airplane->id == plane->id); REQUIRE(read_airplane->brand == plane->brand); REQUIRE(read_airplane->model == plane->model); read_airplane->brand = "Airbus"; read_airplane->model = "A380"; auto update_result = ses.update(read_airplane); } TEST_CASE_METHOD(SessionFixture, "Session relation test", "[session][relation]") { const auto result = ses.attach("airplanes") .and_then([this] { return ses.attach("flights"); } ) .and_then([this] { return ses.create_schema(); } ); REQUIRE(result.is_ok()); tables_to_drop.emplace("airplanes"); tables_to_drop.emplace("flights"); auto plane = ses.insert(1, "Boeing", "A380"); REQUIRE(plane.is_ok()); auto f = ses.insert(2, *plane, "sully"); REQUIRE(f.is_ok()); const auto res = ses.find(2); REQUIRE(res.is_ok()); const auto& rf = *res; REQUIRE(rf->id == (*f)->id); REQUIRE(rf->pilot_name == (*f)->pilot_name); REQUIRE(rf->plane); REQUIRE(rf->plane->id == (*plane)->id); REQUIRE(rf->plane->brand == (*plane)->brand); REQUIRE(rf->plane->model == (*plane)->model); } TEST_CASE_METHOD(SessionFixture, "Use session to find object with id", "[session][find]") { const auto result = ses.attach("airplanes") .and_then([this] { return ses.create_schema(); } ); REQUIRE(result.is_ok()); tables_to_drop.emplace("airplanes"); auto a380 = ses.insert(1, "Boeing", "A380"); REQUIRE(a380.is_ok()); auto find_result = ses.find(2); REQUIRE(!find_result.is_ok()); REQUIRE((find_result.err().ec() == orm::error_code::FailedToFindObject)); find_result = ses.find(1); REQUIRE(find_result); auto read_a380 = find_result.value(); REQUIRE((*a380)->id == read_a380->id); } TEST_CASE_METHOD(SessionFixture, "Use session to find all objects", "[session][find]") { const auto result = ses.attach("airplanes") .and_then([this] { return ses.create_schema(); } ); REQUIRE(result.is_ok()); tables_to_drop.emplace("airplanes"); std::vector> planes; planes.emplace_back(new airplane(1, "Airbus", "A380")); planes.emplace_back(new airplane(2, "Boeing", "707")); planes.emplace_back(new airplane(3, "Boeing", "747")); for (auto &&plane: planes) { auto res = ses.insert(plane.release()); REQUIRE(res.is_ok()); } auto find_result = ses.find(); std::vector> expected_result { {1, "Airbus", "A380"}, {2, "Boeing", "707"}, {3, "Boeing", "747"} }; REQUIRE(find_result); auto all_planes = find_result.release(); size_t index {0}; for (const auto &i: all_planes) { REQUIRE(i.id == std::get<0>(expected_result[index])); REQUIRE(i.brand == std::get<1>(expected_result[index])); REQUIRE(i.model == std::get<2>(expected_result[index])); ++index; } } TEST_CASE_METHOD(SessionFixture, "Use session to find all objects with one-to-many lazy relation", "[session][find][one-to-many][eager]") { auto result = ses.attach("authors") .and_then( [this] { return ses.attach("books"); } ) .and_then( [this] { return ses.create_schema(); } ); tables_to_drop.emplace("authors"); tables_to_drop.emplace("books"); std::vector> authors; authors.emplace_back(new author{1, "Michael", "Crichton", "23.10.1942", 1975, true, {}}); authors.emplace_back(new author{ 2, "Steven", "King", "21.9.1947", 1956, false, {}}); for (auto &&a: authors) { auto res = ses.insert(a.release()); REQUIRE(res.is_ok()); } auto find_result = ses.find(); REQUIRE(find_result.is_ok()); auto all_authors = find_result.release(); std::vector> author_repo; for (auto it = all_authors.begin(); it != all_authors.end(); ++it) { std::cout << "author: " << it->first_name << " (books: " << it->books.size() << ")\n"; author_repo.emplace_back(it.release()); } REQUIRE(author_repo.size() == 2); std::vector> books; books.emplace_back( new book{3, "Jurassic Park", author_repo[0], 1990} ); books.emplace_back( new book{4, "Timeline", author_repo[0], 1999} ); books.emplace_back( new book{5, "The Andromeda Strain", author_repo[0], 1969} ); books.emplace_back( new book{6, "Congo", author_repo[0], 1980} ); books.emplace_back( new book{7, "Prey", author_repo[0], 2002} ); books.emplace_back( new book{8, "Carrie", author_repo[1], 1974} ); books.emplace_back( new book{9, "The Shining", author_repo[1], 1977} ); books.emplace_back( new book{10, "It", author_repo[1], 1986} ); books.emplace_back( new book{11, "Misery", author_repo[1], 1987} ); books.emplace_back( new book{12, "The Dark Tower: The Gunslinger", author_repo[1], 1982} ); for (auto &&b: books) { auto res = ses.insert(b.release()); REQUIRE(res.is_ok()); } find_result = ses.find(); REQUIRE(find_result); all_authors = find_result.release(); for (auto it = all_authors.begin(); it != all_authors.end(); ++it) { std::cout << "author: " << it->first_name << " (books: " << it->books.size() << ")\n"; } } TEST_CASE_METHOD(SessionFixture, "Use session to find all objects with one-to-many eager relation", "[session][find][one-to-many][eager]") { auto result = ses.attach("departments") .and_then( [this] { return ses.attach("employees"); } ) .and_then( [this] { return ses.create_schema(); } ); tables_to_drop.emplace("departments"); tables_to_drop.emplace("employees"); std::vector> departments; departments.emplace_back(new department{1, "Insurance", {}}); departments.emplace_back(new department{ 2, "Invoice", {}}); for (auto &&a: departments) { auto res = ses.insert(a.release()); REQUIRE(res.is_ok()); } auto find_result = ses.find(); REQUIRE(find_result.is_ok()); 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"; departments_repo.emplace_back(it.release()); } REQUIRE(departments_repo.size() == 2); std::vector> employees; employees.emplace_back( new employee{3, "George", "Orwell", departments_repo[0]} ); employees.emplace_back( new employee{4, "Chris", "Tucker", departments_repo[0]} ); employees.emplace_back( new employee{5, "Steven", "King", departments_repo[0]} ); employees.emplace_back( new employee{6, "John", "Wayne", departments_repo[0]} ); employees.emplace_back( new employee{7, "Clint", "Eastwood", departments_repo[0]} ); employees.emplace_back( new employee{8, "Emma", "Thompson", departments_repo[1]} ); employees.emplace_back( new employee{9, "Ed", "Wood", departments_repo[1]} ); employees.emplace_back( new employee{10, "Steven", "Spielberg", departments_repo[1]} ); employees.emplace_back( new employee{11, "Jane", "Fonda", departments_repo[1]} ); employees.emplace_back( new employee{12, "Julia", "Roberts", departments_repo[1]} ); for (auto &&b: employees) { auto res = ses.insert(b.release()); REQUIRE(res.is_ok()); } find_result = ses.find(); REQUIRE(find_result); 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"; } } } TEST_CASE_METHOD(SessionFixture, "Use session to find all objects with many-to-many eager relation", "[session][find][many-to-many][eager]") { auto result = ses.attach("recipes") .and_then( [this] { return ses.attach("ingredients"); } ) .and_then( [this] { return ses.create_schema(); } ); tables_to_drop.emplace("recipes"); tables_to_drop.emplace("ingredients"); tables_to_drop.emplace("recipe_ingredients"); std::vector> ingredients; ingredients.push_back(std::make_unique(1, "Apple")); ingredients.push_back(std::make_unique(2, "Strawberry")); ingredients.push_back(std::make_unique(3, "Pineapple")); ingredients.push_back(std::make_unique(4, "Sugar")); ingredients.push_back(std::make_unique(5, "Flour")); ingredients.push_back(std::make_unique(6, "Butter")); ingredients.push_back(std::make_unique(7, "Beans")); for (auto &i: ingredients) { auto res = ses.insert(i.release()); REQUIRE(res.is_ok()); } std::vector> recipes; recipes.push_back(std::make_unique(7, "Apple Crumble")); recipes.push_back(std::make_unique(8, "Beans Chili")); recipes.push_back(std::make_unique(9, "Fruit Salad")); }