summaryrefslogtreecommitdiff
path: root/libpqpp/unittests/testpq.cpp
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2017-06-05 18:53:22 +0100
committerDan Goodliffe <dan@randomdan.homeip.net>2017-06-05 18:53:22 +0100
commit19609c41b15a818df30508576f50fc0eddc11636 (patch)
treeb7b36a9e69689fd2be7c0660728af59eacb02d10 /libpqpp/unittests/testpq.cpp
parentAdd wrappers for PQgetvalue and PQgetlength in column to simplify access (diff)
downloadlibdbpp-postgresql-19609c41b15a818df30508576f50fc0eddc11636.tar.bz2
libdbpp-postgresql-19609c41b15a818df30508576f50fc0eddc11636.tar.xz
libdbpp-postgresql-19609c41b15a818df30508576f50fc0eddc11636.zip
Support fetching data in binary format (no numeric, datetime, interval support yet)
Diffstat (limited to 'libpqpp/unittests/testpq.cpp')
-rw-r--r--libpqpp/unittests/testpq.cpp43
1 files changed, 43 insertions, 0 deletions
diff --git a/libpqpp/unittests/testpq.cpp b/libpqpp/unittests/testpq.cpp
index 97dc22d..f9ebf64 100644
--- a/libpqpp/unittests/testpq.cpp
+++ b/libpqpp/unittests/testpq.cpp
@@ -407,6 +407,49 @@ BOOST_AUTO_TEST_CASE( blobs )
}
}
+BOOST_AUTO_TEST_CASE( fetchAsBinary )
+{
+ auto ro = DB::ConnectionPtr(DB::MockDatabase::openConnectionTo("PQmock"));
+ std::vector<char> buf(29);
+ memcpy(&buf[0], "This is some binary text data", 29);
+ PQ::CommandOptions opts(0);
+ 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>()) {
+ // Assert the DB understood the insert
+ BOOST_REQUIRE_EQUAL(r.value<2>(), buf.size());
+ BOOST_REQUIRE(r.value<1>());
+ BOOST_REQUIRE_EQUAL(*r.value<1>(), "37c7c3737f93e8d17e845deff8fa74d2");
+ // Assert the fetch of the data is correct
+ BOOST_REQUIRE_EQUAL(r.value<0>().len, buf.size());
+ BOOST_REQUIRE(memcmp(r.value<0>().data, &buf[0], 29) == 0);
+ }
+ *opts.hash += 1;
+ sel = ro->select("SELECT CAST(length(data) AS BIGINT) big, CAST(length(data) AS SMALLINT) small FROM blobtest", &opts);
+ for (const auto & r : sel->as<int64_t, int64_t>()) {
+ BOOST_REQUIRE_EQUAL(r.value<0>(), buf.size());
+ BOOST_REQUIRE_EQUAL(r.value<1>(), buf.size());
+ }
+ *opts.hash += 1;
+ sel = ro->select("SELECT true a, false b", &opts);
+ for (const auto & r : sel->as<bool, bool>()) {
+ BOOST_REQUIRE_EQUAL(r.value<0>(), true);
+ BOOST_REQUIRE_EQUAL(r.value<1>(), false);
+ }
+ *opts.hash += 1;
+ sel = ro->select("SELECT xmlelement(name xml)", &opts);
+ for (const auto & r : sel->as<std::string>()) {
+ BOOST_REQUIRE_EQUAL(r.value<0>(), "<xml/>");
+ }
+ *opts.hash += 1;
+ sel = ro->select("SELECT NULL, now()", &opts);
+ for (const auto & r : sel->as<boost::optional<int64_t>, boost::posix_time::ptime>()) {
+ BOOST_REQUIRE(!r.value<0>());
+ BOOST_REQUIRE_THROW(r.value<1>(), DB::ColumnTypeNotSupported);
+ }
+}
+
BOOST_AUTO_TEST_SUITE_END();
BOOST_AUTO_TEST_CASE( connfail )