added SessionDeleteHasMany and SessionDeleteHasManyToMany tests

This commit is contained in:
sascha 2026-05-26 15:50:32 +02:00
parent 85e6ee4b93
commit b22a830d18
3 changed files with 86 additions and 1 deletions

View File

@ -25,6 +25,8 @@ set(TEST_SOURCES
../../../test/backends/SequenceFixture.cpp ../../../test/backends/SequenceFixture.cpp
../../../test/backends/SequenceFixture.hpp ../../../test/backends/SequenceFixture.hpp
../../../test/backends/SequenceTest.cpp ../../../test/backends/SequenceTest.cpp
../../../test/backends/SessionDeleteHasMany.cpp
../../../test/backends/SessionDeleteHasManyToMany.cpp
../../../test/backends/SessionFixture.cpp ../../../test/backends/SessionFixture.cpp
../../../test/backends/SessionFixture.hpp ../../../test/backends/SessionFixture.hpp
../../../test/backends/SessionInsertBelongsTo.cpp ../../../test/backends/SessionInsertBelongsTo.cpp
@ -46,7 +48,6 @@ set(TEST_SOURCES
../../../test/models/user.hpp ../../../test/models/user.hpp
../../../test/utils/RecordPrinter.cpp ../../../test/utils/RecordPrinter.cpp
../../../test/utils/RecordPrinter.hpp ../../../test/utils/RecordPrinter.hpp
../../../test/backends/SessionDeleteHasMany.cpp
) )
set(LIBRARY_TEST_TARGET PostgresTests) set(LIBRARY_TEST_TARGET PostgresTests)

View File

@ -66,6 +66,10 @@ TEST_CASE_METHOD(SessionFixture, "Test delete object with has many relation", "[
REQUIRE(author_result->is_persistent()); REQUIRE(author_result->is_persistent());
REQUIRE(author_result.value()->books.size() == 5); REQUIRE(author_result.value()->books.size() == 5);
const auto id = s_king->id;
auto del_res = ses.remove(s_king); auto del_res = ses.remove(s_king);
REQUIRE(del_res); REQUIRE(del_res);
author_result = ses.find<author>(id);
REQUIRE_FALSE(author_result);
} }

View File

@ -0,0 +1,80 @@
#include "catch2/catch_test_macros.hpp"
#include "SessionFixture.hpp"
#include "connection.hpp"
#include "matador/query/session.hpp"
#include "models/recipe.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 to many relation", "[session][delete][has_many_to_many]") {
auto result = schema.attach<recipe_sequence>("recipes")
.and_then( [this] { return schema.attach<ingredient_sequence>("ingredients"); } )
.and_then([this] { return schema.create(db); } );
session ses({bus, connection::dns, 4}, schema);
std::vector ingredients {
make_object<ingredient_sequence>("Apple"),
make_object<ingredient_sequence>("Strawberry"),
make_object<ingredient_sequence>("Pineapple"),
make_object<ingredient_sequence>("Sugar"),
make_object<ingredient_sequence>("Flour"),
make_object<ingredient_sequence>("Butter"),
make_object<ingredient_sequence>("Beans")
};
std::vector recipes {
make_object<recipe_sequence>("Apple Pie", std::vector{ingredients[0], ingredients[3], ingredients[4]}),
make_object<recipe_sequence>("Strawberry Cake", std::vector{ingredients[5], ingredients[6]}),
make_object<recipe_sequence>("Pineapple Pie", std::vector{ingredients[0], ingredients[1], ingredients[2]})
};
for (auto &r: recipes) {
REQUIRE(r.is_transient());
auto res = ses.insert(r);
REQUIRE(res.is_ok());
REQUIRE(res->is_persistent());
}
auto recipe_result = ses.find<recipe_sequence>(1);
REQUIRE(recipe_result.is_ok());
REQUIRE(recipe_result->is_persistent());
REQUIRE(recipe_result.value()->ingredients.size() == 3);
const auto ing_result = ses.find<ingredient_sequence>(ingredients[0]->id);
REQUIRE(ing_result.is_ok());
REQUIRE(ing_result.value()->recipes.size() == 2);
}