From 3e56274e69e1e37c3f12887036016c485716da26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Rombauts?= Date: Thu, 30 Jun 2016 18:59:39 +0200 Subject: [PATCH] Add last missing unit-tests to Statement and Transaction --- tests/Statement_test.cpp | 51 +++++++++++++++++++++++++++++++++----- tests/Transaction_test.cpp | 38 +++++++++++++++++++++++----- 2 files changed, 77 insertions(+), 12 deletions(-) diff --git a/tests/Statement_test.cpp b/tests/Statement_test.cpp index 5d39eb7..b8aee3f 100644 --- a/tests/Statement_test.cpp +++ b/tests/Statement_test.cpp @@ -212,10 +212,10 @@ TEST(Statement, bindings) { // Fourth row with string/int64/float const std::string second("second"); const sqlite_int64 int64 = 12345678900000LL; - const float fl32 = 0.123f; + const float float32 = 0.234f; insert.bind(1, second); insert.bind(2, int64); - insert.bind(3, fl32); + insert.bind(3, float32); EXPECT_EQ(1, insert.exec()); EXPECT_EQ(SQLITE_DONE, db.getErrorCode()); @@ -226,7 +226,27 @@ TEST(Statement, bindings) { EXPECT_EQ(4, query.getColumn(0).getInt64()); EXPECT_EQ(second, query.getColumn(1).getText()); 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) { @@ -269,10 +289,10 @@ TEST(Statement, bindByName) { // Second row with string/int64/float const std::string second("second"); const sqlite_int64 int64 = 12345678900000LL; - const float fl32 = 0.123f; + const float float32 = 0.234f; insert.bind("@msg", second); insert.bind("@int", int64); - insert.bind("@double", fl32); + insert.bind("@double", float32); EXPECT_EQ(1, insert.exec()); EXPECT_EQ(SQLITE_DONE, db.getErrorCode()); @@ -283,7 +303,26 @@ TEST(Statement, bindByName) { EXPECT_EQ(2, query.getColumn(0).getInt64()); EXPECT_EQ(second, query.getColumn(1).getText()); 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) { diff --git a/tests/Transaction_test.cpp b/tests/Transaction_test.cpp index 9ed39a1..a117a43 100644 --- a/tests/Transaction_test.cpp +++ b/tests/Transaction_test.cpp @@ -41,7 +41,19 @@ TEST(Transaction, commitRollback) { 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 { // Begin transaction @@ -51,12 +63,11 @@ TEST(Transaction, commitRollback) { EXPECT_EQ(1, db.exec("INSERT INTO test VALUES (NULL, \"second\")")); 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"); - GTEST_FATAL_FAILURE_("we should never get there"); - // Commit transaction - transaction.commit(); + GTEST_FATAL_FAILURE_("we should never get there"); + transaction.commit(); // We should never get there } catch (std::exception& e) { @@ -64,7 +75,22 @@ TEST(Transaction, commitRollback) { // 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"); int nbRows = 0; while (query.executeStep())