Set up some db tests with table create.

This commit is contained in:
jmsgrogan 2023-01-27 17:35:52 +00:00
parent 4d2464c1f5
commit a6d92e142f
5 changed files with 110 additions and 28 deletions

View file

@ -1,6 +1,7 @@
#include "SqliteInterface.h" #include "SqliteInterface.h"
#include "FileLogger.h" #include "FileLogger.h"
#include "Directory.h"
SqliteInterface::SqliteInterface() SqliteInterface::SqliteInterface()
: mSqliteDb(nullptr) : mSqliteDb(nullptr)
@ -20,8 +21,10 @@ std::unique_ptr<SqliteInterface> SqliteInterface::Create()
void SqliteInterface::open(Database* db) void SqliteInterface::open(Database* db)
{ {
Directory::createIfNotExisting(db->getPath());
const auto path = db->getPath().string(); const auto path = db->getPath().string();
int rc = sqlite3_open(path.c_str(), &mSqliteDb); int rc = ::sqlite3_open(path.c_str(), &mSqliteDb);
if( rc ) if( rc )
{ {
MLOG_ERROR("Can't open database: %s\n" << sqlite3_errmsg(mSqliteDb)); MLOG_ERROR("Can't open database: %s\n" << sqlite3_errmsg(mSqliteDb));
@ -42,11 +45,19 @@ static int callback(void *, int argc, char **, char **)
void SqliteInterface::run(const std::string& statement) void SqliteInterface::run(const std::string& statement)
{ {
MLOG_INFO("Running statement: " << statement);
char *zErrMsg = 0; char *zErrMsg = 0;
int rc = sqlite3_exec(mSqliteDb, statement.c_str(), callback, 0, &zErrMsg); int rc = sqlite3_exec(mSqliteDb, statement.c_str(), callback, 0, &zErrMsg);
if (rc != SQLITE_OK) if (rc != SQLITE_OK)
{
if (zErrMsg == nullptr)
{
MLOG_ERROR("Unknown sql error");
}
else
{ {
MLOG_ERROR("SQL error: %s\n" << zErrMsg); MLOG_ERROR("SQL error: %s\n" << zErrMsg);
}
sqlite3_free(zErrMsg); sqlite3_free(zErrMsg);
} }
} }

View file

@ -1,5 +1,7 @@
add_subdirectory(test_utils) add_subdirectory(test_utils)
add_subdirectory(core)
add_subdirectory(database)
add_subdirectory(fonts) add_subdirectory(fonts)
add_subdirectory(geometry) add_subdirectory(geometry)
add_subdirectory(graphics) add_subdirectory(graphics)
@ -13,8 +15,6 @@ set(TEST_MODULES
audio audio
compiler compiler
compression compression
core
database
image image
ipc ipc
mesh mesh

View file

@ -1,13 +1,15 @@
set(CORE_UNIT_TEST_FILES set(MODULE_NAME core)
core/TestByteUtils.cpp
core/TestBitStream.cpp
core/TestDataStructures.cpp
core/TestTomlReader.cpp
core/TestStringUtils.cpp
PARENT_SCOPE
)
set(CORE_UNIT_TEST_DEPENDENCIES list(APPEND UNIT_TEST_FILES
core TestByteUtils.cpp
PARENT_SCOPE TestBitStream.cpp
) TestDataStructures.cpp
TestTomlReader.cpp
TestStringUtils.cpp
)
set(UNIT_TEST_TARGET_NAME ${MODULE_NAME}_unit_tests)
add_executable(${UNIT_TEST_TARGET_NAME} ${CMAKE_SOURCE_DIR}/test/test_runner.cpp ${UNIT_TEST_FILES})
target_link_libraries(${UNIT_TEST_TARGET_NAME} PUBLIC test_utils core)
set_property(TARGET ${UNIT_TEST_TARGET_NAME} PROPERTY FOLDER test/${MODULE_NAME})

View file

@ -1,9 +1,11 @@
set(DATABASE_UNIT_TEST_FILES set(MODULE_NAME database)
database/TestDatabase.cpp
PARENT_SCOPE
)
set(DATABASE_UNIT_TEST_DEPENDENCIES list(APPEND UNIT_TEST_FILES
database TestDatabase.cpp
PARENT_SCOPE )
)
set(UNIT_TEST_TARGET_NAME ${MODULE_NAME}_unit_tests)
add_executable(${UNIT_TEST_TARGET_NAME} ${CMAKE_SOURCE_DIR}/test/test_runner.cpp ${UNIT_TEST_FILES})
target_link_libraries(${UNIT_TEST_TARGET_NAME} PUBLIC test_utils database)
set_property(TARGET ${UNIT_TEST_TARGET_NAME} PROPERTY FOLDER test/${MODULE_NAME})

View file

@ -3,11 +3,78 @@
#include "TestFramework.h" #include "TestFramework.h"
#include "TestUtils.h" #include "TestUtils.h"
#include "FileLogger.h"
class DbTable
{
public:
enum class Type
{
INT,
VAR_CHAR255
};
using Entry = std::pair<std::string, Type>;
std::string mName;
std::vector<Entry> mColumns;
std::string toString(Type type) const
{
switch (type)
{
case Type::INT:
return "int";
case Type::VAR_CHAR255:
return "varchar(255)";
default:
return "varchar(255)";
}
}
std::string getCreateQuery() const
{
std::string query = "CREATE TABLE " + mName + " ( ";
std::size_t count = 0;
for (const auto& column : mColumns)
{
query += column.first + " " + toString(column.second);
if (count < mColumns.size() - 1)
{
query += ", ";
}
count++;
}
query += "); ";
return query;
}
};
TEST_CASE(TestDatabaseManager, "database") TEST_CASE(TestDatabaseManager, "database")
{ {
DatabaseManager db_manager; DatabaseManager db_manager;
db_manager.openDatabase(TestUtils::getTestOutputDir() / "test.db");
std::string statement = "CREATE TABLE corporation;"; const auto db_path = TestUtils::getTestOutputDir() / "test.db";
db_manager.openDatabase(db_path);
DbTable table;
table.mName = "Persons";
table.mColumns = {
{"PersonID", DbTable::Type::INT},
{"LastName", DbTable::Type::VAR_CHAR255},
{"FirstName", DbTable::Type::VAR_CHAR255},
{"Address", DbTable::Type::VAR_CHAR255},
{"City", DbTable::Type::VAR_CHAR255}
};
std::string statement = table.getCreateQuery();
db_manager.run(statement); db_manager.run(statement);
db_manager.onShutDown();
if (std::filesystem::exists(db_path))
{
std::filesystem::remove(db_path);
}
} }