From bdb5c897087d3588f456c020b3633cbd694003da Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 8 Aug 2021 16:14:50 +0100 Subject: Tests, fixes, improvements to WritePqCopyStream --- test/helpers.cpp | 15 ++++++++++++ test/helpers.h | 13 ++++++++++ test/test-postgresql.cpp | 64 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 92 insertions(+) create mode 100644 test/helpers.cpp (limited to 'test') diff --git a/test/helpers.cpp b/test/helpers.cpp new file mode 100644 index 0000000..f4fc030 --- /dev/null +++ b/test/helpers.cpp @@ -0,0 +1,15 @@ +#include "helpers.h" + +MemStream::MemStream() : out {nullptr}, len {}, s {open_memstream(&out, &len)} { } + +MemStream::~MemStream() +{ + fclose(s); + free(out); +} + +void +MemStream::flush() +{ + fflush(s); +} diff --git a/test/helpers.h b/test/helpers.h index 59ae18d..87ef31d 100644 --- a/test/helpers.h +++ b/test/helpers.h @@ -3,8 +3,10 @@ #include #include +#include #include #include +#include #include inline constexpr std::byte operator""_b(const unsigned long long hex) @@ -55,4 +57,15 @@ make_tm(int year, int mon, int day, int hr, int min, int sec) return tm; } +struct MemStream { + MemStream(); + virtual ~MemStream(); + + void flush(); + + char * out; + size_t len; + FILE * s; +}; + #endif diff --git a/test/test-postgresql.cpp b/test/test-postgresql.cpp index 15ba789..727e08c 100644 --- a/test/test-postgresql.cpp +++ b/test/test-postgresql.cpp @@ -1,6 +1,9 @@ #define BOOST_TEST_MODULE PostgreSQL +#include +#include #include +#include "helpers.h" #include "testdb-postgresql.h" #include #include @@ -11,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -91,3 +95,63 @@ BOOST_AUTO_TEST_CASE(mock_schema) auto rs = MyGrate::DbStmt<"SELECT COUNT(*) FROM mygrate.source">::execute(&mdb); BOOST_CHECK_EQUAL(rs->at(0, 0).operator unsigned int(), 0); } + +BOOST_FIXTURE_TEST_SUITE(ms, MemStream); + +BOOST_DATA_TEST_CASE(write_strings, + boost::unit_test::data::make({ + std::make_tuple("", ""), + {"simple", "simple"}, + {"simple with spaces", "simple with spaces"}, + {"simple\twith\ttabs", "simple\\011with\\011tabs"}, + {"\ttab start", "\\011tab start"}, + {"tab end\t", "tab end\\011"}, + {"tab\t\t\t\tmany", "tab\\011\\011\\011\\011many"}, + }), + in, exp) +{ + MyGrate::Output::Pq::WritePqCopyStream c {s}; + c(in); + flush(); + + BOOST_REQUIRE(out); + BOOST_CHECK_EQUAL(out, exp); +} + +using IntTypes = boost::mpl::list; +BOOST_AUTO_TEST_CASE_TEMPLATE(write_ints, T, IntTypes) +{ + MyGrate::Output::Pq::WritePqCopyStream c {s}; + c(T {1}); + flush(); + + BOOST_REQUIRE(out); + BOOST_CHECK_EQUAL(len, 1); + BOOST_CHECK_EQUAL(out, "1"); +} + +using FloatTypes = boost::mpl::list; +BOOST_AUTO_TEST_CASE_TEMPLATE(write_floats, T, FloatTypes) +{ + MyGrate::Output::Pq::WritePqCopyStream c {s}; + c(T {1.1}); + flush(); + + BOOST_REQUIRE(out); + BOOST_CHECK_EQUAL(len, 3); + BOOST_CHECK_EQUAL(out, "1.1"); +} + +BOOST_AUTO_TEST_CASE(write_blob) +{ + MyGrate::Output::Pq::WritePqCopyStream c {s}; + std::array b {0x00_b, 0x10_b, 0x12_b, 0x30_b, 0x90_b, 0xaa_b, 0xff_b}; + c(b); + flush(); + + BOOST_REQUIRE(out); + BOOST_CHECK_EQUAL(len, 23); + BOOST_CHECK_EQUAL(out, R"B(\\x0010123090AAFF000000)B"); +} + +BOOST_AUTO_TEST_SUITE_END(); -- cgit v1.2.3