query/test/backends/SessionDeleteHasMany.cpp

72 lines
2.1 KiB
C++

#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<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));
}
}
}
namespace matador::utils {
template < typename ValueType >
std::ostream& operator<<(std::ostream& os, const result<ValueType, error>& value) {
if (value) {
return os;
}
return os << "Error: " << value.err();
}
std::ostream& operator<<(std::ostream& os, const result<void, error>& 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<book>("books")
.and_then( [this] { return schema.attach<author>("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>(1, "Steven", "King", "21.9.1947", 1956, false);
s_king->books.push_back(make_object<book>(2, "Carrie", nullobj, 1974));
s_king->books.push_back(make_object<book>(3, "The Shining", nullobj, 1977));
s_king->books.push_back(make_object<book>(4, "It", nullobj, 1986));
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));
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<author>(s_king->id);
REQUIRE(author_result);
REQUIRE(author_result->is_persistent());
REQUIRE(author_result.value()->books.size() == 5);
auto del_res = ses.remove(s_king);
REQUIRE(del_res);
}