1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
#include "connectionLoader.h"
#include "../libsqlitepp/connection.h"
#include "sql-modSQLite.h"
#include <misc.h>
#include <scripts.h>
#include <logger.h>
#include <boost/filesystem/operations.hpp>
typedef SQLite::Connection SQLiteConnection;
DECLARE_GENERIC_LOADER("sqlite", ConnectionLoader, SQLiteConnection)
MockSQLiteDatabase::MockSQLiteDatabase(const std::string & name, const std::vector<boost::filesystem::path> & ss) :
testDbPath(boost::filesystem::path("/tmp") / "sqliteut" / stringbf("%d", getpid()) / stringbf("%d", ++MockConnectionLoader::mocked)),
mockName(name)
{
Logger()->messagebf(LOG_DEBUG, "Setting up new mocked database at %s", testDbPath);
DropDatabase();
CreateNewDatabase();
try {
for (auto s : ss) {
Logger()->messagebf(LOG_DEBUG, "%s << %s", testDbPath, s);
if (system(("sqlite3 -bail " + testDbPath.string() + " < " + s.string()).c_str())) {
throw std::runtime_error("Failed to execute " + s.string());
}
}
Logger()->messagebf(LOG_DEBUG, "%s initialized", testDbPath);
MockConnectionLoader::mocks[name] = boost::bind(MockSQLiteDatabase::openConnection, testDbPath.string());
}
catch (...) {
DropDatabase();
throw;
}
}
DB::Connection *
MockSQLiteDatabase::openConnection(const std::string & connStr)
{
return InstanceMap<ConnectionLoader, std::string>::Get<std::invalid_argument>("sqlite")->create(connStr);
}
MockSQLiteDatabase::~MockSQLiteDatabase()
{
Logger()->messagebf(LOG_DEBUG, "Tearing down mocked database %s", testDbPath);
DropDatabase();
MockConnectionLoader::mocks.erase(mockName);
Logger()->messagebf(LOG_DEBUG, "%s torn down", testDbPath);
}
void MockSQLiteDatabase::DropDatabase() const
{
Logger()->messagebf(LOG_INFO, "Deleting database %s", testDbPath);
boost::filesystem::remove(testDbPath);
}
void MockSQLiteDatabase::CreateNewDatabase() const
{
Logger()->messagebf(LOG_INFO, "Creating new database at %s", testDbPath);
boost::filesystem::create_directories(testDbPath.parent_path());
}
|