fixed backend test compilation

This commit is contained in:
Sascha Kühl
2025-02-12 16:01:52 +01:00
parent 42acaf24ce
commit 5d41a592bb
36 changed files with 727 additions and 575 deletions
+89 -80
View File
@@ -2,6 +2,8 @@
#include "matador/object/attribute_definition.hpp"
#include "matador/sql/column_generator.hpp"
#include "matador/query/condition.hpp"
#include "matador/query/query.hpp"
@@ -12,13 +14,16 @@
#include "models/person.hpp"
#include "models/recipe.hpp"
using namespace matador::object;
using namespace matador::query;
using namespace matador::sql;
using namespace matador::test;
using namespace matador::query;
TEST_CASE_METHOD(QueryFixture, "Create table with foreign key relation", "[query][foreign][relation]") {
schema.attach<airplane>("airplane");
schema.attach<flight>("flight");
auto result = schema.attach<airplane>("airplane")
.and_then( [this] { return schema.attach<flight>("flight");} );
REQUIRE(result.is_ok());
auto res = query::create()
.table<airplane>("airplane", schema)
.execute(db);
@@ -38,9 +43,8 @@ TEST_CASE_METHOD(QueryFixture, "Create table with foreign key relation", "[query
tables_to_drop.emplace("flight");
}
TEST_CASE_METHOD(QueryFixture, "Execute select statement with where clause", "[query][where]")
{
schema.attach<person>("person");
TEST_CASE_METHOD(QueryFixture, "Execute select statement with where clause", "[query][where]") {
REQUIRE(schema.attach<person>("person"));
auto res = query::create()
.table<person>("person", schema)
.execute(db);
@@ -94,11 +98,10 @@ TEST_CASE_METHOD(QueryFixture, "Execute select statement with where clause", "[q
}
}
TEST_CASE_METHOD(QueryFixture, "Execute insert statement", "[query][insert]")
{
TEST_CASE_METHOD(QueryFixture, "Execute insert statement", "[query][insert]") {
auto res = query::create()
.table("person", {
make_pk_column<unsigned long>("id"),
make_pk_column<uint32_t>("id"),
make_column<std::string>("name", 255),
make_column<std::string>("color", 63)
})
@@ -127,7 +130,7 @@ TEST_CASE_METHOD(QueryFixture, "Execute insert statement", "[query][insert]")
REQUIRE(i.size() == 3);
REQUIRE(i.at(0).name() == "id");
REQUIRE(i.at(0).is_integer());
REQUIRE(i.at(0).as<unsigned long>() == 7);
REQUIRE(i.at(0).as<uint32_t>() == 7);
REQUIRE(i.at(1).name() == "name");
REQUIRE(i.at(1).is_varchar());
REQUIRE(i.at(1).as<std::string>() == "george");
@@ -137,10 +140,11 @@ TEST_CASE_METHOD(QueryFixture, "Execute insert statement", "[query][insert]")
}
}
TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key", "[query][foreign]")
{
schema.attach<airplane>("airplane");
schema.attach<flight>("flight");
TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key", "[query][foreign]") {
auto result = schema.attach<airplane>("airplane")
.and_then( [this] { return schema.attach<flight>("flight");} );
REQUIRE(result.is_ok());
auto res = query::create()
.table<airplane>("airplane", schema)
.execute(db);
@@ -159,10 +163,10 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key", "[query][for
REQUIRE(db.exists("flight"));
tables_to_drop.emplace("flight");
std::vector<matador::object_ptr<airplane>> planes{
matador::object_ptr<airplane>(new airplane{1, "Airbus", "A380"}),
matador::object_ptr<airplane>(new airplane{2, "Boeing", "707"}),
matador::object_ptr<airplane>(new airplane{3, "Boeing", "747"})
std::vector planes{
object_ptr(new airplane{1, "Airbus", "A380"}),
object_ptr(new airplane{2, "Boeing", "707"}),
object_ptr(new airplane{3, "Boeing", "747"})
};
for (const auto &plane: planes) {
@@ -194,15 +198,16 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key", "[query][for
.fetch_one(db);
REQUIRE(f.is_ok());
REQUIRE(f.value()->at(0).as<unsigned long>() == 4);
REQUIRE(f.value()->at(1).as<unsigned long>() == 2);
REQUIRE(f.value()->at(0).as<uint32_t>() == 4);
REQUIRE(f.value()->at(1).as<uint32_t>() == 2);
REQUIRE(f.value()->at(2).as<std::string>() == "hans");
}
TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key and join_left", "[query][foreign][join_left]")
{
schema.attach<airplane>("airplane");
schema.attach<flight>("flight");
TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key and join_left", "[query][foreign][join_left]") {
auto result = schema.attach<airplane>("airplane")
.and_then( [this] { return schema.attach<flight>("flight");} );
REQUIRE(result.is_ok());
auto res = query::create()
.table<airplane>("airplane", schema)
.execute(db);
@@ -221,10 +226,10 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key and join_left"
REQUIRE(db.exists("flight"));
tables_to_drop.emplace("flight");
std::vector<matador::object_ptr<airplane>> planes{
matador::object_ptr<airplane>(new airplane{1, "Airbus", "A380"}),
matador::object_ptr<airplane>(new airplane{2, "Boeing", "707"}),
matador::object_ptr<airplane>(new airplane{3, "Boeing", "747"})
std::vector planes{
object_ptr(new airplane{1, "Airbus", "A380"}),
object_ptr(new airplane{2, "Boeing", "707"}),
object_ptr(new airplane{3, "Boeing", "747"})
};
for (const auto &plane: planes) {
@@ -241,11 +246,11 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key and join_left"
.fetch_value<int>(db).value();
REQUIRE(count == 3);
std::vector<matador::object_ptr<flight>> flights{
matador::object_ptr<flight>(new flight{4, planes.at(0), "hans"}),
matador::object_ptr<flight>(new flight{5, planes.at(0), "otto"}),
matador::object_ptr<flight>(new flight{6, planes.at(1), "george"}),
matador::object_ptr<flight>(new flight{7, planes.at(2), "paul"})
std::vector flights{
object_ptr(new flight{4, planes.at(0), "hans"}),
object_ptr(new flight{5, planes.at(0), "otto"}),
object_ptr(new flight{6, planes.at(1), "george"}),
object_ptr(new flight{7, planes.at(2), "paul"})
};
for (const auto &f: flights) {
@@ -262,35 +267,37 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key and join_left"
.fetch_one(db);
REQUIRE(f.is_ok());
REQUIRE(f->has_value());
REQUIRE(f.value()->at(0).as<unsigned long>() == 4);
REQUIRE(f.value()->at(1).as<unsigned long>() == 1);
REQUIRE(f.value()->at(0).as<uint32_t>() == 4);
REQUIRE(f.value()->at(1).as<uint32_t>() == 1);
REQUIRE(f.value()->at(2).as<std::string>() == "hans");
auto result = query::select({"f.id", "ap.brand", "ap.model", "f.pilot_name"})
auto select_result = query::select({"f.id", "ap.brand", "ap.model", "f.pilot_name"})
.from({"flight", "f"})
.join_left({"airplane", "ap"})
.on("f.airplane_id"_col == "ap.id"_col)
.order_by("f.id").asc()
.fetch_all(db);
REQUIRE(result.is_ok());
REQUIRE(select_result.is_ok());
std::vector<std::pair<unsigned long, std::string>> expected_result {
std::vector<std::pair<uint32_t, std::string>> expected_result {
{4, "hans"},
{5, "otto"},
{6, "george"},
{7, "paul"}
};
size_t index{0};
for (const auto &r: *result) {
for (const auto &r: *select_result) {
REQUIRE(r.size() == 4);
REQUIRE(r.at(0).as<unsigned long>() == expected_result[index].first);
REQUIRE(r.at(0).as<uint32_t>() == expected_result[index].first);
REQUIRE(r.at(3).as<std::string>() == expected_result[index++].second);
}
}
TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key and for single entity", "[query][join_left][find]") {
schema.attach<airplane>("airplane");
schema.attach<flight>("flight");
auto result = schema.attach<airplane>("airplane")
.and_then( [this] { return schema.attach<flight>("flight");} );
REQUIRE(result.is_ok());
auto res = query::create()
.table<airplane>("airplane", schema)
.execute(db);
@@ -309,10 +316,10 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key and for single
REQUIRE(db.exists("flight"));
tables_to_drop.emplace("flight");
std::vector<matador::object_ptr<airplane>> planes{
matador::object_ptr<airplane>(new airplane{1, "Airbus", "A380"}),
matador::object_ptr<airplane>(new airplane{2, "Boeing", "707"}),
matador::object_ptr<airplane>(new airplane{3, "Boeing", "747"})
std::vector planes{
object_ptr(new airplane{1, "Airbus", "A380"}),
object_ptr(new airplane{2, "Boeing", "707"}),
object_ptr(new airplane{3, "Boeing", "747"})
};
for (const auto &plane: planes) {
@@ -331,11 +338,11 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key and for single
REQUIRE(count->has_value());
REQUIRE(count->value() == 3);
std::vector<matador::object_ptr<flight>> flights{
matador::object_ptr<flight>(new flight{4, planes.at(0), "hans"}),
matador::object_ptr<flight>(new flight{5, planes.at(0), "otto"}),
matador::object_ptr<flight>(new flight{6, planes.at(1), "george"}),
matador::object_ptr<flight>(new flight{7, planes.at(2), "paul"})
std::vector flights{
object_ptr(new flight{4, planes.at(0), "hans"}),
object_ptr(new flight{5, planes.at(0), "otto"}),
object_ptr(new flight{6, planes.at(1), "george"}),
object_ptr(new flight{7, planes.at(2), "paul"})
};
for (const auto &f: flights) {
@@ -352,11 +359,11 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key and for single
.fetch_one(db);
REQUIRE(f.is_ok());
REQUIRE(f->has_value());
REQUIRE(f.value()->at(0).as<unsigned long>() == 4);
REQUIRE(f.value()->at(1).as<unsigned long>() == 1);
REQUIRE(f.value()->at(0).as<uint32_t>() == 4);
REQUIRE(f.value()->at(1).as<uint32_t>() == 1);
REQUIRE(f.value()->at(2).as<std::string>() == "hans");
auto result = query::select({"f.id", "f.airplane_id", "ap.brand", "ap.model", "f.pilot_name"})
auto select_result = query::select({"f.id", "f.airplane_id", "ap.brand", "ap.model", "f.pilot_name"})
.from({"flight", "f"})
.join_left({"airplane", "ap"})
.on("f.airplane_id"_col == "ap.id"_col)
@@ -365,20 +372,22 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key and for single
auto expected_flight = flights[0];
REQUIRE(result.is_ok());
REQUIRE(*result);
REQUIRE(result.value()->id == expected_flight->id);
REQUIRE(result.value()->pilot_name == expected_flight->pilot_name);
REQUIRE(result.value()->airplane.get());
REQUIRE(result.value()->airplane->id == 1);
REQUIRE(result.value()->airplane->model == "A380");
REQUIRE(result.value()->airplane->brand == "Airbus");
REQUIRE(select_result.is_ok());
REQUIRE(*select_result);
auto f1 = select_result.release();
REQUIRE(f1->id == expected_flight->id);
REQUIRE(f1->pilot_name == expected_flight->pilot_name);
REQUIRE(f1->airplane.get());
REQUIRE(f1->airplane->id == 1);
REQUIRE(f1->airplane->model == "A380");
REQUIRE(f1->airplane->brand == "Airbus");
}
TEST_CASE_METHOD(QueryFixture, "Select statement with many to many relationship", "[query][join][many_to_many]") {
schema.attach<recipe>("recipes");
schema.attach<ingredient>("ingredients");
schema.attach<recipe_ingredient>("recipe_ingredients");
auto result = schema.attach<recipe>("recipes")
.and_then( [this] { return schema.attach<ingredient>("ingredients"); } )
.and_then( [this] { return schema.attach<recipe_ingredient>("recipe_ingredients"); } );
auto res = query::create()
.table<recipe>("recipes", schema)
.execute(db);
@@ -460,14 +469,14 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with many to many relationship"
REQUIRE(*res == 1);
}
auto result = query::select({"r.id", "r.name", "ri.ingredient_id"})
auto select_result = query::select({"r.id", "r.name", "ri.ingredient_id"})
.from({"recipes", "r"})
.join_left({"recipe_ingredients", "ri"})
.on("r.id"_col == "ri.recipe_id"_col)
.fetch_all(db);
REQUIRE(result.is_ok());
REQUIRE(select_result.is_ok());
std::vector<std::tuple<unsigned long, std::string, unsigned long>> expected_result_one_join {
std::vector<std::tuple<uint32_t, std::string, uint32_t>> expected_result_one_join {
{7, "Apple Crumble", 1},
{7, "Apple Crumble", 4},
{7, "Apple Crumble", 5},
@@ -478,22 +487,22 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with many to many relationship"
{9, "Fruit Salad", 3}
};
size_t index{0};
for (const auto &r: *result) {
for (const auto &r: *select_result) {
REQUIRE(r.size() == 3);
REQUIRE(r.at(0).as<unsigned long>().value() == std::get<0>(expected_result_one_join[index]));
REQUIRE(r.at(0).as<uint32_t>().value() == std::get<0>(expected_result_one_join[index]));
REQUIRE(r.at(1).as<std::string>().value() == std::get<1>(expected_result_one_join[index]));
REQUIRE(r.at(2).as<unsigned long>().value() == std::get<2>(expected_result_one_join[index]));
REQUIRE(r.at(2).as<uint32_t>().value() == std::get<2>(expected_result_one_join[index]));
++index;
}
result = query::select({"r.id", "r.name", "ri.ingredient_id", "i.name"})
select_result = query::select({"r.id", "r.name", "ri.ingredient_id", "i.name"})
.from({"recipes", "r"})
.join_left({"recipe_ingredients", "ri"}).on("r.id"_col == "ri.recipe_id"_col)
.join_left({"ingredients", "i"}).on("ri.ingredient_id"_col == "i.id"_col)
.fetch_all(db);
REQUIRE(result.is_ok());
std::vector<std::tuple<unsigned long, std::string, unsigned long, std::string>> expected_result_two_joins {
std::vector<std::tuple<uint32_t, std::string, uint32_t, std::string>> expected_result_two_joins {
{7, "Apple Crumble", 1, "Apple"},
{7, "Apple Crumble", 4, "Sugar"},
{7, "Apple Crumble", 5, "Flour"},
@@ -504,16 +513,16 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with many to many relationship"
{9, "Fruit Salad", 3, "Pineapple"}
};
index = 0;
for (const auto &r: *result) {
for (const auto &r: *select_result) {
REQUIRE(r.size() == 4);
REQUIRE(r.at(0).as<unsigned long>().value() == std::get<0>(expected_result_two_joins[index]));
REQUIRE(r.at(0).as<uint32_t>().value() == std::get<0>(expected_result_two_joins[index]));
REQUIRE(r.at(1).as<std::string>().value() == std::get<1>(expected_result_two_joins[index]));
REQUIRE(r.at(2).as<unsigned long>().value() == std::get<2>(expected_result_two_joins[index]));
REQUIRE(r.at(2).as<uint32_t>().value() == std::get<2>(expected_result_two_joins[index]));
REQUIRE(r.at(3).as<std::string>().value() == std::get<3>(expected_result_two_joins[index]));
++index;
}
result = query::select({"r.id", "r.name", "ri.ingredient_id", "i.name"})
select_result = query::select({"r.id", "r.name", "ri.ingredient_id", "i.name"})
.from({"recipes", "r"})
.join_left({"recipe_ingredients", "ri"}).on("r.id"_col == "ri.recipe_id"_col)
.join_left({"ingredients", "i"}).on("ri.ingredient_id"_col == "i.id"_col)
@@ -522,11 +531,11 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with many to many relationship"
REQUIRE(result.is_ok());
index = 3;
for (const auto &r: *result) {
for (const auto &r: *select_result) {
REQUIRE(r.size() == 4);
REQUIRE(r.at(0).as<unsigned long>().value() == std::get<0>(expected_result_two_joins[index]));
REQUIRE(r.at(0).as<uint32_t>().value() == std::get<0>(expected_result_two_joins[index]));
REQUIRE(r.at(1).as<std::string>().value() == std::get<1>(expected_result_two_joins[index]));
REQUIRE(r.at(2).as<unsigned long>().value() == std::get<2>(expected_result_two_joins[index]));
REQUIRE(r.at(2).as<uint32_t>().value() == std::get<2>(expected_result_two_joins[index]));
REQUIRE(r.at(3).as<std::string>().value() == std::get<3>(expected_result_two_joins[index]));
++index;
}