123 lines
3.7 KiB
C++
123 lines
3.7 KiB
C++
#include <catch2/catch_test_macros.hpp>
|
|
|
|
#include <matador/sql/connection.hpp>
|
|
#include <matador/sql/dialect.hpp>
|
|
#include <matador/sql/entity_query_builder.hpp>
|
|
|
|
#include "models/author.hpp"
|
|
#include "models/book.hpp"
|
|
#include "models/recipe.hpp"
|
|
#include "models/order.hpp"
|
|
|
|
using namespace matador::sql;
|
|
|
|
TEST_CASE("Create sql query data for entity with eager belongs to", "[query][entity][builder]") {
|
|
using namespace matador::test;
|
|
connection db("noop://noop.db");
|
|
schema scm("noop");
|
|
scm.attach<author>("authors");
|
|
scm.attach<book>("books");
|
|
|
|
entity_query_builder eqb(17, scm);
|
|
|
|
auto data = eqb.build<book>();
|
|
|
|
REQUIRE(data.has_value());
|
|
REQUIRE(data->root_table_name == "books");
|
|
REQUIRE(data->joins.size() == 1);
|
|
REQUIRE(data->columns.size() == 9);
|
|
REQUIRE(data->where_clause);
|
|
|
|
std::vector<std::pair<std::string, std::string>> expected_join_data {
|
|
{ "books", "books.author_id = authors.id"}
|
|
};
|
|
|
|
query_context qc;
|
|
size_t index{0};
|
|
for (const auto &jd : data->joins) {
|
|
REQUIRE(jd.join_table.name == expected_join_data[index].first);
|
|
REQUIRE(jd.condition->evaluate(db.dialect(), qc) == expected_join_data[index].second);
|
|
++index;
|
|
}
|
|
|
|
auto cond = data->where_clause->evaluate(db.dialect(), qc);
|
|
REQUIRE(cond == "books.id = 17");
|
|
|
|
auto &jd = data->joins.front();
|
|
|
|
auto context = db.query(scm)
|
|
.select(data->columns)
|
|
.from(data->root_table_name)
|
|
.join_left(jd.join_table).on(std::move(jd.condition))
|
|
.where(std::move(data->where_clause))
|
|
.build();
|
|
}
|
|
|
|
TEST_CASE("Create sql query data for entity with eager has many belongs to", "[query][entity][builder]") {
|
|
using namespace matador::test;
|
|
connection db("noop://noop.db");
|
|
schema scm("noop");
|
|
scm.attach<product>("products");
|
|
scm.attach<order_details>("order_details");
|
|
scm.attach<order>("orders");
|
|
|
|
entity_query_builder eqb(17, scm);
|
|
|
|
auto data = eqb.build<order>();
|
|
|
|
REQUIRE(data.has_value());
|
|
REQUIRE(data->root_table_name == "orders");
|
|
REQUIRE(data->joins.size() == 1);
|
|
REQUIRE(data->columns.size() == 15);
|
|
REQUIRE(data->where_clause);
|
|
|
|
std::vector<std::pair<std::string, std::string>> expected_join_data {
|
|
{ "orders", "orders.order_id = order_details.order_id"}
|
|
};
|
|
|
|
query_context qc;
|
|
size_t index{0};
|
|
for (const auto &jd : data->joins) {
|
|
REQUIRE(jd.join_table.name == expected_join_data[index].first);
|
|
REQUIRE(jd.condition->evaluate(db.dialect(), qc) == expected_join_data[index].second);
|
|
++index;
|
|
}
|
|
|
|
auto cond = data->where_clause->evaluate(db.dialect(), qc);
|
|
REQUIRE(cond == "orders.order_id = 17");
|
|
}
|
|
|
|
TEST_CASE("Create sql query data for entity with eager many to many", "[query][entity][builder]") {
|
|
using namespace matador::test;
|
|
connection db("noop://noop.db");
|
|
schema scm("noop");
|
|
scm.attach<recipe>("recipes");
|
|
scm.attach<ingredient>("ingredients");
|
|
scm.attach<recipe_ingredient>("recipe_ingredients");
|
|
|
|
entity_query_builder eqb(17, scm);
|
|
|
|
auto data = eqb.build<ingredient>();
|
|
|
|
REQUIRE(data.has_value());
|
|
REQUIRE(data->root_table_name == "ingredients");
|
|
REQUIRE(data->joins.size() == 2);
|
|
REQUIRE(data->columns.size() == 4);
|
|
REQUIRE(data->where_clause);
|
|
|
|
std::vector<std::pair<std::string, std::string>> expected_join_data {
|
|
{ "ingredients", "ingredients.id = recipe_ingredients.ingredient_id"},
|
|
{ "recipes", "recipes.id = recipe_ingredients.recipe_id"}
|
|
};
|
|
|
|
query_context qc;
|
|
size_t index{0};
|
|
for (const auto &jd : data->joins) {
|
|
REQUIRE(jd.join_table.name == expected_join_data[index].first);
|
|
REQUIRE(jd.condition->evaluate(db.dialect(), qc) == expected_join_data[index].second);
|
|
++index;
|
|
}
|
|
|
|
auto cond = data->where_clause->evaluate(db.dialect(), qc);
|
|
REQUIRE(cond == "ingredients.id = 17");
|
|
} |