added belongs-to-eager test

This commit is contained in:
Sascha Kühl 2026-01-10 18:54:18 +01:00
parent b9f5819be5
commit db8e137c11
20 changed files with 403 additions and 278 deletions

View File

@ -9,6 +9,9 @@
#include "QueryFixture.hpp" #include "QueryFixture.hpp"
#include "models/airplane.hpp" #include "models/airplane.hpp"
#include "models/author.hpp"
#include "models/book.hpp"
#include "models/department.hpp"
#include "models/flight.hpp" #include "models/flight.hpp"
#include "models/person.hpp" #include "models/person.hpp"
#include "models/recipe.hpp" #include "models/recipe.hpp"
@ -578,3 +581,149 @@ TEST_CASE_METHOD(QueryFixture, "Test load entity with eager has many relation",
REQUIRE(s.packages.size() == packages_sizes.at(index++)); REQUIRE(s.packages.size() == packages_sizes.at(index++));
} }
} }
TEST_CASE_METHOD(QueryFixture, "Test load entity with lazy belongs to relation", "[query][belongs_to][lazy]") {
auto result = repo.attach<department>("departments")
.and_then([this] { return repo.attach<employee>("employees"); })
.and_then([this] {return repo.create(db); });
REQUIRE(result.is_ok());
const std::vector deps {
object_ptr{new department{1, "Human Resources"}},
object_ptr{new department{2, "Invoices"}}
};
const std::vector emps {
object_ptr{new employee{3, "Hans", "Wurst", deps[0]}},
object_ptr{new employee{4, "Steven", "Spielberg", deps[0]}},
object_ptr{new employee{5, "Julia", "Roberts", deps[0]}},
object_ptr{new employee{6, "Otto", "Walkes", deps[1]}},
object_ptr{new employee{7, "Miss", "Marple", deps[1]}},
};
for (const auto &dep: deps) {
auto res = query::insert()
.into(DEPARTMENT, {DEPARTMENT.id, DEPARTMENT.name})
.values(*dep)
.execute(db);
REQUIRE(res.is_ok());
REQUIRE(*res == 1);
}
auto count = query::select({count_all()})
.from(DEPARTMENT)
.fetch_value<int>(db);
REQUIRE(count.is_ok());
REQUIRE(*count == 2);
for (const auto &emp: emps) {
auto res = query::insert()
.into(EMPLOYEE, {EMPLOYEE.id, EMPLOYEE.first_name, EMPLOYEE.last_name, EMPLOYEE.dep_id})
.values(*emp)
.execute(db);
REQUIRE(res.is_ok());
REQUIRE(*res == 1);
}
count = query::select({count_all()})
.from(EMPLOYEE)
.fetch_value<int>(db);
REQUIRE(count.is_ok());
REQUIRE(*count == 5);
auto emps_result = query::select({EMPLOYEE.id, EMPLOYEE.first_name, EMPLOYEE.last_name, DEPARTMENT.id, DEPARTMENT.name})
.from(EMPLOYEE)
.join_left(DEPARTMENT)
.on(EMPLOYEE.dep_id == DEPARTMENT.id)
.order_by(EMPLOYEE.id).asc()
.fetch_all<employee>(db);
REQUIRE(emps_result.is_ok());
size_t index{0};
for (const auto &e : *emps_result) {
REQUIRE(e.id == emps.at(index)->id);
REQUIRE(e.first_name == emps.at(index)->first_name);
REQUIRE(e.last_name == emps.at(index)->last_name);
REQUIRE(e.dep->id == emps.at(index)->dep->id);
REQUIRE(e.dep->name == emps.at(index)->dep->name);
++index;
}
}
TEST_CASE_METHOD(QueryFixture, "Test load entity with eager belongs to relation", "[query][belongs_to][eager]") {
auto result = repo.attach<author>("authors")
.and_then( [this] { return repo.attach<book>("books"); } )
.and_then([this] {
return repo.create(db);
} );
const std::vector authors {
object_ptr{new author{1, "Michael", "Crichton", "23.10.1942", 1975, true, {}}},
object_ptr{new author{ 2, "Steven", "King", "21.9.1947", 1956, false, {}}}
};
const std::vector books {
object_ptr{new book{3, "Jurassic Park", authors[0], 1990}},
object_ptr{new book{4, "Timeline", authors[0], 1999}},
object_ptr{new book{5, "The Andromeda Strain", authors[0], 1969}},
object_ptr{new book{6, "Congo", authors[0], 1980}},
object_ptr{new book{7, "Prey", authors[0], 2002}},
object_ptr{new book{8, "Carrie", authors[1], 1974}},
object_ptr{new book{9, "The Shining", authors[1], 1977}},
object_ptr{new book{10, "It", authors[1], 1986}},
object_ptr{new book{11, "Misery", authors[1], 1987}},
object_ptr{new book{12, "The Dark Tower: The Gunslinger", authors[1], 1982}},
};
for (const auto &a: authors) {
auto res = query::insert()
.into(AUTHOR, {AUTHOR.id, AUTHOR.first_name, AUTHOR.last_name, AUTHOR.date_of_birth, AUTHOR.year_of_birth, AUTHOR.distinguished})
.values(*a)
.execute(db);
REQUIRE(res.is_ok());
REQUIRE(*res == 1);
}
auto count = query::select({count_all()})
.from(AUTHOR)
.fetch_value<int>(db);
REQUIRE(count.is_ok());
REQUIRE(*count == 2);
for (const auto &b: books) {
auto res = query::insert()
.into(BOOK, {BOOK.id, BOOK.title, BOOK.author_id, BOOK.published_in})
.values(*b)
.execute(db);
REQUIRE(res.is_ok());
REQUIRE(*res == 1);
}
count = query::select({count_all()})
.from(BOOK)
.fetch_value<int>(db);
REQUIRE(count.is_ok());
REQUIRE(*count == 10);
auto books_result = query::select({BOOK.id, BOOK.title, AUTHOR.id, AUTHOR.first_name, AUTHOR.last_name, AUTHOR.date_of_birth, AUTHOR.year_of_birth, AUTHOR.distinguished, BOOK.published_in})
.from(BOOK)
.join_left(AUTHOR)
.on(BOOK.author_id == AUTHOR.id)
.order_by(BOOK.id).asc()
.fetch_all<book>(db);
REQUIRE(books_result.is_ok());
size_t index{0};
for (const auto &b : *books_result) {
REQUIRE(b.id == books.at(index)->id);
REQUIRE(b.title == books.at(index)->title);
REQUIRE(b.book_author->id == books.at(index)->book_author->id);
REQUIRE(b.book_author->first_name == books.at(index)->book_author->first_name);
REQUIRE(b.book_author->last_name == books.at(index)->book_author->last_name);
REQUIRE(b.book_author->date_of_birth == books.at(index)->book_author->date_of_birth);
REQUIRE(b.book_author->year_of_birth == books.at(index)->book_author->year_of_birth);
REQUIRE(b.book_author->distinguished == books.at(index)->book_author->distinguished);
REQUIRE(b.published_in == books.at(index)->published_in);
++index;
}
}

View File

@ -6,13 +6,15 @@
#include <string> #include <string>
namespace matador::test { namespace matador::test {
struct airplane { struct airplane {
airplane() = default; airplane() = default;
airplane(const unsigned int id, std::string b, std::string m) airplane(const unsigned int id, std::string b, std::string m)
: id(id) : id(id)
, brand(std::move(b)) , brand(std::move(b))
, model(std::move(m)) {} , model(std::move(m)) {
}
unsigned int id{}; unsigned int id{};
std::string brand; std::string brand;
std::string model; std::string model;
@ -26,7 +28,6 @@ struct airplane {
field::attribute(op, "model", model, 255); field::attribute(op, "model", model, 255);
} }
}; };
} }
// namespace matador::access { // namespace matador::access {

View File

@ -9,7 +9,6 @@
#include <vector> #include <vector>
namespace matador::test { namespace matador::test {
struct book; struct book;
struct author { struct author {
@ -30,10 +29,9 @@ struct author {
field::attribute(op, "date_of_birth", date_of_birth, 31); field::attribute(op, "date_of_birth", date_of_birth, 31);
field::attribute(op, "year_of_birth", year_of_birth); field::attribute(op, "year_of_birth", year_of_birth);
field::attribute(op, "distinguished", distinguished); field::attribute(op, "distinguished", distinguished);
field::has_many(op, "books", books, "author_id", utils::fetch_type::Lazy); field::has_many(op, "books", books, "author_id", utils::CascadeAllFetchLazy);
} }
}; };
} }
#endif //QUERY_AUTHOR_HPP #endif //QUERY_AUTHOR_HPP

View File

@ -9,7 +9,6 @@
#include <string> #include <string>
namespace matador::test { namespace matador::test {
struct author; struct author;
struct book { struct book {
@ -23,10 +22,9 @@ struct book {
namespace field = matador::access; namespace field = matador::access;
field::primary_key(op, "id", id); field::primary_key(op, "id", id);
field::attribute(op, "title", title, 511); field::attribute(op, "title", title, 511);
field::belongs_to(op, "author_id", book_author, utils::fetch_type::Eager); field::belongs_to(op, "author_id", book_author, utils::CascadeAllFetchEager);
field::attribute(op, "published_in", published_in); field::attribute(op, "published_in", published_in);
} }
}; };
} }
#endif //QUERY_BOOK_HPP #endif //QUERY_BOOK_HPP

View File

@ -6,7 +6,6 @@
#include <string> #include <string>
namespace matador::test { namespace matador::test {
struct category { struct category {
unsigned int id{}; unsigned int id{};
std::string name; std::string name;
@ -19,6 +18,5 @@ struct category {
field::attribute(op, "name", name, 255); field::attribute(op, "name", name, 255);
} }
}; };
} }
#endif //QUERY_CATEGORY_HPP #endif //QUERY_CATEGORY_HPP

View File

@ -5,24 +5,20 @@
#include "matador/utils/field_attributes.hpp" #include "matador/utils/field_attributes.hpp"
namespace matador::test { namespace matador::test {
struct coordinate struct coordinate {
{
int x{}; int x{};
int y{}; int y{};
int z{}; int z{};
}; };
} }
namespace matador::access { namespace matador::access {
template<class Operator> template<class Operator>
void attribute(Operator &op, const char *id, test::coordinate &value, const utils::field_attributes &attr = utils::null_attributes) { void attribute(Operator &op, const char *id, test::coordinate &value, const utils::field_attributes &attr = utils::null_attributes) {
attribute(op, (std::string(id) + "_x").c_str(), value.x, attr); attribute(op, (std::string(id) + "_x").c_str(), value.x, attr);
attribute(op, (std::string(id) + "_y").c_str(), value.y, attr); attribute(op, (std::string(id) + "_y").c_str(), value.y, attr);
attribute(op, (std::string(id) + "_z").c_str(), value.z, attr); attribute(op, (std::string(id) + "_z").c_str(), value.z, attr);
} }
} }
#endif //QUERY_COORDINATE_HPP #endif //QUERY_COORDINATE_HPP

View File

@ -13,7 +13,7 @@ struct employee;
struct department { struct department {
unsigned int id{}; unsigned int id{};
std::string name; std::string name;
std::vector<object::object_ptr<employee>> employees; std::vector<object::object_ptr<employee>> employees{};
// object::object_ptr<employee> manager; // object::object_ptr<employee> manager;
template<typename Operator> template<typename Operator>

View File

@ -13,11 +13,12 @@
#include <utility> #include <utility>
namespace matador::test { namespace matador::test {
struct flight { struct flight {
flight() = default; flight() = default;
flight(const unsigned int id, const object::object_ptr<airplane> &plane, std::string name) flight(const unsigned int id, const object::object_ptr<airplane> &plane, std::string name)
: id(id), plane(plane), pilot_name(std::move(name)) {} : id(id), plane(plane), pilot_name(std::move(name)) {
}
unsigned int id{}; unsigned int id{};
object::object_ptr<airplane> plane; object::object_ptr<airplane> plane;
@ -32,7 +33,6 @@ struct flight {
field::attribute(op, "pilot_name", pilot_name, 255); field::attribute(op, "pilot_name", pilot_name, 255);
} }
}; };
} }
#endif //QUERY_FLIGHT_HPP #endif //QUERY_FLIGHT_HPP

View File

@ -8,21 +8,18 @@
#include <cstdint> #include <cstdint>
namespace matador::test { namespace matador::test {
enum class Color : uint8_t { enum class Color : uint8_t {
Green, Red, Blue, Yellow, Black, White, Brown Green, Red, Blue, Yellow, Black, White, Brown
}; };
struct location struct location {
{
unsigned int id{}; unsigned int id{};
std::string name; std::string name;
coordinate coord; coordinate coord;
Color color{Color::Green}; Color color{Color::Green};
template<class Operator> template<class Operator>
void process(Operator &op) void process(Operator &op) {
{
namespace field = matador::access; namespace field = matador::access;
field::primary_key(op, "id", id); field::primary_key(op, "id", id);
field::attribute(op, "name", name, 255); field::attribute(op, "name", name, 255);
@ -30,7 +27,6 @@ struct location
field::attribute(op, "color", color); field::attribute(op, "color", color);
} }
}; };
} }
#endif //QUERY_LOCATION_HPP #endif //QUERY_LOCATION_HPP

View File

@ -11,5 +11,9 @@ META_TABLE(flights, FLIGHT, id, airplane_id, pilot_name);
META_TABLE(shipments, SHIPMENT, id, tracking_number) META_TABLE(shipments, SHIPMENT, id, tracking_number)
META_TABLE(packages, PACKAGE, id, weight, shipment_id) META_TABLE(packages, PACKAGE, id, weight, shipment_id)
META_TABLE(persons, PERSON, id, name, age, image) META_TABLE(persons, PERSON, id, name, age, image)
META_TABLE(departments, DEPARTMENT, id, name)
META_TABLE(employees, EMPLOYEE, id, first_name, last_name, dep_id)
META_TABLE(authors, AUTHOR, id, first_name, last_name, date_of_birth, year_of_birth, distinguished)
META_TABLE(books, BOOK, id, title, author_id, published_in)
#endif //MATADOR_MODEL_METAS_HPP #endif //MATADOR_MODEL_METAS_HPP

View File

@ -8,9 +8,7 @@
#include <string> #include <string>
namespace matador::test { namespace matador::test {
struct optional {
struct optional
{
unsigned int id{}; unsigned int id{};
std::optional<std::string> name; std::optional<std::string> name;
std::optional<unsigned int> age{}; std::optional<unsigned int> age{};
@ -24,7 +22,6 @@ struct optional
field::attribute(op, "age", age); field::attribute(op, "age", age);
} }
}; };
} }
#endif //QUERY_OPTIONAL_HPP #endif //QUERY_OPTIONAL_HPP

View File

@ -10,9 +10,7 @@
#include <vector> #include <vector>
namespace matador::test { namespace matador::test {
struct order {
struct order
{
unsigned int order_id{}; unsigned int order_id{};
std::string order_date; std::string order_date;
std::string required_date; std::string required_date;
@ -45,6 +43,5 @@ struct order
field::has_many(op, "order_details", order_details_, "order_id", utils::fetch_type::Eager); field::has_many(op, "order_details", order_details_, "order_id", utils::fetch_type::Eager);
} }
}; };
} }
#endif //QUERY_ORDER_HPP #endif //QUERY_ORDER_HPP

View File

@ -8,14 +8,12 @@
#include "matador/utils/foreign_attributes.hpp" #include "matador/utils/foreign_attributes.hpp"
namespace matador::test { namespace matador::test {
struct order; struct order;
struct order_details struct order_details {
{
unsigned int order_details_id; unsigned int order_details_id;
matador::object::object_ptr<order> order_; object::object_ptr<order> order_;
matador::object::object_ptr<product> product_; object::object_ptr<product> product_;
template<class Operator> template<class Operator>
void process(Operator &op) { void process(Operator &op) {
@ -25,6 +23,5 @@ struct order_details
field::has_one(op, "product_id", product_, utils::CascadeNoneFetchLazy); field::has_one(op, "product_id", product_, utils::CascadeNoneFetchLazy);
} }
}; };
} }
#endif //QUERY_ORDER_DETAILS_HPP #endif //QUERY_ORDER_DETAILS_HPP

View File

@ -8,7 +8,6 @@
#include <string> #include <string>
namespace matador::test { namespace matador::test {
struct person { struct person {
unsigned int id{}; unsigned int id{};
std::string name; std::string name;
@ -25,6 +24,5 @@ struct person {
field::attribute(op, "image", image); field::attribute(op, "image", image);
} }
}; };
} }
#endif //QUERY_PERSON_HPP #endif //QUERY_PERSON_HPP

View File

@ -6,18 +6,16 @@
#include "matador/utils/access.hpp" #include "matador/utils/access.hpp"
#include "matador/utils/cascade_type.hpp" #include "matador/utils/cascade_type.hpp"
#include "matador/utils/field_attributes.hpp"
#include "matador/object/object_ptr.hpp" #include "matador/object/object_ptr.hpp"
#include <string> #include <string>
namespace matador::test { namespace matador::test {
struct product { struct product {
std::string product_name; std::string product_name;
object::object_ptr<test::supplier> supplier; object::object_ptr<supplier> supplier;
object::object_ptr<test::category> category; object::object_ptr<category> category;
std::string quantity_per_unit; std::string quantity_per_unit;
unsigned int unit_price; unsigned int unit_price;
unsigned int units_in_stock; unsigned int units_in_stock;
@ -30,8 +28,8 @@ struct product {
namespace field = matador::access; namespace field = matador::access;
using namespace matador::utils; using namespace matador::utils;
field::primary_key(op, "product_name", product_name, 255); field::primary_key(op, "product_name", product_name, 255);
field::belongs_to(op, "supplier_id", supplier, utils::cascade_type::ALL); field::belongs_to(op, "supplier_id", supplier, CascadeAllFetchLazy);
field::belongs_to(op, "category_id", category, utils::cascade_type::ALL); field::belongs_to(op, "category_id", category, CascadeAllFetchLazy);
field::attribute(op, "quantity_per_unit", quantity_per_unit, 255); field::attribute(op, "quantity_per_unit", quantity_per_unit, 255);
field::attribute(op, "unit_price", unit_price); field::attribute(op, "unit_price", unit_price);
field::attribute(op, "units_in_stock", units_in_stock); field::attribute(op, "units_in_stock", units_in_stock);

View File

@ -10,43 +10,45 @@
#include <string> #include <string>
namespace matador::test { namespace matador::test {
struct recipe; struct recipe;
struct ingredient
{ struct ingredient {
unsigned int id{}; unsigned int id{};
std::string name; std::string name;
std::vector<object::object_ptr<recipe> > recipes{}; std::vector<object::object_ptr<recipe> > recipes{};
ingredient() = default; ingredient() = default;
ingredient(const unsigned int id, std::string name) ingredient(const unsigned int id, std::string name)
: id(id), name(std::move(name)) {} : id(id), name(std::move(name)) {
}
template<class Operator> template<class Operator>
void process(Operator &op) { void process(Operator &op) {
namespace field = matador::access; namespace field = matador::access;
field::primary_key(op, "id", id); field::primary_key(op, "id", id);
field::attribute(op, "name", name, 255); field::attribute(op, "name", name, 255);
field::has_many_to_many(op, "recipe_ingredients", recipes, "ingredient_id", "recipe_id", utils::fetch_type::Eager); field::has_many_to_many(op, "recipe_ingredients", recipes, "ingredient_id", "recipe_id", utils::CascadeAllFetchEager);
} }
}; };
struct recipe struct recipe {
{
unsigned int id{}; unsigned int id{};
std::string name; std::string name;
std::vector<object::object_ptr<ingredient> > ingredients{}; std::vector<object::object_ptr<ingredient> > ingredients{};
recipe() = default; recipe() = default;
recipe(const unsigned int id, std::string name) recipe(const unsigned int id, std::string name)
: id(id), name(std::move(name)) {} : id(id), name(std::move(name)) {
}
template<class Operator> template<class Operator>
void process(Operator &op) { void process(Operator &op) {
namespace field = matador::access; namespace field = matador::access;
field::primary_key(op, "id", id); field::primary_key(op, "id", id);
field::attribute(op, "name", name, 255); field::attribute(op, "name", name, 255);
field::has_many_to_many(op, "recipe_ingredients", ingredients, utils::fetch_type::Lazy); field::has_many_to_many(op, "recipe_ingredients", ingredients, utils::CascadeAllFetchLazy);
} }
}; };
@ -54,7 +56,6 @@ struct recipe
// public: // public:
// recipe_ingredient() : many_to_many_relation("recipe_id", "ingredient_id") {} // recipe_ingredient() : many_to_many_relation("recipe_id", "ingredient_id") {}
// }; // };
} }
#endif //QUERY_RECIPE_HPP #endif //QUERY_RECIPE_HPP

View File

@ -11,6 +11,7 @@
namespace matador::test { namespace matador::test {
struct package; struct package;
struct shipment { struct shipment {
long id{}; long id{};
std::string tracking_number; std::string tracking_number;

View File

@ -11,7 +11,6 @@
#include <vector> #include <vector>
namespace matador::test { namespace matador::test {
struct course; struct course;
struct student { struct student {
@ -20,18 +19,19 @@ struct student {
std::vector<object::object_ptr<course> > courses; std::vector<object::object_ptr<course> > courses;
student() = default; student() = default;
explicit student(unsigned int id, std::string name) explicit student(unsigned int id, std::string name)
: id(id) : id(id)
, name(std::move(name)) {} , name(std::move(name)) {
}
template<class Operator> template<class Operator>
void process(Operator &op) { void process(Operator &op) {
namespace field = matador::access; namespace field = matador::access;
field::primary_key(op, "id", id); field::primary_key(op, "id", id);
field::attribute(op, "name", name, 255); field::attribute(op, "name", name, 255);
field::has_many_to_many(op, "student_courses", courses, "student_id", "course_id", utils::fetch_type::Lazy); field::has_many_to_many(op, "student_courses", courses, "student_id", "course_id", utils::CascadeAllFetchLazy);
} }
}; };
struct course { struct course {
@ -40,23 +40,25 @@ struct course {
std::vector<object::object_ptr<student> > students; std::vector<object::object_ptr<student> > students;
course() = default; course() = default;
explicit course(unsigned int id, std::string title) explicit course(unsigned int id, std::string title)
: id(id) : id(id)
, title(std::move(title)) {} , title(std::move(title)) {
}
template<class Operator> template<class Operator>
void process(Operator &op) { void process(Operator &op) {
namespace field = matador::access; namespace field = matador::access;
field::primary_key(op, "id", id); field::primary_key(op, "id", id);
field::attribute(op, "title", title, 255); field::attribute(op, "title", title, 255);
field::has_many_to_many(op, "student_courses", students, utils::fetch_type::Eager); field::has_many_to_many(op, "student_courses", students, utils::CascadeAllFetchEager);
} }
}; };
class student_course : public object::many_to_many_relation<student, course> { class student_course : public object::many_to_many_relation<student, course> {
public: public:
student_course() : many_to_many_relation("student_id", "course_id") {} student_course() : many_to_many_relation("student_id", "course_id") {
}
}; };
} }
#endif //QUERY_STUDENT_HPP #endif //QUERY_STUDENT_HPP

View File

@ -6,7 +6,6 @@
#include <string> #include <string>
namespace matador::test { namespace matador::test {
struct supplier { struct supplier {
unsigned int id{}; unsigned int id{};
std::string name; std::string name;
@ -19,6 +18,5 @@ struct supplier {
field::attribute(op, "name", name, 255); field::attribute(op, "name", name, 255);
} }
}; };
} }
#endif //QUERY_SUPPLIER_HPP #endif //QUERY_SUPPLIER_HPP

View File

@ -5,9 +5,7 @@
#include "matador/utils/types.hpp" #include "matador/utils/types.hpp"
namespace matador::test { namespace matador::test {
struct types {
struct types
{
// enum { CSTR_LEN=255 }; // enum { CSTR_LEN=255 };
unsigned int id_ = 0; unsigned int id_ = 0;
@ -30,8 +28,7 @@ struct types
utils::blob_type_t binary_{1, 2, 3, 4}; utils::blob_type_t binary_{1, 2, 3, 4};
template<class Operator> template<class Operator>
void process(Operator &op) void process(Operator &op) {
{
namespace field = matador::access; namespace field = matador::access;
using namespace matador::utils; using namespace matador::utils;
field::primary_key(op, "id", id_); field::primary_key(op, "id", id_);
@ -54,7 +51,6 @@ struct types
field::attribute(op, "val_binary", binary_); field::attribute(op, "val_binary", binary_);
} }
}; };
} }
#endif // QUERY_TYPES_HPP #endif // QUERY_TYPES_HPP