renamed column_generator to column_definition_generator and column_name_generator to column_generator
This commit is contained in:
+1
-1
@@ -20,8 +20,8 @@ add_executable(tests
|
||||
models/product.hpp
|
||||
models/order.hpp
|
||||
models/order_details.h
|
||||
ColumnDefinitionGeneratorTest.cpp
|
||||
ColumnGeneratorTest.cpp
|
||||
ColumnNameGeneratorTest.cpp
|
||||
ValueGeneratorTest.cpp
|
||||
models/category.hpp
|
||||
models/supplier.hpp
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
#include <catch2/catch_test_macros.hpp>
|
||||
|
||||
#include "matador/sql/column_definition_generator.hpp"
|
||||
#include "matador/sql/schema.hpp"
|
||||
|
||||
#include "models/product.hpp"
|
||||
#include "models/optional.hpp"
|
||||
|
||||
using namespace matador::sql;
|
||||
using namespace matador::utils;
|
||||
|
||||
TEST_CASE("Generate columns from object", "[column generator]") {
|
||||
schema repo("main");
|
||||
|
||||
auto columns = column_definition_generator::generate<matador::test::product>(repo);
|
||||
|
||||
const std::vector<column_definition> expected_columns = {
|
||||
column_definition{"product_name", data_type_t::type_varchar, constraints::PRIMARY_KEY, null_option::NOT_NULL },
|
||||
column_definition{"supplier_id", data_type_t::type_unsigned_long, constraints::FOREIGN_KEY, null_option::NOT_NULL },
|
||||
column_definition{"category_id", data_type_t::type_unsigned_long, constraints::FOREIGN_KEY, null_option::NOT_NULL },
|
||||
column_definition{"quantity_per_unit", data_type_t::type_varchar, null_attributes, null_option::NOT_NULL },
|
||||
column_definition{"unit_price", data_type_t::type_unsigned_int, null_attributes, null_option::NOT_NULL },
|
||||
column_definition{"units_in_stock", data_type_t::type_unsigned_int, null_attributes, null_option::NOT_NULL },
|
||||
column_definition{"units_in_order", data_type_t::type_unsigned_int, null_attributes, null_option::NOT_NULL },
|
||||
column_definition{"reorder_level", data_type_t::type_unsigned_int, null_attributes, null_option::NOT_NULL },
|
||||
column_definition{"discontinued", data_type_t::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 = column_definition_generator::generate<matador::test::optional>(repo);
|
||||
|
||||
const std::vector<column_definition> expected_columns = {
|
||||
column_definition{"id", data_type_t::type_unsigned_long, constraints::PRIMARY_KEY, null_option::NOT_NULL },
|
||||
column_definition{"name", data_type_t::type_varchar, null_attributes, null_option::NOT_NULL },
|
||||
column_definition{"age", data_type_t::type_unsigned_int, 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() );
|
||||
}
|
||||
}
|
||||
@@ -3,54 +3,96 @@
|
||||
#include "matador/sql/column_generator.hpp"
|
||||
#include "matador/sql/schema.hpp"
|
||||
|
||||
#include "models/order.hpp"
|
||||
#include "models/product.hpp"
|
||||
#include "models/optional.hpp"
|
||||
#include "models/book.hpp"
|
||||
#include "models/author.hpp"
|
||||
|
||||
using namespace matador::sql;
|
||||
using namespace matador::utils;
|
||||
|
||||
TEST_CASE("Generate columns from object", "[column generator]") {
|
||||
schema repo("main");
|
||||
TEST_CASE("Generate columns from object", "[column][generator]") {
|
||||
using namespace matador::test;
|
||||
schema s("main");
|
||||
s.attach<product>("product");
|
||||
|
||||
auto columns = column_generator::generate<matador::test::product>(repo);
|
||||
auto columns = column_generator::generate<product>(s);
|
||||
|
||||
const std::vector<column_definition> expected_columns = {
|
||||
column_definition{"product_name", data_type_t::type_varchar, constraints::PRIMARY_KEY, null_option::NOT_NULL },
|
||||
column_definition{"supplier_id", data_type_t::type_unsigned_long, constraints::FOREIGN_KEY, null_option::NOT_NULL },
|
||||
column_definition{"category_id", data_type_t::type_unsigned_long, constraints::FOREIGN_KEY, null_option::NOT_NULL },
|
||||
column_definition{"quantity_per_unit", data_type_t::type_varchar, null_attributes, null_option::NOT_NULL },
|
||||
column_definition{"unit_price", data_type_t::type_unsigned_int, null_attributes, null_option::NOT_NULL },
|
||||
column_definition{"units_in_stock", data_type_t::type_unsigned_int, null_attributes, null_option::NOT_NULL },
|
||||
column_definition{"units_in_order", data_type_t::type_unsigned_int, null_attributes, null_option::NOT_NULL },
|
||||
column_definition{"reorder_level", data_type_t::type_unsigned_int, null_attributes, null_option::NOT_NULL },
|
||||
column_definition{"discontinued", data_type_t::type_bool, null_attributes, null_option::NOT_NULL }
|
||||
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].name() == columns[i].name());
|
||||
REQUIRE(expected_columns[i].attributes().options() == columns[i].attributes().options() );
|
||||
REQUIRE(expected_columns[i].type() == columns[i].type() );
|
||||
REQUIRE(expected_columns[i] == columns[i].name);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("Generate columns from object with nullable columns", "[column generator]") {
|
||||
schema repo("main");
|
||||
TEST_CASE("Generate columns for object with has many relation", "[column][generator][relation]") {
|
||||
using namespace matador::test;
|
||||
schema s("main");
|
||||
s.attach<product>("product");
|
||||
s.attach<order_details>("order_details");
|
||||
s.attach<order>("order");
|
||||
|
||||
auto columns = column_generator::generate<matador::test::optional>(repo);
|
||||
auto columns = column_generator::generate<order>(s);
|
||||
|
||||
const std::vector<column_definition> expected_columns = {
|
||||
column_definition{"id", data_type_t::type_unsigned_long, constraints::PRIMARY_KEY, null_option::NOT_NULL },
|
||||
column_definition{"name", data_type_t::type_varchar, null_attributes, null_option::NOT_NULL },
|
||||
column_definition{"age", data_type_t::type_unsigned_int, null_attributes, null_option::NOT_NULL }
|
||||
const std::vector<column> expected_columns = {
|
||||
{ "order", "order_id", "c01" },
|
||||
{ "order", "order_date", "c02" },
|
||||
{ "order", "required_date", "c03" },
|
||||
{ "order", "shipped_date", "c04" },
|
||||
{ "order", "ship_via", "c05" },
|
||||
{ "order", "freight", "c06" },
|
||||
{ "order", "ship_name", "c07" },
|
||||
{ "order", "ship_address", "c08" },
|
||||
{ "order", "ship_city", "c09" },
|
||||
{ "order", "ship_region", "c10" },
|
||||
{ "order", "ship_postal_code", "c11" },
|
||||
{ "order", "ship_country", "c12" },
|
||||
{ "order_details", "order_details_id", "c13" },
|
||||
{ "order_details", "order_id", "c14" },
|
||||
{ "order_details", "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].name() == columns[i].name());
|
||||
REQUIRE(expected_columns[i].attributes().options() == columns[i].attributes().options() );
|
||||
REQUIRE(expected_columns[i].type() == columns[i].type() );
|
||||
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");
|
||||
s.attach<book>("books");
|
||||
s.attach<author>("authors");
|
||||
|
||||
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" }
|
||||
};
|
||||
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,68 +0,0 @@
|
||||
#include <catch2/catch_test_macros.hpp>
|
||||
|
||||
#include "matador/sql/column_name_generator.hpp"
|
||||
#include "matador/sql/schema.hpp"
|
||||
|
||||
#include "models/order.hpp"
|
||||
#include "models/product.hpp"
|
||||
|
||||
using namespace matador::sql;
|
||||
|
||||
TEST_CASE("Generate column names from object", "[column name generator]") {
|
||||
schema s("main");
|
||||
s.attach<matador::test::product>("product");
|
||||
|
||||
auto columns = column_name_generator::generate<matador::test::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 column names for object with has many relation", "[column][relation]") {
|
||||
schema s("main");
|
||||
s.attach<matador::test::product>("product");
|
||||
s.attach<matador::test::order_details>("order_details");
|
||||
s.attach<matador::test::order>("order");
|
||||
|
||||
auto columns = column_name_generator::generate<matador::test::order>(s);
|
||||
|
||||
const std::vector<std::string> expected_columns = {
|
||||
"order_id",
|
||||
"order_date",
|
||||
"required_date",
|
||||
"shipped_date",
|
||||
"ship_via",
|
||||
"freight",
|
||||
"ship_name",
|
||||
"ship_address",
|
||||
"ship_city",
|
||||
"ship_region",
|
||||
"ship_postal_code",
|
||||
"ship_country",
|
||||
"order_details_id",
|
||||
"order_id",
|
||||
"product_id",
|
||||
};
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -25,7 +25,7 @@ struct book
|
||||
namespace field = matador::utils::access;
|
||||
field::primary_key(op, "id", id);
|
||||
field::attribute(op, "title", title, 511);
|
||||
field::has_one(op, "author_id", book_author, matador::utils::default_foreign_attributes);
|
||||
field::has_one(op, "author_id", book_author, utils::fetch_type::EAGER);
|
||||
field::attribute(op, "published_in", published_in);
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user