diff options
Diffstat (limited to 'libdbpp/unittests')
-rw-r--r-- | libdbpp/unittests/Jamfile.jam | 4 | ||||
-rw-r--r-- | libdbpp/unittests/mockdb.cpp | 16 | ||||
-rw-r--r-- | libdbpp/unittests/mockdb.h | 6 | ||||
-rw-r--r-- | libdbpp/unittests/testConnection.cpp | 14 | ||||
-rw-r--r-- | libdbpp/unittests/testConnectionPool.cpp | 4 | ||||
-rw-r--r-- | libdbpp/unittests/testPatch.cpp | 4 | ||||
-rw-r--r-- | libdbpp/unittests/testUtils.cpp | 56 |
7 files changed, 50 insertions, 54 deletions
diff --git a/libdbpp/unittests/Jamfile.jam b/libdbpp/unittests/Jamfile.jam index 83e1ad4..69b25e9 100644 --- a/libdbpp/unittests/Jamfile.jam +++ b/libdbpp/unittests/Jamfile.jam @@ -6,7 +6,7 @@ lib pq ; lib boost_date_time ; lib boost_system ; lib boost_utf : : <name>boost_unit_test_framework ; -lib IceUtil ; +lib Ice++11 ; run testConnection.cpp mockdb.cpp @@ -105,7 +105,7 @@ run <library>..//adhocutil <library>dbpp-local-postgresql <library>boost_utf - <library>IceUtil + <library>Ice++11 <dependency>util.sql : testUtils diff --git a/libdbpp/unittests/mockdb.cpp b/libdbpp/unittests/mockdb.cpp index cf29e01..6b5a7cc 100644 --- a/libdbpp/unittests/mockdb.cpp +++ b/libdbpp/unittests/mockdb.cpp @@ -46,29 +46,29 @@ MockDb::execute(const std::string & sql, const DB::CommandOptions *) executed.push_back(sql); } -DB::SelectCommand * -MockDb::newSelectCommand(const std::string &, const DB::CommandOptions *) +DB::SelectCommandPtr +MockDb::select(const std::string &, const DB::CommandOptions *) { return nullptr; } -DB::ModifyCommand * -MockDb::newModifyCommand(const std::string &, const DB::CommandOptions *) +DB::ModifyCommandPtr +MockDb::modify(const std::string &, const DB::CommandOptions *) { return nullptr; } -MockMock::MockMock(const std::string &, const std::string & name, const std::vector<boost::filesystem::path> & ss) : - DB::MockDatabase(name) +MockMock::MockMock(const std::string &, const std::string &, const std::vector<boost::filesystem::path> & ss) : + DB::MockDatabase() { CreateNewDatabase(); PlaySchemaScripts(ss); } -DB::Connection * +DB::ConnectionPtr MockMock::openConnection() const { - return new MockDb(""); + return std::make_shared<MockDb>(""); } void diff --git a/libdbpp/unittests/mockdb.h b/libdbpp/unittests/mockdb.h index b67dfe4..53fd451 100644 --- a/libdbpp/unittests/mockdb.h +++ b/libdbpp/unittests/mockdb.h @@ -16,8 +16,8 @@ class MockDb : public DB::Connection { DB::BulkUpdateStyle bulkUpdateStyle() const override; void execute(const std::string & sql, const DB::CommandOptions *) override; - DB::SelectCommand * newSelectCommand(const std::string &, const DB::CommandOptions *) override; - DB::ModifyCommand * newModifyCommand(const std::string &, const DB::CommandOptions *) override; + DB::SelectCommandPtr select(const std::string &, const DB::CommandOptions *) override; + DB::ModifyCommandPtr modify(const std::string &, const DB::CommandOptions *) override; mutable std::vector<std::string> executed; }; @@ -25,7 +25,7 @@ class MockDb : public DB::Connection { class MockMock : public DB::MockDatabase { public: MockMock(const std::string &, const std::string &, const std::vector<boost::filesystem::path> &); - DB::Connection * openConnection() const override; + DB::ConnectionPtr openConnection() const override; void CreateNewDatabase() const override; void DropDatabase() const override; }; diff --git a/libdbpp/unittests/testConnection.cpp b/libdbpp/unittests/testConnection.cpp index e738511..7229f6e 100644 --- a/libdbpp/unittests/testConnection.cpp +++ b/libdbpp/unittests/testConnection.cpp @@ -16,9 +16,8 @@ BOOST_AUTO_TEST_CASE( create ) auto mock = DB::ConnectionFactory::createNew("MockDb", "doesn't matter"); BOOST_REQUIRE(mock); // MockDb is fake, just returns nullptr, but the call should otherwise succeed. - BOOST_REQUIRE(!mock->newModifyCommand("")); - BOOST_REQUIRE(!mock->newSelectCommand("")); - delete mock; + BOOST_REQUIRE(!mock->modify("")); + BOOST_REQUIRE(!mock->select("")); } BOOST_AUTO_TEST_CASE( resolve ) @@ -42,7 +41,6 @@ BOOST_AUTO_TEST_CASE( finish ) BOOST_REQUIRE_THROW(mock->finish(), DB::TransactionStillOpen); mock->commitTx(); mock->finish(); - delete mock; } BOOST_AUTO_TEST_CASE( tx ) @@ -62,7 +60,6 @@ BOOST_AUTO_TEST_CASE( tx ) BOOST_REQUIRE_EQUAL(true, mock->inTx()); mock->rollbackTx(); // 0 BOOST_REQUIRE_EQUAL(false, mock->inTx()); - delete mock; } BOOST_AUTO_TEST_CASE( txscope ) @@ -88,7 +85,7 @@ BOOST_AUTO_TEST_CASE( txscope ) BOOST_AUTO_TEST_CASE( savepoints ) { auto mock = DB::ConnectionFactory::createNew("MockDb", "doesn't matter"); - MockDb * mockdb = dynamic_cast<MockDb *>(mock); + auto mockdb = std::dynamic_pointer_cast<MockDb>(mock); BOOST_REQUIRE(mockdb); mock->savepoint("sp"); BOOST_REQUIRE_EQUAL("SAVEPOINT sp", *mockdb->executed.rbegin()); @@ -100,7 +97,6 @@ BOOST_AUTO_TEST_CASE( savepoints ) BOOST_REQUIRE_EQUAL("SAVEPOINT sp2", *mockdb->executed.rbegin()); mock->rollbackToSavepoint("sp1"); BOOST_REQUIRE_EQUAL("ROLLBACK TO SAVEPOINT sp1", *mockdb->executed.rbegin()); - delete mock; } BOOST_AUTO_TEST_CASE( commandOptions ) @@ -118,7 +114,7 @@ BOOST_AUTO_TEST_CASE( commandOptionsPq1 ) {"page-size", "5"} }); BOOST_REQUIRE(optsBase); - auto optsPq = dynamic_cast<PQ::CommandOptions *>(optsBase); + auto optsPq = std::dynamic_pointer_cast<PQ::CommandOptions>(optsBase); BOOST_REQUIRE(optsPq); BOOST_REQUIRE(optsBase->hash); BOOST_REQUIRE_EQUAL(12345, *optsBase->hash); @@ -132,7 +128,7 @@ BOOST_AUTO_TEST_CASE( commandOptionsPq2 ) {"page-size", "50"} }); BOOST_REQUIRE(optsBase); - auto optsPq = dynamic_cast<PQ::CommandOptions *>(optsBase); + auto optsPq = std::dynamic_pointer_cast<PQ::CommandOptions>(optsBase); BOOST_REQUIRE(optsPq); BOOST_REQUIRE(optsBase->hash); BOOST_REQUIRE_EQUAL(123456, *optsBase->hash); diff --git a/libdbpp/unittests/testConnectionPool.cpp b/libdbpp/unittests/testConnectionPool.cpp index 5c73f1e..124df5b 100644 --- a/libdbpp/unittests/testConnectionPool.cpp +++ b/libdbpp/unittests/testConnectionPool.cpp @@ -5,10 +5,10 @@ #include <pq-mock.h> #include <buffer.h> -class MockPool : public PQ::Mock, public DB::ConnectionPool { +class MockPool : public DB::PluginMock<PQ::Mock>, public DB::ConnectionPool { public: MockPool() : - PQ::Mock("user=postgres dbname=postgres", "pqmock", { }), + PluginMock<PQ::Mock>("user=postgres dbname=postgres", "pqmock", { }), DB::ConnectionPool(4, 2, "postgresql", stringbf("user=postgres dbname=%s", databaseName())) { } diff --git a/libdbpp/unittests/testPatch.cpp b/libdbpp/unittests/testPatch.cpp index 39ad5e3..b9ede04 100644 --- a/libdbpp/unittests/testPatch.cpp +++ b/libdbpp/unittests/testPatch.cpp @@ -10,10 +10,10 @@ #include <buffer.h> #include <selectcommandUtil.impl.h> -class Mock : public PQ::Mock { +class Mock : public DB::PluginMock<PQ::Mock> { public: Mock() : - PQ::Mock("user=postgres dbname=postgres", "pqmock", { rootDir / "patch.sql" }) + DB::PluginMock<PQ::Mock>("user=postgres dbname=postgres", "pqmock", { rootDir / "patch.sql" }) { } }; diff --git a/libdbpp/unittests/testUtils.cpp b/libdbpp/unittests/testUtils.cpp index 2dca88e..c926a47 100644 --- a/libdbpp/unittests/testUtils.cpp +++ b/libdbpp/unittests/testUtils.cpp @@ -13,9 +13,9 @@ #include <IceUtil/Optional.h> #include <testCore.h> -class StandardMockDatabase : public PQ::Mock { +class StandardMockDatabase : public DB::PluginMock<PQ::Mock> { public: - StandardMockDatabase() : PQ::Mock("user=postgres dbname=postgres", "pqmock", { + StandardMockDatabase() : DB::PluginMock<PQ::Mock>("user=postgres dbname=postgres", "pqmock", { rootDir / "util.sql" }) { } @@ -25,8 +25,8 @@ BOOST_GLOBAL_FIXTURE( StandardMockDatabase ); BOOST_AUTO_TEST_CASE( forEachRow ) { - auto db = DB::ConnectionPtr(DB::MockDatabase::openConnectionTo("pqmock")); - auto sel = DB::SelectCommandPtr(db->newSelectCommand("SELECT a, b, c, d, e, f FROM forEachRow ORDER BY a LIMIT 1")); + auto db = DB::MockDatabase::openConnectionTo("pqmock"); + auto sel = db->select("SELECT a, b, c, d, e, f FROM forEachRow ORDER BY a LIMIT 1"); sel->forEachRow<int64_t, double, std::string, boost::posix_time::ptime, boost::posix_time::time_duration, bool>( [](auto a, auto b, auto c, auto d, auto e, auto f) { BOOST_REQUIRE_EQUAL(1, a); @@ -40,9 +40,9 @@ BOOST_AUTO_TEST_CASE( forEachRow ) BOOST_AUTO_TEST_CASE( forEachRowNulls ) { - auto db = DB::ConnectionPtr(DB::MockDatabase::openConnectionTo("pqmock")); - auto sel = DB::SelectCommandPtr(db->newSelectCommand("SELECT a, b, c, d, e, f FROM forEachRow ORDER BY a DESC LIMIT 1")); - sel->forEachRow<int64_t, boost::optional<double>, std::string, boost::optional<boost::posix_time::ptime>, boost::optional<boost::posix_time::time_duration>, bool>( + auto db = DB::MockDatabase::openConnectionTo("pqmock"); + auto sel = db->select("SELECT a, b, c, d, e, f FROM forEachRow ORDER BY a DESC LIMIT 1"); + sel->forEachRow<int64_t, std::optional<double>, std::string, std::optional<boost::posix_time::ptime>, std::optional<boost::posix_time::time_duration>, bool>( [](auto a, auto b, auto c, auto d, auto e, auto f) { BOOST_REQUIRE_EQUAL(2, a); BOOST_REQUIRE(b); @@ -56,7 +56,7 @@ BOOST_AUTO_TEST_CASE( forEachRowNulls ) BOOST_AUTO_TEST_CASE( stdforOverRows ) { - auto db = DB::ConnectionPtr(DB::MockDatabase::openConnectionTo("pqmock")); + auto db = DB::MockDatabase::openConnectionTo("pqmock"); unsigned int count = 0; int64_t totalOfa = 0; std::string totalOfc; @@ -82,8 +82,8 @@ BOOST_AUTO_TEST_CASE( execute ) BOOST_AUTO_TEST_CASE( columns ) { - auto db = DB::ConnectionPtr(DB::MockDatabase::openConnectionTo("pqmock")); - auto sel = DB::SelectCommandPtr(db->newSelectCommand("SELECT a, b, c, d, e FROM forEachRow ORDER BY a LIMIT 1")); + auto db = DB::MockDatabase::openConnectionTo("pqmock"); + auto sel = db->select("SELECT a, b, c, d, e FROM forEachRow ORDER BY a LIMIT 1"); sel->execute(); BOOST_REQUIRE_THROW((*sel)[5], DB::ColumnIndexOutOfRange); BOOST_REQUIRE_THROW((*sel)[-1], DB::ColumnIndexOutOfRange); @@ -101,8 +101,8 @@ BOOST_AUTO_TEST_CASE( columns ) BOOST_AUTO_TEST_CASE( extract ) { - auto db = DB::ConnectionPtr(DB::MockDatabase::openConnectionTo("pqmock")); - auto sel = DB::SelectCommandPtr(db->newSelectCommand("SELECT a, b, c FROM forEachRow WHERE f")); + auto db = DB::MockDatabase::openConnectionTo("pqmock"); + auto sel = db->select("SELECT a, b, c FROM forEachRow WHERE f"); BOOST_REQUIRE(sel->fetch()); int64_t lint; double ldouble; @@ -154,16 +154,16 @@ BOOST_AUTO_TEST_CASE( bulkLoadFile ) BOOST_AUTO_TEST_CASE( nullBind ) { - auto db = DB::ConnectionPtr(DB::MockDatabase::openConnectionTo("pqmock")); + auto db = DB::MockDatabase::openConnectionTo("pqmock"); auto ins = db->modify("INSERT INTO forEachRow VALUES(?, ?, ?, ?, ?, ?)"); - ins->bindParamI(0, boost::optional<int>()); - ins->bindParamF(1, boost::optional<double>()); - ins->bindParamS(2, boost::optional<Glib::ustring>()); - ins->bindParamT(3, boost::optional<boost::posix_time::ptime>()); - ins->bindParamT(4, boost::optional<boost::posix_time::time_duration>()); - ins->bindParamB(5, boost::optional<bool>()); + ins->bindParamI(0, std::optional<int>()); + ins->bindParamF(1, std::optional<double>()); + ins->bindParamS(2, std::optional<Glib::ustring>()); + ins->bindParamT(3, std::optional<boost::posix_time::ptime>()); + ins->bindParamT(4, std::optional<boost::posix_time::time_duration>()); + ins->bindParamB(5, std::optional<bool>()); ins->execute(); - auto sel = DB::SelectCommandPtr(db->newSelectCommand("SELECT a, b, c, d, e, f FROM forEachRow WHERE a IS NULL AND b IS NULL AND c IS NULL AND d IS NULL AND e IS NULL AND f IS NULL")); + auto sel = db->select("SELECT a, b, c, d, e, f FROM forEachRow WHERE a IS NULL AND b IS NULL AND c IS NULL AND d IS NULL AND e IS NULL AND f IS NULL"); unsigned int count = 0; for (const auto & row : sel->as<>()) { (void)row; @@ -183,7 +183,7 @@ BOOST_AUTO_TEST_CASE( iceNullBind ) ins->bindParamT(4, IceUtil::Optional<boost::posix_time::time_duration>()); ins->bindParamB(5, IceUtil::Optional<bool>()); ins->execute(); - auto sel = DB::SelectCommandPtr(db->newSelectCommand("SELECT a, b, c, d, e, f FROM forEachRow WHERE a IS NULL AND b IS NULL AND c IS NULL AND d IS NULL AND e IS NULL AND f IS NULL")); + auto sel = db->select("SELECT a, b, c, d, e, f FROM forEachRow WHERE a IS NULL AND b IS NULL AND c IS NULL AND d IS NULL AND e IS NULL AND f IS NULL"); unsigned int count = 0; for (const auto & row : sel->as<>()) { (void)row; @@ -213,8 +213,8 @@ BOOST_AUTO_TEST_CASE( charStarBindNull ) ins->bindParamS(1, ccsc2); ins->execute(); free(cs2); - auto sel = DB::SelectCommandPtr(db->newSelectCommand("SELECT a, c FROM forEachRow")); - for (const auto & row : sel->as<boost::optional<int64_t>, boost::optional<std::string>>()) { + auto sel = db->select("SELECT a, c FROM forEachRow"); + for (const auto & row : sel->as<std::optional<int64_t>, std::optional<std::string>>()) { BOOST_REQUIRE(row[0].isNull()); BOOST_REQUIRE(!row[1].isNull()); } @@ -250,9 +250,9 @@ BOOST_AUTO_TEST_CASE( bindIntPtr ) ins->bindParamI(1, cisc2); ins->execute(); delete is2; - auto sel = DB::SelectCommandPtr(db->newSelectCommand("SELECT a, b FROM forEachRow")); + auto sel = db->select("SELECT a, b FROM forEachRow"); unsigned int total = 0; - for (const auto & row : sel->as<boost::optional<int64_t>, boost::optional<double>>()) { + for (const auto & row : sel->as<std::optional<int64_t>, std::optional<double>>()) { BOOST_REQUIRE(row[0].isNull()); BOOST_REQUIRE(!row[1].isNull()); total += *row.value<1>(); @@ -336,13 +336,13 @@ testExtractT(DB::SelectCommandPtr sel) { T test; (void)test; for (const auto & row : sel->as<T>()) { testExtractT(row); } - for (const auto & row : sel->as<boost::optional<T>>()) { testExtractT(row); } + for (const auto & row : sel->as<std::optional<T>>()) { testExtractT(row); } } BOOST_AUTO_TEST_CASE( testExtractTypes ) { - auto db = DB::ConnectionPtr(DB::MockDatabase::openConnectionTo("pqmock")); - auto sel = DB::SelectCommandPtr(db->newSelectCommand("SELECT 1 FROM forEachRow LIMIT 0")); + auto db = DB::MockDatabase::openConnectionTo("pqmock"); + auto sel = db->select("SELECT 1 FROM forEachRow LIMIT 0"); // testExtractT<int8_t>(sel); // testExtractT<int16_t>(sel); // testExtractT<int32_t>(sel); |