query/test/backends/SessionInsertBelongsTo.cpp

81 lines
2.8 KiB
C++

#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<book_identity>("books")
.and_then( [this] { return schema.attach<author_identity>("authors"); } )
.and_then([this] { return schema.create(db); } );
REQUIRE(result.is_ok());
session ses({bus, connection::dns, 4}, schema);
auto s_king = make_object<author_identity>("Steven King");
const auto carrie = make_object<book_identity>("Carrie", s_king, 1974);
REQUIRE(carrie.is_transient());
REQUIRE(ses.insert(carrie).is_ok());
REQUIRE(carrie.is_persistent());
auto found_author = ses.find<author_identity>(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<book_table>("books")
.and_then( [this] { return schema.attach<author_table>("authors"); } )
.and_then([this] { return schema.create(db); } );
REQUIRE(result.is_ok());
session ses({bus, connection::dns, 4}, schema);
auto s_king = make_object<author_table>("Steven King");
const auto carrie = make_object<book_table>("Carrie", s_king, 1974);
REQUIRE(carrie.is_transient());
REQUIRE(ses.insert(carrie).is_ok());
REQUIRE(carrie.is_persistent());
auto found_author = ses.find<author_table>(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<book_sequence>("books")
.and_then( [this] { return schema.attach<author_sequence>("authors"); } )
.and_then([this] { return schema.create(db); } );
REQUIRE(result.is_ok());
session ses({bus, connection::dns, 4}, schema);
auto s_king = make_object<author_sequence>("Steven King");
const auto carrie = make_object<book_sequence>("Carrie", s_king, 1974);
REQUIRE(carrie.is_transient());
REQUIRE(ses.insert(carrie).is_ok());
REQUIRE(carrie.is_persistent());
auto found_author = ses.find<author_sequence>(s_king->id);
REQUIRE(found_author);
REQUIRE(found_author.value()->name == "Steven King");
REQUIRE(found_author.value()->books.size() == 1);
}