diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2015-09-24 03:04:15 +0100 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2015-09-24 03:06:07 +0100 |
commit | 274adf39c78bf9050ed34815a3472e9e418dd165 (patch) | |
tree | 4f45327778fefee7ceb849def04c3fdd66a7b297 /libodbcpp/unittests/testodbc.cpp | |
parent | Connector compatibility fix (diff) | |
download | libdbpp-odbc-274adf39c78bf9050ed34815a3472e9e418dd165.tar.bz2 libdbpp-odbc-274adf39c78bf9050ed34815a3472e9e418dd165.tar.xz libdbpp-odbc-274adf39c78bf9050ed34815a3472e9e418dd165.zip |
ODBC mocking and tests from Project2
Diffstat (limited to 'libodbcpp/unittests/testodbc.cpp')
-rw-r--r-- | libodbcpp/unittests/testodbc.cpp | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/libodbcpp/unittests/testodbc.cpp b/libodbcpp/unittests/testodbc.cpp new file mode 100644 index 0000000..15e0166 --- /dev/null +++ b/libodbcpp/unittests/testodbc.cpp @@ -0,0 +1,101 @@ +#define BOOST_TEST_MODULE TestODBC +#include <boost/test/unit_test.hpp> + +#include <definedDirs.h> +#include <modifycommand.h> +#include <selectcommand.h> +#include <column.h> +#include <mock.h> +#include <testCore.h> +#include <definedDirs.h> +#include <boost/date_time/posix_time/posix_time.hpp> + +class StandardMockDatabase : public ODBC::Mock { + public: + StandardMockDatabase() : ODBC::Mock("Driver=postgresql;Database=postgres;uid=postgres;servername=/run/postgresql", "odbcmock", { + rootDir / "odbcschema.sql" }) + { + } +}; + +BOOST_GLOBAL_FIXTURE( StandardMockDatabase ); + +BOOST_FIXTURE_TEST_SUITE( Core, DB::TestCore ); + +BOOST_AUTO_TEST_CASE( transactions ) +{ + auto ro = DB::MockDatabase::openConnectionTo("odbcmock"); + + 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("odbcmock"); + + 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->execute(); + delete mod; + delete rw; +} + +BOOST_AUTO_TEST_CASE( bindAndSelect ) +{ + auto ro = DB::MockDatabase::openConnectionTo("odbcmock"); + + 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); + rows += 1; + } + delete select; + BOOST_REQUIRE_EQUAL(1, rows); + delete ro; +} + +BOOST_AUTO_TEST_CASE( bindAndSelectOther ) +{ + auto ro = DB::MockDatabase::openConnectionTo("odbcmock"); + + 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})); + rows += 1; + } + delete select; + BOOST_REQUIRE_EQUAL(1, rows); + delete ro; +} + +BOOST_AUTO_TEST_SUITE_END(); + |