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 | 1069fd56f363e34e6a56992e90776fb084f65495 (patch) | |
tree | ca1ddf45679263c301c5247d814e1dc20004df58 | |
parent | Make the mock connector not PQ specific (diff) | |
download | p2pvr-1069fd56f363e34e6a56992e90776fb084f65495.tar.bz2 p2pvr-1069fd56f363e34e6a56992e90776fb084f65495.tar.xz p2pvr-1069fd56f363e34e6a56992e90776fb084f65495.zip |
Move the mocked PQ db into the PQ module and start the basis of the SQL mod unit tests
-rw-r--r-- | p2pvr/daemon/unittests/mockDatasource.cpp | 70 | ||||
-rw-r--r-- | p2pvr/daemon/unittests/mockDatasource.h | 32 |
2 files changed, 0 insertions, 102 deletions
diff --git a/p2pvr/daemon/unittests/mockDatasource.cpp b/p2pvr/daemon/unittests/mockDatasource.cpp deleted file mode 100644 index cb3c95a..0000000 --- a/p2pvr/daemon/unittests/mockDatasource.cpp +++ /dev/null @@ -1,70 +0,0 @@ -#include "mockDatasource.h" -#include <misc.h> -#include <scripts.h> -#include <connection.h> -#include <logger.h> -#include <boost/filesystem/operations.hpp> -#include <definedDirs.h> - -DECLARE_CUSTOM_COMPONENT_LOADER("mock", T, MockConnectionLoader, ConnectionLoader); - -std::map<std::string, MockConnectionLoader::ConnectionCreator> MockConnectionLoader::mocks; -unsigned int MockConnectionLoader::mocked = 0; - -DB::Connection * -MockConnectionLoader::create(const std::string & n) const -{ - return mocks.find(n)->second(); -} - -MockDatabase::MockDatabase(const std::string & masterdb, const std::string & name, const std::vector<std::string> & 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); - system(("psql -q -1 -U postgres " + testDbName + " -f " + (RootDir / s).string()).c_str()); - } - Logger()->messagebf(LOG_DEBUG, "%s initialized", testDbName); - MockConnectionLoader::mocks[name] = boost::bind(MockDatabase::openConnection, stringbf("user=postgres dbname=%s", testDbName)); - } - catch (...) { - DropDatabase(); - throw; - } -} - -DB::Connection * -MockDatabase::openConnection(const std::string & connStr) -{ - return InstanceMap<ConnectionLoader, std::string>::Get<std::invalid_argument>("postgresql")->create(connStr); -} - -MockDatabase::~MockDatabase() -{ - 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 MockDatabase::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 MockDatabase::CreateNewDatabase() const -{ - Logger()->messagebf(LOG_INFO, "Creating new database %s", testDbName); - master->execute("CREATE DATABASE " + testDbName); -} - diff --git a/p2pvr/daemon/unittests/mockDatasource.h b/p2pvr/daemon/unittests/mockDatasource.h deleted file mode 100644 index df68602..0000000 --- a/p2pvr/daemon/unittests/mockDatasource.h +++ /dev/null @@ -1,32 +0,0 @@ -#ifndef TESTDATASOURCE_H -#define TESTDATASOURCE_H - -#include <rdbmsDataSource.h> - -class MockConnectionLoader : public ConnectionLoader { - public: - DB::Connection * create(const std::string &) const; - - static unsigned int mocked; - typedef boost::function<DB::Connection *()> ConnectionCreator; - static std::map<std::string, ConnectionCreator> mocks; -}; - -class MockDatabase { - public: - MockDatabase(const std::string & master, const std::string & name, const std::vector<std::string> & ss); - ~MockDatabase(); - - protected: - void DropDatabase() const; - void CreateNewDatabase() const; - - private: - static DB::Connection * openConnection(const std::string & connStr); - const DB::Connection * master; - const std::string testDbName; - const std::string mockName; -}; - -#endif - |