stuff-from-scratch/test/database/TestDatabase.cpp

81 lines
1.7 KiB
C++
Raw Normal View History

2022-01-01 18:46:31 +00:00
#include "DatabaseManager.h"
2022-11-29 18:00:19 +00:00
#include "TestFramework.h"
2022-12-01 10:52:48 +00:00
#include "TestUtils.h"
2022-11-29 18:00:19 +00:00
#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;
}
};
2022-11-29 18:00:19 +00:00
TEST_CASE(TestDatabaseManager, "database")
2022-01-01 18:46:31 +00:00
{
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}
};
std::string statement = table.getCreateQuery();
2022-12-01 10:52:48 +00:00
db_manager.run(statement);
db_manager.onShutDown();
if (std::filesystem::exists(db_path))
{
std::filesystem::remove(db_path);
}
2022-01-01 18:46:31 +00:00
}