added checks to SessionInsertHasMany Test

This commit is contained in:
sascha 2026-05-07 10:21:29 +02:00
parent 8753042b3c
commit 3658664eb9
1 changed files with 57 additions and 38 deletions

View File

@ -16,16 +16,16 @@ using namespace matador::test;
namespace matador::test { namespace matador::test {
template<const utils::primary_key_attribute &PkAttribute> template<const utils::primary_key_attribute &PkAttribute>
struct book_identity; struct book_pk_generator;
template<const utils::primary_key_attribute &PkAttribute = utils::Identity> template<const utils::primary_key_attribute &PkAttribute>
struct author_identity { struct author_pk_generator {
unsigned int id{}; unsigned int id{};
std::string name; std::string name;
collection<object_ptr<book_identity<PkAttribute>>> books; collection<object_ptr<book_pk_generator<PkAttribute>>> books;
author_identity() = default; author_pk_generator() = default;
explicit author_identity(std::string name) explicit author_pk_generator(std::string name)
: name(std::move(name)) {} : name(std::move(name)) {}
template<typename Operator> template<typename Operator>
@ -37,15 +37,15 @@ struct author_identity {
} }
}; };
template<const utils::primary_key_attribute &PkAttribute = utils::Identity> template<const utils::primary_key_attribute &PkAttribute>
struct book_identity { struct book_pk_generator {
unsigned int id{}; unsigned int id{};
std::string title; std::string title;
object_ptr<author_identity<PkAttribute>> book_author; object_ptr<author_pk_generator<PkAttribute>> book_author;
unsigned short published_in{}; unsigned short published_in{};
book_identity() = default; book_pk_generator() = default;
book_identity(std::string title, object_ptr<author_identity<PkAttribute>> author, const unsigned short published_in) book_pk_generator(std::string title, object_ptr<author_pk_generator<PkAttribute>> author, const unsigned short published_in)
: title(std::move(title)), book_author(std::move(author)), published_in(published_in) {} : title(std::move(title)), book_author(std::move(author)), published_in(published_in) {}
template<typename Operator> template<typename Operator>
@ -58,13 +58,36 @@ struct book_identity {
} }
}; };
using author_sequence = author_identity<utils::Sequence>; using author_identity = author_pk_generator<utils::Identity>;
using author_table = author_identity<utils::Table>; using author_sequence = author_pk_generator<utils::Sequence>;
using book_sequence = book_identity<utils::Sequence>; using author_table = author_pk_generator<utils::Table>;
using book_table = book_identity<utils::Table>; using book_identity = book_pk_generator<utils::Identity>;
using book_sequence = book_pk_generator<utils::Sequence>;
using book_table = book_pk_generator<utils::Table>;
} }
template<typename AuthorType, typename BookType>
object_ptr<AuthorType> create_author() {
auto s_king = make_object<AuthorType>("Steven King");
s_king->books.push_back(make_object<BookType>("Carrie", nullobj, 1974));
s_king->books.push_back(make_object<BookType>("The Shining", nullobj, 1977));
s_king->books.push_back(make_object<BookType>("It", nullobj, 1986));
s_king->books.push_back(make_object<BookType>("Misery", nullobj, 1987));
s_king->books.push_back(make_object<BookType>("The Dark Tower: The Gunslinger", nullobj, 1982));
return s_king;
}
template<typename AuthorType>
void validate_author_state(const object_ptr<AuthorType>& ptr, object_state expected_state) {
REQUIRE(ptr.is_state(expected_state));
for (auto &b: ptr->books) {
REQUIRE(b.is_state(expected_state));
}
}
TEST_CASE_METHOD(SessionFixture, "Test insert object with has many relation", "[session][insert][has_many]") { TEST_CASE_METHOD(SessionFixture, "Test insert object with has many relation", "[session][insert][has_many]") {
const auto result = schema.attach<book>("books") const auto result = schema.attach<book>("books")
.and_then( [this] { return schema.attach<author>("authors"); } ) .and_then( [this] { return schema.attach<author>("authors"); } )
@ -81,28 +104,26 @@ TEST_CASE_METHOD(SessionFixture, "Test insert object with has many relation", "[
s_king->books.push_back(make_object<book>(5, "Misery", nullobj, 1987)); s_king->books.push_back(make_object<book>(5, "Misery", nullobj, 1987));
s_king->books.push_back(make_object<book>(6, "The Dark Tower: The Gunslinger", nullobj, 1982)); s_king->books.push_back(make_object<book>(6, "The Dark Tower: The Gunslinger", nullobj, 1982));
validate_author_state(s_king, object_state::Transient);
auto res = ses.insert(s_king); auto res = ses.insert(s_king);
REQUIRE(res.is_ok()); REQUIRE(res.is_ok());
validate_author_state(s_king, object_state::Persistent);
} }
TEST_CASE_METHOD(SessionFixture, "Test insert object with has many relation with identity", "[session][insert][has_many][identity]") { TEST_CASE_METHOD(SessionFixture, "Test insert object with has many relation with identity", "[session][insert][has_many][identity]") {
const auto result = schema.attach<book_identity<>>("books") const auto result = schema.attach<book_identity>("books")
.and_then( [this] { return schema.attach<author_identity<>>("authors"); } ) .and_then( [this] { return schema.attach<author_identity>("authors"); } )
.and_then([this] { return schema.create(db); } ); .and_then([this] { return schema.create(db); } );
REQUIRE(result.is_ok()); REQUIRE(result.is_ok());
session ses({bus, connection::dns, 4}, schema); session ses({bus, connection::dns, 4}, schema);
auto s_king = make_object<author_identity<>>("Steven King"); auto s_king = create_author<author_identity, book_identity>();
s_king->books.push_back(make_object<book_identity<>>("Carrie", nullobj, 1974));
s_king->books.push_back(make_object<book_identity<>>("The Shining", nullobj, 1977));
s_king->books.push_back(make_object<book_identity<>>("It", nullobj, 1986));
s_king->books.push_back(make_object<book_identity<>>("Misery", nullobj, 1987));
s_king->books.push_back(make_object<book_identity<>>("The Dark Tower: The Gunslinger", nullobj, 1982));
validate_author_state(s_king, object_state::Transient);
auto res = ses.insert(s_king); auto res = ses.insert(s_king);
REQUIRE(res.is_ok()); REQUIRE(res.is_ok());
validate_author_state(s_king, object_state::Persistent);
} }
TEST_CASE_METHOD(SessionFixture, "Test insert object with has many relation with sequence", "[session][insert][has_many][sequence]") { TEST_CASE_METHOD(SessionFixture, "Test insert object with has many relation with sequence", "[session][insert][has_many][sequence]") {
@ -113,16 +134,12 @@ TEST_CASE_METHOD(SessionFixture, "Test insert object with has many relation with
session ses({bus, connection::dns, 4}, schema); session ses({bus, connection::dns, 4}, schema);
auto s_king = make_object<author_sequence>("Steven King"); auto s_king = create_author<author_sequence, book_sequence>();
s_king->books.push_back(make_object<book_sequence>("Carrie", nullobj, 1974));
s_king->books.push_back(make_object<book_sequence>("The Shining", nullobj, 1977));
s_king->books.push_back(make_object<book_sequence>("It", nullobj, 1986));
s_king->books.push_back(make_object<book_sequence>("Misery", nullobj, 1987));
s_king->books.push_back(make_object<book_sequence>("The Dark Tower: The Gunslinger", nullobj, 1982));
validate_author_state(s_king, object_state::Transient);
auto res = ses.insert(s_king); auto res = ses.insert(s_king);
REQUIRE(res.is_ok()); REQUIRE(res.is_ok());
validate_author_state(s_king, object_state::Persistent);
} }
TEST_CASE_METHOD(SessionFixture, "Test insert object with has many relation with table sequence", "[session][insert][has_many][table_sequence]") { TEST_CASE_METHOD(SessionFixture, "Test insert object with has many relation with table sequence", "[session][insert][has_many][table_sequence]") {
@ -133,14 +150,16 @@ TEST_CASE_METHOD(SessionFixture, "Test insert object with has many relation with
session ses({bus, connection::dns, 4}, schema); session ses({bus, connection::dns, 4}, schema);
auto s_king = make_object<author_table>("Steven King"); auto s_king = create_author<author_table, book_table>();
s_king->books.push_back(make_object<book_table>("Carrie", nullobj, 1974));
s_king->books.push_back(make_object<book_table>("The Shining", nullobj, 1977));
s_king->books.push_back(make_object<book_table>("It", nullobj, 1986));
s_king->books.push_back(make_object<book_table>("Misery", nullobj, 1987));
s_king->books.push_back(make_object<book_table>("The Dark Tower: The Gunslinger", nullobj, 1982));
validate_author_state(s_king, object_state::Transient);
auto res = ses.insert(s_king); auto res = ses.insert(s_king);
REQUIRE(res.is_ok()); REQUIRE(res.is_ok());
validate_author_state(s_king, object_state::Persistent);
auto author_result = ses.find<author_table>(s_king->id);
REQUIRE(author_result);
auto author = *author_result;
REQUIRE(author.is_persistent());
REQUIRE(author->books.size() == 5);
} }