used make_object for object_ptr in tests

This commit is contained in:
Sascha Kühl 2026-03-08 20:24:34 +01:00
parent 801fbdd187
commit 6bdf281eab
3 changed files with 75 additions and 69 deletions

View File

@ -139,7 +139,7 @@ public:
on_foreign_object(obj, attr); on_foreign_object(obj, attr);
} }
template<class Collection> template<class Collection>
static void on_has_many(const char * /*id*/, Collection &con, const char * /*join_column*/, const utils::foreign_attributes & ) {} static void on_has_many(const char * /*id*/, Collection &/*con*/, const char * /*join_column*/, const utils::foreign_attributes & ) {}
template<class Collection> template<class Collection>
void on_has_many_to_many(const char *id, Collection &con, const char *join_column, const char *inverse_join_column, const utils::foreign_attributes & ) { void on_has_many_to_many(const char *id, Collection &con, const char *join_column, const char *inverse_join_column, const utils::foreign_attributes & ) {
if (id == nullptr || join_column == nullptr || inverse_join_column == nullptr) { if (id == nullptr || join_column == nullptr || inverse_join_column == nullptr) {

View File

@ -6,6 +6,8 @@
#include "matador/query/value_extractor.hpp" #include "matador/query/value_extractor.hpp"
#include "matador/object/object_ptr.hpp"
#include "matador/utils/placeholder.hpp" #include "matador/utils/placeholder.hpp"
namespace matador::query { namespace matador::query {
@ -21,6 +23,10 @@ public:
query_values_intermediate values(const Type &obj) { query_values_intermediate values(const Type &obj) {
return values(value_extractor::extract(obj)); return values(value_extractor::extract(obj));
} }
template<class Type>
query_values_intermediate values(const object::object_ptr<Type> &obj) {
return values(*obj);
}
}; };
} }

View File

@ -186,15 +186,15 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key", "[query][for
REQUIRE(db.exists(FLIGHT.table_name())); REQUIRE(db.exists(FLIGHT.table_name()));
std::vector planes{ std::vector planes{
object_ptr(std::make_shared<airplane>(1, "Airbus", "A380")), make_object<airplane>(1, "Airbus", "A380"),
object_ptr(std::make_shared<airplane>(2, "Boeing", "707")), make_object<airplane>(2, "Boeing", "707"),
object_ptr(std::make_shared<airplane>(3, "Boeing", "747")) make_object<airplane>(3, "Boeing", "747")
}; };
for (const auto &plane: planes) { for (const auto &plane: planes) {
auto res = query::insert() auto res = query::insert()
.into(AIRPLANE, {AIRPLANE.id, AIRPLANE.brand, AIRPLANE.model}) .into(AIRPLANE, {AIRPLANE.id, AIRPLANE.brand, AIRPLANE.model})
.values(*plane) .values(plane)
.execute(db); .execute(db);
REQUIRE(res.is_ok()); REQUIRE(res.is_ok());
REQUIRE(res->affected_rows == 1); REQUIRE(res->affected_rows == 1);
@ -236,15 +236,15 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key and join_left"
REQUIRE(db.exists(FLIGHT.table_name())); REQUIRE(db.exists(FLIGHT.table_name()));
std::vector planes{ std::vector planes{
object_ptr(std::make_shared<airplane>(1, "Airbus", "A380")), make_object<airplane>(1, "Airbus", "A380"),
object_ptr(std::make_shared<airplane>(2, "Boeing", "707")), make_object<airplane>(2, "Boeing", "707"),
object_ptr(std::make_shared<airplane>(3, "Boeing", "747")) make_object<airplane>(3, "Boeing", "747")
}; };
for (const auto &plane: planes) { for (const auto &plane: planes) {
auto res = query::insert() auto res = query::insert()
.into(AIRPLANE, {AIRPLANE.id, AIRPLANE.brand, AIRPLANE.model}) .into(AIRPLANE, {AIRPLANE.id, AIRPLANE.brand, AIRPLANE.model})
.values(*plane) .values(plane)
.execute(db); .execute(db);
REQUIRE(res.is_ok()); REQUIRE(res.is_ok());
REQUIRE(res->affected_rows == 1); REQUIRE(res->affected_rows == 1);
@ -256,16 +256,16 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key and join_left"
REQUIRE(count == 3); REQUIRE(count == 3);
std::vector flights{ std::vector flights{
object_ptr(std::make_shared<flight>(4, planes.at(0), "hans")), make_object<flight>(4, planes.at(0), "hans"),
object_ptr(std::make_shared<flight>(5, planes.at(0), "otto")), make_object<flight>(5, planes.at(0), "otto"),
object_ptr(std::make_shared<flight>(6, planes.at(1), "george")), make_object<flight>(6, planes.at(1), "george"),
object_ptr(std::make_shared<flight>(7, planes.at(2), "paul")) make_object<flight>(7, planes.at(2), "paul"),
}; };
for (const auto &f: flights) { for (const auto &f: flights) {
auto res = query::insert() auto res = query::insert()
.into(FLIGHT, {FLIGHT.id, FLIGHT.airplane_id, FLIGHT.pilot_name}) .into(FLIGHT, {FLIGHT.id, FLIGHT.airplane_id, FLIGHT.pilot_name})
.values(*f) .values(f)
.execute(db); .execute(db);
REQUIRE(res.is_ok()); REQUIRE(res.is_ok());
REQUIRE(res->affected_rows == 1); REQUIRE(res->affected_rows == 1);
@ -317,15 +317,15 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key and for single
REQUIRE(db.exists(FLIGHT.table_name())); REQUIRE(db.exists(FLIGHT.table_name()));
std::vector planes{ std::vector planes{
object_ptr(std::make_shared<airplane>(1, "Airbus", "A380")), make_object<airplane>(1, "Airbus", "A380"),
object_ptr(std::make_shared<airplane>(2, "Boeing", "707")), make_object<airplane>(2, "Boeing", "707"),
object_ptr(std::make_shared<airplane>(3, "Boeing", "747")) make_object<airplane>(3, "Boeing", "747")
}; };
for (const auto &plane: planes) { for (const auto &plane: planes) {
auto res = query::insert() auto res = query::insert()
.into(AIRPLANE, {AIRPLANE.id, AIRPLANE.brand, AIRPLANE.model}) .into(AIRPLANE, {AIRPLANE.id, AIRPLANE.brand, AIRPLANE.model})
.values(*plane) .values(plane)
.execute(db); .execute(db);
REQUIRE(res.is_ok()); REQUIRE(res.is_ok());
REQUIRE(res->affected_rows == 1); REQUIRE(res->affected_rows == 1);
@ -339,16 +339,16 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key and for single
REQUIRE(count->value() == 3); REQUIRE(count->value() == 3);
std::vector flights{ std::vector flights{
object_ptr(std::make_shared<flight>(4, planes.at(0), "hans")), make_object<flight>(4, planes.at(0), "hans"),
object_ptr(std::make_shared<flight>(5, planes.at(0), "otto")), make_object<flight>(5, planes.at(0), "otto"),
object_ptr(std::make_shared<flight>(6, planes.at(1), "george")), make_object<flight>(6, planes.at(1), "george"),
object_ptr(std::make_shared<flight>(7, planes.at(2), "paul")) make_object<flight>(7, planes.at(2), "paul")
}; };
for (const auto &f: flights) { for (const auto &f: flights) {
auto res = query::insert() auto res = query::insert()
.into(FLIGHT, {FLIGHT.id, FLIGHT.airplane_id, FLIGHT.pilot_name}) .into(FLIGHT, {FLIGHT.id, FLIGHT.airplane_id, FLIGHT.pilot_name})
.values(*f) .values(f)
.execute(db); .execute(db);
REQUIRE(res.is_ok()); REQUIRE(res.is_ok());
REQUIRE(res->affected_rows == 1); REQUIRE(res->affected_rows == 1);
@ -541,14 +541,14 @@ TEST_CASE_METHOD(QueryFixture, "Test load entity with eager has many relation",
repo.initialize_executor(db); repo.initialize_executor(db);
const std::vector shipments { const std::vector shipments {
object_ptr{std::make_shared<shipment>(1, "4711")}, make_object<shipment>(1, "4711"),
object_ptr{std::make_shared<shipment>(2, "0815")} make_object<shipment>(2, "0815")
}; };
for (const auto &sh: shipments) { for (const auto &sh: shipments) {
auto res = query::insert() auto res = query::insert()
.into(SHIPMENT, SHIPMENT) .into(SHIPMENT, SHIPMENT)
.values(*sh) .values(sh)
.execute(db); .execute(db);
REQUIRE(res.is_ok()); REQUIRE(res.is_ok());
REQUIRE(res->affected_rows == 1); REQUIRE(res->affected_rows == 1);
@ -561,17 +561,17 @@ TEST_CASE_METHOD(QueryFixture, "Test load entity with eager has many relation",
REQUIRE(*count == 2); REQUIRE(*count == 2);
std::vector packages { std::vector packages {
object_ptr{std::make_shared<package>(3, 15.4, shipments.at(0))}, make_object<package>(3, 15.4, shipments.at(0)),
object_ptr{std::make_shared<package>(4, 1.3, shipments.at(0))}, make_object<package>(4, 1.3, shipments.at(0)),
object_ptr{std::make_shared<package>(5, 30.9, shipments.at(1))}, make_object<package>(5, 30.9, shipments.at(1)),
object_ptr{std::make_shared<package>(6, 22.8, shipments.at(1))}, make_object<package>(6, 22.8, shipments.at(1)),
object_ptr{std::make_shared<package>(7, 17.2, shipments.at(1))} make_object<package>(7, 17.2, shipments.at(1))
}; };
for (const auto &pkg: packages) { for (const auto &pkg: packages) {
auto res = query::insert() auto res = query::insert()
.into(PACKAGE, PACKAGE) .into(PACKAGE, PACKAGE)
.values(*pkg) .values(pkg)
.execute(db); .execute(db);
REQUIRE(res.is_ok()); REQUIRE(res.is_ok());
REQUIRE(res->affected_rows == 1); REQUIRE(res->affected_rows == 1);
@ -639,27 +639,27 @@ TEST_CASE_METHOD(QueryFixture, "Test load entity with lazy has many relation", "
repo.initialize_executor(db); repo.initialize_executor(db);
const std::vector authors { const std::vector authors {
object_ptr{std::make_shared<author>(1, "Michael", "Crichton", "23.10.1942", 1975, true)}, make_object<author>(1, "Michael", "Crichton", "23.10.1942", 1975, true),
object_ptr{std::make_shared<author>( 2, "Steven", "King", "21.9.1947", 1956, false)} make_object<author>(2, "Steven", "King", "21.9.1947", 1956, false)
}; };
const std::vector books { const std::vector books {
object_ptr{std::make_shared<book>(3, "Jurassic Park", authors[0], 1990)}, make_object<book>(3, "Jurassic Park", authors.at(0), 1990),
object_ptr{std::make_shared<book>(4, "Timeline", authors[0], 1999)}, make_object<book>(4, "Timeline", authors.at(0), 1999),
object_ptr{std::make_shared<book>(5, "The Andromeda Strain", authors[0], 1969)}, make_object<book>(5, "The Andromeda Strain", authors.at(0), 1969),
object_ptr{std::make_shared<book>(6, "Congo", authors[0], 1980)}, make_object<book>(6, "Congo", authors.at(0), 1980),
object_ptr{std::make_shared<book>(7, "Prey", authors[0], 2002)}, make_object<book>(7, "Prey", authors.at(0), 2002),
object_ptr{std::make_shared<book>(8, "Carrie", authors[1], 1974)}, make_object<book>(8, "Carrie", authors.at(1), 1974),
object_ptr{std::make_shared<book>(9, "The Shining", authors[1], 1977)}, make_object<book>(9, "The Shining", authors.at(1), 1977),
object_ptr{std::make_shared<book>(10, "It", authors[1], 1986)}, make_object<book>(10, "It", authors.at(1), 1986),
object_ptr{std::make_shared<book>(11, "Misery", authors[1], 1987)}, make_object<book>(11, "Misery", authors.at(1), 1987),
object_ptr{std::make_shared<book>(12, "The Dark Tower: The Gunslinger", authors[1], 1982)}, make_object<book>(12, "The Dark Tower: The Gunslinger", authors.at(1), 1982)
}; };
for (const auto &a: authors) { for (const auto &a: authors) {
auto res = query::insert() auto res = query::insert()
.into(AUTHOR, {AUTHOR.id, AUTHOR.first_name, AUTHOR.last_name, AUTHOR.date_of_birth, AUTHOR.year_of_birth, AUTHOR.distinguished}) .into(AUTHOR, {AUTHOR.id, AUTHOR.first_name, AUTHOR.last_name, AUTHOR.date_of_birth, AUTHOR.year_of_birth, AUTHOR.distinguished})
.values(*a) .values(a)
.execute(db); .execute(db);
REQUIRE(res.is_ok()); REQUIRE(res.is_ok());
REQUIRE(res->affected_rows == 1); REQUIRE(res->affected_rows == 1);
@ -674,7 +674,7 @@ TEST_CASE_METHOD(QueryFixture, "Test load entity with lazy has many relation", "
for (const auto &b: books) { for (const auto &b: books) {
auto res = query::insert() auto res = query::insert()
.into(BOOK, {BOOK.id, BOOK.title, BOOK.author_id, BOOK.published_in}) .into(BOOK, {BOOK.id, BOOK.title, BOOK.author_id, BOOK.published_in})
.values(*b) .values(b)
.execute(db); .execute(db);
REQUIRE(res.is_ok()); REQUIRE(res.is_ok());
REQUIRE(res->affected_rows == 1); REQUIRE(res->affected_rows == 1);
@ -721,22 +721,22 @@ TEST_CASE_METHOD(QueryFixture, "Test load entity with lazy belongs to relation",
repo.initialize_executor(db); repo.initialize_executor(db);
const std::vector deps { const std::vector deps {
object_ptr{std::make_shared<department>(1, "Human Resources")}, make_object<department>(1, "Human Resources"),
object_ptr{std::make_shared<department>(2, "Invoices")} make_object<department>(2, "Invoices")
}; };
const std::vector emps { const std::vector emps {
object_ptr{std::make_shared<employee>(3, "Hans", "Wurst", deps[0])}, make_object<employee>(3, "Hans", "Wurst", deps.at(0)),
object_ptr{std::make_shared<employee>(4, "Steven", "Spielberg", deps[0])}, make_object<employee>(4, "Steven", "Spielberg", deps.at(0)),
object_ptr{std::make_shared<employee>(5, "Julia", "Roberts", deps[0])}, make_object<employee>(5, "Julia", "Roberts", deps.at(0)),
object_ptr{std::make_shared<employee>(6, "Otto", "Walkes", deps[1])}, make_object<employee>(6, "Otto", "Walkes", deps.at(1)),
object_ptr{std::make_shared<employee>(7, "Miss", "Marple", deps[1])}, make_object<employee>(7, "Miss", "Marple", deps.at(1))
}; };
for (const auto &dep: deps) { for (const auto &dep: deps) {
auto res = query::insert() auto res = query::insert()
.into(DEPARTMENT, {DEPARTMENT.id, DEPARTMENT.name}) .into(DEPARTMENT, {DEPARTMENT.id, DEPARTMENT.name})
.values(*dep) .values(dep)
.execute(db); .execute(db);
REQUIRE(res.is_ok()); REQUIRE(res.is_ok());
REQUIRE(res->affected_rows == 1); REQUIRE(res->affected_rows == 1);
@ -751,7 +751,7 @@ TEST_CASE_METHOD(QueryFixture, "Test load entity with lazy belongs to relation",
for (const auto &emp: emps) { for (const auto &emp: emps) {
auto res = query::insert() auto res = query::insert()
.into(EMPLOYEE, {EMPLOYEE.id, EMPLOYEE.first_name, EMPLOYEE.last_name, EMPLOYEE.dep_id}) .into(EMPLOYEE, {EMPLOYEE.id, EMPLOYEE.first_name, EMPLOYEE.last_name, EMPLOYEE.dep_id})
.values(*emp) .values(emp)
.execute(db); .execute(db);
REQUIRE(res.is_ok()); REQUIRE(res.is_ok());
REQUIRE(res->affected_rows == 1); REQUIRE(res->affected_rows == 1);
@ -787,27 +787,27 @@ TEST_CASE_METHOD(QueryFixture, "Test load entity with eager belongs to relation"
REQUIRE(result.is_ok()); REQUIRE(result.is_ok());
const std::vector authors { const std::vector authors {
object_ptr{std::make_shared<author>(1, "Michael", "Crichton", "23.10.1942", 1975, true)}, make_object<author>(1, "Michael", "Crichton", "23.10.1942", 1975, true),
object_ptr{std::make_shared<author>( 2, "Steven", "King", "21.9.1947", 1956, false)} make_object<author>(2, "Steven", "King", "21.9.1947", 1956, false)
}; };
const std::vector books { const std::vector books {
object_ptr{std::make_shared<book>(3, "Jurassic Park", authors[0], 1990)}, make_object<book>(3, "Jurassic Park", authors.at(0), 1990),
object_ptr{std::make_shared<book>(4, "Timeline", authors[0], 1999)}, make_object<book>(4, "Timeline", authors.at(0), 1999),
object_ptr{std::make_shared<book>(5, "The Andromeda Strain", authors[0], 1969)}, make_object<book>(5, "The Andromeda Strain", authors.at(0), 1969),
object_ptr{std::make_shared<book>(6, "Congo", authors[0], 1980)}, make_object<book>(6, "Congo", authors.at(0), 1980),
object_ptr{std::make_shared<book>(7, "Prey", authors[0], 2002)}, make_object<book>(7, "Prey", authors.at(0), 2002),
object_ptr{std::make_shared<book>(8, "Carrie", authors[1], 1974)}, make_object<book>(8, "Carrie", authors.at(1), 1974),
object_ptr{std::make_shared<book>(9, "The Shining", authors[1], 1977)}, make_object<book>(9, "The Shining", authors.at(1), 1977),
object_ptr{std::make_shared<book>(10, "It", authors[1], 1986)}, make_object<book>(10, "It", authors.at(1), 1986),
object_ptr{std::make_shared<book>(11, "Misery", authors[1], 1987)}, make_object<book>(11, "Misery", authors.at(1), 1987),
object_ptr{std::make_shared<book>(12, "The Dark Tower: The Gunslinger", authors[1], 1982)}, make_object<book>(12, "The Dark Tower: The Gunslinger", authors.at(1), 1982)
}; };
for (const auto &a: authors) { for (const auto &a: authors) {
auto res = query::insert() auto res = query::insert()
.into(AUTHOR, {AUTHOR.id, AUTHOR.first_name, AUTHOR.last_name, AUTHOR.date_of_birth, AUTHOR.year_of_birth, AUTHOR.distinguished}) .into(AUTHOR, {AUTHOR.id, AUTHOR.first_name, AUTHOR.last_name, AUTHOR.date_of_birth, AUTHOR.year_of_birth, AUTHOR.distinguished})
.values(*a) .values(a)
.execute(db); .execute(db);
REQUIRE(res.is_ok()); REQUIRE(res.is_ok());
REQUIRE(res->affected_rows == 1); REQUIRE(res->affected_rows == 1);
@ -822,7 +822,7 @@ TEST_CASE_METHOD(QueryFixture, "Test load entity with eager belongs to relation"
for (const auto &b: books) { for (const auto &b: books) {
auto res = query::insert() auto res = query::insert()
.into(BOOK, {BOOK.id, BOOK.title, BOOK.author_id, BOOK.published_in}) .into(BOOK, {BOOK.id, BOOK.title, BOOK.author_id, BOOK.published_in})
.values(*b) .values(b)
.execute(db); .execute(db);
REQUIRE(res.is_ok()); REQUIRE(res.is_ok());
REQUIRE(res->affected_rows == 1); REQUIRE(res->affected_rows == 1);