added column generator and tests
This commit is contained in:
+4
-1
@@ -13,7 +13,10 @@ add_executable(tests builder.cpp
|
||||
record.cpp
|
||||
connection_pool.cpp
|
||||
query.cpp
|
||||
backend_provider.cpp)
|
||||
backend_provider.cpp
|
||||
connection.cpp
|
||||
product.hpp
|
||||
column_generator.cpp)
|
||||
target_link_libraries(tests PRIVATE
|
||||
Catch2::Catch2WithMain
|
||||
matador
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
#include <catch2/catch_test_macros.hpp>
|
||||
|
||||
#include "matador/sql/column_generator.hpp"
|
||||
|
||||
#include "product.hpp"
|
||||
|
||||
using namespace matador::sql;
|
||||
|
||||
TEST_CASE("Generate columns from object", "[column generator]") {
|
||||
|
||||
auto columns = column_generator::generate<matador::test::product>();
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
#include <catch2/catch_test_macros.hpp>
|
||||
|
||||
#include "matador/sql/connection.hpp"
|
||||
|
||||
using namespace matador::sql;
|
||||
|
||||
TEST_CASE("Create connection", "[connection]") {
|
||||
connection c("sqlite://test.db");
|
||||
REQUIRE(!c.is_open());
|
||||
|
||||
c.open();
|
||||
REQUIRE(c.is_open());
|
||||
|
||||
c.close();
|
||||
REQUIRE(!c.is_open());
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
#ifndef QUERY_PRODUCT_HPP
|
||||
#define QUERY_PRODUCT_HPP
|
||||
|
||||
#include "matador/utils/access.hpp"
|
||||
#include "matador/utils/field_attributes.hpp"
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace matador::test {
|
||||
|
||||
struct product
|
||||
{
|
||||
std::string product_name;
|
||||
unsigned long supplier_id;
|
||||
unsigned long category_id;
|
||||
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::utils::access;
|
||||
using namespace matador::utils;
|
||||
field::primary_key(op, "product_name", product_name, 255);
|
||||
field::attribute(op, "supplier_id", supplier_id, { constraints::FOREIGN_KEY | constraints::NOT_NULL });
|
||||
field::attribute(op, "category_id", category_id, { constraints::FOREIGN_KEY | constraints::NOT_NULL });
|
||||
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
|
||||
+1
-1
@@ -1,5 +1,5 @@
|
||||
#include <catch2/catch_test_macros.hpp>
|
||||
#include "catch2/generators/catch_generators.hpp"
|
||||
#include <catch2/generators/catch_generators.hpp>
|
||||
|
||||
TEST_CASE("Query test", "[query]") {
|
||||
auto dns = GENERATE(as<std::string>{}, "mssql", "sqlite", "postgres" );
|
||||
|
||||
+5
-2
@@ -4,21 +4,24 @@
|
||||
#include <matador/sql/condition.hpp>
|
||||
#include <matador/sql/session.hpp>
|
||||
|
||||
#include "product.hpp"
|
||||
|
||||
using namespace matador::sql;
|
||||
|
||||
TEST_CASE("Execute create table statement", "[connection]") {
|
||||
connection_pool<connection> pool("sqlite://sqlite.db", 4);
|
||||
session s(pool);
|
||||
|
||||
const auto res = s.create()
|
||||
auto res = s.create()
|
||||
.table("person", {
|
||||
make_pk_column<unsigned long>("id"),
|
||||
make_column<std::string>("name", 255),
|
||||
make_column<unsigned short>("age")
|
||||
}).execute();
|
||||
|
||||
REQUIRE(true);
|
||||
REQUIRE(res.second == R"(CREATE TABLE "person" ("id" BIGINT NOT NULL PRIMARY KEY, "name" VARCHAR(255), "age" INTEGER))");
|
||||
|
||||
res = s.create().table<matador::test::product>("product").execute();
|
||||
}
|
||||
|
||||
TEST_CASE("Execute drop table statement", "[connection]") {
|
||||
|
||||
Reference in New Issue
Block a user