query/test/ColumnGeneratorTest.cpp

56 lines
2.6 KiB
C++

#include <catch2/catch_test_macros.hpp>
#include "matador/sql/column_generator.hpp"
#include "matador/sql/table_repository.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]") {
table_repository repo;
auto columns = column_generator::generate<matador::test::product>(repo);
const std::vector<column> expected_columns = {
column{ "product_name", data_type_t::type_varchar, constraints::PRIMARY_KEY, null_option::NOT_NULL },
column{ "supplier_id", data_type_t::type_unsigned_long, constraints::FOREIGN_KEY, null_option::NOT_NULL },
column{ "category_id", data_type_t::type_unsigned_long, constraints::FOREIGN_KEY, null_option::NOT_NULL },
column{ "quantity_per_unit", data_type_t::type_varchar, null_attributes, null_option::NOT_NULL },
column{ "unit_price", data_type_t::type_unsigned_int, null_attributes, null_option::NOT_NULL },
column{ "units_in_stock", data_type_t::type_unsigned_int, null_attributes, null_option::NOT_NULL },
column{ "units_in_order", data_type_t::type_unsigned_int, null_attributes, null_option::NOT_NULL },
column{ "reorder_level", data_type_t::type_unsigned_int, null_attributes, null_option::NOT_NULL },
column{ "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]") {
table_repository repo;
auto columns = column_generator::generate<matador::test::optional>(repo);
const std::vector<column> expected_columns = {
column{ "id", data_type_t::type_unsigned_long, constraints::PRIMARY_KEY, null_option::NOT_NULL },
column{ "name", data_type_t::type_varchar, null_attributes, null_option::NOT_NULL },
column{ "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() );
}
}