#include "catch2/catch_test_macros.hpp" #include "SessionFixture.hpp" #include "connection.hpp" #include "models/author.hpp" #include "models/book.hpp" using namespace matador; using namespace matador::object; using namespace matador::test; namespace matador::test { struct book_identity; struct author_identity { unsigned int id{}; std::string name; collection > books; author_identity() = default; explicit author_identity(std::string name) : name(std::move(name)) {} template void process(Operator &op) { namespace field = matador::access; field::primary_key(op, "id", id, utils::Identity); field::attribute(op, "first_name", name, VarChar63); field::has_many(op, "books", books, "author_id", utils::CascadeAllFetchLazy); } }; struct book_identity { unsigned int id{}; std::string title; object_ptr book_author; unsigned short published_in{}; book_identity() = default; book_identity(std::string title, object_ptr author, const unsigned short published_in) : title(std::move(title)), book_author(std::move(author)), published_in(published_in) {} template void process(Operator &op) { namespace field = matador::access; field::primary_key(op, "id", id, utils::Identity); field::attribute(op, "title", title, VarChar511); field::belongs_to(op, "author_id", book_author, utils::CascadeAllFetchEager); field::attribute(op, "published_in", published_in); } }; } TEST_CASE_METHOD(SessionFixture, "Test insert object with has many relation", "[session][insert][has_many]") { const auto result = schema.attach("books") .and_then( [this] { return schema.attach("authors"); } ) .and_then([this] { return schema.create(db); } ); REQUIRE(result.is_ok()); orm::session ses({bus, connection::dns, 4}, schema); schema.initialize(ses); auto s_king = make_object(1, "Steven", "King", "21.9.1947", 1956, false); s_king->books.push_back(make_object(2, "Carrie", nullobj, 1974)); s_king->books.push_back(make_object(3, "The Shining", nullobj, 1977)); s_king->books.push_back(make_object(4, "It", nullobj, 1986)); s_king->books.push_back(make_object(5, "Misery", nullobj, 1987)); s_king->books.push_back(make_object(6, "The Dark Tower: The Gunslinger", nullobj, 1982)); auto res = ses.insert(s_king); REQUIRE(res.is_ok()); } TEST_CASE_METHOD(SessionFixture, "Test insert object with has many relation with identity", "[session][insert][has_many][identity]") { const auto result = schema.attach("books") .and_then( [this] { return schema.attach("authors"); } ) .and_then([this] { return schema.create(db); } ); REQUIRE(result.is_ok()); orm::session ses({bus, connection::dns, 4}, schema); schema.initialize(ses); auto s_king = make_object("Steven King"); s_king->books.push_back(make_object("Carrie", nullobj, 1974)); s_king->books.push_back(make_object("The Shining", nullobj, 1977)); s_king->books.push_back(make_object("It", nullobj, 1986)); s_king->books.push_back(make_object("Misery", nullobj, 1987)); s_king->books.push_back(make_object("The Dark Tower: The Gunslinger", nullobj, 1982)); auto res = ses.insert(s_king); REQUIRE(res.is_ok()); }