added more tests
This commit is contained in:
@@ -13,12 +13,14 @@ add_executable(OrmTests
|
||||
backend/test_result_reader.hpp
|
||||
backend/test_statement.cpp
|
||||
backend/test_statement.hpp
|
||||
orm/SessionQueryBuilderTest.cpp
|
||||
query/ConditionTests.cpp
|
||||
query/QueryBuilderTest.cpp
|
||||
query/QueryFixture.cpp
|
||||
query/QueryFixture.hpp
|
||||
query/QueryTest.cpp
|
||||
sql/ColumnTest.cpp
|
||||
sql/ColumnGeneratorTest.cpp
|
||||
sql/ConnectionPoolTest.cpp
|
||||
sql/FieldTest.cpp
|
||||
utils/auto_reset_event.cpp
|
||||
|
||||
@@ -62,9 +62,9 @@ utils::result<std::unique_ptr<sql::statement_impl>, utils::error> test_connectio
|
||||
return utils::ok(std::unique_ptr<sql::statement_impl>{});
|
||||
}
|
||||
|
||||
utils::result<std::vector<sql::column_definition>, utils::error> test_connection::describe(const std::string &/*table*/)
|
||||
utils::result<std::vector<object::attribute_definition>, utils::error> test_connection::describe(const std::string &/*table*/)
|
||||
{
|
||||
return utils::ok(std::vector<sql::column_definition>{});
|
||||
return utils::ok(std::vector<object::attribute_definition>{});
|
||||
}
|
||||
|
||||
utils::result<bool, utils::error> test_connection::exists(const std::string &/*schema_name*/, const std::string &/*table_name*/)
|
||||
|
||||
@@ -19,7 +19,7 @@ public:
|
||||
utils::result<size_t, utils::error> execute(const std::string &stmt) override;
|
||||
utils::result<std::unique_ptr<sql::query_result_impl>, utils::error> fetch(const sql::query_context &context) override;
|
||||
utils::result<std::unique_ptr<sql::statement_impl>, utils::error> prepare(const sql::query_context &context) override;
|
||||
utils::result<std::vector<sql::column_definition>, utils::error> describe(const std::string &table) override;
|
||||
utils::result<std::vector<object::attribute_definition>, utils::error> describe(const std::string &table) override;
|
||||
utils::result<bool, utils::error> exists(const std::string &schema_name, const std::string &table_name) override;
|
||||
|
||||
[[nodiscard]] std::string to_escaped_string( const utils::blob& value ) const override;
|
||||
|
||||
@@ -0,0 +1,265 @@
|
||||
#include <catch2/catch_test_macros.hpp>
|
||||
|
||||
#include "matador/sql/connection.hpp"
|
||||
|
||||
#include "matador/query/query.hpp"
|
||||
|
||||
#include "matador/orm/session_query_builder.hpp"
|
||||
|
||||
#include "../../models/airplane.hpp"
|
||||
#include "../../models/author.hpp"
|
||||
#include "../../models/book.hpp"
|
||||
#include "../../models/flight.hpp"
|
||||
#include "../../models/recipe.hpp"
|
||||
#include "../../models/order.hpp"
|
||||
#include "../../models/student.hpp"
|
||||
|
||||
using namespace matador::object;
|
||||
using namespace matador::orm;
|
||||
using namespace matador::sql;
|
||||
|
||||
TEST_CASE("Create sql query data for entity with eager has one", "[query][entity][builder]") {
|
||||
using namespace matador::test;
|
||||
connection db("noop://noop.db");
|
||||
schema scm("noop");
|
||||
auto result = scm.attach<airplane>("airplanes")
|
||||
.and_then( [&scm] { return scm.attach<flight>("flights"); } );
|
||||
REQUIRE(result);
|
||||
|
||||
session_query_builder eqb(scm);
|
||||
|
||||
auto data = eqb.build<flight>(17U);
|
||||
|
||||
REQUIRE(data.is_ok());
|
||||
REQUIRE(data->root_table_name == "flights");
|
||||
REQUIRE(data->joins.size() == 1);
|
||||
const std::vector<column> expected_columns {
|
||||
{ "flights", "id", "c01" },
|
||||
{ "airplanes", "id", "c02" },
|
||||
{ "airplanes", "brand", "c03" },
|
||||
{ "airplanes", "model", "c04" },
|
||||
{ "flights", "pilot_name", "c05" },
|
||||
};
|
||||
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 {
|
||||
{ "airplanes", R"("flights"."airplane_id" = "airplanes"."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 == R"("flights"."id" = 17)");
|
||||
}
|
||||
|
||||
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");
|
||||
auto result = scm.attach<author>("authors")
|
||||
.and_then( [&scm] { return scm.attach<book>("books"); } );
|
||||
REQUIRE(result);
|
||||
|
||||
session_query_builder eqb(scm);
|
||||
|
||||
auto data = eqb.build<book>(17);
|
||||
|
||||
REQUIRE(data.is_ok());
|
||||
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 {
|
||||
{ "authors", R"("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 == R"("books"."id" = 17)");
|
||||
|
||||
auto q = matador::query::query::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))
|
||||
.str(db);
|
||||
}
|
||||
|
||||
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");
|
||||
auto result = scm.attach<product>("products")
|
||||
.and_then( [&scm] { return scm.attach<order_details>("order_details"); } )
|
||||
.and_then( [&scm] { return scm.attach<order>("orders"); } );
|
||||
REQUIRE(result);
|
||||
|
||||
session_query_builder eqb(scm);
|
||||
|
||||
auto data = eqb.build<order>(17);
|
||||
|
||||
REQUIRE(data.is_ok());
|
||||
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 {
|
||||
{ "order_details", R"("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 == R"("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");
|
||||
auto result = scm.attach<recipe>("recipes")
|
||||
.and_then( [&scm] { return scm.attach<ingredient>("ingredients"); } )
|
||||
.and_then( [&scm] { return scm.attach<recipe_ingredient>("recipe_ingredients"); } );
|
||||
|
||||
session_query_builder eqb(scm);
|
||||
|
||||
auto data = eqb.build<ingredient>(17);
|
||||
|
||||
REQUIRE(data.is_ok());
|
||||
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 {
|
||||
{ "recipe_ingredients", R"("ingredients"."id" = "recipe_ingredients"."ingredient_id")"},
|
||||
{ "recipes", R"("recipe_ingredients"."recipe_id" = "recipes"."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 == R"("ingredients"."id" = 17)");
|
||||
}
|
||||
|
||||
TEST_CASE("Create sql query data for entity with eager many to many (inverse part)", "[query][entity][builder]") {
|
||||
using namespace matador::test;
|
||||
connection db("noop://noop.db");
|
||||
schema scm("noop");
|
||||
auto result = scm.attach<student>("students")
|
||||
.and_then( [&scm] { return scm.attach<course>("courses"); } )
|
||||
.and_then( [&scm] { return scm.attach<student_course>("student_courses"); } );
|
||||
|
||||
session_query_builder eqb(scm);
|
||||
|
||||
auto data = eqb.build<course>(17);
|
||||
|
||||
REQUIRE(data.is_ok());
|
||||
REQUIRE(data->root_table_name == "courses");
|
||||
REQUIRE(data->joins.size() == 2);
|
||||
const std::vector<column> expected_columns {
|
||||
{ "courses", "id", "c01" },
|
||||
{ "courses", "title", "c02" },
|
||||
{ "students", "id", "c03" },
|
||||
{ "students", "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 {
|
||||
{ "student_courses", R"("courses"."id" = "student_courses"."course_id")"},
|
||||
{ "students", R"("student_courses"."student_id" = "students"."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 == R"("courses"."id" = 17)");
|
||||
}
|
||||
@@ -5,16 +5,18 @@
|
||||
#include <matador/query/condition.hpp>
|
||||
#include <matador/query/query.hpp>
|
||||
|
||||
#include <matador/sql/column_definition.hpp>
|
||||
#include <matador/sql/connection.hpp>
|
||||
#include <matador/sql/table.hpp>
|
||||
#include "matador/object/attribute_definition_generator.hpp"
|
||||
|
||||
#include <matador/utils/placeholder.hpp>
|
||||
#include "matador/sql/connection.hpp"
|
||||
#include "matador/sql/table.hpp"
|
||||
|
||||
#include "matador/utils/placeholder.hpp"
|
||||
|
||||
// #include "models/author.hpp"
|
||||
// #include "models/book.hpp"
|
||||
|
||||
using namespace matador::test;
|
||||
using namespace matador::object;
|
||||
using namespace matador::sql;
|
||||
using namespace matador::query;
|
||||
using namespace matador::utils;
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
#include <catch2/catch_test_macros.hpp>
|
||||
|
||||
#include "matador/sql/column_generator.hpp"
|
||||
#include "matador/object/schema.hpp"
|
||||
|
||||
#include "../test/models/product.hpp"
|
||||
#include "../test/models/order.hpp"
|
||||
#include "../test/models/book.hpp"
|
||||
#include "../test/models/author.hpp"
|
||||
#include "matador/sql/table.hpp"
|
||||
|
||||
using namespace matador::sql;
|
||||
using namespace matador::object;
|
||||
|
||||
TEST_CASE("Generate columns from object", "[column][generator]") {
|
||||
using namespace matador::test;
|
||||
schema s("main");
|
||||
auto result = s.attach<product>("product");
|
||||
REQUIRE( result );
|
||||
|
||||
auto columns = column_generator::generate<product>(s);
|
||||
|
||||
const std::vector<std::string> expected_columns = {
|
||||
"product_name",
|
||||
"supplier_id",
|
||||
"category_id",
|
||||
"quantity_per_unit",
|
||||
"unit_price",
|
||||
"units_in_stock",
|
||||
"units_in_order",
|
||||
"reorder_level",
|
||||
"discontinued"
|
||||
};
|
||||
REQUIRE(!columns.empty());
|
||||
REQUIRE(columns.size() == expected_columns.size());
|
||||
|
||||
for (size_t i = 0; i != expected_columns.size(); ++i) {
|
||||
REQUIRE(expected_columns[i] == columns[i].name);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("Generate columns for object with has many relation", "[column][generator][relation]") {
|
||||
using namespace matador::test;
|
||||
schema s("main");
|
||||
auto result = s.attach<product>("product")
|
||||
.and_then( [&s] { return s.attach<order_details>("order_details"); } )
|
||||
.and_then( [&s] { return s.attach<order>("order"); } );
|
||||
REQUIRE(result);
|
||||
|
||||
auto columns = column_generator::generate<order>(s);
|
||||
|
||||
const auto order_table = std::make_shared<table>("order");
|
||||
const auto order_details_table = std::make_shared<table>("order_details");
|
||||
const std::vector<column> expected_columns = {
|
||||
{ order_table, "order_id", "c01" },
|
||||
{ order_table, "order_date", "c02" },
|
||||
{ order_table, "required_date", "c03" },
|
||||
{ order_table, "shipped_date", "c04" },
|
||||
{ order_table, "ship_via", "c05" },
|
||||
{ order_table, "freight", "c06" },
|
||||
{ order_table, "ship_name", "c07" },
|
||||
{ order_table, "ship_address", "c08" },
|
||||
{ order_table, "ship_city", "c09" },
|
||||
{ order_table, "ship_region", "c10" },
|
||||
{ order_table, "ship_postal_code", "c11" },
|
||||
{ order_table, "ship_country", "c12" },
|
||||
{ order_details_table, "order_details_id", "c13" },
|
||||
{ order_details_table, "order_id", "c14" },
|
||||
{ order_details_table, "product_id", "c15" }
|
||||
};
|
||||
REQUIRE(!columns.empty());
|
||||
REQUIRE(columns.size() == expected_columns.size());
|
||||
|
||||
for (size_t i = 0; i != expected_columns.size(); ++i) {
|
||||
REQUIRE(expected_columns[i].equals(columns[i]));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
TEST_CASE("Generate columns for object with eager foreign key relation", "[column][generator][eager]") {
|
||||
using namespace matador::test;
|
||||
schema s("main");
|
||||
auto result = s.attach<book>("books")
|
||||
.and_then( [&s] { return s.attach<author>("authors"); } );
|
||||
REQUIRE(result);
|
||||
|
||||
const auto books_table = std::make_shared<table>("books");
|
||||
const auto authors_table = std::make_shared<table>("authors");
|
||||
const std::vector<column> expected_columns {
|
||||
{ books_table, "id", "c01" },
|
||||
{ books_table, "title", "c02" },
|
||||
{ authors_table, "id", "c03" },
|
||||
{ authors_table, "first_name", "c04" },
|
||||
{ authors_table, "last_name", "c05" },
|
||||
{ authors_table, "date_of_birth", "c06" },
|
||||
{ authors_table, "year_of_birth", "c07" },
|
||||
{ authors_table, "distinguished", "c08" },
|
||||
{ books_table, "published_in", "c09" }
|
||||
};
|
||||
auto columns = column_generator::generate<book>(s);
|
||||
|
||||
REQUIRE(!columns.empty());
|
||||
REQUIRE(columns.size() == expected_columns.size());
|
||||
for (size_t i = 0; i != expected_columns.size(); ++i) {
|
||||
REQUIRE(expected_columns[i].equals(columns[i]));
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,12 @@
|
||||
#include <catch2/catch_test_macros.hpp>
|
||||
|
||||
#include "matador/sql/column_definition.hpp"
|
||||
#include "matador/object/attribute_definition.hpp"
|
||||
|
||||
using namespace matador::sql;
|
||||
using namespace matador::object;
|
||||
using namespace matador::utils;
|
||||
|
||||
TEST_CASE("Test create empty column", "[column]") {
|
||||
column_definition c("name");
|
||||
attribute_definition c("name");
|
||||
|
||||
REQUIRE(c.name() == "name");
|
||||
REQUIRE(c.index() == -1);
|
||||
@@ -26,7 +26,7 @@ TEST_CASE("Test create empty column", "[column]") {
|
||||
}
|
||||
|
||||
TEST_CASE("Test copy and move column", "[column]") {
|
||||
column_definition c("name");
|
||||
attribute_definition c("name");
|
||||
c.set(std::string{"george"}, 255);
|
||||
REQUIRE(c.name() == "name");
|
||||
REQUIRE(c.index() == -1);
|
||||
|
||||
Reference in New Issue
Block a user