From 2d4b1c02d4b95590ca1571592d142d31e70d441b Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Tue, 2 Jun 2015 01:42:13 +0100 Subject: Prebuild (once) SQL for cursor operations. Don't open another transaction. Don't make another fetch to the DB if the previous read was short anyway. Add bulk load and bulk read tests. --- project2/sql/unittests/pqschema.sql | 4 +++ project2/sql/unittests/testpq.cpp | 53 +++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/project2/sql/unittests/pqschema.sql b/project2/sql/unittests/pqschema.sql index b0581ab..2c7e94a 100644 --- a/project2/sql/unittests/pqschema.sql +++ b/project2/sql/unittests/pqschema.sql @@ -31,3 +31,7 @@ AS $tag$ WHERE string != 'complex '' string;'; $tag$; +CREATE TABLE bulktest( + id int, + string text); + diff --git a/project2/sql/unittests/testpq.cpp b/project2/sql/unittests/testpq.cpp index 0890bee..da38a34 100644 --- a/project2/sql/unittests/testpq.cpp +++ b/project2/sql/unittests/testpq.cpp @@ -9,6 +9,7 @@ #include #include "testCore.h" #include +#include class StandardMockDatabase : public MockPqDatabase { public: @@ -59,6 +60,17 @@ BOOST_AUTO_TEST_CASE( bindAndSend ) ds->close(); } +template +void +assertColumnValueHelper(DB::SelectCommand & sel, const T & t) +{ + while (sel.fetch()) { + HandleAsVariableType h; + sel[0].apply(h); + BOOST_REQUIRE_EQUAL(t, h.variable.as()); + } +} + template void assertColumnValueHelper(DB::SelectCommand & sel, unsigned int col, const T & t) @@ -130,5 +142,46 @@ BOOST_AUTO_TEST_CASE( testP2MockScriptDir ) ds->close(); } +BOOST_AUTO_TEST_CASE( bulkload ) +{ + RdbmsDataSource * ds = CommonObjects::dataSource("pqmock"); + auto ro = ds->getReadonly(); + + auto count = ro->newSelectCommand("SELECT COUNT(*) FROM bulktest"); + // Test empty + ro->beginBulkUpload("bulktest", ""); + ro->endBulkUpload(NULL); + assertColumnValueHelper(*count, 0); + // Test sample file + ro->beginBulkUpload("bulktest", ""); + std::ifstream in((RootDir / "bulk.sample").string()); + if (!in.good()) throw std::runtime_error("Couldn't open bulk.sample"); + char buf[BUFSIZ]; + for (std::streamsize r; (r = in.readsome(buf, sizeof(buf))) > 0; ) { + ro->bulkUploadData(buf, r); + } + ro->endBulkUpload(NULL); + assertColumnValueHelper(*count, 800); + + delete count; + ds->close(); +} + +BOOST_AUTO_TEST_CASE( bigIterate ) +{ + RdbmsDataSource * ds = CommonObjects::dataSource("pqmock"); + auto ro = ds->getReadonly(); + + auto count = ro->newSelectCommand("SELECT * FROM bulktest"); + unsigned int rows = 0; + while (count->fetch()) { + rows += 1; + } + BOOST_REQUIRE_EQUAL(800, rows); + + delete count; + ds->close(); +} + BOOST_AUTO_TEST_SUITE_END(); -- cgit v1.2.3