stuff-from-scratch/test/database/TestDatabase.cpp
2023-12-27 12:20:02 +00:00

80 lines
1.7 KiB
C++

#include "DatabaseManager.h"
#include "TestFramework.h"
#include "TestUtils.h"
#include "FileLogger.h"
class DbTable
{
public:
enum class Type
{
INT,
VAR_CHAR255
};
using Entry = std::pair<String, Type>;
String mName;
Vector<Entry> mColumns;
String toString(Type type) const
{
switch (type)
{
case Type::INT:
return "int";
case Type::VAR_CHAR255:
return "varchar(255)";
default:
return "varchar(255)";
}
}
String getCreateQuery() const
{
String query = "CREATE TABLE " + mName + " ( ";
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")
{
DatabaseManager db_manager;
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}
};
String statement = table.getCreateQuery();
db_manager.run(statement);
db_manager.onShutDown();
if (std::filesystem::exists(db_path))
{
std::filesystem::remove(db_path);
}
}