#include "catch2/catch_test_macros.hpp" #include "SessionFixture.hpp" #include "models/airplane.hpp" #include "models/author.hpp" #include "models/book.hpp" #include "models/flight.hpp" #include using namespace matador; using namespace matador::object; using namespace matador::test; 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()); 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 res = ses.find(2); REQUIRE(!result.is_ok()); REQUIRE((result.err().ec() == orm::error_code::FailedToFindObject)); auto 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 relation", "[session][find][one-to-many]") { 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"; } }