Add last missing unit-tests to Statement and Transaction

This commit is contained in:
Sébastien Rombauts 2016-06-30 18:59:39 +02:00
parent c0b2d81db9
commit 3e56274e69
2 changed files with 77 additions and 12 deletions

View File

@ -212,10 +212,10 @@ TEST(Statement, bindings) {
// Fourth row with string/int64/float // Fourth row with string/int64/float
const std::string second("second"); const std::string second("second");
const sqlite_int64 int64 = 12345678900000LL; const sqlite_int64 int64 = 12345678900000LL;
const float fl32 = 0.123f; const float float32 = 0.234f;
insert.bind(1, second); insert.bind(1, second);
insert.bind(2, int64); insert.bind(2, int64);
insert.bind(3, fl32); insert.bind(3, float32);
EXPECT_EQ(1, insert.exec()); EXPECT_EQ(1, insert.exec());
EXPECT_EQ(SQLITE_DONE, db.getErrorCode()); EXPECT_EQ(SQLITE_DONE, db.getErrorCode());
@ -226,7 +226,27 @@ TEST(Statement, bindings) {
EXPECT_EQ(4, query.getColumn(0).getInt64()); EXPECT_EQ(4, query.getColumn(0).getInt64());
EXPECT_EQ(second, query.getColumn(1).getText()); EXPECT_EQ(second, query.getColumn(1).getText());
EXPECT_EQ(12345678900000LL, query.getColumn(2).getInt64()); EXPECT_EQ(12345678900000LL, query.getColumn(2).getInt64());
EXPECT_EQ(0.123f, query.getColumn(3).getDouble()); EXPECT_EQ(0.234f, query.getColumn(3).getDouble());
// reset() without clearbindings()
insert.reset();
// Fourth row with binary buffer and a null parameter
const char buffer[] = "binary";
insert.bind(1, buffer, sizeof(buffer));
insert.bind(2);
EXPECT_EQ(1, insert.exec());
// Check the fifth row
query.executeStep();
EXPECT_TRUE (query.isOk());
EXPECT_FALSE(query.isDone());
EXPECT_EQ(5, query.getColumn(0).getInt64());
EXPECT_STREQ(buffer, query.getColumn(1).getText());
EXPECT_TRUE (query.isColumnNull(2));
EXPECT_EQ(0, query.getColumn(2).getInt());
EXPECT_EQ(0.234f, query.getColumn(3).getDouble());
} }
TEST(Statement, bindByName) { TEST(Statement, bindByName) {
@ -269,10 +289,10 @@ TEST(Statement, bindByName) {
// Second row with string/int64/float // Second row with string/int64/float
const std::string second("second"); const std::string second("second");
const sqlite_int64 int64 = 12345678900000LL; const sqlite_int64 int64 = 12345678900000LL;
const float fl32 = 0.123f; const float float32 = 0.234f;
insert.bind("@msg", second); insert.bind("@msg", second);
insert.bind("@int", int64); insert.bind("@int", int64);
insert.bind("@double", fl32); insert.bind("@double", float32);
EXPECT_EQ(1, insert.exec()); EXPECT_EQ(1, insert.exec());
EXPECT_EQ(SQLITE_DONE, db.getErrorCode()); EXPECT_EQ(SQLITE_DONE, db.getErrorCode());
@ -283,7 +303,26 @@ TEST(Statement, bindByName) {
EXPECT_EQ(2, query.getColumn(0).getInt64()); EXPECT_EQ(2, query.getColumn(0).getInt64());
EXPECT_EQ(second, query.getColumn(1).getText()); EXPECT_EQ(second, query.getColumn(1).getText());
EXPECT_EQ(12345678900000LL, query.getColumn(2).getInt64()); EXPECT_EQ(12345678900000LL, query.getColumn(2).getInt64());
EXPECT_EQ(0.123f, query.getColumn(3).getDouble()); EXPECT_EQ(0.234f, query.getColumn(3).getDouble());
// reset() without clearbindings()
insert.reset();
// Third row with binary buffer and a null parameter
const char buffer[] = "binary";
insert.bind("@msg", buffer, sizeof(buffer));
insert.bind("@int");
EXPECT_EQ(1, insert.exec());
// Check the third row
query.executeStep();
EXPECT_TRUE (query.isOk());
EXPECT_FALSE(query.isDone());
EXPECT_EQ(3, query.getColumn(0).getInt64());
EXPECT_STREQ(buffer, query.getColumn(1).getText());
EXPECT_TRUE (query.isColumnNull(2));
EXPECT_EQ(0, query.getColumn(2).getInt());
EXPECT_EQ(0.234f, query.getColumn(3).getDouble());
} }
TEST(Statement, isColumnNull) { TEST(Statement, isColumnNull) {

View File

@ -41,7 +41,19 @@ TEST(Transaction, commitRollback) {
EXPECT_THROW(transaction.commit(), SQLite::Exception); EXPECT_THROW(transaction.commit(), SQLite::Exception);
} }
// Auto rollback of a transaction on error // Auto rollback if no commit() before the end of scope
{
// Begin transaction
SQLite::Transaction transaction(db);
// Insert a second value (that will be rollbacked)
EXPECT_EQ(1, db.exec("INSERT INTO test VALUES (NULL, \"third\")"));
EXPECT_EQ(2, db.getLastInsertRowid());
// end of scope: automatic rollback
}
// Auto rollback of a transaction on error/exception
try try
{ {
// Begin transaction // Begin transaction
@ -51,12 +63,11 @@ TEST(Transaction, commitRollback) {
EXPECT_EQ(1, db.exec("INSERT INTO test VALUES (NULL, \"second\")")); EXPECT_EQ(1, db.exec("INSERT INTO test VALUES (NULL, \"second\")"));
EXPECT_EQ(2, db.getLastInsertRowid()); EXPECT_EQ(2, db.getLastInsertRowid());
// Execute with an error to rollback // Execute with an error => exception with auto-rollback
db.exec("DesiredSyntaxError to raise an exception to rollback the transaction"); db.exec("DesiredSyntaxError to raise an exception to rollback the transaction");
GTEST_FATAL_FAILURE_("we should never get there");
// Commit transaction GTEST_FATAL_FAILURE_("we should never get there");
transaction.commit(); transaction.commit(); // We should never get there
} }
catch (std::exception& e) catch (std::exception& e)
{ {
@ -64,7 +75,22 @@ TEST(Transaction, commitRollback) {
// expected error, see above // expected error, see above
} }
// Check the results (expect only one row of result, as the second one has been rollbacked by the error) // Double rollback with a manual command before the end of scope
{
// Begin transaction
SQLite::Transaction transaction(db);
// Insert a second value (that will be rollbacked)
EXPECT_EQ(1, db.exec("INSERT INTO test VALUES (NULL, \"third\")"));
EXPECT_EQ(2, db.getLastInsertRowid());
// Execute a manual rollback (no real use case I can think of, so no rollback() method)
db.exec("ROLLBACK");
// end of scope: the automatic rollback should not raise an error because it is harmless
}
// Check the results (expect only one row of result, as all other one have been rollbacked)
SQLite::Statement query(db, "SELECT * FROM test"); SQLite::Statement query(db, "SELECT * FROM test");
int nbRows = 0; int nbRows = 0;
while (query.executeStep()) while (query.executeStep())