fixed PostgreSQL tests
This commit is contained in:
+104
-83
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user