114 lines
3.5 KiB
C++
114 lines
3.5 KiB
C++
#include <catch2/catch_test_macros.hpp>
|
|
|
|
#include <matador/sql/column.hpp>
|
|
#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);
|
|
|
|
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 PRIMARY KEY, "name" VARCHAR(255), "age" INTEGER))");
|
|
|
|
res = s.create().table<matador::test::product>("product").execute();
|
|
}
|
|
|
|
TEST_CASE("Execute drop table statement", "[connection]") {
|
|
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", "[connection]") {
|
|
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();
|
|
|
|
REQUIRE(res.str() == R"(SELECT "id", "name", "color" FROM "person" WHERE "id" = 8)");
|
|
}
|
|
|
|
TEST_CASE("Execute select statement with order by", "[connection]") {
|
|
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();
|
|
|
|
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", "[connection]") {
|
|
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();
|
|
|
|
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", "[connection]") {
|
|
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'))");
|
|
}
|
|
|
|
TEST_CASE("Execute update statement", "[connection]") {
|
|
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)");
|
|
}
|
|
|
|
TEST_CASE("Execute delete statement", "[connection]") {
|
|
connection_pool<connection> pool("sqlite://sqlite.db", 4);
|
|
session s(pool);
|
|
|
|
auto res = s.remove()
|
|
.from("person", "p")
|
|
.where("id"_col == 9)
|
|
.execute();
|
|
|
|
REQUIRE(res.second == R"(DELETE FROM "person" p WHERE "id" = 9)");
|
|
} |