extract not null constraint into enum

This commit is contained in:
2024-01-19 07:58:37 +01:00
parent ed6366fef6
commit 9355f7ea27
13 changed files with 90 additions and 87 deletions
+12 -12
View File
@@ -15,15 +15,15 @@ TEST_CASE("Generate columns from object", "[column generator]") {
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 | constraints::NOT_NULL } },
column{ "supplier_id", data_type_t::type_unsigned_long, constraints::FOREIGN_KEY },
column{ "category_id", data_type_t::type_unsigned_long, constraints::FOREIGN_KEY },
column{ "quantity_per_unit", data_type_t::type_varchar, constraints::NOT_NULL },
column{ "unit_price", data_type_t::type_unsigned_int, constraints::NOT_NULL },
column{ "units_in_stock", data_type_t::type_unsigned_int, constraints::NOT_NULL },
column{ "units_in_order", data_type_t::type_unsigned_int, constraints::NOT_NULL },
column{ "reorder_level", data_type_t::type_unsigned_int, constraints::NOT_NULL },
column{ "discontinued", data_type_t::type_bool, constraints::NOT_NULL }
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());
@@ -41,9 +41,9 @@ TEST_CASE("Generate columns from object with nullable columns", "[column generat
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 | constraints::NOT_NULL } },
column{ "name", data_type_t::type_varchar, null_attributes },
column{ "age", data_type_t::type_unsigned_int, null_attributes }
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());
+2 -2
View File
@@ -22,12 +22,12 @@ TEST_CASE("Create table sql statement string", "[query]") {
q = query.create().table("person", {
make_pk_column<unsigned long>("id"),
make_column<std::string>("name", { 255, constraints::UNIQUE | constraints::NOT_NULL }),
make_column<std::string>("name", { 255, constraints::UNIQUE }, null_option::NOT_NULL),
make_column<unsigned short>("age"),
make_fk_column<unsigned long>("address", "address", "id")
}).compile();
REQUIRE(q.sql == R"##(CREATE TABLE "person" ("id" BIGINT NOT NULL, "name" VARCHAR(255) NOT NULL UNIQUE, "age" INTEGER NOT NULL, "address" BIGINT, CONSTRAINT PK_person PRIMARY KEY (id), CONSTRAINT FK_person_address FOREIGN KEY (address) REFERENCES address(id)))##");
REQUIRE(q.sql == R"##(CREATE TABLE "person" ("id" BIGINT NOT NULL, "name" VARCHAR(255) NOT NULL UNIQUE, "age" INTEGER NOT NULL, "address" BIGINT NOT NULL, CONSTRAINT PK_person PRIMARY KEY (id), CONSTRAINT FK_person_address FOREIGN KEY (address) REFERENCES address(id)))##");
REQUIRE(q.table_name == "person");
}
+7 -13
View File
@@ -49,10 +49,9 @@ TEMPLATE_TEST_CASE_METHOD(SessionRecordTestFixture, "Create and drop table state
REQUIRE(!s.table_exists("person"));
}
TEST_CASE("Create and drop table statement with foreign key", "[session record]")
TEMPLATE_TEST_CASE_METHOD(SessionRecordTestFixture, "Create and drop table statement with foreign key", "[session record]", Sqlite, Postgres, MySql)
{
connection_pool<connection> pool("sqlite://sqlite.db", 4);
session s(pool);
auto &s = SessionRecordTestFixture<TestType>::session();
s.create()
.table("airplane", {
@@ -87,14 +86,9 @@ TEST_CASE("Create and drop table statement with foreign key", "[session record]"
REQUIRE(!s.table_exists("airplane"));
}
TEST_CASE("Execute insert record statement", "[session record]")
TEMPLATE_TEST_CASE_METHOD(SessionRecordTestFixture, "Execute insert record statement", "[session record]", MySql)
{
auto dns = GENERATE(as < std::string > {},
"sqlite://sqlite.db",
"postgres://test:test123@127.0.0.1:5432/matador_test");
connection_pool<connection> pool(dns, 4);
session s(pool);
auto &s = SessionRecordTestFixture<TestType>::session();
s.create()
.table("person", {
@@ -119,13 +113,13 @@ TEST_CASE("Execute insert record statement", "[session record]")
REQUIRE(i.size() == 3);
REQUIRE(i.at(0).name() == "id");
REQUIRE(i.at(0).type() == data_type_t::type_long_long);
REQUIRE(i.at(0).as<long long>() == 7);
REQUIRE(i.at(0).template as<long long>() == 7);
REQUIRE(i.at(1).name() == "name");
REQUIRE(i.at(1).type() == data_type_t::type_varchar);
REQUIRE(i.at(1).as<std::string>() == "george");
REQUIRE(i.at(1).template as<std::string>() == "george");
REQUIRE(i.at(2).name() == "age");
REQUIRE(i.at(2).type() == matador::sql::data_type_t::type_int);
REQUIRE(i.at(2).as<int>() == 45);
REQUIRE(i.at(2).template as<int>() == 45);
}
s.drop()