103 lines
2.8 KiB
C++
103 lines
2.8 KiB
C++
#include <catch2/catch_test_macros.hpp>
|
|
|
|
#include <matador/sql/column.hpp>
|
|
#include <matador/sql/condition.hpp>
|
|
#include <matador/sql/connection.hpp>
|
|
|
|
using namespace matador::sql;
|
|
|
|
TEST_CASE("Execute create table statement", "[connection]") {
|
|
connection c;
|
|
|
|
c.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.sql == R"(SELECT "id", "name", "color" FROM "person" WHERE "id" = 8)");
|
|
}
|
|
|
|
TEST_CASE("Execute drop table statement", "[connection]") {
|
|
connection c;
|
|
|
|
c.drop()
|
|
.table("person").execute();
|
|
|
|
REQUIRE(true);
|
|
// REQUIRE(res.sql == R"(SELECT "id", "name", "color" FROM "person" WHERE "id" = 8)");
|
|
}
|
|
|
|
TEST_CASE("Execute select statement with where clause", "[connection]") {
|
|
connection c;
|
|
|
|
auto res = c.select({"id", "name", "color"})
|
|
.from("person")
|
|
.where("id"_col == 8)
|
|
.fetch_all();
|
|
|
|
REQUIRE(res.sql == R"(SELECT "id", "name", "color" FROM "person" WHERE "id" = 8)");
|
|
}
|
|
|
|
TEST_CASE("Execute select statement with order by", "[connection]") {
|
|
connection c;
|
|
|
|
auto res = c.select({"id", "name", "color"})
|
|
.from("person")
|
|
.where("id"_col == 8)
|
|
.order_by("name").desc()
|
|
.fetch_all();
|
|
|
|
REQUIRE(res.sql == 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 c;
|
|
|
|
auto res = c.select({"id", "name", "color"})
|
|
.from("person")
|
|
.where("id"_col == 8)
|
|
.group_by("color")
|
|
.order_by("name").asc()
|
|
.fetch_all();
|
|
|
|
REQUIRE(res.sql == R"(SELECT "id", "name", "color" FROM "person" WHERE "id" = 8 GROUP BY "color" ORDER BY "name" ASC)");
|
|
}
|
|
|
|
TEST_CASE("Execute insert statement", "[connection]") {
|
|
connection c;
|
|
|
|
auto res = c.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 c;
|
|
|
|
auto res = c.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 c;
|
|
|
|
auto res = c.remove()
|
|
.from("person", "p")
|
|
.where("id"_col == 9)
|
|
.execute();
|
|
|
|
REQUIRE(res.second == R"(DELETE FROM "person" p WHERE "id" = 9)");
|
|
} |