#include "catch2/catch_test_macros.hpp" #include "SessionFixture.hpp" #include "connection.hpp" #include "matador/query/session.hpp" #include "models/author.hpp" using namespace matador::test; using namespace matador::query; using namespace matador::object; TEST_CASE_METHOD(SessionFixture, "Test insert object with belongs to relation with identity", "[session][insert][belongs_to][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()); session ses({bus, connection::dns, 4}, schema); auto s_king = make_object("Steven King"); const auto carrie = make_object("Carrie", s_king, 1974); REQUIRE(carrie.is_transient()); REQUIRE(ses.insert(carrie).is_ok()); REQUIRE(carrie.is_persistent()); auto found_author = ses.find(s_king->id); REQUIRE(found_author); REQUIRE(found_author.value()->name == "Steven King"); REQUIRE(found_author.value()->books.size() == 1); } TEST_CASE_METHOD(SessionFixture, "Test insert object with belongs to relation with table sequence", "[session][insert][belongs_to][table_sequence]") { const auto result = schema.attach("books") .and_then( [this] { return schema.attach("authors"); } ) .and_then([this] { return schema.create(db); } ); REQUIRE(result.is_ok()); session ses({bus, connection::dns, 4}, schema); auto s_king = make_object("Steven King"); const auto carrie = make_object("Carrie", s_king, 1974); REQUIRE(carrie.is_transient()); REQUIRE(ses.insert(carrie).is_ok()); REQUIRE(carrie.is_persistent()); auto found_author = ses.find(s_king->id); REQUIRE(found_author); REQUIRE(found_author.value()->name == "Steven King"); REQUIRE(found_author.value()->books.size() == 1); } TEST_CASE_METHOD(SessionFixture, "Test insert object with belongs to relation with sequence", "[session][insert][belongs_to][sequence]") { const auto result = schema.attach("books") .and_then( [this] { return schema.attach("authors"); } ) .and_then([this] { return schema.create(db); } ); REQUIRE(result.is_ok()); session ses({bus, connection::dns, 4}, schema); auto s_king = make_object("Steven King"); const auto carrie = make_object("Carrie", s_king, 1974); REQUIRE(carrie.is_transient()); REQUIRE(ses.insert(carrie).is_ok()); REQUIRE(carrie.is_persistent()); auto found_author = ses.find(s_king->id); REQUIRE(found_author); REQUIRE(found_author.value()->name == "Steven King"); REQUIRE(found_author.value()->books.size() == 1); }