mysql test db autocreate

This commit is contained in:
Alex Fabijanic 2014-05-05 19:50:42 -05:00
parent 5e1be8cee7
commit 3d36b0a8c5
2 changed files with 71 additions and 24 deletions

View File

@ -6,7 +6,27 @@
// Copyright (c) 2008, Applied Informatics Software Engineering GmbH. // Copyright (c) 2008, Applied Informatics Software Engineering GmbH.
// and Contributors. // and Contributors.
// //
// SPDX-License-Identifier: BSL-1.0 // Permission is hereby granted, free of charge, to any person or organization
// obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute,
// execute, and transmit the Software, and to prepare derivative works of the
// Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following:
//
// The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer,
// must be included in all copies of the Software, in whole or in part, and
// all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by
// a source language processor.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
// //
@ -48,20 +68,20 @@ Poco::SharedPtr<SQLExecutor> MySQLTest::_pExecutor = 0;
#define MYSQL_PWD "poco" #define MYSQL_PWD "poco"
#define MYSQL_HOST "localhost" #define MYSQL_HOST "localhost"
#define MYSQL_PORT 3306 #define MYSQL_PORT 3306
#define MYSQL_DB "test" #define MYSQL_DB "pocotestdb"
// //
// Connection string // Connection string
std::string MySQLTest::_dbConnString = "host=" MYSQL_HOST std::string MySQLTest::_dbConnString = "host=" MYSQL_HOST
";user=" MYSQL_USER ";user=" MYSQL_USER
";password=" MYSQL_PWD ";password=" MYSQL_PWD
";db=" MYSQL_DB ";db=" MYSQL_DB
";compress=true" ";compress=true"
";auto-reconnect=true" ";auto-reconnect=true"
";secure-auth=true"; ";secure-auth=true";
MySQLTest::MySQLTest(const std::string& name): MySQLTest::MySQLTest(const std::string& name):
CppUnit::TestCase(name) CppUnit::TestCase(name)
{ {
MySQL::Connector::registerConnector(); MySQL::Connector::registerConnector();
@ -76,25 +96,25 @@ MySQLTest::~MySQLTest()
void MySQLTest::dbInfo(Session& session) void MySQLTest::dbInfo(Session& session)
{ {
std::cout << "Server Info: " << Utility::serverInfo(session) << std::endl;
std::cout << "Server Info: " << Utility::serverInfo(session) << std::endl; std::cout << "Server Version: " << Utility::serverVersion(session) << std::endl;
std::cout << "Server Version: " << Utility::serverVersion(session) << std::endl; std::cout << "Host Info: " << Utility::hostInfo(session) << std::endl;
std::cout << "Host Info: " << Utility::hostInfo(session) << std::endl;
} }
void MySQLTest::testConnectNoDB() void MySQLTest::connectNoDB()
{ {
std::string dbConnString = "host=" MYSQL_HOST std::string dbConnString = "host=" MYSQL_HOST
";user=" MYSQL_USER ";user=" MYSQL_USER
";password=" MYSQL_PWD ";password=" MYSQL_PWD
";compress=true;auto-reconnect=true"; ";compress=true;auto-reconnect=true";
try try
{ {
Session session(MySQL::Connector::KEY, dbConnString); Session session(MySQL::Connector::KEY, dbConnString);
std::cout << "Connected to [" << "MySQL" << "] without database." << std::endl; std::cout << "Connected to [" << "MySQL" << "] without database." << std::endl;
dbInfo(session); dbInfo(session);
session << "CREATE DATABASE IF NOT EXISTS " MYSQL_DB ";", now;
std::cout << "Disconnecting ..." << std::endl; std::cout << "Disconnecting ..." << std::endl;
session.close(); session.close();
std::cout << "Disconnected." << std::endl; std::cout << "Disconnected." << std::endl;
@ -104,8 +124,8 @@ void MySQLTest::testConnectNoDB()
std::cout << ex.displayText() << std::endl; std::cout << ex.displayText() << std::endl;
} }
} }
void MySQLTest::testBareboneMySQL() void MySQLTest::testBareboneMySQL()
{ {
if (!_pSession) fail ("Test not available."); if (!_pSession) fail ("Test not available.");
@ -217,7 +237,6 @@ void MySQLTest::testLimitOnce()
recreateIntsTable(); recreateIntsTable();
_pExecutor->limitOnce(); _pExecutor->limitOnce();
} }
@ -428,7 +447,7 @@ void MySQLTest::testDateTime()
void MySQLTest::testBLOB() void MySQLTest::testBLOB()
{ {
if (!_pSession) fail ("Test not available."); if (!_pSession) fail ("Test not available.");
recreatePersonBLOBTable(); recreatePersonBLOBTable();
_pExecutor->blob(); _pExecutor->blob();
@ -441,7 +460,7 @@ void MySQLTest::testBLOB()
_pExecutor->blob(maxFldSize); _pExecutor->blob(maxFldSize);
fail ("must fail"); fail ("must fail");
} }
catch (DataException&) catch (DataException&)
{ {
_pSession->setProperty("maxFieldSize", Poco::Any(maxFldSize)); _pSession->setProperty("maxFieldSize", Poco::Any(maxFldSize));
} }
@ -648,7 +667,7 @@ void MySQLTest::testTupleWithNullable()
Info info(0, std::string("Address"), 10); Info info(0, std::string("Address"), 10);
*_pSession << "INSERT INTO NullableStringTest VALUES(?, ?, ?)", use(info), now; *_pSession << "INSERT INTO NullableStringTest VALUES(?, ?, ?)", use(info), now;
info.set<0>(info.get<0>()++); info.set<0>(info.get<0>()++);
info.set<1>(null); info.set<1>(null);
*_pSession << "INSERT INTO NullableStringTest VALUES(?, ?, ?)", use(info), now; *_pSession << "INSERT INTO NullableStringTest VALUES(?, ?, ?)", use(info), now;
@ -692,7 +711,6 @@ void MySQLTest::testTupleWithNullable()
void MySQLTest::dropTable(const std::string& tableName) void MySQLTest::dropTable(const std::string& tableName)
{ {
try { *_pSession << format("DROP TABLE IF EXISTS %s", tableName), now; } try { *_pSession << format("DROP TABLE IF EXISTS %s", tableName), now; }
catch(ConnectionException& ce){ std::cout << ce.displayText() << std::endl; fail ("dropTable()"); } catch(ConnectionException& ce){ std::cout << ce.displayText() << std::endl; fail ("dropTable()"); }
catch(StatementException& se){ std::cout << se.displayText() << std::endl; fail ("dropTable()"); } catch(StatementException& se){ std::cout << se.displayText() << std::endl; fail ("dropTable()"); }
@ -795,7 +813,7 @@ void MySQLTest::recreateTuplesTable()
void MySQLTest::recreateNullableIntTable() void MySQLTest::recreateNullableIntTable()
{ {
dropTable("NullableIntTest"); dropTable("NullableIntTest");
try { try {
*_pSession << "CREATE TABLE NullableIntTest (Id INTEGER(10), Value INTEGER(10))", now; *_pSession << "CREATE TABLE NullableIntTest (Id INTEGER(10), Value INTEGER(10))", now;
} }
catch(ConnectionException& ce){ std::cout << ce.displayText() << std::endl; fail ("recreateNullableIntTable()"); } catch(ConnectionException& ce){ std::cout << ce.displayText() << std::endl; fail ("recreateNullableIntTable()"); }
@ -806,7 +824,7 @@ void MySQLTest::recreateNullableIntTable()
void MySQLTest::recreateNullableStringTable() void MySQLTest::recreateNullableStringTable()
{ {
dropTable("NullableStringTest"); dropTable("NullableStringTest");
try { try {
*_pSession << "CREATE TABLE NullableStringTest (Id INTEGER(10), Address VARCHAR(30), Age INTEGER(10))", now; *_pSession << "CREATE TABLE NullableStringTest (Id INTEGER(10), Address VARCHAR(30), Age INTEGER(10))", now;
} }
catch(ConnectionException& ce){ std::cout << ce.displayText() << std::endl; fail ("recreateNullableStringTable()"); } catch(ConnectionException& ce){ std::cout << ce.displayText() << std::endl; fail ("recreateNullableStringTable()"); }
@ -846,7 +864,17 @@ CppUnit::Test* MySQLTest::suite()
catch (ConnectionFailedException& ex) catch (ConnectionFailedException& ex)
{ {
std::cout << ex.displayText() << std::endl; std::cout << ex.displayText() << std::endl;
return 0; std::cout << "Trying to connect without DB and create one ..." << std::endl;
connectNoDB();
try
{
_pSession = new Session(MySQL::Connector::KEY, _dbConnString);
}
catch (ConnectionFailedException& ex)
{
std::cout << ex.displayText() << std::endl;
return 0;
}
} }
std::cout << "*** Connected to [" << "MySQL" << "] test database." << std::endl; std::cout << "*** Connected to [" << "MySQL" << "] test database." << std::endl;
@ -856,7 +884,6 @@ CppUnit::Test* MySQLTest::suite()
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("MySQLTest"); CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("MySQLTest");
CppUnit_addTest(pSuite, MySQLTest, testConnectNoDB);
CppUnit_addTest(pSuite, MySQLTest, testBareboneMySQL); CppUnit_addTest(pSuite, MySQLTest, testBareboneMySQL);
CppUnit_addTest(pSuite, MySQLTest, testSimpleAccess); CppUnit_addTest(pSuite, MySQLTest, testSimpleAccess);
CppUnit_addTest(pSuite, MySQLTest, testComplexType); CppUnit_addTest(pSuite, MySQLTest, testComplexType);

View File

@ -8,7 +8,27 @@
// Copyright (c) 2008, Applied Informatics Software Engineering GmbH. // Copyright (c) 2008, Applied Informatics Software Engineering GmbH.
// and Contributors. // and Contributors.
// //
// SPDX-License-Identifier: BSL-1.0 // Permission is hereby granted, free of charge, to any person or organization
// obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute,
// execute, and transmit the Software, and to prepare derivative works of the
// Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following:
//
// The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer,
// must be included in all copies of the Software, in whole or in part, and
// all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by
// a source language processor.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
// //
@ -37,7 +57,6 @@ public:
MySQLTest(const std::string& name); MySQLTest(const std::string& name);
~MySQLTest(); ~MySQLTest();
void testConnectNoDB();
void testBareboneMySQL(); void testBareboneMySQL();
void testSimpleAccess(); void testSimpleAccess();
@ -106,6 +125,7 @@ public:
static CppUnit::Test* suite(); static CppUnit::Test* suite();
private: private:
static void connectNoDB();
void dropTable(const std::string& tableName); void dropTable(const std::string& tableName);
void recreatePersonTable(); void recreatePersonTable();