From d2747d7d3ccacd6f9f0923c4b67a29927489661f Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 8 Nov 2015 02:26:19 +0000 Subject: Test error handling in mock setup --- libdbpp/mockDatabase.cpp | 2 +- libdbpp/unittests/Jamfile.jam | 14 ++++++++++++++ libdbpp/unittests/badMock.sql | 1 + libdbpp/unittests/testMock.cpp | 22 ++++++++++++++++++++++ 4 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 libdbpp/unittests/badMock.sql create mode 100644 libdbpp/unittests/testMock.cpp diff --git a/libdbpp/mockDatabase.cpp b/libdbpp/mockDatabase.cpp index a0ae7e5..9cb916f 100644 --- a/libdbpp/mockDatabase.cpp +++ b/libdbpp/mockDatabase.cpp @@ -67,7 +67,7 @@ MockDatabase::PlaySchemaScript(DB::Connection * conn, const boost::filesystem::p std::ifstream f; f.open(s.string()); if (!f.good()) { - throw std::runtime_error("Failed to open mock script: " + s.string()); + throw std::fstream::failure("Failed to open mock script: " + s.string()); } conn->executeScript(f, s.parent_path()); f.close(); diff --git a/libdbpp/unittests/Jamfile.jam b/libdbpp/unittests/Jamfile.jam index ad90ec6..cee679f 100644 --- a/libdbpp/unittests/Jamfile.jam +++ b/libdbpp/unittests/Jamfile.jam @@ -31,3 +31,17 @@ run testUtils ; +run + testMock.cpp + : : : + ROOT=\"$(me)\" + BOOST_TEST_DYN_LINK + ..//dbppcore + ..//adhocutil + ../../libpqpp//dbpp-postgresql + boost_utf + badMock.sql + : + testMock + ; + diff --git a/libdbpp/unittests/badMock.sql b/libdbpp/unittests/badMock.sql new file mode 100644 index 0000000..e1405cc --- /dev/null +++ b/libdbpp/unittests/badMock.sql @@ -0,0 +1 @@ +Not good Sql ;-) diff --git a/libdbpp/unittests/testMock.cpp b/libdbpp/unittests/testMock.cpp new file mode 100644 index 0000000..4340886 --- /dev/null +++ b/libdbpp/unittests/testMock.cpp @@ -0,0 +1,22 @@ +#define BOOST_TEST_MODULE DbMock +#include + +#include +#include +#include +#include "error.h" + +BOOST_AUTO_TEST_CASE( missingMock ) +{ + BOOST_REQUIRE_THROW({ + PQ::Mock m ("user=postgres dbname=postgres", "pqmock", { 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); +} + -- cgit v1.2.3