backend tests progress

This commit is contained in:
Sascha Kuehl 2024-01-29 07:22:28 +01:00
parent 615143efb0
commit 47848ff674
21 changed files with 410 additions and 27 deletions

View File

@ -20,6 +20,8 @@ set(SOURCES
set(LIBRARY_TARGET matador-mysql)
add_subdirectory(test)
add_library(${LIBRARY_TARGET} MODULE ${SOURCES} ${HEADER})
set_target_properties(${LIBRARY_TARGET}

View File

@ -0,0 +1,42 @@
Include(FetchContent)
FetchContent_Declare(
Catch2
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
GIT_TAG v3.4.0 # or a later release
)
FetchContent_MakeAvailable(Catch2)
list(APPEND CMAKE_MODULE_PATH ${catch2_SOURCE_DIR}/extras)
include(CTest)
include(Catch)
set(MYSQL_CONNECTION_STRING "mysql://test:test123!@127.0.0.1:3306/testdb")
configure_file(Connection.hpp.in ${PROJECT_BINARY_DIR}/backends/mysql/test/connection.hpp @ONLY IMMEDIATE)
message(STATUS "mysql connection string: ${MYSQL_CONNECTION_STRING}")
set(TEST_SOURCES
../../tests/QueryTest.cpp
../../tests/SessionTest.cpp
../../tests/ConnectionTest.cpp
../../tests/SessionRecordTest.cpp)
set(LIBRARY_TEST_TARGET mysql_tests)
add_executable(${LIBRARY_TEST_TARGET} ${TEST_SOURCES})
target_link_libraries(${LIBRARY_TEST_TARGET} PRIVATE
Catch2::Catch2WithMain
matador
${CMAKE_DL_LIBS}
${MySQL_LIBRARY})
target_include_directories(${LIBRARY_TEST_TARGET}
PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}>/include
PRIVATE $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}>/test
PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
catch_discover_tests(${LIBRARY_TEST_TARGET} TEST_SUFFIX " (MySQL)")

View File

@ -0,0 +1,8 @@
#ifndef MYSQL_CONNECTION_HPP
#define MYSQL_CONNECTION_HPP
namespace matador::test::connection {
const char* const dns = "@MYSQL_CONNECTION_STRING@";
}
#endif /*SQLITE_CONNECTION_HPP*/

View File

@ -25,9 +25,10 @@ void postgres_connection::open()
conn_ = PQconnectdb(connection.c_str());
if (PQstatus(conn_) == CONNECTION_BAD) {
const auto msg = PQerrorMessage(conn_);
const std::string msg = PQerrorMessage(conn_);
PQfinish(conn_);
throw_postgres_error(msg, "postgres");
conn_ = nullptr;
throw_postgres_error(msg.c_str(), "postgres");
}
}

View File

@ -11,7 +11,7 @@
return "$" + std::to_string(index);
})
.with_token_replace_map({
{dialect::token_t::BEGIN_BINARY_DATA, "E'\\\\"}
{dialect::token_t::BEGIN_BINARY_DATA, "E'\\"}
})
.with_data_type_replace_map({
{data_type_t::type_blob, "BYTEA"}

View File

@ -8,14 +8,21 @@ FetchContent_Declare(
FetchContent_MakeAvailable(Catch2)
SET(POSTGRES_CONNECTION_STRING "postgresql://test:test123@127.0.0.1/matador_test" CACHE STRING "postgresql connection string")
list(APPEND CMAKE_MODULE_PATH ${catch2_SOURCE_DIR}/extras)
include(CTest)
include(Catch)
CONFIGURE_FILE(Connection.hpp.in ${PROJECT_BINARY_DIR}/backends/postgres/test/connection.hpp @ONLY IMMEDIATE)
set(POSTGRES_CONNECTION_STRING "postgres://test:test123@127.0.0.1:15432/test")
MESSAGE(STATUS "postgresql connection string: ${POSTGRES_CONNECTION_STRING}")
MESSAGE(STATUS "current binary dir: ${CMAKE_CURRENT_BINARY_DIR}")
configure_file(Connection.hpp.in ${PROJECT_BINARY_DIR}/backends/postgres/test/connection.hpp @ONLY IMMEDIATE)
set(TEST_SOURCES ../../tests/QueryTest.cpp)
message(STATUS "postgresql connection string: ${POSTGRES_CONNECTION_STRING}")
set(TEST_SOURCES
../../tests/QueryTest.cpp
../../tests/SessionTest.cpp
../../tests/ConnectionTest.cpp
../../tests/SessionRecordTest.cpp)
set(LIBRARY_TEST_TARGET postgres_tests)
@ -29,6 +36,7 @@ target_link_libraries(${LIBRARY_TEST_TARGET} PRIVATE
target_include_directories(${LIBRARY_TEST_TARGET}
PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}>/include
PRIVATE $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}>/test
PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
add_test(NAME matador_postgres_tests COMMAND ${LIBRARY_TEST_TARGET})
catch_discover_tests(${LIBRARY_TEST_TARGET} TEST_SUFFIX " (PostgreSQL)")

View File

@ -1,8 +1,6 @@
#ifndef POSTGRES_CONNECTION_HPP
#define POSTGRES_CONNECTION_HPP
#define MATADOR_DB_TYPE "Postgres"
namespace matador::test::connection {
const char* const dns = "@POSTGRES_CONNECTION_STRING@";
}

View File

@ -20,6 +20,8 @@ set(SOURCES
set(LIBRARY_TARGET matador-sqlite)
add_subdirectory(test)
add_library(${LIBRARY_TARGET} MODULE ${SOURCES} ${HEADER})
set_target_properties(${LIBRARY_TARGET}

View File

@ -0,0 +1,42 @@
Include(FetchContent)
FetchContent_Declare(
Catch2
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
GIT_TAG v3.4.0 # or a later release
)
FetchContent_MakeAvailable(Catch2)
list(APPEND CMAKE_MODULE_PATH ${catch2_SOURCE_DIR}/extras)
include(CTest)
include(Catch)
set(SQLITE_CONNECTION_STRING "sqlite://test.db")
configure_file(Connection.hpp.in ${PROJECT_BINARY_DIR}/backends/sqlite/test/connection.hpp @ONLY IMMEDIATE)
message(STATUS "sqlite connection string: ${SQLITE_CONNECTION_STRING}")
set(TEST_SOURCES
../../tests/QueryTest.cpp
../../tests/SessionTest.cpp
../../tests/ConnectionTest.cpp
../../tests/SessionRecordTest.cpp)
set(LIBRARY_TEST_TARGET sqlite_tests)
add_executable(${LIBRARY_TEST_TARGET} ${TEST_SOURCES})
target_link_libraries(${LIBRARY_TEST_TARGET} PRIVATE
Catch2::Catch2WithMain
matador
${CMAKE_DL_LIBS}
${SQLite3_LIBRARY})
target_include_directories(${LIBRARY_TEST_TARGET}
PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}>/include
PRIVATE $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}>/test
PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
catch_discover_tests(${LIBRARY_TEST_TARGET} TEST_SUFFIX " (SQLite3)")

View File

@ -0,0 +1,8 @@
#ifndef SQLITE_CONNECTION_HPP
#define SQLITE_CONNECTION_HPP
namespace matador::test::connection {
const char* const dns = "@SQLITE_CONNECTION_STRING@";
}
#endif /*SQLITE_CONNECTION_HPP*/

View File

@ -0,0 +1,19 @@
#include "catch2/catch_test_macros.hpp"
#include "matador/sql/connection.hpp"
#include "connection.hpp"
using namespace matador::sql;
TEST_CASE("Create connection test", "[connection]") {
connection c(matador::test::connection::dns);
REQUIRE(!c.is_open());
c.open();
REQUIRE(c.is_open());
c.close();
REQUIRE(!c.is_open());
}

View File

@ -2,6 +2,14 @@
#include <connection.hpp>
TEST_CASE(MATADOR_DB_TYPE " - Query test", "[query]") {
REQUIRE(std::string(matador::test::connection::dns) == "hallo welt");
TEST_CASE("Query test", "[query]") {
SECTION("Create") {
REQUIRE(true);
}
SECTION("Insert") {
REQUIRE(true);
}
SECTION("Select") {
REQUIRE(true);
}
}

View File

@ -0,0 +1,3 @@
//
// Created by sascha on 28.01.24.
//

View File

@ -0,0 +1,181 @@
#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 "models/airplane.hpp"
#include "models/flight.hpp"
#include "models/person.hpp"
using namespace matador::sql;
using namespace matador::test;
class SessionFixture
{
public:
SessionFixture()
: pool(matador::test::connection::dns, 4), ses(pool)
{}
~SessionFixture() {
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();
}
}
};
TEST_CASE_METHOD(SessionFixture, " Create table with foreign key relation", "[session]") {
ses.create()
.table<airplane>("airplane")
.execute();
REQUIRE(ses.table_exists("airplane"));
ses.create()
.table<flight>("flight")
.execute();
REQUIRE(ses.table_exists("flight"));
ses.drop().table("flight").execute();
ses.drop().table("airplane").execute();
REQUIRE(!ses.table_exists("flight"));
REQUIRE(!ses.table_exists("airplane"));
}
TEST_CASE_METHOD(SessionFixture, " Execute select statement with where clause", "[session]") {
ses.create()
.table<person>("person")
.execute();
person george{7, "george", 45};
george.image.push_back(37);
auto res = ses.insert()
.into("person", george)
.execute();
REQUIRE(res == 1);
// fetch person as record
auto result_record = ses.select<person>()
.from("person")
.where("id"_col == 7)
.fetch_all();
for (const auto& i : result_record) {
REQUIRE(i.size() == 4);
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 = ses.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);
}
ses.drop().table("person").execute();
}
TEST_CASE_METHOD(SessionFixture, " Execute insert statement", "[session]") {
ses.create()
.table("person", {
make_pk_column<unsigned long>("id"),
make_column<std::string>("name", 255),
make_column<std::string>("color", 63)
})
.execute();
auto res = ses.insert()
.into("person", {"id", "name", "color"})
.values({7, "george", "green"})
.execute();
REQUIRE(res == 1);
// fetch person as record
auto result_record = ses.select({"id", "name", "color"})
.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<unsigned 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>() == "george");
REQUIRE(i.at(2).name() == "color");
REQUIRE(i.at(2).type() == matador::sql::data_type_t::type_varchar);
REQUIRE(i.at(2).as<std::string>() == "green");
}
ses.drop().table("person").execute();
}
TEST_CASE_METHOD(SessionFixture, " Select statement with foreign key", "[session]") {
ses.create()
.table<airplane>("airplane")
.execute();
ses.create()
.table<flight>("flight")
.execute();
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) {
auto res = ses.insert().into<airplane>("airplane").values(*plane).execute();
REQUIRE(res == 1);
}
auto count = ses.select({count_all()}).from("airplane").fetch_value<int>();
REQUIRE(count == 3);
flight f4711{4, planes.at(1), "hans"};
auto res = ses.insert().into<flight>("flight").values(f4711).execute();
REQUIRE(res == 1);
auto f = *ses.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);
ses.drop().table("flight").execute();
ses.drop().table("airplane").execute();
}

View File

@ -73,14 +73,15 @@ public:
using connection_pointer = connection_ptr<Connection>;
public:
connection_pool(const std::string &dns, unsigned int count)
connection_pool(const std::string &dns, size_t count)
: info_(connection_info::parse(dns)) {
connection_repo_.reserve(count);
for (auto i = 0U; i < count; ++i) {
connection_repo_.emplace_back(i+1, info_);
while (count) {
connection_repo_.emplace_back(count, info_);
auto &conn = connection_repo_.back();
idle_connections_.emplace(conn.first, &conn);
conn.second.open();
--count;
}
}

View File

@ -175,7 +175,8 @@ void query_result_reader::read_value(const char *id, size_t index, any_type &val
break;
}
case sql::data_type_t::type_blob: {
throw std::logic_error("data type blob not supported");
value = utils::blob{};
break;
}
case sql::data_type_t::type_unknown: {
value = std::string(column(index));

View File

@ -33,7 +33,9 @@ add_executable(tests QueryBuilderTest.cpp
TypeTraitsTest.cpp
Databases.hpp
models/optional.hpp
ConvertTest.cpp)
ConvertTest.cpp
DummyConnection.hpp
DummyConnection.cpp)
target_link_libraries(tests PRIVATE
Catch2::Catch2WithMain
@ -42,5 +44,3 @@ target_link_libraries(tests PRIVATE
${SQLite3_LIBRARIES}
${PostgreSQL_LIBRARY})
target_include_directories(tests PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}>/include)
add_test(NAME matador_tests COMMAND tests)

View File

@ -1,12 +1,13 @@
#include <catch2/catch_test_macros.hpp>
#include <matador/sql/connection.hpp>
#include <matador/sql/connection_pool.hpp>
#include "DummyConnection.hpp"
using namespace matador::sql;
TEST_CASE("Create connection pool", "[connection pool]") {
using pool_t = connection_pool<connection>;
using pool_t = connection_pool<matador::test::DummyConnection>;
pool_t pool("sqlite://sqlite.db", 4);
@ -50,7 +51,7 @@ TEST_CASE("Create connection pool", "[connection pool]") {
}
TEST_CASE("Acquire connection by id", "[connection pool]") {
using pool_t = connection_pool<connection>;
using pool_t = connection_pool<matador::test::DummyConnection>;
pool_t pool("sqlite://sqlite.db", 4);

View File

@ -3,8 +3,8 @@
struct Postgres
{
// constexpr static const char *dns{"postgres://test:test123@127.0.0.1:15432/test"};
constexpr static const char *dns{"postgres://test:test123@127.0.0.1:5432/matador_test"};
constexpr static const char *dns{"postgres://test:test123@127.0.0.1:15432/test"};
// constexpr static const char *dns{"postgres://test:test123@127.0.0.1:5432/matador_test"};
};
struct Sqlite
@ -14,8 +14,8 @@ struct Sqlite
struct MySql
{
// constexpr static const char *dns{"mysql://test:test123!@127.0.0.1:3306/testdb"};
constexpr static const char *dns{"mysql://test:test123!@127.0.0.1:3306/matador_test"};
constexpr static const char *dns{"mysql://test:test123!@127.0.0.1:3306/testdb"};
// constexpr static const char *dns{"mysql://test:test123!@127.0.0.1:3306/matador_test"};
};
#endif //QUERY_DATABASES_HPP

28
test/DummyConnection.cpp Normal file
View File

@ -0,0 +1,28 @@
#include "DummyConnection.hpp"
namespace matador::test {
DummyConnection::DummyConnection(sql::connection_info info)
: connection_info_(std::move(info))
{}
DummyConnection::DummyConnection(const std::string &dns)
: DummyConnection(sql::connection_info::parse(dns))
{}
void DummyConnection::open()
{
is_open_ = true;
}
void DummyConnection::close()
{
is_open_ = false;
}
bool DummyConnection::is_open() const
{
return is_open_;
}
}

30
test/DummyConnection.hpp Normal file
View File

@ -0,0 +1,30 @@
#ifndef QUERY_DUMMYCONNECTION_HPP
#define QUERY_DUMMYCONNECTION_HPP
#include "matador/sql/connection_info.hpp"
namespace matador::test {
class DummyConnection
{
public:
explicit DummyConnection(sql::connection_info info);
explicit DummyConnection(const std::string& dns);
DummyConnection(const DummyConnection &x) = default;
DummyConnection& operator=(const DummyConnection &x) = default;
DummyConnection(DummyConnection &&x) noexcept = default;
DummyConnection& operator=(DummyConnection &&x) noexcept = default;
~DummyConnection() = default;
void open();
void close();
[[nodiscard]] bool is_open() const;
private:
sql::connection_info connection_info_;
bool is_open_{};
};
}
#endif //QUERY_DUMMYCONNECTION_HPP