summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--libdbpp/mockDatabase.cpp4
-rw-r--r--libdbpp/mockDatabase.h1
-rw-r--r--libdbpp/unittests/Jamfile.jam4
-rw-r--r--libdbpp/unittests/mockdb.cpp88
-rw-r--r--libdbpp/unittests/mockdb.h34
-rw-r--r--libdbpp/unittests/testConnection.cpp34
-rw-r--r--libdbpp/unittests/testMock.cpp33
7 files changed, 157 insertions, 41 deletions
diff --git a/libdbpp/mockDatabase.cpp b/libdbpp/mockDatabase.cpp
index 8951189..ddc13af 100644
--- a/libdbpp/mockDatabase.cpp
+++ b/libdbpp/mockDatabase.cpp
@@ -1,8 +1,11 @@
+#define BOOST_FILESYSTEM_DYN_LINK
+#define BOOST_FILESYSTEM_SOURCE
#include "mockDatabase.h"
#include <compileTimeFormatter.h>
#include <fstream>
#include <modifycommand.h>
#include <plugins.impl.h>
+#include <factory.impl.h>
namespace AdHoc {
template <>
@@ -14,6 +17,7 @@ namespace AdHoc {
}
}
INSTANTIATEPLUGINOF(DB::MockDatabase);
+INSTANTIATEFACTORY(DB::MockDatabase, const std::string &, const std::string &, const std::vector<boost::filesystem::path> &);
namespace DB {
diff --git a/libdbpp/mockDatabase.h b/libdbpp/mockDatabase.h
index d6d2328..5d3ee52 100644
--- a/libdbpp/mockDatabase.h
+++ b/libdbpp/mockDatabase.h
@@ -63,6 +63,7 @@ class DLL_PUBLIC MockServerDatabase : public MockDatabase {
const std::string testDbName;
};
+typedef AdHoc::Factory<MockDatabase, const std::string &, const std::string &, const std::vector<boost::filesystem::path> &> MockDatabaseFactory;
}
#endif
diff --git a/libdbpp/unittests/Jamfile.jam b/libdbpp/unittests/Jamfile.jam
index 93b26b3..45c069d 100644
--- a/libdbpp/unittests/Jamfile.jam
+++ b/libdbpp/unittests/Jamfile.jam
@@ -6,7 +6,7 @@ lib boost_utf : : <name>boost_unit_test_framework ;
lib IceUtil ;
run
- testConnection.cpp
+ testConnection.cpp mockdb.cpp
: : :
<define>ROOT=\"$(me)\"
<define>BOOST_TEST_DYN_LINK
@@ -70,7 +70,7 @@ run
;
run
- testMock.cpp
+ testMock.cpp mockdb.cpp
: : :
<define>ROOT=\"$(me)\"
<define>BOOST_TEST_DYN_LINK
diff --git a/libdbpp/unittests/mockdb.cpp b/libdbpp/unittests/mockdb.cpp
new file mode 100644
index 0000000..6ae6810
--- /dev/null
+++ b/libdbpp/unittests/mockdb.cpp
@@ -0,0 +1,88 @@
+#include "mockdb.h"
+
+// LCOV_EXCL_START
+
+MockDb::MockDb(const std::string &)
+{
+}
+
+void
+MockDb::beginTxInt()
+{
+}
+
+void
+MockDb::commitTxInt()
+{
+}
+void
+MockDb::rollbackTxInt()
+{
+}
+void
+MockDb::ping() const
+{
+
+}
+
+DB::BulkDeleteStyle
+MockDb::bulkDeleteStyle() const
+{
+ return DB::BulkDeleteUsingUsing;
+}
+
+DB::BulkUpdateStyle
+MockDb::bulkUpdateStyle() const
+{
+ return DB::BulkUpdateUsingJoin;
+}
+
+void
+MockDb::execute(const std::string & sql)
+{
+ if (sql.substr(0, 3) == "Not") {
+ throw DB::Error();
+ }
+ executed.push_back(sql);
+}
+
+DB::SelectCommand *
+MockDb::newSelectCommand(const std::string &)
+{
+ return nullptr;
+}
+
+DB::ModifyCommand *
+MockDb::newModifyCommand(const std::string &)
+{
+ return nullptr;
+}
+
+MockMock::MockMock(const std::string &, const std::string & name, const std::vector<boost::filesystem::path> & ss) :
+ DB::MockDatabase(name)
+{
+ CreateNewDatabase();
+ PlaySchemaScripts(ss);
+}
+
+DB::Connection *
+MockMock::openConnection() const
+{
+ return new MockDb("");
+}
+
+void
+MockMock::CreateNewDatabase() const
+{
+}
+
+void
+MockMock::DropDatabase() const
+{
+}
+
+FACTORY(MockMock, DB::MockDatabaseFactory);
+FACTORY(MockDb, DB::ConnectionFactory);
+
+// LCOV_EXCL_STOP
+
diff --git a/libdbpp/unittests/mockdb.h b/libdbpp/unittests/mockdb.h
new file mode 100644
index 0000000..cd6fb26
--- /dev/null
+++ b/libdbpp/unittests/mockdb.h
@@ -0,0 +1,34 @@
+#ifndef DB_MOCKDB_H
+#define DB_MOCKDB_H
+
+#include "../connection.h"
+#include "../mockDatabase.h"
+
+class MockDb : public DB::Connection {
+ public:
+ MockDb(const std::string &);
+
+ void beginTxInt() override;
+ void commitTxInt() override;
+ void rollbackTxInt() override;
+ void ping() const override;
+ DB::BulkDeleteStyle bulkDeleteStyle() const override;
+ DB::BulkUpdateStyle bulkUpdateStyle() const override;
+
+ void execute(const std::string & sql) override;
+ DB::SelectCommand * newSelectCommand(const std::string &) override;
+ DB::ModifyCommand * newModifyCommand(const std::string &) override;
+
+ mutable std::vector<std::string> executed;
+};
+
+class MockMock : public DB::MockDatabase {
+ public:
+ MockMock(const std::string &, const std::string &, const std::vector<boost::filesystem::path> &);
+ DB::Connection * openConnection() const override;
+ void CreateNewDatabase() const override;
+ void DropDatabase() const override;
+};
+
+#endif
+
diff --git a/libdbpp/unittests/testConnection.cpp b/libdbpp/unittests/testConnection.cpp
index d4b1610..f1df3b0 100644
--- a/libdbpp/unittests/testConnection.cpp
+++ b/libdbpp/unittests/testConnection.cpp
@@ -8,39 +8,7 @@
#include <vector>
#include <error.h>
#include <sqlParse.h>
-
-// LCOV_EXCL_START
-class MockDb : public DB::Connection {
- public:
- MockDb(const std::string &) {}
-
- void beginTxInt() override { }
- void commitTxInt() override { }
- void rollbackTxInt() override { }
- void ping() const override {}
- DB::BulkDeleteStyle bulkDeleteStyle() const override { return DB::BulkDeleteUsingUsing; }
- DB::BulkUpdateStyle bulkUpdateStyle() const override { return DB::BulkUpdateUsingJoin; }
-
- void execute(const std::string & sql) override {
- executed.push_back(sql);
- }
- DB::SelectCommand * newSelectCommand(const std::string &) override { return nullptr; }
- DB::ModifyCommand * newModifyCommand(const std::string &) override { return nullptr; }
-
- mutable std::vector<std::string> executed;
-};
-// LCOV_EXCL_STOP
-
-FACTORY(MockDb, DB::ConnectionFactory);
-
-BOOST_AUTO_TEST_CASE( plugins )
-{
- auto pm = AdHoc::PluginManager::getDefault();
- BOOST_REQUIRE(pm);
- BOOST_REQUIRE_EQUAL(1, pm->count());
- BOOST_REQUIRE_EQUAL(1, pm->getAll().size());
- BOOST_REQUIRE_EQUAL(1, pm->getAll<DB::ConnectionFactory>().size());
-}
+#include "mockdb.h"
BOOST_AUTO_TEST_CASE( create )
{
diff --git a/libdbpp/unittests/testMock.cpp b/libdbpp/unittests/testMock.cpp
index 0b1efd5..add0720 100644
--- a/libdbpp/unittests/testMock.cpp
+++ b/libdbpp/unittests/testMock.cpp
@@ -3,20 +3,41 @@
#include <definedDirs.h>
#include <fstream>
-#include <pq-mock.h>
-#include "error.h"
+#include "../error.h"
+#include "../mockDatabase.h"
+#include "mockdb.h"
+
+BOOST_AUTO_TEST_CASE( noFactory )
+{
+ BOOST_REQUIRE_THROW({
+ DB::MockDatabaseFactory::get("not-found");
+ }, AdHoc::NoSuchPluginException);
+}
+
+BOOST_AUTO_TEST_CASE( mockFactory )
+{
+ auto f = DB::MockDatabaseFactory::get("MockMock");
+ BOOST_REQUIRE(f);
+ auto m = f->create("", typeid(this).name(), {});
+ BOOST_REQUIRE(m);
+ auto c = m->openConnection();
+ BOOST_REQUIRE(c);
+ BOOST_REQUIRE_EQUAL(typeid(MockDb), typeid(*c));
+}
BOOST_AUTO_TEST_CASE( missingMock )
{
BOOST_REQUIRE_THROW({
- PQ::Mock m ("user=postgres dbname=postgres", "pqmock", { rootDir / "missing.sql" });
- }, std::fstream::failure);
+ DB::MockDatabaseFactory::createNew("MockMock",
+ "user=postgres dbname=postgres", typeid(this).name(), { rootDir / "missing.sql" });
+ }, std::fstream::failure);
}
BOOST_AUTO_TEST_CASE( failingMock )
{
BOOST_REQUIRE_THROW({
- PQ::Mock m ("user=postgres dbname=postgres", "pqmock", { rootDir / "badMock.sql" });
- }, DB::Error);
+ DB::MockDatabaseFactory::createNew("MockMock",
+ "user=postgres dbname=postgres", typeid(this).name(), { rootDir / "badMock.sql" });
+ }, DB::Error);
}