#include "catch2/catch_test_macros.hpp" #include "SessionFixture.hpp" #include "connection.hpp" #include "matador/query/session.hpp" #include "models/author.hpp" #include "models/book.hpp" using namespace matador::test; using namespace matador::query; using namespace matador::object; namespace matador::test { 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)); } } } namespace matador::utils { template < typename ValueType > std::ostream& operator<<(std::ostream& os, const result& value) { if (value) { return os; } return os << "Error: " << value.err(); } std::ostream& operator<<(std::ostream& os, const result& value) { if (value) { return os; } return os << "Error: " << value.err(); } } TEST_CASE_METHOD(SessionFixture, "Test delete object with has many relation", "[session][delete][has_many]") { 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(1, "Steven", "King", "21.9.1947", 1956, false); s_king->books.push_back(make_object(2, "Carrie", nullobj, 1974)); s_king->books.push_back(make_object(3, "The Shining", nullobj, 1977)); s_king->books.push_back(make_object(4, "It", nullobj, 1986)); 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); validate_author_state(s_king, object_state::Persistent); auto author_result = ses.find(s_king->id); REQUIRE(author_result); REQUIRE(author_result->is_persistent()); REQUIRE(author_result.value()->books.size() == 5); const auto id = s_king->id; auto del_res = ses.remove(s_king); REQUIRE(del_res); author_result = ses.find(id); REQUIRE_FALSE(author_result); }