fixed PostgreSQL tests

This commit is contained in:
2026-01-01 17:08:20 +01:00
parent 7cb64515bd
commit a79d50e6e8
17 changed files with 230 additions and 329 deletions
+104 -83
View File
@@ -4,6 +4,7 @@
#include "matador/query/criteria.hpp"
#include "matador/query/generator.hpp"
#include "matador/query/query.hpp"
#include "matador/query/meta_table_macro.hpp"
#include "QueryFixture.hpp"
@@ -17,6 +18,12 @@ using namespace matador::query;
using namespace matador::sql;
using namespace matador::test;
META_TABLE(recipes, RECIPE, id, name);
META_TABLE(ingredients, INGREDIENT, id, name);
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);
TEST_CASE_METHOD(QueryFixture, "Create table with foreign key relation", "[query][foreign][relation]") {
auto result = repo.attach<airplane>("airplane")
.and_then( [this] { return repo.attach<flight>("flight");} );
@@ -80,13 +87,13 @@ TEST_CASE_METHOD(QueryFixture, "Execute select statement with where clause", "[q
for (const auto &i: *result_record) {
REQUIRE(i.size() == 4);
REQUIRE(i.at(0).name() == "id");
REQUIRE(i.at(0).name() == "person.id");
REQUIRE(i.at(0).is_integer());
REQUIRE(i.at(0).as<uint32_t>() == george.id);
REQUIRE(i.at(1).name() == "name");
REQUIRE(i.at(1).name() == "person.name");
REQUIRE(i.at(1).is_varchar());
REQUIRE(i.at(1).as<std::string>() == george.name);
REQUIRE(i.at(2).name() == "age");
REQUIRE(i.at(2).name() == "person.age");
REQUIRE(i.at(2).is_integer());
REQUIRE(i.at(2).as<uint32_t>() == george.age);
}
@@ -221,11 +228,11 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key", "[query][for
}
TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key and join_left", "[query][foreign][join_left]") {
auto result = repo.attach<airplane>("airplane")
.and_then( [this] { return repo.attach<flight>("flight");} );
auto result = repo.attach<airplane>("airplanes")
.and_then( [this] { return repo.attach<flight>("flights");} );
REQUIRE(result.is_ok());
auto obj = object_generator::generate<airplane>(repo.repo(), "airplane");
auto obj = object_generator::generate<airplane>(repo.repo(), "airplanes");
auto res = query::create()
.table(obj->name())
.columns(obj->attributes())
@@ -234,10 +241,10 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key and join_left"
REQUIRE(res.is_ok());
REQUIRE(*res == 0);
REQUIRE(db.exists("airplane"));
tables_to_drop.emplace("airplane");
REQUIRE(db.exists("airplanes"));
tables_to_drop.emplace("airplanes");
obj = object_generator::generate<flight>(repo.repo(), "flight");
obj = object_generator::generate<flight>(repo.repo(), "flights");
res = query::create()
.table(obj->name())
.columns(obj->attributes())
@@ -246,8 +253,8 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key and join_left"
REQUIRE(res.is_ok());
REQUIRE(*res == 0);
REQUIRE(db.exists("flight"));
tables_to_drop.emplace("flight");
REQUIRE(db.exists("flights"));
tables_to_drop.emplace("flights");
std::vector planes{
object_ptr(new airplane{1, "Airbus", "A380"}),
@@ -257,7 +264,7 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key and join_left"
for (const auto &plane: planes) {
res = query::insert()
.into("airplane", generator::columns<airplane>(repo))
.into("airplanes", generator::columns<airplane>(repo))
.values(*plane)
.execute(db);
REQUIRE(res.is_ok());
@@ -265,7 +272,7 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key and join_left"
}
auto count = query::select({count_all()})
.from("airplane")
.from("airplanes")
.fetch_value<int>(db).value();
REQUIRE(count == 3);
@@ -278,27 +285,31 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key and join_left"
for (const auto &f: flights) {
res = query::insert()
.into("flight", {"id", "airplane_id", "pilot_name"})
.into("flights", {"id", "airplane_id", "pilot_name"})
.values(*f)
.execute(db);
REQUIRE(res.is_ok());
REQUIRE(*res == 1);
}
auto f = query::select(generator::columns<flight>(repo))
.from("flight")
auto flight_result = query::select(generator::columns<flight>(repo))
.from("flights")
.fetch_one(db);
REQUIRE(f.is_ok());
REQUIRE(f->has_value());
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");
REQUIRE(flight_result.is_ok());
REQUIRE(flight_result->has_value());
REQUIRE(flight_result.value()->at(0).as<uint32_t>() == 4);
REQUIRE(flight_result.value()->at(1).as<uint32_t>() == 1);
REQUIRE(flight_result.value()->at(2).as<std::string>() == "hans");
auto select_result = query::select({"f.id", "ap.brand", "ap.model", "f.pilot_name"})
.from("flight"_tab.as("f"))
.join_left("airplane"_tab.as("ap"))
.on("f.airplane_id"_col == "ap.id"_col)
.order_by("f.id").asc()
using namespace matador::query::meta;
const auto f = FLIGHT.as("f");
const auto ap = AIRPLANE.as("ap");
auto select_result = query::select({f.id, ap.brand, ap.model, f.pilot_name})
.from(f)
.join_left(ap)
.on(f.airplane_id == ap.id)
.order_by(f.id).asc()
.fetch_all(db);
REQUIRE(select_result.is_ok());
@@ -317,11 +328,11 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key and join_left"
}
TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key and for single entity", "[query][join_left][find]") {
auto result = repo.attach<airplane>("airplane")
.and_then( [this] { return repo.attach<flight>("flight");} );
auto result = repo.attach<airplane>("airplanes")
.and_then( [this] { return repo.attach<flight>("flights");} );
REQUIRE(result.is_ok());
auto obj = object_generator::generate<airplane>(repo.repo(), "airplane");
auto obj = object_generator::generate<airplane>(repo.repo(), "airplanes");
auto res = query::create()
.table(obj->name())
.columns(obj->attributes())
@@ -330,10 +341,10 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key and for single
REQUIRE(res.is_ok());
REQUIRE(*res == 0);
REQUIRE(db.exists("airplane"));
tables_to_drop.emplace("airplane");
REQUIRE(db.exists("airplanes"));
tables_to_drop.emplace("airplanes");
obj = object_generator::generate<flight>(repo.repo(), "flight");
obj = object_generator::generate<flight>(repo.repo(), "flights");
res = query::create()
.table(obj->name())
.columns(obj->attributes())
@@ -342,8 +353,8 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key and for single
REQUIRE(res.is_ok());
REQUIRE(*res == 0);
REQUIRE(db.exists("flight"));
tables_to_drop.emplace("flight");
REQUIRE(db.exists("flights"));
tables_to_drop.emplace("flights");
std::vector planes{
object_ptr(new airplane{1, "Airbus", "A380"}),
@@ -353,7 +364,7 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key and for single
for (const auto &plane: planes) {
res = query::insert()
.into("airplane", generator::columns<airplane>(repo))
.into("airplanes", generator::columns<airplane>(repo))
.values(*plane)
.execute(db);
REQUIRE(res.is_ok());
@@ -361,7 +372,7 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key and for single
}
auto count = query::select({count_all()})
.from("airplane")
.from("airplanes")
.fetch_value<int>(db);
REQUIRE(count.is_ok());
REQUIRE(count->has_value());
@@ -376,27 +387,31 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key and for single
for (const auto &f: flights) {
res = query::insert()
.into("flight", generator::columns<flight>(repo))
.into("flights", generator::columns<flight>(repo))
.values(*f)
.execute(db);
REQUIRE(res.is_ok());
REQUIRE(*res == 1);
}
auto f = query::select(generator::columns<flight>(repo))
.from("flight")
auto flight_result = query::select(generator::columns<flight>(repo))
.from("flights")
.fetch_one(db);
REQUIRE(f.is_ok());
REQUIRE(f->has_value());
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");
REQUIRE(flight_result.is_ok());
REQUIRE(flight_result->has_value());
REQUIRE(flight_result.value()->at(0).as<uint32_t>() == 4);
REQUIRE(flight_result.value()->at(1).as<uint32_t>() == 1);
REQUIRE(flight_result.value()->at(2).as<std::string>() == "hans");
auto select_result = query::select({"f.id", "f.airplane_id", "ap.brand", "ap.model", "f.pilot_name"})
.from("flight"_tab.as("f"))
.join_left("airplane"_tab.as("ap"))
.on("f.airplane_id"_col == "ap.id"_col)
.where("f.id"_col == 4)
using namespace matador::query::meta;
const auto f = FLIGHT.as("f");
const auto ap = AIRPLANE.as("ap");
auto select_result = query::select({f.id, f.airplane_id, ap.brand, ap.model, f.pilot_name})
.from(f)
.join_left(ap)
.on(f.airplane_id == ap.id)
.where(f.id == 4)
.fetch_one<flight>(db);
auto expected_flight = flights[0];
@@ -506,10 +521,16 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with many to many relationship"
REQUIRE(*res == 1);
}
auto select_result = query::select({"r.id", "r.name", "ri.ingredient_id"})
.from("recipes"_tab.as("r"))
.join_left("recipe_ingredients"_tab.as("ri"))
.on("r.id"_col == "ri.recipe_id"_col)
using namespace matador::query::meta;
const auto r = RECIPE.as("r");
const auto ri= RECIPE_INGREDIENT.as("ri");
const auto i = INGREDIENT.as("i");
auto select_result = query::select({ r.id, r.name, ri.ingredient_id})
.from(r)
.join_left(ri)
.on(r.id == ri.recipe_id)
.fetch_all(db);
REQUIRE(select_result.is_ok());
@@ -524,20 +545,20 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with many to many relationship"
{9, "Fruit Salad", 3}
};
size_t index{0};
for (const auto &r: *select_result) {
REQUIRE(r.size() == 3);
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<uint32_t>().value() == std::get<2>(expected_result_one_join[index]));
for (const auto &row: *select_result) {
REQUIRE(row.size() == 3);
REQUIRE(row.at(0).as<uint32_t>().value() == std::get<0>(expected_result_one_join[index]));
REQUIRE(row.at(1).as<std::string>().value() == std::get<1>(expected_result_one_join[index]));
REQUIRE(row.at(2).as<uint32_t>().value() == std::get<2>(expected_result_one_join[index]));
++index;
}
select_result = query::select({"r.id", "r.name", "ri.ingredient_id", "i.name"})
.from("recipes"_tab.as("r"))
.join_left("recipe_ingredients"_tab.as("ri"))
.on("r.id"_col == "ri.recipe_id"_col)
.join_left("ingredients"_tab.as("i"))
.on("ri.ingredient_id"_col == "i.id"_col)
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)
.fetch_all(db);
REQUIRE(result.is_ok());
@@ -552,32 +573,32 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with many to many relationship"
{9, "Fruit Salad", 3, "Pineapple"}
};
index = 0;
for (const auto &r: *select_result) {
REQUIRE(r.size() == 4);
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<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]));
for (const auto &row: *select_result) {
REQUIRE(row.size() == 4);
REQUIRE(row.at(0).as<uint32_t>().value() == std::get<0>(expected_result_two_joins[index]));
REQUIRE(row.at(1).as<std::string>().value() == std::get<1>(expected_result_two_joins[index]));
REQUIRE(row.at(2).as<uint32_t>().value() == std::get<2>(expected_result_two_joins[index]));
REQUIRE(row.at(3).as<std::string>().value() == std::get<3>(expected_result_two_joins[index]));
++index;
}
select_result = query::select({"r.id", "r.name", "ri.ingredient_id", "i.name"})
.from("recipes"_tab.as("r"))
.join_left("recipe_ingredients"_tab.as("ri"))
.on("r.id"_col == "ri.recipe_id"_col)
.join_left("ingredients"_tab.as("i"))
.on("ri.ingredient_id"_col == "i.id"_col)
.where("r.id"_col == 8)
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)
.where(r.id == 8)
.fetch_all(db);
REQUIRE(result.is_ok());
index = 3;
for (const auto &r: *select_result) {
REQUIRE(r.size() == 4);
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<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]));
for (const auto &row: *select_result) {
REQUIRE(row.size() == 4);
REQUIRE(row.at(0).as<uint32_t>().value() == std::get<0>(expected_result_two_joins[index]));
REQUIRE(row.at(1).as<std::string>().value() == std::get<1>(expected_result_two_joins[index]));
REQUIRE(row.at(2).as<uint32_t>().value() == std::get<2>(expected_result_two_joins[index]));
REQUIRE(row.at(3).as<std::string>().value() == std::get<3>(expected_result_two_joins[index]));
++index;
}
}
+5 -2
View File
@@ -1,15 +1,18 @@
#include "SessionFixture.hpp"
#include "connection.hpp"
#include "catch2/catch_test_macros.hpp"
namespace matador::test {
SessionFixture::SessionFixture()
: pool(connection::dns, 4)
, ses({bus, pool}) {}
, ses({bus, pool}, schema) {}
SessionFixture::~SessionFixture() {
const auto result = ses.drop_schema();
const auto conn = pool.acquire();
const auto result = schema.drop(*conn);
REQUIRE(result.is_ok());
}
+2 -2
View File
@@ -5,8 +5,6 @@
#include "matador/utils/message_bus.hpp"
#include "connection.hpp"
#include <stack>
namespace matador::test {
@@ -21,6 +19,8 @@ protected:
orm::session ses;
utils::message_bus bus;
query::schema schema;
private:
void drop_table_if_exists(const std::string &table_name) const;
+49 -22
View File
@@ -16,8 +16,11 @@ using namespace matador::object;
using namespace matador::test;
TEST_CASE_METHOD(SessionFixture, "Session insert test", "[session][insert]") {
const auto result = ses.attach<airplane>("airplanes")
.and_then([this] { return ses.create_schema(); } );
const auto result = schema.attach<airplane>("airplanes")
.and_then([this] {
const auto conn = pool.acquire();
return schema.create(*conn);
} );
REQUIRE(result.is_ok());
auto plane = ses.insert<airplane>(1, "Boeing", "A380");
@@ -32,8 +35,11 @@ TEST_CASE_METHOD(SessionFixture, "Session insert test", "[session][insert]") {
}
TEST_CASE_METHOD(SessionFixture, "Session update test", "[session][update]") {
const auto res = ses.attach<airplane>("airplanes")
.and_then([this] { return ses.create_schema(); } );
const auto res = schema.attach<airplane>("airplanes")
.and_then([this] {
const auto conn = pool.acquire();
return schema.create(*conn);
} );
REQUIRE(res.is_ok());
auto result = ses.insert<airplane>(1, "Boeing", "747");
@@ -62,8 +68,11 @@ TEST_CASE_METHOD(SessionFixture, "Session update test", "[session][update]") {
}
TEST_CASE_METHOD(SessionFixture, "Session delete test", "[session][delete]") {
const auto res = ses.attach<airplane>("airplanes")
.and_then([this] { return ses.create_schema(); } );
const auto res = schema.attach<airplane>("airplanes")
.and_then([this] {
const auto conn = pool.acquire();
return schema.create(*conn);
} );
REQUIRE(res.is_ok());
auto result = ses.insert<airplane>(1, "Boeing", "747");
@@ -83,9 +92,12 @@ TEST_CASE_METHOD(SessionFixture, "Session delete test", "[session][delete]") {
}
TEST_CASE_METHOD(SessionFixture, "Session relation test", "[session][relation]") {
const auto result = ses.attach<airplane>("airplanes")
.and_then([this] { return ses.attach<flight>("flights"); } )
.and_then([this] { return ses.create_schema(); } );
const auto result = schema.attach<airplane>("airplanes")
.and_then([this] { return schema.attach<flight>("flights"); } )
.and_then([this] {
const auto conn = pool.acquire();
return schema.create(*conn);
} );
REQUIRE(result.is_ok());
auto plane = ses.insert<airplane>(1, "Boeing", "A380");
@@ -105,8 +117,11 @@ TEST_CASE_METHOD(SessionFixture, "Session relation test", "[session][relation]")
}
TEST_CASE_METHOD(SessionFixture, "Use session to find object with id", "[session][find]") {
const auto result = ses.attach<airplane>("airplanes")
.and_then([this] { return ses.create_schema(); } );
const auto result = schema.attach<airplane>("airplanes")
.and_then([this] {
const auto conn = pool.acquire();
return schema.create(*conn);
} );
REQUIRE(result.is_ok());
auto a380 = ses.insert<airplane>(1, "Boeing", "A380");
@@ -124,8 +139,11 @@ TEST_CASE_METHOD(SessionFixture, "Use session to find object with id", "[session
}
TEST_CASE_METHOD(SessionFixture, "Use session to find all objects", "[session][find]") {
const auto result = ses.attach<airplane>("airplanes")
.and_then([this] { return ses.create_schema(); } );
const auto result = schema.attach<airplane>("airplanes")
.and_then([this] {
const auto conn = pool.acquire();
return schema.create(*conn);
} );
REQUIRE(result.is_ok());
std::vector<std::unique_ptr<airplane>> planes;
@@ -157,9 +175,12 @@ TEST_CASE_METHOD(SessionFixture, "Use session to find all objects", "[session][f
}
TEST_CASE_METHOD(SessionFixture, "Use session to find all objects with one-to-many lazy relation", "[session][find][one-to-many][eager]") {
auto result = ses.attach<author>("authors")
.and_then( [this] { return ses.attach<book>("books"); } )
.and_then( [this] { return ses.create_schema(); } );
auto result = schema.attach<author>("authors")
.and_then( [this] { return schema.attach<book>("books"); } )
.and_then([this] {
const auto conn = pool.acquire();
return schema.create(*conn);
} );
std::vector<std::unique_ptr<author>> authors;
authors.emplace_back(new author{1, "Michael", "Crichton", "23.10.1942", 1975, true, {}});
@@ -207,9 +228,12 @@ TEST_CASE_METHOD(SessionFixture, "Use session to find all objects with one-to-ma
}
TEST_CASE_METHOD(SessionFixture, "Use session to find all objects with one-to-many eager relation", "[session][find][one-to-many][eager]") {
auto result = ses.attach<department>("departments")
.and_then( [this] { return ses.attach<employee>("employees"); } )
.and_then( [this] { return ses.create_schema(); } );
auto result = schema.attach<department>("departments")
.and_then( [this] { return schema.attach<employee>("employees"); } )
.and_then([this] {
const auto conn = pool.acquire();
return schema.create(*conn);
} );
std::vector<std::unique_ptr<department>> departments;
departments.emplace_back(new department{1, "Insurance", {}});
@@ -260,9 +284,12 @@ TEST_CASE_METHOD(SessionFixture, "Use session to find all objects with one-to-ma
}
TEST_CASE_METHOD(SessionFixture, "Use session to find all objects with many-to-many eager relation", "[session][find][many-to-many][eager]") {
auto result = ses.attach<recipe>("recipes")
.and_then( [this] { return ses.attach<ingredient>("ingredients"); } )
.and_then( [this] { return ses.create_schema(); } );
auto result = schema.attach<recipe>("recipes")
.and_then( [this] { return schema.attach<ingredient>("ingredients"); } )
.and_then([this] {
const auto conn = pool.acquire();
return schema.create(*conn);
} );
std::vector<std::unique_ptr<ingredient>> ingredients;
ingredients.push_back(std::make_unique<ingredient>(1, "Apple"));