summaryrefslogtreecommitdiff
path: root/libmysqlpp/embeddedmy-mock.cpp
blob: 9e66bb3e2dd901d0596842138f480f6d11c87789 (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
50
51
52
#include "embeddedmy-connection.h"
#include "embeddedmy-mock.h"
#include "embeddedmy-server.h"
#include <boost/filesystem/convenience.hpp>
#include <buffer.h>

namespace MySQL {
	namespace Embedded {

		Mock::Mock(const std::string & name, const std::vector<boost::filesystem::path> & ss) :
			MockDatabase(name),
			dbname(stringbf("test_%d_%d", getpid(), ++DB::MockDatabase::mocked))
		{
			CreateNewDatabase();
			PlaySchemaScripts(ss);
		}

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

		DB::Connection *
		Mock::openConnection() const
		{
			return new Connection(stringbf("path=%s;database=%s", mockDbPath(), dbname));
		}

		void
		Mock::CreateNewDatabase() const
		{
			auto path = mockDbPath();
			embeddedServer = Server::getMock(path);
			Connection initialize(embeddedServer.get(), "mysql");
			initialize.execute("CREATE DATABASE " + dbname);
		}

		void
		Mock::DropDatabase() const
		{
			Connection initialize(embeddedServer.get(), "mysql");
			initialize.execute("DROP DATABASE " + dbname);
		}

		boost::filesystem::path
		Mock::mockDbPath()
		{
			return boost::filesystem::temp_directory_path() / stringbf("mysql-mock-%d", getpid());
		}
	}
}