added session tests

This commit is contained in:
Sascha Kühl
2024-04-08 16:38:49 +02:00
parent 5a3cdc8645
commit f977b2afc9
7 changed files with 133 additions and 179 deletions
+57 -12
View File
@@ -1,7 +1,6 @@
#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"
@@ -25,8 +24,21 @@ TEST_CASE("Create sql query data for entity with eager belongs to", "[query][ent
REQUIRE(data.has_value());
REQUIRE(data->root_table_name == "books");
REQUIRE(data->joins.size() == 1);
REQUIRE(data->columns.size() == 9);
REQUIRE(data->where_clause);
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"}
@@ -40,15 +52,19 @@ TEST_CASE("Create sql query data for entity with eager belongs to", "[query][ent
++index;
}
REQUIRE(data->where_clause);
auto cond = data->where_clause->evaluate(db.dialect(), qc);
REQUIRE(cond == "books.id = 17");
auto &jd = data->joins.front();
auto context = db.query(scm)
auto q = db.query(scm)
.select(data->columns)
.from(data->root_table_name)
.join_left(jd.join_table).on(std::move(jd.condition))
.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();
}
@@ -68,8 +84,27 @@ TEST_CASE("Create sql query data for entity with eager has many belongs to", "[q
REQUIRE(data.has_value());
REQUIRE(data->root_table_name == "orders");
REQUIRE(data->joins.size() == 1);
REQUIRE(data->columns.size() == 15);
REQUIRE(data->where_clause);
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"}
@@ -83,6 +118,7 @@ TEST_CASE("Create sql query data for entity with eager has many belongs to", "[q
++index;
}
REQUIRE(data->where_clause);
auto cond = data->where_clause->evaluate(db.dialect(), qc);
REQUIRE(cond == "orders.order_id = 17");
}
@@ -102,8 +138,16 @@ TEST_CASE("Create sql query data for entity with eager many to many", "[query][e
REQUIRE(data.has_value());
REQUIRE(data->root_table_name == "ingredients");
REQUIRE(data->joins.size() == 2);
REQUIRE(data->columns.size() == 4);
REQUIRE(data->where_clause);
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"},
@@ -118,6 +162,7 @@ TEST_CASE("Create sql query data for entity with eager many to many", "[query][e
++index;
}
REQUIRE(data->where_clause);
auto cond = data->where_clause->evaluate(db.dialect(), qc);
REQUIRE(cond == "ingredients.id = 17");
}