diff --git a/test/backends/SessionInsertHasMany.cpp b/test/backends/SessionInsertHasMany.cpp index 40a42e1..3891173 100644 --- a/test/backends/SessionInsertHasMany.cpp +++ b/test/backends/SessionInsertHasMany.cpp @@ -16,16 +16,16 @@ using namespace matador::test; namespace matador::test { template -struct book_identity; +struct book_pk_generator; -template -struct author_identity { +template +struct author_pk_generator { unsigned int id{}; std::string name; - collection>> books; + collection>> books; - author_identity() = default; - explicit author_identity(std::string name) + author_pk_generator() = default; + explicit author_pk_generator(std::string name) : name(std::move(name)) {} template @@ -37,15 +37,15 @@ struct author_identity { } }; -template -struct book_identity { +template +struct book_pk_generator { unsigned int id{}; std::string title; - object_ptr> book_author; + object_ptr> book_author; unsigned short published_in{}; - book_identity() = default; - book_identity(std::string title, object_ptr> author, const unsigned short published_in) + book_pk_generator() = default; + book_pk_generator(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 @@ -58,13 +58,36 @@ struct book_identity { } }; -using author_sequence = author_identity; -using author_table = author_identity; -using book_sequence = book_identity; -using book_table = book_identity; +using author_identity = author_pk_generator; +using author_sequence = author_pk_generator; +using author_table = author_pk_generator; +using book_identity = book_pk_generator; +using book_sequence = book_pk_generator; +using book_table = book_pk_generator; } +template +object_ptr create_author() { + 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)); + + return s_king; +} + +template +void validate_author_state(const object_ptr& 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]") { const auto result = schema.attach("books") .and_then( [this] { return schema.attach("authors"); } ) @@ -81,28 +104,26 @@ TEST_CASE_METHOD(SessionFixture, "Test insert object with has many relation", "[ 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)); + validate_author_state(s_king, object_state::Transient); auto res = ses.insert(s_king); 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]") { - const auto result = schema.attach>("books") - .and_then( [this] { return schema.attach>("authors"); } ) + 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"); - - 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 s_king = create_author(); + validate_author_state(s_king, object_state::Transient); auto res = ses.insert(s_king); 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]") { @@ -113,16 +134,12 @@ TEST_CASE_METHOD(SessionFixture, "Test insert object with has many relation with session ses({bus, connection::dns, 4}, schema); - 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 s_king = create_author(); + validate_author_state(s_king, object_state::Transient); auto res = ses.insert(s_king); 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]") { @@ -133,14 +150,16 @@ TEST_CASE_METHOD(SessionFixture, "Test insert object with has many relation with session ses({bus, connection::dns, 4}, schema); - 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 s_king = create_author(); + validate_author_state(s_king, object_state::Transient); auto res = ses.insert(s_king); REQUIRE(res.is_ok()); + validate_author_state(s_king, object_state::Persistent); + + auto author_result = ses.find(s_king->id); + REQUIRE(author_result); + auto author = *author_result; + REQUIRE(author.is_persistent()); + REQUIRE(author->books.size() == 5); } \ No newline at end of file