From 94927bfef6548ac20118586bfcd51e60b296e569 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Thu, 24 Dec 2015 03:43:16 +0000 Subject: Beginnings of embbeded MySQL --- libmysqlpp/unittests/Jamfile.jam | 15 ++++ libmysqlpp/unittests/testMain.cpp | 156 ++++++++++++++++++++++++++++++++++++ libmysqlpp/unittests/testmysql.cpp | 156 +----------------------------------- libmysqlpp/unittests/testmysqle.cpp | 16 ++++ 4 files changed, 188 insertions(+), 155 deletions(-) create mode 100644 libmysqlpp/unittests/testMain.cpp create mode 100644 libmysqlpp/unittests/testmysqle.cpp (limited to 'libmysqlpp/unittests') diff --git a/libmysqlpp/unittests/Jamfile.jam b/libmysqlpp/unittests/Jamfile.jam index 167939a..c7bf637 100644 --- a/libmysqlpp/unittests/Jamfile.jam +++ b/libmysqlpp/unittests/Jamfile.jam @@ -22,3 +22,18 @@ run testmysql ; +run + testmysqle.cpp + : : : + ROOT=\"$(me)\" + BOOST_TEST_DYN_LINK + ..//dbpp-mysql-embedded + dbpptestcore + boost_utf + boost_system + boost_filesystem + mysqlschema.sql + : + testmysqle + ; + diff --git a/libmysqlpp/unittests/testMain.cpp b/libmysqlpp/unittests/testMain.cpp new file mode 100644 index 0000000..0811d5c --- /dev/null +++ b/libmysqlpp/unittests/testMain.cpp @@ -0,0 +1,156 @@ +#include +#include +#include +#include +#include +#include + +BOOST_GLOBAL_FIXTURE( StandardMockDatabase ); + +BOOST_FIXTURE_TEST_SUITE( Core, DB::TestCore ); + +BOOST_AUTO_TEST_CASE( transactions ) +{ + auto ro = DB::MockDatabase::openConnectionTo("mysqlmock"); + + BOOST_REQUIRE_EQUAL(false, ro->inTx()); + ro->beginTx(); + BOOST_REQUIRE_EQUAL(true, ro->inTx()); + ro->rollbackTx(); + BOOST_REQUIRE_EQUAL(false, ro->inTx()); + + ro->beginTx(); + 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("mysqlmock"); + + auto mod = rw->newModifyCommand("INSERT INTO test VALUES(?, ?, ?, ?, ?, ?)"); + mod->bindParamI(0, testInt); + mod->bindParamF(1, testDouble); + mod->bindParamS(2, testString); + mod->bindParamB(3, testBool); + mod->bindParamT(4, testDateTime); + mod->bindParamT(5, testInterval); + mod->execute(); + delete mod; + delete rw; +} + +BOOST_AUTO_TEST_CASE( bindAndSelect ) +{ + auto ro = DB::MockDatabase::openConnectionTo("mysqlmock"); + + auto select = ro->newSelectCommand("SELECT * FROM test WHERE id = ?"); + select->bindParamI(0, testInt); + select->execute(); + int rows = 0; + while (select->fetch()) { + assertColumnValueHelper(*select, 0, testInt); + assertColumnValueHelper(*select, 1, testDouble); + assertColumnValueHelper(*select, 2, testString); + assertColumnValueHelper(*select, 3, testBool); + assertColumnValueHelper(*select, 4, testDateTime); + assertColumnValueHelper(*select, 5, testInterval); + rows += 1; + } + delete select; + BOOST_REQUIRE_EQUAL(1, rows); + delete ro; +} + +BOOST_AUTO_TEST_CASE( bindAndSelectOther ) +{ + auto ro = DB::MockDatabase::openConnectionTo("mysqlmock"); + + auto select = ro->newSelectCommand("SELECT * FROM test WHERE id != ?"); + select->bindParamI(0, testInt); + select->execute(); + int rows = 0; + while (select->fetch()) { + assertColumnValueHelper(*select, 0, 4); + assertColumnValueHelper(*select, 1, 123.45); + assertColumnValueHelper(*select, 2, std::string("some text")); + assertColumnValueHelper(*select, 3, true); + assertColumnValueHelper(*select, 4, boost::posix_time::ptime_from_tm({ 3, 6, 23, 27, 3, 115, 0, 0, 0, 0, 0})); + 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( bulkload ) +{ + auto ro = DB::MockDatabase::openConnectionTo("mysqlmock"); + + auto count = ro->newSelectCommand("SELECT COUNT(*) FROM bulktest"); + // Test empty + ro->beginBulkUpload("bulktest", ""); + ro->endBulkUpload(NULL); + assertScalarValueHelper(*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); + assertScalarValueHelper(*count, 800); + + delete count; + delete ro; +} + +BOOST_AUTO_TEST_CASE( bigIterate ) +{ + auto ro = DB::MockDatabase::openConnectionTo("mysqlmock"); + + auto count = ro->newSelectCommand("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("mysqlmock"); + auto ins = ro->newModifyCommand("INSERT INTO inserts(num) VALUES(?)"); + ins->bindParamI(0, 4); + ins->execute(); + BOOST_REQUIRE_EQUAL(1, ro->insertId()); + ins->bindParamI(0, 40); + ins->execute(); + BOOST_REQUIRE_EQUAL(2, ro->insertId()); + ins->bindParamI(0, -4); + ins->execute(); + BOOST_REQUIRE_EQUAL(3, ro->insertId()); + delete ins; + delete ro; +} + +BOOST_AUTO_TEST_CASE( errors ) +{ + auto ro = DB::MockDatabase::openConnectionTo("mysqlmock"); + BOOST_REQUIRE_THROW(ro->execute("nonsense"), DB::Error); + delete ro; + BOOST_REQUIRE_THROW(DB::ConnectionFactory::createNew("mysql", "server=nohost"), DB::ConnectionError); +} + +BOOST_AUTO_TEST_SUITE_END(); + + diff --git a/libmysqlpp/unittests/testmysql.cpp b/libmysqlpp/unittests/testmysql.cpp index 00c15aa..d363716 100644 --- a/libmysqlpp/unittests/testmysql.cpp +++ b/libmysqlpp/unittests/testmysql.cpp @@ -2,15 +2,7 @@ #include #include -#include #include -#include -#include -#include -#include -#include -#include -#include class StandardMockDatabase : public MySQL::Mock { public: @@ -20,151 +12,5 @@ class StandardMockDatabase : public MySQL::Mock { } }; -BOOST_GLOBAL_FIXTURE( StandardMockDatabase ); - -BOOST_FIXTURE_TEST_SUITE( Core, DB::TestCore ); - -BOOST_AUTO_TEST_CASE( transactions ) -{ - auto ro = DB::MockDatabase::openConnectionTo("mysqlmock"); - - BOOST_REQUIRE_EQUAL(false, ro->inTx()); - ro->beginTx(); - BOOST_REQUIRE_EQUAL(true, ro->inTx()); - ro->rollbackTx(); - BOOST_REQUIRE_EQUAL(false, ro->inTx()); - - ro->beginTx(); - 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("mysqlmock"); - - auto mod = rw->newModifyCommand("INSERT INTO test VALUES(?, ?, ?, ?, ?, ?)"); - mod->bindParamI(0, testInt); - mod->bindParamF(1, testDouble); - mod->bindParamS(2, testString); - mod->bindParamB(3, testBool); - mod->bindParamT(4, testDateTime); - mod->bindParamT(5, testInterval); - mod->execute(); - delete mod; - delete rw; -} - -BOOST_AUTO_TEST_CASE( bindAndSelect ) -{ - auto ro = DB::MockDatabase::openConnectionTo("mysqlmock"); - - auto select = ro->newSelectCommand("SELECT * FROM test WHERE id = ?"); - select->bindParamI(0, testInt); - select->execute(); - int rows = 0; - while (select->fetch()) { - assertColumnValueHelper(*select, 0, testInt); - assertColumnValueHelper(*select, 1, testDouble); - assertColumnValueHelper(*select, 2, testString); - assertColumnValueHelper(*select, 3, testBool); - assertColumnValueHelper(*select, 4, testDateTime); - assertColumnValueHelper(*select, 5, testInterval); - rows += 1; - } - delete select; - BOOST_REQUIRE_EQUAL(1, rows); - delete ro; -} - -BOOST_AUTO_TEST_CASE( bindAndSelectOther ) -{ - auto ro = DB::MockDatabase::openConnectionTo("mysqlmock"); - - auto select = ro->newSelectCommand("SELECT * FROM test WHERE id != ?"); - select->bindParamI(0, testInt); - select->execute(); - int rows = 0; - while (select->fetch()) { - assertColumnValueHelper(*select, 0, 4); - assertColumnValueHelper(*select, 1, 123.45); - assertColumnValueHelper(*select, 2, std::string("some text")); - assertColumnValueHelper(*select, 3, true); - assertColumnValueHelper(*select, 4, boost::posix_time::ptime_from_tm({ 3, 6, 23, 27, 3, 115, 0, 0, 0, 0, 0})); - 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( bulkload ) -{ - auto ro = DB::MockDatabase::openConnectionTo("mysqlmock"); - - auto count = ro->newSelectCommand("SELECT COUNT(*) FROM bulktest"); - // Test empty - ro->beginBulkUpload("bulktest", ""); - ro->endBulkUpload(NULL); - assertScalarValueHelper(*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); - assertScalarValueHelper(*count, 800); - - delete count; - delete ro; -} - -BOOST_AUTO_TEST_CASE( bigIterate ) -{ - auto ro = DB::MockDatabase::openConnectionTo("mysqlmock"); - - auto count = ro->newSelectCommand("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("mysqlmock"); - auto ins = ro->newModifyCommand("INSERT INTO inserts(num) VALUES(?)"); - ins->bindParamI(0, 4); - ins->execute(); - BOOST_REQUIRE_EQUAL(1, ro->insertId()); - ins->bindParamI(0, 40); - ins->execute(); - BOOST_REQUIRE_EQUAL(2, ro->insertId()); - ins->bindParamI(0, -4); - ins->execute(); - BOOST_REQUIRE_EQUAL(3, ro->insertId()); - delete ins; - delete ro; -} - -BOOST_AUTO_TEST_CASE( errors ) -{ - auto ro = DB::MockDatabase::openConnectionTo("mysqlmock"); - BOOST_REQUIRE_THROW(ro->execute("nonsense"), DB::Error); - delete ro; - BOOST_REQUIRE_THROW(DB::ConnectionFactory::createNew("mysql", "server=nohost"), DB::ConnectionError); -} - -BOOST_AUTO_TEST_SUITE_END(); +#include "testMain.cpp" diff --git a/libmysqlpp/unittests/testmysqle.cpp b/libmysqlpp/unittests/testmysqle.cpp new file mode 100644 index 0000000..0d1defb --- /dev/null +++ b/libmysqlpp/unittests/testmysqle.cpp @@ -0,0 +1,16 @@ +#define BOOST_TEST_MODULE TestEmbeddedMySQL +#include + +#include +#include + +class StandardMockDatabase : public MySQL::Embedded::Mock { + public: + StandardMockDatabase() : MySQL::Embedded::Mock("mysqlmock", { + rootDir / "mysqlschema.sql" }) + { + } +}; + +#include "testMain.cpp" + -- cgit v1.2.3