158 lines
5.1 KiB
C++
158 lines
5.1 KiB
C++
#include <catch2/catch_test_macros.hpp>
|
|
#include <catch2/generators/catch_generators.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]") {
|
|
auto dns = GENERATE(as<std::string>{},
|
|
"sqlite://sqlite.db",
|
|
"postgres://test:test123@127.0.0.1:5432/matador_test" );
|
|
|
|
|
|
connection_pool<connection> pool(dns, 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 person as record
|
|
auto 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_unsigned_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_unsigned_int);
|
|
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("Select statement with foreign key", "[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)))");
|
|
|
|
std::vector<entity<airplane>> planes {
|
|
make_entity<airplane>(1, "Airbus", "A380"),
|
|
make_entity<airplane>(2, "Boeing", "707"),
|
|
make_entity<airplane>(3, "Boeing", "747")
|
|
};
|
|
|
|
for (const auto &plane : planes) {
|
|
res = s.insert().into<airplane>("airplane").values(*plane).execute();
|
|
REQUIRE(res.first == 1);
|
|
}
|
|
|
|
auto count = s.select({count_all()}).from("airplane").fetch_value<int>();
|
|
REQUIRE(count == 3);
|
|
|
|
flight f4711{4, planes.at(1), "hans"};
|
|
|
|
res = s.insert().into<flight>("flight").values(f4711).execute();
|
|
REQUIRE(res.first == 1);
|
|
|
|
auto f = *s.select<flight>().from("flight").fetch_all<flight>().begin();
|
|
REQUIRE(f.id == 4);
|
|
REQUIRE(f.pilot_name == "hans");
|
|
REQUIRE(f.airplane.get() != nullptr);
|
|
REQUIRE(f.airplane->id == 2);
|
|
|
|
s.drop().table("flight").execute();
|
|
s.drop().table("airplane").execute();
|
|
} |