#include #include #include #include #include "models/product.hpp" using namespace matador::sql; using namespace matador::test; TEST_CASE("Execute create and drop table statement", "[session]") { connection_pool pool("sqlite://sqlite.db", 4); session s(pool); auto res = s.create() .table("person", { make_pk_column("id"), make_column("name", 255), make_column("age") }).execute(); REQUIRE(res.second == R"(CREATE TABLE "person" ("id" BIGINT NOT NULL, "name" VARCHAR(255), "age" INTEGER, CONSTRAINT PK_person PRIMARY KEY (id)))"); res = s.drop().table("person").execute(); REQUIRE(res.second == R"(DROP TABLE "person")"); } TEST_CASE("Execute create table statement with foreign keys", "[session]") { // res = s.create().table("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 insert record statement", "[session]") { connection_pool pool("sqlite://sqlite.db", 4); session s(pool); auto res = s .create() .table("person", { make_pk_column("id"), make_column("name", 255), make_column("age") }) .execute(); REQUIRE(res.first == 0); res = s .insert() .into("person", {"id", "name", "age"}) .values({7, "george", 45}) .execute(); REQUIRE(res.first == 1); REQUIRE(res.second == R"(INSERT INTO "person" ("id", "name", "age") VALUES (7, 'george', 45))"); auto result = s .select({"id", "name", "age"}) .from("person") .fetch_all(); for (const auto& i : result) { REQUIRE(i.size() == 3); REQUIRE(i.at(0).name() == "id"); REQUIRE(i.at(0).type() == data_type_t::type_long_long); REQUIRE(i.at(1).name() == "name"); REQUIRE(i.at(1).type() == data_type_t::type_varchar); REQUIRE(i.at(2).name() == "age"); REQUIRE(i.at(2).type() == matador::sql::data_type_t::type_int); } s.drop().table("person").execute(); // 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(); // p.category->id = 7; // p.supplier = make_foreign();; // p.supplier->id = 13; // p.product_name = "candle"; // // res = s.insert().into("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 select statement with where clause", "[session]") { connection_pool 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 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 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 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(); p.category->id = 7; p.supplier = make_foreign(); p.supplier->id = 13; p.product_name = "candle"; res = s.insert().into("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 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(); p.category->id = 7; p.supplier = make_foreign(); 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 pool("sqlite://sqlite.db", 4); session s(pool); auto res = s.create() .table("person", { make_pk_column("id"), make_column("name", 255), make_column("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); }