summaryrefslogtreecommitdiff
path: root/libsqlitepp/sqlite-mock.cpp
blob: 9c37012de425539bc9b346f4d27fb31153d5e30a (plain)
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
#include "sqlite-mock.h"
#include "mockDatabase.h"
#include "sqlite-connection.h"
#include <factory.h>
#include <memory>
#include <unistd.h>

NAMEDFACTORY("sqlite", SQLite::Mock, DB::MockDatabaseFactory)

namespace SQLite {

	Mock::Mock(const std::string & root, const std::string & name, const std::vector<std::filesystem::path> & ss) :
		testDbPath(std::filesystem::path(root) / name / std::to_string(getpid())
				/ std::to_string(++DB::MockDatabase::mocked))
	{
		Mock::CreateNewDatabase();
		PlaySchemaScripts(ss);
	}

	Mock::Mock(const std::string & name, const std::vector<std::filesystem::path> & ss) :
		Mock("/tmp/sqliteut", name, ss)
	{
	}

	DB::ConnectionPtr
	Mock::openConnection() const
	{
		return std::make_shared<Connection>(testDbPath.string());
	}

	Mock::~Mock()
	{
		Mock::DropDatabase();
	}

	void
	Mock::DropDatabase() const
	{
		std::filesystem::remove(testDbPath);
	}

	void
	Mock::CreateNewDatabase() const
	{
		std::filesystem::create_directories(testDbPath.parent_path());
		(void)Mock::openConnection(); // Triggers file creation
	}

}