backend tests progress

This commit is contained in:
2024-01-29 07:22:28 +01:00
parent 615143efb0
commit 47848ff674
21 changed files with 410 additions and 27 deletions
+2
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}
+42
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)")
+8
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*/
@@ -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");
}
}
+1 -1
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"}
+14 -6
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)")
-2
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@";
}
+2
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}
+42
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)")
+8
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*/
+19
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());
}
+10 -2
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);
}
}
+3
View File
@@ -0,0 +1,3 @@
//
// Created by sascha on 28.01.24.
//
+181
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();
}