backend tests progress

This commit is contained in:
2024-01-29 20:37:02 +01:00
parent 47848ff674
commit e171147915
12 changed files with 414 additions and 660 deletions
+361 -3
View File
@@ -1,3 +1,361 @@
//
// Created by sascha on 28.01.24.
//
#include <catch2/catch_test_macros.hpp>
#include "matador/sql/column.hpp"
#include "matador/sql/condition.hpp"
#include "matador/sql/session.hpp"
#include "connection.hpp"
#include <list>
class SessionRecordFixture
{
public:
SessionRecordFixture()
: pool(matador::test::connection::dns, 4), ses(pool)
{}
~SessionRecordFixture() {
drop_table_if_exists("flight");
drop_table_if_exists("airplane");
drop_table_if_exists("person");
}
protected:
matador::sql::connection_pool<matador::sql::connection> pool;
matador::sql::session ses;
private:
void drop_table_if_exists(const std::string &table_name) {
if (ses.table_exists(table_name)) {
ses.drop().table(table_name).execute();
}
}
};
using namespace matador::sql;
TEST_CASE_METHOD(SessionRecordFixture, " Create and drop table statement", "[session record]")
{
REQUIRE(!ses.table_exists("person"));
ses.create()
.table("person", {
make_pk_column<unsigned long>("id"),
make_column<std::string>("name", 255),
make_column<unsigned short>("age")
})
.execute();
REQUIRE(ses.table_exists("person"));
ses.drop()
.table("person")
.execute();
REQUIRE(!ses.table_exists("person"));
}
TEST_CASE_METHOD(SessionRecordFixture, " Create and drop table statement with foreign key", "[session record]")
{
ses.create()
.table("airplane", {
make_pk_column<unsigned long>("id"),
make_column<std::string>("brand", 255),
make_column<std::string>("model", 255),
})
.execute();
REQUIRE(ses.table_exists("airplane"));
ses.create()
.table("flight", {
make_pk_column<unsigned long>("id"),
make_fk_column<unsigned long>("airplane_id", "airplane", "id"),
make_column<std::string>("pilot_name", 255),
})
.execute();
REQUIRE(ses.table_exists("flight"));
ses.drop()
.table("flight")
.execute();
REQUIRE(!ses.table_exists("flight"));
ses.drop()
.table("airplane")
.execute();
REQUIRE(!ses.table_exists("airplane"));
}
TEST_CASE_METHOD(SessionRecordFixture, " Execute insert record statement", "[session record]")
{
ses.create()
.table("person", {
make_pk_column<unsigned long>("id"),
make_column<std::string>("name", 255),
make_column<unsigned short>("age")
})
.execute();
auto res = ses.insert()
.into("person", {"id", "name", "age"})
.values({7, "george", 45})
.execute();
REQUIRE(res == 1);
auto result = ses.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(0).template as<long long>() == 7);
REQUIRE(i.at(1).name() == "name");
// REQUIRE(i.at(1).type() == data_type_t::type_varchar);
REQUIRE(i.at(1).template as<std::string>() == "george");
REQUIRE(i.at(2).name() == "age");
// REQUIRE(i.at(2).type() == matador::sql::data_type_t::type_int);
REQUIRE(i.at(2).template as<int>() == 45);
}
ses.drop()
.table("person")
.execute();
}
TEST_CASE_METHOD(SessionRecordFixture, " Execute insert record statement with foreign key", "[session record]")
{
ses.create()
.table("airplane", {
make_pk_column<unsigned long>("id"),
make_column<std::string>("brand", 255),
make_column<std::string>("model", 255),
})
.execute();
ses.create()
.table("flight", {
make_pk_column<unsigned long>("id"),
make_fk_column<unsigned long>("airplane_id", "airplane", "id"),
make_column<std::string>("pilot_name", 255),
})
.execute();
auto res = ses.insert().into("airplane", {"id", "brand", "model"}).values({1, "Airbus", "A380"}).execute();
REQUIRE(res == 1);
res = ses.insert().into("airplane", {"id", "brand", "model"}).values({2, "Boeing", "707"}).execute();
REQUIRE(res == 1);
res = ses.insert().into("airplane", {"id", "brand", "model"}).values({3, "Boeing", "747"}).execute();
REQUIRE(res == 1);
auto count = ses.select({count_all()}).from("airplane").fetch_value<int>();
REQUIRE(count == 3);
res = ses.insert().into("flight", {"id", "airplane_id", "pilot_name"}).values({4, 1, "George"}).execute();
REQUIRE(res == 1);
ses.drop().table("flight").execute();
ses.drop().table("airplane").execute();
REQUIRE(!ses.table_exists("flight"));
REQUIRE(!ses.table_exists("airplane"));
}
TEST_CASE_METHOD(SessionRecordFixture, " Execute update record statement", "[session record]")
{
ses.create()
.table("person", {
make_pk_column<unsigned long>("id"),
make_column<std::string>("name", 255),
make_column<unsigned short>("age")
})
.execute();
auto res = ses.insert()
.into("person", {"id", "name", "age"})
.values({7, "george", 45})
.execute();
REQUIRE(res == 1);
res = ses.update("person")
.set({{"id", 7},
{"name", "jane"},
{"age", 35}})
.where("id"_col == 7)
.execute();
REQUIRE(res == 1);
auto result = ses.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(0).as<long long>() == 7);
REQUIRE(i.at(1).name() == "name");
REQUIRE(i.at(1).type() == data_type_t::type_varchar);
REQUIRE(i.at(1).as<std::string>() == "jane");
REQUIRE(i.at(2).name() == "age");
REQUIRE(i.at(2).type() == matador::sql::data_type_t::type_int);
REQUIRE(i.at(2).as<int>() == 35);
}
ses.drop().table("person").execute();
}
TEST_CASE_METHOD(SessionRecordFixture, " Execute select statement", "[session record]")
{
ses.create()
.table("person", {
make_pk_column<unsigned long>("id"),
make_column<std::string>("name", 255),
make_column<unsigned short>("age")
})
.execute();
auto res = ses.insert().into("person", {"id", "name", "age"}).values({1, "george", 45}).execute();
REQUIRE(res == 1);
res = ses.insert().into("person", {"id", "name", "age"}).values({2, "jane", 32}).execute();
REQUIRE(res == 1);
res = ses.insert().into("person", {"id", "name", "age"}).values({3, "michael", 67}).execute();
REQUIRE(res == 1);
res = ses.insert().into("person", {"id", "name", "age"}).values({4, "bob", 13}).execute();
REQUIRE(res == 1);
auto result = ses.select({"id", "name", "age"})
.from("person")
.fetch_all();
std::list<std::string> expected_names{"george", "jane", "michael", "bob"};
for (const auto &p: result) {
REQUIRE(p.at(1).str() == expected_names.front());
expected_names.pop_front();
}
REQUIRE(expected_names.empty());
auto rec = ses.select({"id", "name", "age"})
.from("person")
.fetch_one();
REQUIRE(rec.at(1).str() == "george");
auto name = ses.select({"name"})
.from("person")
.fetch_value<std::string>();
REQUIRE(name == "george");
ses.drop().table("person").execute();
}
TEST_CASE_METHOD(SessionRecordFixture, " Execute select statement with order by", "[session record]")
{
ses.create()
.table("person", {
make_pk_column<unsigned long>("id"),
make_column<std::string>("name", 255),
make_column<unsigned short>("age")
})
.execute();
auto res = ses.insert().into("person", {"id", "name", "age"}).values({1, "george", 45}).execute();
REQUIRE(res == 1);
res = ses.insert().into("person", {"id", "name", "age"}).values({2, "jane", 32}).execute();
REQUIRE(res == 1);
res = ses.insert().into("person", {"id", "name", "age"}).values({3, "michael", 67}).execute();
REQUIRE(res == 1);
res = ses.insert().into("person", {"id", "name", "age"}).values({4, "bob", 13}).execute();
REQUIRE(res == 1);
auto result = ses.select({"id", "name", "age"})
.from("person")
.order_by("name").asc()
.fetch_all();
std::list<std::string> expected_names{"bob", "george", "jane", "michael"};
for (const auto &p: result) {
REQUIRE(p.at(1).str() == expected_names.front());
expected_names.pop_front();
}
REQUIRE(expected_names.empty());
ses.drop().table("person").execute();
}
TEST_CASE_METHOD(SessionRecordFixture, " Execute select statement with group by and order by", "[session record]")
{
ses.create()
.table("person", {
make_pk_column<unsigned long>("id"),
make_column<std::string>("name", 255),
make_column<unsigned short>("age")
})
.execute();
auto res = ses.insert().into("person", {"id", "name", "age"}).values({1, "george", 45}).execute();
REQUIRE(res == 1);
res = ses.insert().into("person", {"id", "name", "age"}).values({2, "jane", 45}).execute();
REQUIRE(res == 1);
res = ses.insert().into("person", {"id", "name", "age"}).values({3, "michael", 13}).execute();
REQUIRE(res == 1);
res = ses.insert().into("person", {"id", "name", "age"}).values({4, "bob", 13}).execute();
REQUIRE(res == 1);
res = ses.insert().into("person", {"id", "name", "age"}).values({5, "charlie", 67}).execute();
REQUIRE(res == 1);
auto result = ses.select({alias(count("age"), "age_count"), "age"})
.from("person")
.group_by("age")
.order_by("age_count").desc()
.fetch_all();
std::list<std::pair<int, int>> expected_values{{2, 45},
{2, 13},
{1, 67}};
for (const auto &r: result) {
REQUIRE(r.at(0).as<int>() == expected_values.front().first);
REQUIRE(r.at(1).as<int>() == expected_values.front().second);
expected_values.pop_front();
}
ses.drop().table("person").execute();
}
TEST_CASE_METHOD(SessionRecordFixture, " Execute delete statement", "[session record]")
{
ses.create()
.table("person", {
make_pk_column<unsigned long>("id"),
make_column<std::string>("name", 255),
make_column<unsigned short>("age")
}).execute();
auto res = ses.insert().into("person", {"id", "name", "age"}).values({1, "george", 45}).execute();
REQUIRE(res == 1);
res = ses.insert().into("person", {"id", "name", "age"}).values({2, "jane", 45}).execute();
REQUIRE(res == 1);
auto count = ses.select({count_all()}).from("person").fetch_value<int>();
REQUIRE(count == 2);
res = ses.remove()
.from("person")
.where("id"_col == 1)
.execute();
REQUIRE(res == 1);
count = ses.select({count_all()}).from("person").fetch_value<int>();
REQUIRE(count == 1);
ses.drop().table("person").execute();
}
+103
View File
@@ -0,0 +1,103 @@
#include <catch2/catch_test_macros.hpp>
#include "matador/sql/column.hpp"
#include "matador/sql/condition.hpp"
#include "matador/sql/connection_info.hpp"
#include "matador/sql/connection_pool.hpp"
#include "matador/sql/session.hpp"
#include "connection.hpp"
#include "models/airplane.hpp"
using namespace matador::sql;
using namespace matador::test;
class StatementTestFixture
{
public:
StatementTestFixture()
: pool(matador::test::connection::dns, 4), ses(pool)
{
ses.create().table<airplane>("airplane").execute();
}
~StatementTestFixture()
{
drop_table_if_exists("airplane");
}
protected:
matador::sql::connection_pool<matador::sql::connection> pool;
matador::sql::session ses;
std::vector<entity<airplane>> planes{
make_entity<airplane>(1, "Airbus", "A380"),
make_entity<airplane>(2, "Boeing", "707"),
make_entity<airplane>(3, "Boeing", "747")
};
private:
void drop_table_if_exists(const std::string &table_name) {
if (ses.table_exists(table_name)) {
ses.drop().table(table_name).execute();
}
}
};
TEST_CASE_METHOD(StatementTestFixture, " Create prepared statement", "[statement]")
{
SECTION("Insert with prepared statement and placeholder") {
auto stmt = ses.insert()
.into<airplane>("airplane")
.values<airplane>().prepare();
for (const auto &plane: planes) {
auto res = stmt.bind(*plane).execute();
REQUIRE(res == 1);
stmt.reset();
}
auto result = ses.select<airplane>().from("airplane").fetch_all<airplane>();
size_t index{0};
for (const auto &i: result) {
REQUIRE(i.id == planes[index]->id);
REQUIRE(i.brand == planes[index]->brand);
REQUIRE(i.model == planes[index++]->model);
}
}
SECTION("Select with prepared statement") {
for (const auto &plane: planes) {
auto res = ses.insert().into<airplane>("airplane").values(*plane).execute();
REQUIRE(res == 1);
}
auto stmt = ses.select<airplane>().from("airplane").where("brand"_col == _).prepare();
stmt.bind(0, "Airbus");
auto result = stmt.fetch<airplane>();
for (const auto &i: result) {
REQUIRE(i.id == planes[0]->id);
REQUIRE(i.brand == planes[0]->brand);
REQUIRE(i.model == planes[0]->model);
}
stmt.reset();
stmt.bind(0, "Boeing");
result = stmt.fetch<airplane>();
size_t index{1};
for (const auto &i: result) {
REQUIRE(i.id == planes[index]->id);
REQUIRE(i.brand == planes[index]->brand);
REQUIRE(i.model == planes[index++]->model);
}
}
}
+112
View File
@@ -0,0 +1,112 @@
#include <catch2/catch_test_macros.hpp>
#include "matador/sql/session.hpp"
#include "matador/utils/enum_mapper.hpp"
#include "connection.hpp"
#include "models/location.hpp"
using namespace matador::test;
class TypeTraitsTestFixture
{
public:
TypeTraitsTestFixture()
: pool(matador::test::connection::dns, 4), ses(pool)
{
ses.create()
.table<location>("location")
.execute();
}
~TypeTraitsTestFixture()
{
ses.drop().table("location").execute();
}
protected:
matador::sql::connection_pool<matador::sql::connection> pool;
matador::sql::session ses;
private:
void drop_table_if_exists(const std::string &table_name) {
if (ses.table_exists(table_name)) {
ses.drop().table(table_name).execute();
}
}
};
static const matador::utils::enum_mapper<Color> color_enum({
{Color::Green, "green"},
{Color::Red, "red"},
{Color::Blue, "blue"},
{Color::Yellow, "yellow"},
{Color::Black, "black"},
{Color::White, "white"},
{Color::Brown, "brown"}
});
namespace matador::sql {
template<>
struct data_type_traits<Color, void>
{
inline static data_type_t builtin_type(std::size_t size)
{ return data_type_traits<std::string>::builtin_type(size); }
static void read_value(query_result_reader &reader, const char *id, size_t index, Color &value)
{
std::string enum_string;
reader.read_value(id, index, enum_string, 64);
auto enum_opt = color_enum.to_enum(enum_string);
if (enum_opt) {
value = enum_opt.value();
}
}
static any_type create_value(Color &value)
{
return color_enum.to_string(value);
}
static void bind_value(parameter_binder &binder, size_t index, Color &value)
{
binder.bind(index, color_enum.to_string(value));
}
};
}
TEST_CASE_METHOD(TypeTraitsTestFixture, "Special handling of attributes with type traits", "[typetraits]")
{
SECTION("Insert and select with direct execution") {
location loc{1, "center", {1, 2, 3}, Color::Black};
auto res = ses.insert().into<location>("location").values(loc).execute();
REQUIRE(res == 1);
auto result = ses.select<location>().from("location").fetch_all<location>();
for (const auto &l: result) {
REQUIRE(l.name == "center");
}
}
SECTION("Insert and select with prepared statement") {
location loc{1, "center", {1, 2, 3}, Color::Black};
auto stmt = ses.insert().into<location>("location").values<location>().prepare();
auto res = stmt.bind(loc).execute();
REQUIRE(res == 1);
auto result = ses.select<location>().from("location").
template fetch_all<location>();
for (const auto &l: result) {
REQUIRE(l.name == "center");
}
}
}