added SchemaTest, fixed bug in class shipment, added contains methods to class schema and enabled some tests

This commit is contained in:
Sascha Kühl 2026-01-09 09:32:47 +01:00
parent 930b9d1aa4
commit ac53526a27
7 changed files with 102 additions and 25 deletions

View File

@ -102,12 +102,25 @@ public:
const_iterator begin() const;
const_iterator end() const;
iterator find(const std::type_index& ti) { return schema_nodes_.find(ti); }
const_iterator find(const std::type_index& ti) const { return schema_nodes_.find(ti); }
[[nodiscard]] size_t size() const;
[[nodiscard]] bool empty() const;
template<typename Type>
iterator find() { return find(typeid(Type)); }
iterator find(const std::type_index& ti);
iterator find(const std::string& name);
template<typename Type>
const_iterator find() const { return find(typeid(Type)); }
const_iterator find(const std::type_index& ti) const;
const_iterator find(const std::string& name) const;
[[nodiscard]] bool contains(const std::type_index &index) const;
template < typename Type >
[[nodiscard]] bool contains() const {
return contains(std::type_index(typeid(Type)));
}
const object::repository &repo() const { return repo_; }
object::repository &repo() { return repo_; }

View File

@ -200,6 +200,22 @@ schema::const_iterator schema::end() const {
return schema_nodes_.end();
}
size_t schema::size() const {
return schema_nodes_.size();
}
bool schema::empty() const {
return schema_nodes_.empty();
}
schema::iterator schema::find(const std::type_index &ti) {
return schema_nodes_.find(ti);
}
schema::const_iterator schema::find(const std::type_index &ti) const {
return schema_nodes_.find(ti);
}
schema::iterator schema::find(const std::string &name) {
const auto result = repo_.basic_info(name);
if (!result) {
@ -218,6 +234,10 @@ schema::const_iterator schema::find(const std::string &name) const {
return schema_nodes_.find(result->get().type_index());
}
bool schema::contains(const std::type_index &index) const {
return schema_nodes_.count(index) == 1;
}
void schema::dump(std::ostream &os) const {
repo_.dump(os);
}

View File

@ -27,7 +27,7 @@ META_TABLE(recipe_ingredients, RECIPE_INGREDIENT, recipe_id, ingredient_id);
META_TABLE(airplanes, AIRPLANE, id, brand, model);
META_TABLE(flights, FLIGHT, id, airplane_id, pilot_name);
META_TABLE(shipments, SHIPMENT, id, tracking_number)
META_TABLE(packages, PACKAGE, id, weight, shipment)
META_TABLE(packages, PACKAGE, id, weight, shipment_id)
TEST_CASE_METHOD(QueryFixture, "Create table with foreign key relation", "[query][foreign][relation]") {
auto result = repo.attach<airplane>("airplane")
@ -593,10 +593,8 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with many to many relationship"
select_result = query::select({r.id, r.name, ri.ingredient_id, i.name})
.from(r)
.join_left(ri)
.on(r.id == ri.recipe_id)
.join_left(i)
.on(ri.ingredient_id == i.id)
.join_left(ri).on(r.id == ri.recipe_id)
.join_left(i).on(ri.ingredient_id == i.id)
.where(r.id == 8)
.fetch_all(db);
REQUIRE(result.is_ok());
@ -621,8 +619,6 @@ void print(std::ostream& out, const record& row) {
TEST_CASE_METHOD(QueryFixture, "Test load entity with eager has many relation", "[query][has_many][eager]") {
auto result = repo.attach<package>("packages")
.and_then( [this] { return repo.attach<shipment>("shipments"); } );
// auto result = repo.attach<shipment>("shipments")
// .and_then( [this] { return repo.attach<package>("packages"); } );
REQUIRE(result.is_ok());
tables_to_drop.emplace("shipments");
tables_to_drop.emplace("packages");
@ -675,10 +671,10 @@ TEST_CASE_METHOD(QueryFixture, "Test load entity with eager has many relation",
REQUIRE(count.is_ok());
REQUIRE(*count == 5);
auto pkgs = query::select({PACKAGE.id, PACKAGE.weight, PACKAGE.shipment, SHIPMENT.tracking_number})
auto pkgs = query::select({PACKAGE.id, PACKAGE.weight, PACKAGE.shipment_id, SHIPMENT.tracking_number})
.from(PACKAGE)
.join_left(SHIPMENT)
.on( PACKAGE.shipment == SHIPMENT.id )
.on( PACKAGE.shipment_id == SHIPMENT.id )
.where( PACKAGE.weight > 20.0 && SHIPMENT.tracking_number == "0815" )
.group_by({PACKAGE.id, SHIPMENT.tracking_number})
.order_by(PACKAGE.weight).asc()
@ -694,23 +690,28 @@ TEST_CASE_METHOD(QueryFixture, "Test load entity with eager has many relation",
auto shipment_records = query::select({SHIPMENT.tracking_number, SHIPMENT.id, PACKAGE.weight, PACKAGE.weight})
.from(SHIPMENT)
.join_left(PACKAGE)
.on( SHIPMENT.id == PACKAGE.shipment )
.on( SHIPMENT.id == PACKAGE.shipment_id )
.fetch_all(db);
REQUIRE(shipment_records.is_ok());
printer.print(*shipment_records);
auto shipment_result = query::select({SHIPMENT.id, SHIPMENT.tracking_number, PACKAGE.id, PACKAGE.weight, PACKAGE.shipment})
auto shipment_result = query::select({SHIPMENT.id, SHIPMENT.tracking_number, PACKAGE.id, PACKAGE.weight, PACKAGE.shipment_id})
.from(SHIPMENT)
.join_left(PACKAGE)
.on( SHIPMENT.id == PACKAGE.shipment )
.on( SHIPMENT.id == PACKAGE.shipment_id )
.order_by(SHIPMENT.id).asc()
.fetch_all<shipment>(db);
REQUIRE(shipment_result.is_ok());
size_t index{0};
std::vector<size_t> packages_sizes{2, 3};
std::cout << "\n";
for (const auto &s : *shipment_result) {
std::cout << s.id << " " << s.tracking_number << " packages: " << s.packages.size() << std::endl;
REQUIRE(s.id == shipments.at(index)->id);
REQUIRE(s.tracking_number == shipments.at(index)->tracking_number);
REQUIRE(s.packages.size() == packages_sizes.at(index++));
}
}

View File

@ -48,15 +48,15 @@ TEST_CASE("Test add type to prototype tree", "[schema_node][add]") {
REQUIRE(repo.empty());
// auto res = repo.attach<person>("person");
// REQUIRE(res.is_ok());
// res = repo.attach<student, person>("student");
// REQUIRE(res.is_ok());
// res = repo.attach<teacher, person>("teacher");
// REQUIRE(res.is_ok());
//
// REQUIRE(!repo.empty());
// REQUIRE(repo.size() == 3);
auto res = repo.attach<person>("person");
REQUIRE(res.is_ok());
res = repo.attach<student, person>("student");
REQUIRE(res.is_ok());
res = repo.attach<teacher, person>("teacher");
REQUIRE(res.is_ok());
REQUIRE(!repo.empty());
REQUIRE(repo.size() == 3);
}
TEST_CASE("Test next and previous of schema node", "[schema_node][next][previous]") {

View File

@ -35,7 +35,7 @@ struct package {
namespace field = matador::access;
field::primary_key(op, "id", id);
field::attribute(op, "weight", weight);
field::belongs_to(op, "shipment", delivery, utils::CascadeAllFetchLazy);
field::belongs_to(op, "shipment_id", delivery, utils::CascadeAllFetchLazy);
}
};
}

View File

@ -28,6 +28,7 @@ add_executable(OrmTests
utils/auto_reset_event.cpp
utils/auto_reset_event.hpp
query/GeneratorTests.cpp
query/SchemaTest.cpp
)
target_link_libraries(OrmTests matador-orm matador-core Catch2::Catch2WithMain)

View File

@ -0,0 +1,42 @@
#include <catch2/catch_test_macros.hpp>
#include "matador/query/schema.hpp"
#include "../../models/shipment.hpp"
using namespace matador::query;
using namespace matador::test;
TEST_CASE("Test schema with belongs-to to has-many relation" "[schema][relation][belongs_to]") {
schema repo;
auto result = repo.attach<package>("packages")
.and_then( [&repo] { return repo.attach<shipment>("shipments"); } );
REQUIRE(result.is_ok());
REQUIRE(repo.size() == 2);
REQUIRE(repo.contains<shipment>());
REQUIRE(repo.contains<package>());
auto it = repo.find<shipment>();
REQUIRE(it != repo.end());
REQUIRE(it->second.name() == "shipments");
it = repo.find<package>();
REQUIRE(it != repo.end());
REQUIRE(it->second.name() == "packages");
}
TEST_CASE("Test schema with has-many to belongs-to relation" "[schema][relation][has_many]") {
schema repo;
auto result = repo.attach<shipment>("shipments")
.and_then( [&repo] { return repo.attach<package>("packages"); } );
REQUIRE(result.is_ok());
REQUIRE(repo.size() == 2);
REQUIRE(repo.contains<shipment>());
REQUIRE(repo.contains<package>());
auto it = repo.find<shipment>();
REQUIRE(it != repo.end());
REQUIRE(it->second.name() == "shipments");
it = repo.find<package>();
REQUIRE(it != repo.end());
REQUIRE(it->second.name() == "packages");
}