has many primitive relation progress

This commit is contained in:
2026-05-08 15:53:52 +02:00
parent 1e08996087
commit a714cd2a53
6 changed files with 89 additions and 6 deletions
+45
View File
@@ -1,3 +1,5 @@
#include <utility>
#include "catch2/catch_test_macros.hpp"
#include "SessionFixture.hpp"
@@ -65,6 +67,30 @@ 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<const utils::primary_key_attribute &PkAttribute>
struct colorlist_pk_generator {
unsigned int id{};
std::string name;
collection<std::string> colors;
colorlist_pk_generator() = default;
colorlist_pk_generator(std::string name, const std::vector<std::string>& color_names)
: name(std::move(name))
, colors(color_names) {}
template<typename Operator>
void process(Operator &op) {
namespace field = matador::access;
field::primary_key(op, "id", id, PkAttribute);
field::attribute(op, "name", name, VarChar511);
field::has_many(op, "colors", colors, "colorlist_id", utils::CascadeAllFetchLazy);
}
};
using colorlist_identity = colorlist_pk_generator<utils::Identity>;
using colorlist_sequence = colorlist_pk_generator<utils::Sequence>;
using colorlist_table = colorlist_pk_generator<utils::Table>;
}
template<typename AuthorType, typename BookType>
@@ -162,4 +188,23 @@ TEST_CASE_METHOD(SessionFixture, "Test insert object with has many relation with
auto author = *author_result;
REQUIRE(author.is_persistent());
REQUIRE(author->books.size() == 5);
}
TEST_CASE_METHOD(SessionFixture, "Test insert object with has many primitive relation with table sequence", "[session][insert][has_many][primitive][table_sequence]") {
const auto result = schema.attach<colorlist_identity>("colorlist")
.and_then([this] { return schema.create(db); } );
REQUIRE(result.is_ok());
session ses({bus, connection::dns, 4}, schema);
const auto c = make_object<colorlist_identity>("rgb", std::vector<std::string>{"red", "green", "blue"});
auto res = ses.insert(c);
REQUIRE(res.is_ok());
auto colorlist_result = ses.find<colorlist_identity>(c->id);
REQUIRE(colorlist_result);
auto clist = *colorlist_result;
REQUIRE(clist.is_persistent());
REQUIRE(clist->colors.size() == 3);
}