177 lines
5.4 KiB
C++
177 lines
5.4 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"
|
|
#include "models/airplane.hpp"
|
|
#include "models/flight.hpp"
|
|
#include "models/person.hpp"
|
|
|
|
using namespace matador::sql;
|
|
using namespace matador::test;
|
|
|
|
TEST_CASE("Create table with foreign key relation", "[session]") {
|
|
connection_pool<connection> pool("sqlite://sqlite.db", 4);
|
|
session s(pool);
|
|
|
|
auto res = s.create().table<airplane>("airplane").execute();
|
|
REQUIRE(res.first == 0);
|
|
REQUIRE(res.second == R"(CREATE TABLE "airplane" ("id" BIGINT, "brand" VARCHAR(255), "model" VARCHAR(255), CONSTRAINT PK_airplane PRIMARY KEY (id)))");
|
|
|
|
res = s.create().table<flight>("flight").execute();
|
|
REQUIRE(res.first == 0);
|
|
REQUIRE(res.second == R"(CREATE TABLE "flight" ("id" BIGINT, "airplane_id" BIGINT, "pilot_name" VARCHAR(255), CONSTRAINT PK_flight PRIMARY KEY (id), CONSTRAINT FK_flight_airplane_id FOREIGN KEY (airplane_id) REFERENCES airplane(id)))");
|
|
|
|
s.drop().table("flight").execute();
|
|
s.drop().table("airplane").execute();
|
|
}
|
|
|
|
TEST_CASE("Execute select statement with where clause", "[session]") {
|
|
connection_pool<connection> pool("sqlite://sqlite.db", 4);
|
|
session s(pool);
|
|
|
|
auto res = s.create()
|
|
.table<person>("person")
|
|
.execute();
|
|
|
|
REQUIRE(res.first == 0);
|
|
|
|
person george{7, "george", 45};
|
|
|
|
res = s.insert()
|
|
.into("person", george)
|
|
.execute();
|
|
REQUIRE(res.first == 1);
|
|
|
|
// fetch specific columns as record
|
|
auto result_record = s.select({"id", "name", "age"})
|
|
.from("person")
|
|
.fetch_all();
|
|
|
|
for (const auto& i : result_record) {
|
|
REQUIRE(i.size() == 3);
|
|
REQUIRE(i.at(0).name() == "id");
|
|
REQUIRE(i.at(0).type() == data_type_t::type_long_long);
|
|
REQUIRE(i.at(0).as<long long>() == george.id);
|
|
REQUIRE(i.at(1).name() == "name");
|
|
REQUIRE(i.at(1).type() == data_type_t::type_varchar);
|
|
REQUIRE(i.at(1).as<std::string>() == george.name);
|
|
REQUIRE(i.at(2).name() == "age");
|
|
REQUIRE(i.at(2).type() == matador::sql::data_type_t::type_long_long);
|
|
REQUIRE(i.at(2).as<long long>() == george.age);
|
|
}
|
|
|
|
// fetch person as record
|
|
result_record = s.select<person>()
|
|
.from("person")
|
|
.where("id"_col == 7)
|
|
.fetch_all();
|
|
|
|
for (const auto& i : result_record) {
|
|
REQUIRE(i.size() == 3);
|
|
REQUIRE(i.at(0).name() == "id");
|
|
REQUIRE(i.at(0).type() == data_type_t::type_long_long);
|
|
REQUIRE(i.at(0).as<long long>() == george.id);
|
|
REQUIRE(i.at(1).name() == "name");
|
|
REQUIRE(i.at(1).type() == data_type_t::type_varchar);
|
|
REQUIRE(i.at(1).as<std::string>() == george.name);
|
|
REQUIRE(i.at(2).name() == "age");
|
|
REQUIRE(i.at(2).type() == matador::sql::data_type_t::type_long_long);
|
|
REQUIRE(i.at(2).as<long long>() == george.age);
|
|
}
|
|
|
|
// fetch person as person
|
|
auto result_person = s.select<person>()
|
|
.from("person")
|
|
.where("id"_col == 7)
|
|
.fetch_all<person>();
|
|
|
|
for (const auto& i : result_person) {
|
|
REQUIRE(i.id == 7);
|
|
REQUIRE(i.name == "george");
|
|
REQUIRE(i.age == 45);
|
|
}
|
|
|
|
s.drop().table("person").execute();
|
|
}
|
|
|
|
TEST_CASE("Execute insert 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<std::string>("color", 63)
|
|
})
|
|
.execute();
|
|
|
|
res = s.insert()
|
|
.into("person", {"id", "name", "color"})
|
|
.values({7, "george", "green"})
|
|
.execute();
|
|
|
|
REQUIRE(res.first == 1);
|
|
REQUIRE(res.second == R"(INSERT INTO "person" ("id", "name", "color") VALUES (7, 'george', 'green'))");
|
|
|
|
s.drop().table("person").execute();
|
|
}
|
|
|
|
TEST_CASE("Execute update 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<std::string>("color", 63)
|
|
})
|
|
.execute();
|
|
|
|
res = s.insert()
|
|
.into("person", {"id", "name", "color"})
|
|
.values({7, "george", "green"})
|
|
.execute();
|
|
|
|
REQUIRE(res.first == 1);
|
|
REQUIRE(res.second == R"(INSERT INTO "person" ("id", "name", "color") VALUES (7, 'george', 'green'))");
|
|
|
|
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)");
|
|
|
|
s.drop().table("person").execute();
|
|
}
|
|
|
|
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);
|
|
|
|
} |