added more tests
This commit is contained in:
@@ -12,11 +12,17 @@ add_executable(CoreTests
|
||||
utils/FieldAttributeTest.cpp
|
||||
utils/VersionTest.cpp
|
||||
utils/StringTest.cpp
|
||||
object/ColumnDefinitionGeneratorTest.cpp
|
||||
object/SchemaTest.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(CoreTests matador-core Catch2::Catch2WithMain)
|
||||
|
||||
target_include_directories(CoreTests
|
||||
PUBLIC
|
||||
${CMAKE_SOURCE_DIR}/test/models}
|
||||
)
|
||||
|
||||
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
|
||||
target_compile_options(CoreTests PRIVATE -coverage)
|
||||
target_link_options(CoreTests PRIVATE -coverage)
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
#include <catch2/catch_test_macros.hpp>
|
||||
|
||||
#include "matador/object/attribute_definition_generator.hpp"
|
||||
#include "matador/object/schema.hpp"
|
||||
|
||||
#include "../test/models/product.hpp"
|
||||
#include "../test/models/optional.hpp"
|
||||
|
||||
using namespace matador::object;
|
||||
using namespace matador::utils;
|
||||
|
||||
TEST_CASE("Generate column definitions from object", "[column][definition][generator]") {
|
||||
schema repo("main");
|
||||
|
||||
auto columns = attribute_definition_generator::generate<matador::test::product>(repo);
|
||||
|
||||
const std::vector expected_columns = {
|
||||
attribute_definition{"product_name", basic_type::type_varchar, constraints::PRIMARY_KEY, null_option::NOT_NULL },
|
||||
attribute_definition{"supplier_id", basic_type::type_uint32, constraints::FOREIGN_KEY, null_option::NOT_NULL },
|
||||
attribute_definition{"category_id", basic_type::type_uint32, constraints::FOREIGN_KEY, null_option::NOT_NULL },
|
||||
attribute_definition{"quantity_per_unit", basic_type::type_varchar, null_attributes, null_option::NOT_NULL },
|
||||
attribute_definition{"unit_price", basic_type::type_uint32, null_attributes, null_option::NOT_NULL },
|
||||
attribute_definition{"units_in_stock", basic_type::type_uint32, null_attributes, null_option::NOT_NULL },
|
||||
attribute_definition{"units_in_order", basic_type::type_uint32, null_attributes, null_option::NOT_NULL },
|
||||
attribute_definition{"reorder_level", basic_type::type_uint32, null_attributes, null_option::NOT_NULL },
|
||||
attribute_definition{"discontinued", basic_type::type_bool, null_attributes, null_option::NOT_NULL }
|
||||
};
|
||||
REQUIRE(!columns.empty());
|
||||
REQUIRE(columns.size() == expected_columns.size());
|
||||
|
||||
for (size_t i = 0; i != expected_columns.size(); ++i) {
|
||||
REQUIRE(expected_columns[i].name() == columns[i].name());
|
||||
REQUIRE(expected_columns[i].attributes().options() == columns[i].attributes().options() );
|
||||
REQUIRE(expected_columns[i].type() == columns[i].type() );
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("Generate columns from object with nullable columns", "[column generator]") {
|
||||
schema repo("main");
|
||||
|
||||
auto columns = attribute_definition_generator::generate<matador::test::optional>(repo);
|
||||
|
||||
const std::vector expected_columns = {
|
||||
attribute_definition{"id", basic_type::type_uint32, constraints::PRIMARY_KEY, null_option::NOT_NULL },
|
||||
attribute_definition{"name", basic_type::type_varchar, null_attributes, null_option::NOT_NULL },
|
||||
attribute_definition{"age", basic_type::type_uint32, null_attributes, null_option::NOT_NULL }
|
||||
};
|
||||
REQUIRE(!columns.empty());
|
||||
REQUIRE(columns.size() == expected_columns.size());
|
||||
|
||||
for (size_t i = 0; i != expected_columns.size(); ++i) {
|
||||
REQUIRE(expected_columns[i].name() == columns[i].name());
|
||||
REQUIRE(expected_columns[i].attributes().options() == columns[i].attributes().options() );
|
||||
REQUIRE(expected_columns[i].type() == columns[i].type() );
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,8 @@ using namespace matador;
|
||||
|
||||
struct person {
|
||||
virtual ~person() = default;
|
||||
template < typename Operator >
|
||||
void process(Operator &op) {}
|
||||
};
|
||||
|
||||
struct student final : person {};
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
#ifndef QUERY_AIRPLANE_HPP
|
||||
#define QUERY_AIRPLANE_HPP
|
||||
|
||||
#include "category.hpp"
|
||||
#include "supplier.hpp"
|
||||
|
||||
#include "matador/utils/access.hpp"
|
||||
#include "matador/utils/cascade_type.hpp"
|
||||
#include "matador/utils/field_attributes.hpp"
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace matador::test {
|
||||
|
||||
struct airplane {
|
||||
airplane() = default;
|
||||
airplane(unsigned int id, std::string b, std::string m)
|
||||
: id(id)
|
||||
, brand(std::move(b))
|
||||
, model(std::move(m)) {}
|
||||
unsigned int id{};
|
||||
std::string brand;
|
||||
std::string model;
|
||||
|
||||
template<class Operator>
|
||||
void process(Operator &op) {
|
||||
namespace field = matador::access;
|
||||
using namespace matador::utils;
|
||||
field::primary_key(op, "id", id);
|
||||
field::attribute(op, "brand", brand, 255);
|
||||
field::attribute(op, "model", model, 255);
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif //QUERY_AIRPLANE_HPP
|
||||
@@ -0,0 +1,40 @@
|
||||
#ifndef QUERY_AUTHOR_HPP
|
||||
#define QUERY_AUTHOR_HPP
|
||||
|
||||
#include "matador/utils/access.hpp"
|
||||
|
||||
#include "matador/object/object_ptr.hpp"
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace matador::test {
|
||||
|
||||
struct book;
|
||||
|
||||
struct author {
|
||||
unsigned int id{};
|
||||
std::string first_name;
|
||||
std::string last_name;
|
||||
std::string date_of_birth;
|
||||
unsigned short year_of_birth{};
|
||||
bool distinguished{false};
|
||||
std::vector<matador::object::object_ptr<book>> books;
|
||||
|
||||
template<typename Operator>
|
||||
void process(Operator &op)
|
||||
{
|
||||
namespace field = matador::access;
|
||||
field::primary_key(op, "id", id);
|
||||
field::attribute(op, "first_name", first_name, 63);
|
||||
field::attribute(op, "last_name", last_name, 63);
|
||||
field::attribute(op, "date_of_birth", date_of_birth, 31);
|
||||
field::attribute(op, "year_of_birth", year_of_birth);
|
||||
field::attribute(op, "distinguished", distinguished);
|
||||
// field::has_many(op, books, "author_id", utils::fetch_type::LAZY);
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif //QUERY_AUTHOR_HPP
|
||||
@@ -0,0 +1,33 @@
|
||||
#ifndef QUERY_BOOK_HPP
|
||||
#define QUERY_BOOK_HPP
|
||||
|
||||
#include "matador/object/object_ptr.hpp"
|
||||
|
||||
#include "matador/utils/access.hpp"
|
||||
#include "matador/utils/foreign_attributes.hpp"
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace matador::test {
|
||||
|
||||
struct author;
|
||||
|
||||
struct book {
|
||||
unsigned int id{};
|
||||
matador::object::object_ptr<author> book_author;
|
||||
std::string title;
|
||||
unsigned short published_in{};
|
||||
|
||||
template<typename Operator>
|
||||
void process(Operator &op)
|
||||
{
|
||||
namespace field = matador::access;
|
||||
field::primary_key(op, "id", id);
|
||||
field::attribute(op, "title", title, 511);
|
||||
field::belongs_to(op, "author_id", book_author, utils::fetch_type::EAGER);
|
||||
field::attribute(op, "published_in", published_in);
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
#endif //QUERY_BOOK_HPP
|
||||
@@ -0,0 +1,24 @@
|
||||
#ifndef QUERY_CATEGORY_HPP
|
||||
#define QUERY_CATEGORY_HPP
|
||||
|
||||
#include "matador/utils/access.hpp"
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace matador::test {
|
||||
|
||||
struct category {
|
||||
unsigned int id{};
|
||||
std::string name;
|
||||
|
||||
template<class Operator>
|
||||
void process(Operator &op) {
|
||||
namespace field = matador::access;
|
||||
using namespace matador::utils;
|
||||
field::primary_key(op, "id", id);
|
||||
field::attribute(op, "name", name, 255);
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
#endif //QUERY_CATEGORY_HPP
|
||||
@@ -0,0 +1,39 @@
|
||||
#ifndef QUERY_FLIGHT_HPP
|
||||
#define QUERY_FLIGHT_HPP
|
||||
|
||||
#include "airplane.hpp"
|
||||
|
||||
#include "matador/utils/access.hpp"
|
||||
#include "matador/utils/cascade_type.hpp"
|
||||
#include "matador/utils/fetch_type.hpp"
|
||||
|
||||
#include "matador/object/object_ptr.hpp"
|
||||
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
namespace matador::test {
|
||||
|
||||
struct flight
|
||||
{
|
||||
flight() = default;
|
||||
flight(const unsigned int id, const object::object_ptr<airplane> &plane, std::string name)
|
||||
: id(id), airplane(plane), pilot_name(std::move(name)) {}
|
||||
|
||||
unsigned int id{};
|
||||
object::object_ptr<test::airplane> airplane;
|
||||
std::string pilot_name;
|
||||
|
||||
template<class Operator>
|
||||
void process(Operator &op) {
|
||||
namespace field = matador::access;
|
||||
using namespace matador::utils;
|
||||
field::primary_key(op, "id", id);
|
||||
// field::has_one(op, "airplane_id", airplane, {utils::cascade_type::ALL, utils::fetch_type::EAGER});
|
||||
field::attribute(op, "pilot_name", pilot_name, 255);
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif //QUERY_FLIGHT_HPP
|
||||
@@ -15,7 +15,7 @@ enum class Color : uint8_t {
|
||||
|
||||
struct location
|
||||
{
|
||||
unsigned long id{};
|
||||
unsigned int id{};
|
||||
std::string name;
|
||||
coordinate coord;
|
||||
Color color{Color::Green};
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
#ifndef QUERY_OPTIONAL_HPP
|
||||
#define QUERY_OPTIONAL_HPP
|
||||
|
||||
#include "matador/utils/access.hpp"
|
||||
#include "matador/utils/field_attributes.hpp"
|
||||
|
||||
#include <optional>
|
||||
#include <string>
|
||||
|
||||
namespace matador::test {
|
||||
|
||||
struct optional
|
||||
{
|
||||
unsigned int id{};
|
||||
std::optional<std::string> name;
|
||||
std::optional<unsigned int> age{};
|
||||
|
||||
template<class Operator>
|
||||
void process(Operator &op) {
|
||||
namespace field = matador::access;
|
||||
using namespace matador::utils;
|
||||
field::primary_key(op, "id", id);
|
||||
field::attribute(op, "name", name, 255);
|
||||
field::attribute(op, "age", age);
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif //QUERY_OPTIONAL_HPP
|
||||
@@ -0,0 +1,50 @@
|
||||
#ifndef QUERY_ORDER_HPP
|
||||
#define QUERY_ORDER_HPP
|
||||
|
||||
#include "order_details.hpp"
|
||||
|
||||
#include "matador/utils/access.hpp"
|
||||
|
||||
#include "matador/object/object_ptr.hpp"
|
||||
|
||||
#include <vector>
|
||||
|
||||
namespace matador::test {
|
||||
|
||||
struct order
|
||||
{
|
||||
unsigned int order_id{};
|
||||
std::string order_date;
|
||||
std::string required_date;
|
||||
std::string shipped_date;
|
||||
unsigned int ship_via{};
|
||||
unsigned int freight{};
|
||||
std::string ship_name;
|
||||
std::string ship_address;
|
||||
std::string ship_city;
|
||||
std::string ship_region;
|
||||
std::string ship_postal_code;
|
||||
std::string ship_country;
|
||||
std::vector<matador::object::object_ptr<order_details>> order_details_;
|
||||
|
||||
template<class Operator>
|
||||
void process(Operator &op) {
|
||||
namespace field = matador::access;
|
||||
field::primary_key(op, "order_id", order_id);
|
||||
field::attribute(op, "order_date", order_date, 255);
|
||||
field::attribute(op, "required_date", required_date, 255);
|
||||
field::attribute(op, "shipped_date", shipped_date, 255);
|
||||
field::attribute(op, "ship_via", ship_via);
|
||||
field::attribute(op, "freight", freight);
|
||||
field::attribute(op, "ship_name", ship_name, 255);
|
||||
field::attribute(op, "ship_address", ship_address, 255);
|
||||
field::attribute(op, "ship_city", ship_city, 255);
|
||||
field::attribute(op, "ship_region", ship_region, 255);
|
||||
field::attribute(op, "ship_postal_code", ship_postal_code, 255);
|
||||
field::attribute(op, "ship_country", ship_country, 255);
|
||||
// field::has_many(op, order_details_, "order_id", utils::fetch_type::EAGER);
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
#endif //QUERY_ORDER_HPP
|
||||
@@ -0,0 +1,30 @@
|
||||
#ifndef QUERY_ORDER_DETAILS_HPP
|
||||
#define QUERY_ORDER_DETAILS_HPP
|
||||
|
||||
#include "product.hpp"
|
||||
|
||||
#include "matador/object/object_ptr.hpp"
|
||||
|
||||
#include "matador/utils/foreign_attributes.hpp"
|
||||
|
||||
namespace matador::test {
|
||||
|
||||
struct order;
|
||||
|
||||
struct order_details
|
||||
{
|
||||
unsigned int order_details_id;
|
||||
matador::object::object_ptr<order> order_;
|
||||
matador::object::object_ptr<product> product_;
|
||||
|
||||
template<class Operator>
|
||||
void process(Operator &op) {
|
||||
namespace field = matador::access;
|
||||
field::primary_key(op, "order_details_id", order_details_id);
|
||||
field::belongs_to(op, "order_id", order_, utils::default_foreign_attributes);
|
||||
field::has_one(op, "product_id", product_, utils::default_foreign_attributes);
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
#endif //QUERY_ORDER_DETAILS_HPP
|
||||
@@ -0,0 +1,45 @@
|
||||
#ifndef QUERY_PRODUCT_HPP
|
||||
#define QUERY_PRODUCT_HPP
|
||||
|
||||
#include "category.hpp"
|
||||
#include "supplier.hpp"
|
||||
|
||||
#include "matador/utils/access.hpp"
|
||||
#include "matador/utils/cascade_type.hpp"
|
||||
#include "matador/utils/field_attributes.hpp"
|
||||
|
||||
#include "matador/object/object_ptr.hpp"
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace matador::test {
|
||||
|
||||
struct product {
|
||||
std::string product_name;
|
||||
object::object_ptr<test::supplier> supplier;
|
||||
object::object_ptr<test::category> category;
|
||||
std::string quantity_per_unit;
|
||||
unsigned int unit_price;
|
||||
unsigned int units_in_stock;
|
||||
unsigned int units_in_order;
|
||||
unsigned int reorder_level;
|
||||
bool discontinued;
|
||||
|
||||
template<class Operator>
|
||||
void process(Operator &op) {
|
||||
namespace field = matador::access;
|
||||
using namespace matador::utils;
|
||||
field::primary_key(op, "product_name", product_name, 255);
|
||||
field::has_one(op, "supplier_id", supplier, utils::cascade_type::ALL);
|
||||
field::has_one(op, "category_id", category, utils::cascade_type::ALL);
|
||||
field::attribute(op, "quantity_per_unit", quantity_per_unit, 255);
|
||||
field::attribute(op, "unit_price", unit_price);
|
||||
field::attribute(op, "units_in_stock", units_in_stock);
|
||||
field::attribute(op, "units_in_order", units_in_order);
|
||||
field::attribute(op, "reorder_level", reorder_level);
|
||||
field::attribute(op, "discontinued", discontinued);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#endif //QUERY_PRODUCT_HPP
|
||||
@@ -0,0 +1,52 @@
|
||||
#ifndef QUERY_RECIPE_HPP
|
||||
#define QUERY_RECIPE_HPP
|
||||
|
||||
#include "matador/utils/access.hpp"
|
||||
#include "matador/utils/foreign_attributes.hpp"
|
||||
|
||||
#include "matador/object/object_ptr.hpp"
|
||||
#include "matador/object/many_to_many_relation.hpp"
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace matador::test {
|
||||
|
||||
struct recipe;
|
||||
struct ingredient
|
||||
{
|
||||
unsigned int id{};
|
||||
std::string name;
|
||||
std::vector<matador::object::object_ptr<recipe>> recipes;
|
||||
|
||||
template<class Operator>
|
||||
void process(Operator &op) {
|
||||
namespace field = matador::access;
|
||||
field::primary_key(op, "id", id);
|
||||
field::attribute(op, "name", name, 255);
|
||||
// field::has_many_to_many(op, "recipe_ingredients", recipes, "ingredient_id", "recipe_id", utils::fetch_type::EAGER);
|
||||
}
|
||||
};
|
||||
|
||||
struct recipe
|
||||
{
|
||||
unsigned int id{};
|
||||
std::string name;
|
||||
std::vector<matador::object::object_ptr<ingredient>> ingredients;
|
||||
|
||||
template<class Operator>
|
||||
void process(Operator &op) {
|
||||
namespace field = matador::access;
|
||||
field::primary_key(op, "id", id);
|
||||
field::attribute(op, "name", name, 255);
|
||||
// field::has_many_to_many(op, "recipe_ingredients", ingredients, utils::fetch_type::LAZY);
|
||||
}
|
||||
};
|
||||
|
||||
class recipe_ingredient : public object::many_to_many_relation<recipe, ingredient> {
|
||||
public:
|
||||
recipe_ingredient() : many_to_many_relation("recipe_id", "ingredient_id") {}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif //QUERY_RECIPE_HPP
|
||||
@@ -0,0 +1,62 @@
|
||||
#ifndef QUERY_STUDENT_HPP
|
||||
#define QUERY_STUDENT_HPP
|
||||
|
||||
#include "matador/utils/access.hpp"
|
||||
#include "matador/utils/foreign_attributes.hpp"
|
||||
|
||||
#include "matador/object/object_ptr.hpp"
|
||||
#include "matador/object/many_to_many_relation.hpp"
|
||||
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
namespace matador::test {
|
||||
|
||||
struct course;
|
||||
|
||||
struct student {
|
||||
unsigned int id{};
|
||||
std::string name;
|
||||
std::vector<matador::object::object_ptr<course>> courses;
|
||||
|
||||
student() = default;
|
||||
explicit student(unsigned int id, std::string name)
|
||||
: id(id)
|
||||
, name(std::move(name)) {}
|
||||
|
||||
template < class Operator >
|
||||
void process(Operator &op) {
|
||||
namespace field = matador::access;
|
||||
field::primary_key(op, "id", id);
|
||||
field::attribute(op, "name", name, 255);
|
||||
// field::has_many_to_many(op, "student_courses", courses, "student_id", "course_id", utils::fetch_type::LAZY);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
struct course {
|
||||
unsigned int id{};
|
||||
std::string title;
|
||||
std::vector<matador::object::object_ptr<student>> students;
|
||||
|
||||
course() = default;
|
||||
explicit course(unsigned int id, std::string title)
|
||||
: id(id)
|
||||
, title(std::move(title)) {}
|
||||
|
||||
template < class Operator >
|
||||
void process(Operator &op) {
|
||||
namespace field = matador::access;
|
||||
field::primary_key(op, "id", id);
|
||||
field::attribute(op, "title", title, 255);
|
||||
// field::has_many_to_many(op, "student_courses", students, utils::fetch_type::EAGER);
|
||||
}
|
||||
};
|
||||
|
||||
class student_course : public object::many_to_many_relation<student, course> {
|
||||
public:
|
||||
student_course() : many_to_many_relation("student_id", "course_id") {}
|
||||
};
|
||||
|
||||
}
|
||||
#endif //QUERY_STUDENT_HPP
|
||||
@@ -0,0 +1,24 @@
|
||||
#ifndef QUERY_SUPPLIER_HPP
|
||||
#define QUERY_SUPPLIER_HPP
|
||||
|
||||
#include "matador/utils/access.hpp"
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace matador::test {
|
||||
|
||||
struct supplier {
|
||||
unsigned int id{};
|
||||
std::string name;
|
||||
|
||||
template<class Operator>
|
||||
void process(Operator &op) {
|
||||
namespace field = matador::access;
|
||||
using namespace matador::utils;
|
||||
field::primary_key(op, "id", id);
|
||||
field::attribute(op, "name", name, 255);
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
#endif //QUERY_SUPPLIER_HPP
|
||||
@@ -10,7 +10,7 @@ struct types
|
||||
{
|
||||
enum { CSTR_LEN=255 };
|
||||
|
||||
unsigned long id_ = 0;
|
||||
unsigned int id_ = 0;
|
||||
char char_ = 'c';
|
||||
short short_ = -127;
|
||||
int int_ = -65000;
|
||||
@@ -27,8 +27,8 @@ struct types
|
||||
char cstr_[CSTR_LEN]{};
|
||||
std::string string_ = "Welt";
|
||||
std::string varchar_ = "Erde";
|
||||
matador::date date_;
|
||||
matador::time time_;
|
||||
// matador::date date_;
|
||||
// matador::time time_;
|
||||
utils::blob binary_{ 1, 2, 3, 4 };
|
||||
|
||||
template < class Operator >
|
||||
|
||||
@@ -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