diff options
| author | Dan Goodliffe <dan@randomdan.homeip.net> | 2015-04-28 21:02:14 +0100 | 
|---|---|---|
| committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2015-04-28 21:02:14 +0100 | 
| commit | 8af06d99317d2fdc499eef86d9d5e9c157a052c2 (patch) | |
| tree | abae878df2134e562b76ce8ab40860140e70e9f4 /project2/sql/sql-modPQ.cpp | |
| parent | Pass include dirs to slice tools (diff) | |
| download | project2-8af06d99317d2fdc499eef86d9d5e9c157a052c2.tar.bz2 project2-8af06d99317d2fdc499eef86d9d5e9c157a052c2.tar.xz project2-8af06d99317d2fdc499eef86d9d5e9c157a052c2.zip  | |
Move the mocked PQ db into the PQ module and start the basis of the SQL mod unit tests
Diffstat (limited to 'project2/sql/sql-modPQ.cpp')
| -rw-r--r-- | project2/sql/sql-modPQ.cpp | 60 | 
1 files changed, 60 insertions, 0 deletions
diff --git a/project2/sql/sql-modPQ.cpp b/project2/sql/sql-modPQ.cpp index cb753e0..5267f28 100644 --- a/project2/sql/sql-modPQ.cpp +++ b/project2/sql/sql-modPQ.cpp @@ -1,4 +1,64 @@  #include "connectionLoader.h"  #include "../libpqpp/connection.h" +#include "sql-modPQ.h" +#include <misc.h> +#include <scripts.h> +#include <logger.h> +  typedef PQ::Connection PQConnection;  DECLARE_GENERIC_LOADER("postgresql", ConnectionLoader, PQConnection) + +MockPqDatabase::MockPqDatabase(const std::string & masterdb, const std::string & name, const std::vector<boost::filesystem::path> & ss) : +	master(InstanceMap<ConnectionLoader, std::string>::Get<std::invalid_argument>("postgresql")->create(masterdb)), +	testDbName(stringbf("test_%d_%d", getpid(), ++MockConnectionLoader::mocked)), +	mockName(name) +{ +	Logger()->messagebf(LOG_DEBUG, "Setting up new mocked database %s", testDbName); +	DropDatabase(); +	CreateNewDatabase(); +	try { +		for (auto s : ss) { +			Logger()->messagebf(LOG_DEBUG, "%s << %s", testDbName, s); +			if (system(("psql -v ON_ERROR_STOP=1 -q -1 -U postgres " + testDbName + " -f " + s.string()).c_str())) { +				throw std::runtime_error("Failed to execute " + s.string()); +			} +		} +		Logger()->messagebf(LOG_DEBUG, "%s initialized", testDbName); +		MockConnectionLoader::mocks[name] = boost::bind(MockPqDatabase::openConnection, stringbf("user=postgres dbname=%s", testDbName)); +	} +	catch (...) { +		DropDatabase(); +		throw; +	} +} + +DB::Connection * +MockPqDatabase::openConnection(const std::string & connStr) +{ +	return InstanceMap<ConnectionLoader, std::string>::Get<std::invalid_argument>("postgresql")->create(connStr); +} + +MockPqDatabase::~MockPqDatabase() +{ +	Logger()->messagebf(LOG_DEBUG, "Tearing down mocked database %s", testDbName); +	DropDatabase(); +	delete master; +	MockConnectionLoader::mocks.erase(mockName); +	Logger()->messagebf(LOG_DEBUG, "%s torn down", testDbName); +} + +void MockPqDatabase::DropDatabase() const +{ +	Logger()->messagebf(LOG_INFO, "Killing any active connections to database %s", testDbName); +	master->execute("SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = '" + testDbName + "'"); +	Logger()->messagebf(LOG_INFO, "Dropping (if exists) old database %s", testDbName); +	master->execute("DROP DATABASE IF EXISTS " + testDbName); +} + +void MockPqDatabase::CreateNewDatabase() const +{ +	Logger()->messagebf(LOG_INFO, "Creating new database %s", testDbName); +	master->execute("CREATE DATABASE " + testDbName); +} + +  | 
