|
| 1 | +/** |
| 2 | + * @file Savepoint_test.cpp |
| 3 | + * @ingroup tests |
| 4 | + * @brief Test of a SQLite Savepoint. |
| 5 | + * |
| 6 | + * Copyright (c) 2020 Kelvin Hammond ([email protected]) |
| 7 | + * |
| 8 | + * Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt or |
| 9 | + * copy at http://opensource.org/licenses/MIT) |
| 10 | + */ |
| 11 | + |
| 12 | +#include <SQLiteCpp/Database.h> |
| 13 | +#include <SQLiteCpp/Exception.h> |
| 14 | +#include <SQLiteCpp/Savepoint.h> |
| 15 | +#include <SQLiteCpp/Statement.h> |
| 16 | +#include <SQLiteCpp/Transaction.h> |
| 17 | +#include <gtest/gtest.h> |
| 18 | + |
| 19 | +#include <cstdio> |
| 20 | + |
| 21 | +TEST(Savepoint, commitRollback) { |
| 22 | + // Create a new database |
| 23 | + SQLite::Database db(":memory:", |
| 24 | + SQLite::OPEN_READWRITE | SQLite::OPEN_CREATE); |
| 25 | + EXPECT_EQ(SQLite::OK, db.getErrorCode()); |
| 26 | + |
| 27 | + { |
| 28 | + // Begin savepoint |
| 29 | + SQLite::Savepoint savepoint(db, "sp1"); |
| 30 | + |
| 31 | + EXPECT_EQ( |
| 32 | + 0, |
| 33 | + db.exec("CREATE TABLE test (id INTEGER PRIMARY KEY, value TEXT)")); |
| 34 | + EXPECT_EQ(SQLite::OK, db.getErrorCode()); |
| 35 | + |
| 36 | + // Insert a first valu |
| 37 | + EXPECT_EQ(1, db.exec("INSERT INTO test VALUES (NULL, 'first')")); |
| 38 | + EXPECT_EQ(1, db.getLastInsertRowid()); |
| 39 | + |
| 40 | + // release savepoint |
| 41 | + savepoint.release(); |
| 42 | + |
| 43 | + // Commit again throw an exception |
| 44 | + EXPECT_THROW(savepoint.release(), SQLite::Exception); |
| 45 | + EXPECT_THROW(savepoint.rollback(), SQLite::Exception); |
| 46 | + } |
| 47 | + |
| 48 | + // Auto rollback if no release() before the end of scope |
| 49 | + { |
| 50 | + // Begin savepoint |
| 51 | + SQLite::Savepoint savepoint(db, "sp2"); |
| 52 | + |
| 53 | + // Insert a second value (that will be rollbacked) |
| 54 | + EXPECT_EQ(1, db.exec("INSERT INTO test VALUES (NULL, 'third')")); |
| 55 | + EXPECT_EQ(2, db.getLastInsertRowid()); |
| 56 | + |
| 57 | + // end of scope: automatic rollback |
| 58 | + } |
| 59 | + |
| 60 | + // Auto rollback of a transaction on error / exception |
| 61 | + try { |
| 62 | + // Begin savepoint |
| 63 | + SQLite::Savepoint savepoint(db, "sp3"); |
| 64 | + |
| 65 | + // Insert a second value (that will be rollbacked) |
| 66 | + EXPECT_EQ(1, db.exec("INSERT INTO test VALUES (NULL, 'second')")); |
| 67 | + EXPECT_EQ(2, db.getLastInsertRowid()); |
| 68 | + |
| 69 | + // Execute with an error => exception with auto-rollback |
| 70 | + db.exec( |
| 71 | + "DesiredSyntaxError to raise an exception to rollback the " |
| 72 | + "transaction"); |
| 73 | + |
| 74 | + GTEST_FATAL_FAILURE_("we should never get there"); |
| 75 | + savepoint.release(); // We should never get there |
| 76 | + } catch (std::exception& e) { |
| 77 | + std::cout << "SQLite exception: " << e.what() << std::endl; |
| 78 | + // expected error, see above |
| 79 | + } |
| 80 | + |
| 81 | + // Double rollback with a manual command before the end of scope |
| 82 | + { |
| 83 | + // Begin savepoint |
| 84 | + SQLite::Savepoint savepoint(db, "sp4"); |
| 85 | + |
| 86 | + // Insert a second value (that will be rollbacked) |
| 87 | + EXPECT_EQ(1, db.exec("INSERT INTO test VALUES (NULL, 'third')")); |
| 88 | + EXPECT_EQ(2, db.getLastInsertRowid()); |
| 89 | + |
| 90 | + // Execute a manual rollback (no real use case I can think of, so no |
| 91 | + // rollback() method) |
| 92 | + db.exec("ROLLBACK"); |
| 93 | + |
| 94 | + // end of scope: the automatic rollback should not raise an error |
| 95 | + // because it is harmless |
| 96 | + } |
| 97 | + |
| 98 | + // Check the results (expect only one row of result, as all other one have |
| 99 | + // been rollbacked) |
| 100 | + SQLite::Statement query(db, "SELECT * FROM test"); |
| 101 | + int nbRows = 0; |
| 102 | + while (query.executeStep()) { |
| 103 | + nbRows++; |
| 104 | + EXPECT_EQ(1, query.getColumn(0).getInt()); |
| 105 | + EXPECT_STREQ("first", query.getColumn(1).getText()); |
| 106 | + } |
| 107 | + EXPECT_EQ(1, nbRows); |
| 108 | +} |
0 commit comments