187 lines
6.2 KiB
C++
187 lines
6.2 KiB
C++
#include <catch2/catch_test_macros.hpp>
|
|
|
|
#include <matador/sql/column.hpp>
|
|
#include <matador/sql/condition.hpp>
|
|
#include <matador/sql/session.hpp>
|
|
|
|
#include "models/product.hpp"
|
|
|
|
using namespace matador::sql;
|
|
using namespace matador::test;
|
|
|
|
TEST_CASE("Execute create table statement", "[session]") {
|
|
connection_pool<connection> pool("sqlite://sqlite.db", 4);
|
|
session s(pool);
|
|
|
|
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(res.second == R"(CREATE TABLE "person" ("id" BIGINT NOT NULL, "name" VARCHAR(255), "age" INTEGER, CONSTRAINT PK_person PRIMARY KEY (id)))");
|
|
|
|
REQUIRE(s.drop().table("person").execute().first == 0);
|
|
// REQUIRE(res.first == 1);
|
|
|
|
// res = s.create().table<product>("product").execute();
|
|
|
|
// REQUIRE(res.second == R"(CREATE TABLE "product" ("product_name" VARCHAR(255) PRIMARY KEY, "supplier_id" BIGINT NOT NULL FOREIGN KEY, "category_id" BIGINT NOT NULL FOREIGN KEY, "quantity_per_unit" VARCHAR(255), "unit_price" BIGINT, "units_in_stock" BIGINT, "units_in_order" BIGINT, "reorder_level" BIGINT, "discontinued" BOOLEAN))");
|
|
|
|
// res = s.drop().table("person").execute();
|
|
// REQUIRE(res.first == 1);
|
|
}
|
|
|
|
TEST_CASE("Execute create table statement with foreign keys", "[session]") {
|
|
|
|
}
|
|
|
|
TEST_CASE("Execute drop table statement", "[session]") {
|
|
connection_pool<connection> pool("sqlite://sqlite.db", 4);
|
|
session s(pool);
|
|
|
|
const auto res = s.drop()
|
|
.table("person")
|
|
.execute();
|
|
|
|
REQUIRE(res.second == R"(DROP TABLE "person")");
|
|
}
|
|
|
|
TEST_CASE("Execute select statement with where clause", "[session]") {
|
|
connection_pool<connection> pool("sqlite://sqlite.db", 4);
|
|
session s(pool);
|
|
|
|
auto res = s.select({"id", "name", "color"})
|
|
.from("person")
|
|
.where("id"_col == 8)
|
|
.fetch_all();
|
|
|
|
// Todo: prepare test data
|
|
REQUIRE(true);
|
|
// REQUIRE(res.str() == R"(SELECT "id", "name", "color" FROM "person" WHERE "id" = 8)");
|
|
}
|
|
|
|
TEST_CASE("Execute select statement with order by", "[session]") {
|
|
connection_pool<connection> pool("sqlite://sqlite.db", 4);
|
|
session s(pool);
|
|
|
|
auto res = s.select({"id", "name", "color"})
|
|
.from("person")
|
|
.where("id"_col == 8)
|
|
.order_by("name").desc()
|
|
.fetch_all();
|
|
|
|
// Todo: prepare test data
|
|
REQUIRE(true);
|
|
// REQUIRE(res.str() == R"(SELECT "id", "name", "color" FROM "person" WHERE "id" = 8 ORDER BY "name" DESC)");
|
|
}
|
|
|
|
TEST_CASE("Execute select statement with group by and order by", "[session]") {
|
|
connection_pool<connection> pool("sqlite://sqlite.db", 4);
|
|
session s(pool);
|
|
|
|
auto res = s.select({"id", "name", "color"})
|
|
.from("person")
|
|
.where("id"_col == 8)
|
|
.group_by("color")
|
|
.order_by("name").asc()
|
|
.fetch_all();
|
|
|
|
// Todo: prepare test data
|
|
REQUIRE(true);
|
|
// REQUIRE(res.str() == R"(SELECT "id", "name", "color" FROM "person" WHERE "id" = 8 GROUP BY "color" ORDER BY "name" ASC)");
|
|
}
|
|
|
|
TEST_CASE("Execute insert statement", "[session]") {
|
|
connection_pool<connection> pool("sqlite://sqlite.db", 4);
|
|
session s(pool);
|
|
|
|
auto res = s.insert()
|
|
.into("person", {"id", "name", "color"})
|
|
.values({7, "george", "green"})
|
|
.execute();
|
|
|
|
REQUIRE(res.second == R"(INSERT INTO "person" ("id", "name", "color") VALUES (7, 'george', 'green'))");
|
|
|
|
using namespace matador::test;
|
|
product p;
|
|
p.discontinued = false;
|
|
p.reorder_level = 1;
|
|
p.units_in_order = 2;
|
|
p.units_in_stock = 100;
|
|
p.unit_price = 49;
|
|
p.quantity_per_unit = "pcs";
|
|
p.category = make_foreign<matador::test::category>();
|
|
p.category->id = 7;
|
|
p.supplier = make_foreign<matador::test::supplier>();;
|
|
p.supplier->id = 13;
|
|
p.product_name = "candle";
|
|
|
|
res = s.insert().into<product>("product").values(p).execute();
|
|
|
|
REQUIRE(res.second == R"(INSERT INTO "product" ("product_name", "supplier_id", "category_id", "quantity_per_unit", "unit_price", "units_in_stock", "units_in_order", "reorder_level", "discontinued") VALUES ('candle', 13, 7, 'pcs', 49, 100, 2, 1, 0))");
|
|
|
|
res = s.insert().into("product", p).execute();
|
|
|
|
REQUIRE(res.second == R"(INSERT INTO "product" ("product_name", "supplier_id", "category_id", "quantity_per_unit", "unit_price", "units_in_stock", "units_in_order", "reorder_level", "discontinued") VALUES ('candle', 13, 7, 'pcs', 49, 100, 2, 1, 0))");
|
|
}
|
|
|
|
TEST_CASE("Execute update statement", "[session]") {
|
|
connection_pool<connection> pool("sqlite://sqlite.db", 4);
|
|
session s(pool);
|
|
|
|
auto res = s.update("person")
|
|
.set({
|
|
{"name", "george"},
|
|
{"color", "green"}
|
|
})
|
|
.where("id"_col == 9)
|
|
.execute();
|
|
|
|
REQUIRE(res.second == R"(UPDATE "person" SET "name"='george', "color"='green' WHERE "id" = 9)");
|
|
|
|
using namespace matador::test;
|
|
product p;
|
|
p.discontinued = false;
|
|
p.reorder_level = 1;
|
|
p.units_in_order = 2;
|
|
p.units_in_stock = 100;
|
|
p.unit_price = 49;
|
|
p.quantity_per_unit = "pcs";
|
|
p.category = make_foreign<matador::test::category>();
|
|
p.category->id = 7;
|
|
p.supplier = make_foreign<matador::test::supplier>();;
|
|
p.supplier->id = 13;
|
|
p.product_name = "candle";
|
|
|
|
res = s.update("product")
|
|
.set(p)
|
|
.where("id"_col == 9)
|
|
.execute();
|
|
|
|
REQUIRE(res.second == R"(UPDATE "product" SET "product_name"='candle', "supplier_id"=13, "category_id"=7, "quantity_per_unit"='pcs', "unit_price"=49, "units_in_stock"=100, "units_in_order"=2, "reorder_level"=1, "discontinued"=0 WHERE "id" = 9)");
|
|
}
|
|
|
|
TEST_CASE("Execute delete statement", "[session]") {
|
|
connection_pool<connection> pool("sqlite://sqlite.db", 4);
|
|
session s(pool);
|
|
|
|
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(res.first == 0);
|
|
res = s.remove()
|
|
.from("person")
|
|
.where("id"_col == 9)
|
|
.execute();
|
|
|
|
REQUIRE(res.second == R"(DELETE FROM "person" WHERE "id" = 9)");
|
|
|
|
REQUIRE(s.drop().table("person").execute().first == 0);
|
|
|
|
} |