Date: Thursday, December 8, 2005 @ 15:19:41
Author: gilles
Path: /cvsroot/carob/carob/test
Modified: TestStatement.cpp (1.13 -> 1.14) TestStatement.hpp (1.3 -> 1.4)
Added execute() function tests
Minor fix in queries: removed final ';'
-------------------+
TestStatement.cpp | 148 +++++++++++++++++++++++++++++++++++++++++++++++++---
TestStatement.hpp | 18 +++++-
2 files changed, 158 insertions(+), 8 deletions(-)
Index: carob/test/TestStatement.cpp
diff -u carob/test/TestStatement.cpp:1.13 carob/test/TestStatement.cpp:1.14
--- carob/test/TestStatement.cpp:1.13 Tue Dec 6 17:54:01 2005
+++ carob/test/TestStatement.cpp Thu Dec 8 15:19:41 2005
@@ -48,7 +48,7 @@
{
logDebug(fctName, L"Executing dummy read - should fail");
}
- statementPtr->executeQuery(L"dummy request;");
+ statementPtr->executeQuery(L"dummy request");
// We should receive an exception instead of coming here
CPPUNIT_ASSERT(false);
}
@@ -74,7 +74,7 @@
{
logDebug(fctName, L"Executing bad table read - should fail");
}
- statementPtr->executeQuery(L"select * from dummy;");
+ statementPtr->executeQuery(L"select * from dummy");
// We should receive an exception instead of coming here
CPPUNIT_ASSERT(false);
}
@@ -100,7 +100,7 @@
}
statementPtr->setFetchSize(1);
DriverResultSet* drsPtr = statementPtr->executeQuery(
- L"select * from address;");
+ L"select * from address");
if (isDebugEnabled())
{
logDebug(fctName, L"Read succeeded. Displaying 50 rows:");
@@ -132,7 +132,7 @@
{
logDebug(fctName, L"Executing dummy update - should fail");
}
- statementPtr->executeUpdate(L"dummy request;");
+ statementPtr->executeUpdate(L"dummy request");
// We should receive an exception instead of coming here
CPPUNIT_ASSERT(false);
}
@@ -158,7 +158,7 @@
{
logDebug(fctName, L"Executing bad table update - this should fail");
}
- statementPtr->executeUpdate(L"update dummy set name='gotit' where id=0;");
+ statementPtr->executeUpdate(L"update dummy set name='gotit' where id=0");
// We should receive an exception instead of coming here
CPPUNIT_ASSERT(false);
}
@@ -183,7 +183,7 @@
logDebug(fctName, L"Executing update - should succeed");
}
int32_t nbRowsAffected = statementPtr->executeUpdate(
- L"update product set name='changed by testExecuteUpdateGood' where
id=0;");
+ L"update product set name='changed by testExecuteUpdateGood' where
id=0");
if (isDebugEnabled())
{
logDebug(fctName, L"Update succeeded. Number of affected rows = "
@@ -204,7 +204,7 @@
}
statementPtr->setMaxRows(1);
DriverResultSet* drsPtr = statementPtr->executeQuery(
- L"select * from address;");
+ L"select * from address");
if (isDebugEnabled())
{
logDebug(fctName, L"executeQuery succeeded. Displaying first row.");
@@ -222,6 +222,128 @@
delete statementPtr;
}
+void TestStatement::testExecuteBadRequests()
+{
+ wstring fctName(L"TestStatement::testExecuteBadRequests");
+ Statement* statementPtr = NULL;
+ if (isDebugEnabled())
+ {
+ logDebug(fctName, L"Testing execute with bad query \"dummy\" (should
fail)");
+ }
+ try
+ {
+ statementPtr = connectionPtr->createStatement();
+ statementPtr->execute(L"dummy");
+ // We should receive an exception instead of coming here
+ CPPUNIT_ASSERT(false);
+ }
+ catch (BackendException be)
+ {
+ if (isErrorEnabled())
+ {
+ logError(fctName, L"Read failed (this is ok). Exception: "
+ + be.description());
+ }
+ }
+ if (isDebugEnabled())
+ {
+ logDebug(fctName, L"Testing execute with bad query \"SELECT * FROM dummy\"
(should fail)");
+ }
+ try
+ {
+ statementPtr->execute(L"SELECT * FROM dummy");
+ // We should receive an exception instead of coming here
+ CPPUNIT_ASSERT(false);
+ }
+ catch (BackendException be)
+ {
+ if (isErrorEnabled())
+ {
+ logError(fctName, L"Read failed (this is ok). Exception: "
+ + be.description());
+ }
+ }
+ delete statementPtr;
+}
+
+void TestStatement::testExecuteWithSelect()
+{
+ wstring fctName(L"TestStatement::testExecuteWithSelect");
+ Statement* statementPtr = NULL;
+ statementPtr = connectionPtr->createStatement();
+ if (isDebugEnabled())
+ {
+ logDebug(fctName, L"Executing execute(SELECT * FROM address) - should
succeed");
+ }
+ bool isRS = statementPtr->execute(L"SELECT * FROM address");
+ CPPUNIT_ASSERT(isRS);
+
+ DriverResultSet* drsPtr = statementPtr->getResultSet();
+ if (isDebugEnabled())
+ {
+ logDebug(fctName, L"Read succeeded. Displaying 50 rows:");
+ }
+ //Display 50 rows using metadata and toString
+ ResultSetMetaData rsmd(drsPtr);
+ for (int i=0; i<50; i++)
+ {
+ drsPtr->next();
+ wcerr<<i+1<<L"\t";
+ for (int j=0; j<rsmd.getColumnCount(); j++)
+ wcerr<<drsPtr->getString(j+1)<<L"\t";
+ wcerr<<endl;
+ }
+ delete statementPtr;
+}
+void TestStatement::testExecuteWithUpdate()
+{
+ wstring fctName(L"TestStatement::testExecuteWithUpdate");
+ Statement* statementPtr = NULL;
+ statementPtr = connectionPtr->createStatement();
+ if (isDebugEnabled())
+ {
+ logDebug(fctName, L"Executing execute(UPDATE FROM address...) - should
succeed");
+ }
+ bool isRS = statementPtr->execute(L"UPDATE product SET name='changed by
testExecuteWithUpdate' WHERE id=0");
+ CPPUNIT_ASSERT(!isRS);
+ if (isDebugEnabled())
+ {
+ logDebug(fctName, L"Update with execute succeeded. Number of affected rows
= "
+ + toWString(statementPtr->getUpdateCount()));
+ }
+ CPPUNIT_ASSERT(statementPtr->getUpdateCount() == 1);
+ delete statementPtr;
+}
+void TestStatement::testExecuteWithSelectStreamed()
+{
+ wstring fctName(L"TestStatement::testExecuteWithSelectStreamed");
+ Statement* statementPtr = NULL;
+ statementPtr = connectionPtr->createStatement();
+ if (isDebugEnabled())
+ {
+ logDebug(fctName, L"Executing streamed execute(SELECT * FROM address) -
should succeed");
+ }
+ statementPtr->setFetchSize(1);
+ bool isRS = statementPtr->execute(L"SELECT * FROM address");
+ CPPUNIT_ASSERT(isRS);
+
+ DriverResultSet* drsPtr = statementPtr->getResultSet();
+ ResultSetMetaData rsmd(drsPtr);
+ if (isDebugEnabled())
+ {
+ logDebug(fctName, L"Read succeeded. Displaying 50 rows:");
+ }
+ //Display rows using metadata and toString
+ while (drsPtr->next())
+ {
+ ResultSetMetaData rsmd(drsPtr);
+ for (int j=0; j<rsmd.getColumnCount(); j++)
+ wcerr<<drsPtr->getString(j+1)<<L"\t";
+ wcerr<<endl;
+ }
+ delete statementPtr;
+}
+
CppUnit::Test* TestStatement::suite()
{
CppUnit::TestSuite *suiteOfTests = new CppUnit::TestSuite( "TestStatement" );
@@ -246,6 +368,18 @@
suiteOfTests->addTest(new CppUnit::TestCaller<TestStatement>(
"testExecuteQueryWithMaxRows",
&TestStatement::testExecuteQueryWithMaxRows));
+ suiteOfTests->addTest(new CppUnit::TestCaller<TestStatement>(
+ "testExecuteBadRequests",
+ &TestStatement::testExecuteBadRequests));
+ suiteOfTests->addTest(new CppUnit::TestCaller<TestStatement>(
+ "testExecuteWithSelect",
+ &TestStatement::testExecuteWithSelect));
+ suiteOfTests->addTest(new CppUnit::TestCaller<TestStatement>(
+ "testExecuteWithUpdate",
+ &TestStatement::testExecuteWithUpdate));
+ suiteOfTests->addTest(new CppUnit::TestCaller<TestStatement>(
+ "testExecuteWithSelectStreamed",
+
&TestStatement::testExecuteWithSelectStreamed));
return suiteOfTests;
}
Index: carob/test/TestStatement.hpp
diff -u carob/test/TestStatement.hpp:1.3 carob/test/TestStatement.hpp:1.4
--- carob/test/TestStatement.hpp:1.3 Fri Dec 2 15:53:07 2005
+++ carob/test/TestStatement.hpp Thu Dec 8 15:19:41 2005
@@ -69,12 +69,28 @@
* that there is no error.
*/
void testExecuteUpdateGood();
-
/**
* Tests a read with max rows to 1 and checks that the second call to next()
* returns false
*/
void testExecuteQueryWithMaxRows();
+ /**
+ * Tests execute function with bad query of different forms (bad query, wrong
+ * table)
+ */
+ void testExecuteBadRequests();
+ /**
+ * Tests execute function with a select statement
+ */
+ void testExecuteWithSelect();
+ /**
+ * Tests execute function with an update statement
+ */
+ void testExecuteWithUpdate();
+ /**
+ * Tests execute function with a select statement and max rows set to 1
+ */
+ void testExecuteWithSelectStreamed();
};
#endif /*TESTSTATEMENT_H_*/
_______________________________________________
Carob-commits mailing list
[email protected]
https://forge.continuent.org/mailman/listinfo/carob-commits