168 lines
5.3 KiB
C++
168 lines
5.3 KiB
C++
#include <catch2/catch_test_macros.hpp>
|
|
|
|
#include <matador/sql/connection.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);
|
|
const std::vector<column> expected_columns {
|
|
{ "books", "id", "c01" },
|
|
{ "books", "title", "c02" },
|
|
{ "authors", "id", "c03" },
|
|
{ "authors", "first_name", "c04" },
|
|
{ "authors", "last_name", "c05" },
|
|
{ "authors", "date_of_birth", "c06" },
|
|
{ "authors", "year_of_birth", "c07" },
|
|
{ "authors", "distinguished", "c08" },
|
|
{ "books", "published_in", "c09" }
|
|
};
|
|
REQUIRE(data->columns.size() == expected_columns.size());
|
|
for (size_t i = 0; i != expected_columns.size(); ++i) {
|
|
REQUIRE(expected_columns[i].equals(data->columns[i]));
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
REQUIRE(data->where_clause);
|
|
auto cond = data->where_clause->evaluate(db.dialect(), qc);
|
|
REQUIRE(cond == "books.id = 17");
|
|
|
|
auto q = db.query(scm)
|
|
.select(data->columns)
|
|
.from(data->root_table_name);
|
|
|
|
for (auto &jd : data->joins) {
|
|
q.join_left(jd.join_table)
|
|
.on(std::move(jd.condition));
|
|
}
|
|
auto context = q
|
|
.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);
|
|
const std::vector<column> expected_columns = {
|
|
{ "orders", "order_id", "c01" },
|
|
{ "orders", "order_date", "c02" },
|
|
{ "orders", "required_date", "c03" },
|
|
{ "orders", "shipped_date", "c04" },
|
|
{ "orders", "ship_via", "c05" },
|
|
{ "orders", "freight", "c06" },
|
|
{ "orders", "ship_name", "c07" },
|
|
{ "orders", "ship_address", "c08" },
|
|
{ "orders", "ship_city", "c09" },
|
|
{ "orders", "ship_region", "c10" },
|
|
{ "orders", "ship_postal_code", "c11" },
|
|
{ "orders", "ship_country", "c12" },
|
|
{ "order_details", "order_details_id", "c13" },
|
|
{ "order_details", "order_id", "c14" },
|
|
{ "order_details", "product_id", "c15" }
|
|
};
|
|
REQUIRE(data->columns.size() == expected_columns.size());
|
|
for (size_t i = 0; i != expected_columns.size(); ++i) {
|
|
REQUIRE(expected_columns[i].equals(data->columns[i]));
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
REQUIRE(data->where_clause);
|
|
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);
|
|
const std::vector<column> expected_columns {
|
|
{ "ingredients", "id", "c01" },
|
|
{ "ingredients", "name", "c02" },
|
|
{ "recipes", "id", "c03" },
|
|
{ "recipes", "name", "c04" }
|
|
};
|
|
REQUIRE(data->columns.size() == expected_columns.size());
|
|
for (size_t i = 0; i != expected_columns.size(); ++i) {
|
|
REQUIRE(expected_columns[i].equals(data->columns[i]));
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
REQUIRE(data->where_clause);
|
|
auto cond = data->where_clause->evaluate(db.dialect(), qc);
|
|
REQUIRE(cond == "ingredients.id = 17");
|
|
} |