diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2018-04-09 12:06:17 +0100 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2018-04-09 12:06:17 +0100 |
commit | f86b8d2e23339e27d416145ad7b660d66267e151 (patch) | |
tree | 682ee06157befe34571bd1a8acdfab8e4dc27a9c /libpqpp/unittests/testpq.cpp | |
parent | Updated compile flags (diff) | |
download | libdbpp-postgresql-f86b8d2e23339e27d416145ad7b660d66267e151.tar.bz2 libdbpp-postgresql-f86b8d2e23339e27d416145ad7b660d66267e151.tar.xz libdbpp-postgresql-f86b8d2e23339e27d416145ad7b660d66267e151.zip |
C++17
Updates code largely in-keeping with updates to be C++17 in line with
libadhocutil and libdbpp.
Diffstat (limited to 'libpqpp/unittests/testpq.cpp')
-rw-r--r-- | libpqpp/unittests/testpq.cpp | 88 |
1 files changed, 24 insertions, 64 deletions
diff --git a/libpqpp/unittests/testpq.cpp b/libpqpp/unittests/testpq.cpp index 56ab20f..59389c8 100644 --- a/libpqpp/unittests/testpq.cpp +++ b/libpqpp/unittests/testpq.cpp @@ -15,9 +15,9 @@ #include <selectcommandUtil.impl.h> #include <fileUtils.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 / "pqschema.sql" }) { } @@ -41,15 +41,13 @@ BOOST_AUTO_TEST_CASE( transactions ) BOOST_REQUIRE_EQUAL(true, ro->inTx()); ro->commitTx(); BOOST_REQUIRE_EQUAL(false, ro->inTx()); - - delete ro; } BOOST_AUTO_TEST_CASE( bindAndSend ) { auto rw = DB::MockDatabase::openConnectionTo("PQmock"); - auto mod = rw->newModifyCommand("INSERT INTO test VALUES(?, ?, ?, ?, ?, ?)"); + auto mod = rw->modify("INSERT INTO test VALUES(?, ?, ?, ?, ?, ?)"); mod->bindParamI(0, testInt); mod->bindParamF(1, testDouble); mod->bindParamS(2, testString); @@ -78,19 +76,16 @@ BOOST_AUTO_TEST_CASE( bindAndSend ) mod->bindParamS(4, "2016-01-01T12:34:56"); mod->bindNull(5); mod->execute(); - delete mod; - mod = rw->newModifyCommand("DELETE FROM test WHERE string = '?'"); + mod = rw->modify("DELETE FROM test WHERE string = '?'"); BOOST_REQUIRE_THROW(mod->execute(false), DB::NoRowsAffected); BOOST_REQUIRE_EQUAL(0, mod->execute(true)); - delete mod; - delete rw; } BOOST_AUTO_TEST_CASE( bindAndSelect ) { auto ro = DB::MockDatabase::openConnectionTo("PQmock"); - auto select = ro->newSelectCommand("SELECT * FROM test WHERE id = ?"); + auto select = ro->select("SELECT * FROM test WHERE id = ?"); select->bindParamI(0, testInt); select->execute(); int rows = 0; @@ -103,9 +98,7 @@ BOOST_AUTO_TEST_CASE( bindAndSelect ) assertColumnValueHelper(*select, 5, testInterval); rows += 1; } - delete select; BOOST_REQUIRE_EQUAL(1, rows); - delete ro; } BOOST_AUTO_TEST_CASE( selectInTx ) @@ -123,20 +116,17 @@ BOOST_AUTO_TEST_CASE( selectInTx ) db->finish(); db->beginTx(); - auto select = db->newSelectCommand("SELECT * FROM test"); + auto select = db->select("SELECT * FROM test"); while (select->fetch()) { } - delete select; db->commitTx(); db->finish(); - - delete db; } BOOST_AUTO_TEST_CASE( bindAndSelectOther ) { auto ro = DB::MockDatabase::openConnectionTo("PQmock"); - auto select = ro->newSelectCommand("SELECT * FROM test WHERE id != ? AND id != ?"); + auto select = ro->select("SELECT * FROM test WHERE id != ? AND id != ?"); select->bindParamI(0, testInt); select->bindParamI(1, testInt + 10); select->execute(); @@ -150,31 +140,27 @@ BOOST_AUTO_TEST_CASE( bindAndSelectOther ) assertColumnValueHelper(*select, 5, boost::posix_time::time_duration(38, 13, 12)); rows += 1; } - delete select; BOOST_REQUIRE_EQUAL(1, rows); - delete ro; } BOOST_AUTO_TEST_CASE( testP2MockScriptDir ) { auto ro = DB::MockDatabase::openConnectionTo("PQmock"); - auto select = ro->newSelectCommand("SELECT path FROM test2"); + auto select = ro->select("SELECT path FROM test2"); select->execute(); while (select->fetch()) { std::string path; (*select)[0] >> path; BOOST_REQUIRE(boost::filesystem::exists(path)); } - delete select; - delete ro; } BOOST_AUTO_TEST_CASE( bulkload ) { auto ro = DB::MockDatabase::openConnectionTo("PQmock"); - auto count = ro->newSelectCommand("SELECT COUNT(*) FROM bulktest"); + auto count = ro->select("SELECT COUNT(*) FROM bulktest"); // Test empty ro->beginBulkUpload("bulktest", ""); ro->endBulkUpload(NULL); @@ -189,93 +175,77 @@ BOOST_AUTO_TEST_CASE( bulkload ) } ro->endBulkUpload(NULL); assertScalarValueHelper(*count, 800); - - delete count; - delete ro; } BOOST_AUTO_TEST_CASE( nofetch ) { auto ro = DB::MockDatabase::openConnectionTo("PQmock"); - auto count = ro->newSelectCommand("SELECT * FROM bulktest"); + auto count = ro->select("SELECT * FROM bulktest"); count->execute(); - delete count; - delete ro; } BOOST_AUTO_TEST_CASE( bigIterate ) { auto ro = DB::MockDatabase::openConnectionTo("PQmock"); - auto count = ro->newSelectCommand("SELECT * FROM bulktest"); + auto count = ro->select("SELECT * FROM bulktest"); unsigned int rows = 0; while (count->fetch()) { rows += 1; } BOOST_REQUIRE_EQUAL(800, rows); - - delete count; - delete ro; } BOOST_AUTO_TEST_CASE( insertId ) { auto ro = DB::MockDatabase::openConnectionTo("PQmock"); - auto ins = ro->newModifyCommand("INSERT INTO idtest(foo) VALUES(1)"); + auto ins = ro->modify("INSERT INTO idtest(foo) VALUES(1)"); for (int x = 1; x < 4; x++) { ins->execute(); BOOST_REQUIRE_EQUAL(x, ro->insertId()); } - delete ins; - delete ro; } BOOST_AUTO_TEST_CASE( reconnect ) { auto ro = DB::MockDatabase::openConnectionTo("PQmock"); auto rok = DB::MockDatabase::openConnectionTo("PQmock"); - auto pqconn = dynamic_cast<PQ::Connection *>(ro); + auto pqconn = std::dynamic_pointer_cast<PQ::Connection>(ro); int pid1 = PQbackendPID(pqconn->conn); BOOST_REQUIRE(pid1); ro->ping(); ro->modify("TRUNCATE TABLE test")->execute(); - auto kil = rok->newModifyCommand("SELECT pg_terminate_backend(?)"); + auto kil = rok->modify("SELECT pg_terminate_backend(?)"); kil->bindParamI(0, pid1); kil->execute(); - delete kil; usleep(5000); ro->ping(); int pid2 = PQbackendPID(pqconn->conn); BOOST_REQUIRE(pid2); BOOST_REQUIRE(pid1 != pid2); ro->modify("TRUNCATE TABLE test")->execute(); - delete ro; - delete rok; } BOOST_AUTO_TEST_CASE( reconnectInTx ) { auto ro = DB::MockDatabase::openConnectionTo("PQmock"); auto rok = DB::MockDatabase::openConnectionTo("PQmock"); - auto pqconn = dynamic_cast<PQ::Connection *>(ro); + auto pqconn = std::dynamic_pointer_cast<PQ::Connection>(ro); int pid1 = PQbackendPID(pqconn->conn); BOOST_REQUIRE(pid1); ro->ping(); ro->beginTx(); - auto kil = rok->newModifyCommand("SELECT pg_terminate_backend(?)"); + auto kil = rok->modify("SELECT pg_terminate_backend(?)"); kil->bindParamI(0, pid1); kil->execute(); - delete kil; usleep(5000); BOOST_REQUIRE_THROW(ro->ping(), DB::ConnectionError); - delete ro; - delete rok; } BOOST_AUTO_TEST_CASE( statementReuse ) { auto ro = DB::MockDatabase::openConnectionTo("PQmock"); - auto pqconn = dynamic_cast<PQ::Connection *>(ro); + auto pqconn = std::dynamic_pointer_cast<PQ::Connection>(ro); BOOST_REQUIRE_EQUAL(pqconn->preparedStatements.size(), 0); ro->modify("DELETE FROM test")->execute(); BOOST_REQUIRE_EQUAL(pqconn->preparedStatements.size(), 1); @@ -288,47 +258,41 @@ BOOST_AUTO_TEST_CASE( statementReuse ) } } BOOST_REQUIRE_EQUAL(pqconn->preparedStatements.size(), 2); - auto select = ro->newSelectCommand("SELECT COUNT(id), SUM(id) FROM test"); + auto select = ro->select("SELECT COUNT(id), SUM(id) FROM test"); while (select->fetch()) { assertColumnValueHelper(*select, 0, 16); assertColumnValueHelper(*select, 1, 24); } - delete select; - delete ro; } BOOST_AUTO_TEST_CASE( bulkSelect ) { auto ro = DB::MockDatabase::openConnectionTo("PQmock"); PQ::CommandOptions co(0, 35, false); - auto sel = ro->newSelectCommand("SELECT * FROM test WHERE id > ?", &co); + auto sel = ro->select("SELECT * FROM test WHERE id > ?", &co); sel->bindParamI(0, 1); int totalInt = 0, count = 0; sel->forEachRow<int64_t>([&totalInt, &count](auto i) { totalInt += i; count += 1; }); - delete sel; BOOST_REQUIRE_EQUAL(20, totalInt); BOOST_REQUIRE_EQUAL(8, count); - delete ro; } BOOST_AUTO_TEST_CASE( selectWithSmallPages ) { auto ro = DB::MockDatabase::openConnectionTo("PQmock"); PQ::CommandOptions co(0, 1, true); - auto sel = ro->newSelectCommand("SELECT * FROM test WHERE id > ?", &co); + auto sel = ro->select("SELECT * FROM test WHERE id > ?", &co); sel->bindParamI(0, 1); int totalInt = 0, count = 0; sel->forEachRow<int64_t>([&totalInt, &count](auto i) { totalInt += i; count += 1; }); - delete sel; BOOST_REQUIRE_EQUAL(20, totalInt); BOOST_REQUIRE_EQUAL(8, count); - delete ro; } BOOST_AUTO_TEST_CASE( dateoid ) @@ -339,23 +303,20 @@ BOOST_AUTO_TEST_CASE( dateoid ) for (const auto & r : sel->as<boost::posix_time::ptime>()) { BOOST_REQUIRE_EQUAL(boost::posix_time::ptime(boost::gregorian::date(2017, 1, 8)), r.value<0>()); } - delete ro; } BOOST_AUTO_TEST_CASE( insertReturning ) { auto ro = DB::MockDatabase::openConnectionTo("PQmock"); PQ::CommandOptions co(0, 35, false); - auto sel = ro->newSelectCommand("INSERT INTO test(id, fl) VALUES(1, 3) RETURNING id + fl", &co); + auto sel = ro->select("INSERT INTO test(id, fl) VALUES(1, 3) RETURNING id + fl", &co); int totalInt = 0, count = 0; sel->forEachRow<int64_t>([&totalInt, &count](auto i) { totalInt += i; count += 1; }); - delete sel; BOOST_REQUIRE_EQUAL(4, totalInt); BOOST_REQUIRE_EQUAL(1, count); - delete ro; } BOOST_AUTO_TEST_CASE( closeOnError ) @@ -424,7 +385,7 @@ BOOST_AUTO_TEST_CASE( fetchAsBinary ) opts.fetchBinary = true; opts.useCursor = false; auto sel = ro->select("SELECT data, md5, length(data) FROM blobtest", &opts); - for (const auto & r : sel->as<DB::Blob, boost::optional<std::string>, int64_t>()) { + for (const auto & r : sel->as<DB::Blob, std::optional<std::string>, int64_t>()) { // Assert the DB understood the insert BOOST_REQUIRE_EQUAL(r.value<2>(), buf.size()); BOOST_REQUIRE(r.value<1>()); @@ -451,7 +412,7 @@ BOOST_AUTO_TEST_CASE( fetchAsBinary ) } *opts.hash += 1; sel = ro->select("SELECT NULL, now()", &opts); - for (const auto & r : sel->as<boost::optional<int64_t>, boost::posix_time::ptime>()) { + for (const auto & r : sel->as<std::optional<int64_t>, boost::posix_time::ptime>()) { BOOST_REQUIRE(!r.value<0>()); BOOST_REQUIRE_THROW(r.value<1>(), DB::ColumnTypeNotSupported); } @@ -495,9 +456,8 @@ BOOST_AUTO_TEST_CASE( ssl ) { auto conn = DB::ConnectionFactory::createNew("postgresql", "host=randomdan.homeip.net user=gentoo dbname=postgres sslmode=require"); BOOST_REQUIRE(conn); - auto pqconn = dynamic_cast<PQ::Connection *>(conn); + auto pqconn = std::dynamic_pointer_cast<PQ::Connection>(conn); BOOST_REQUIRE(pqconn); BOOST_REQUIRE(PQgetssl(pqconn->conn)); - delete conn; } |