diff options
Diffstat (limited to 'project2/sql/unittests/testmysql.cpp')
-rw-r--r-- | project2/sql/unittests/testmysql.cpp | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/project2/sql/unittests/testmysql.cpp b/project2/sql/unittests/testmysql.cpp new file mode 100644 index 0000000..4debd35 --- /dev/null +++ b/project2/sql/unittests/testmysql.cpp @@ -0,0 +1,115 @@ +#define BOOST_TEST_MODULE TestMySQL +#include <boost/test/unit_test.hpp> + +#include <mockDatasource.h> +#include <definedDirs.h> +#include <modifycommand.h> +#include <selectcommand.h> +#include <column.h> +#include <sql-modMySQL.h> +#include "testCore.h" +#include <sqlHandleAsVariableType.h> + +class StandardMockDatabase : public MockMySQLDatabase { + public: + StandardMockDatabase() : MockMySQLDatabase("mysqlmock", { + RootDir / "mysqlschema.sql" }) + { + } +}; + +BOOST_GLOBAL_FIXTURE( StandardMockDatabase ); + +BOOST_FIXTURE_TEST_SUITE( Core, TestCore ); + +BOOST_AUTO_TEST_CASE( transactions ) +{ + RdbmsDataSource * ds = CommonObjects::dataSource<RdbmsDataSource>("mysqlmock"); + auto ro = ds->getReadonly(); + + 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()); + + ds->close(); +} + +BOOST_AUTO_TEST_CASE( bindAndSend ) +{ + RdbmsDataSource * ds = CommonObjects::dataSource<RdbmsDataSource>("mysqlmock"); + auto rw = ds->getWritable(); + + auto mod = rw->newModifyCommand("INSERT INTO test VALUES(?, ?, ?, ?, ?)"); + mod->bindParamI(0, testInt); + mod->bindParamF(1, testDouble); + mod->bindParamS(2, testString); + mod->bindParamT(3, testDateTime); + mod->bindParamT(4, testInterval); + mod->execute(); + delete mod; + ds->commit(); + ds->close(); +} + +template<typename T> +void +assertColumnValueHelper(DB::SelectCommand & sel, unsigned int col, const T & t) +{ + HandleAsVariableType h; + sel[col].apply(h); + BOOST_REQUIRE_EQUAL(t, h.variable.as<T>()); +} + +BOOST_AUTO_TEST_CASE( bindAndSelect ) +{ + RdbmsDataSource * ds = CommonObjects::dataSource<RdbmsDataSource>("mysqlmock"); + auto ro = ds->getReadonly(); + + 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, testDateTime); + assertColumnValueHelper(*select, 4, testInterval); + rows += 1; + } + delete select; + BOOST_REQUIRE_EQUAL(1, rows); + ds->close(); +} + +BOOST_AUTO_TEST_CASE( bindAndSelectOther ) +{ + RdbmsDataSource * ds = CommonObjects::dataSource<RdbmsDataSource>("mysqlmock"); + auto ro = ds->getReadonly(); + + 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, boost::posix_time::ptime_from_tm({ 3, 6, 23, 27, 3, 115, 0, 0, 0, 0, 0})); + assertColumnValueHelper(*select, 4, boost::posix_time::time_duration(38, 13, 12)); + rows += 1; + } + delete select; + BOOST_REQUIRE_EQUAL(1, rows); + ds->close(); +} + +BOOST_AUTO_TEST_SUITE_END(); + |