[email protected] | 3a305db | 2011-04-12 13:40:53 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Scott Graham | 47ed2c3 | 2017-09-15 02:17:07 | [diff] [blame] | 5 | #include "sql/transaction.h" |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 6 | |
thestig | 22dfc401 | 2014-09-05 08:29:44 | [diff] [blame] | 7 | #include "base/files/file_util.h" |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 8 | #include "base/files/scoped_temp_dir.h" |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 9 | #include "sql/database.h" |
[email protected] | f0a54b2 | 2011-07-19 18:40:21 | [diff] [blame] | 10 | #include "sql/statement.h" |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 11 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | e33cba4 | 2010-08-18 23:37:03 | [diff] [blame] | 12 | #include "third_party/sqlite/sqlite3.h" |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 13 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 14 | namespace sql { |
Victor Costan | 525b30a | 2021-05-05 17:54:10 | [diff] [blame] | 15 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 16 | namespace { |
| 17 | |
| 18 | class SQLTransactionTest : public testing::Test { |
| 19 | public: |
| 20 | ~SQLTransactionTest() override = default; |
| 21 | |
| 22 | void SetUp() override { |
| 23 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 24 | ASSERT_TRUE( |
| 25 | db_.Open(temp_dir_.GetPath().AppendASCII("transaction_test.sqlite"))); |
| 26 | |
| 27 | ASSERT_TRUE(db_.Execute("CREATE TABLE foo (a, b)")); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 28 | } |
| 29 | |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 30 | // Returns the number of rows in table "foo". |
| 31 | int CountFoo() { |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 32 | Statement count(db_.GetUniqueStatement("SELECT count(*) FROM foo")); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 33 | count.Step(); |
| 34 | return count.ColumnInt(0); |
| 35 | } |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 36 | |
| 37 | protected: |
| 38 | base::ScopedTempDir temp_dir_; |
| 39 | Database db_; |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 40 | }; |
| 41 | |
| 42 | TEST_F(SQLTransactionTest, Commit) { |
| 43 | { |
Victor Costan | 628afca | 2021-09-21 02:36:09 | [diff] [blame] | 44 | Transaction transaction(&db_); |
| 45 | EXPECT_FALSE(db_.HasActiveTransactions()); |
| 46 | EXPECT_FALSE(transaction.IsActiveForTesting()); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 47 | |
Victor Costan | 628afca | 2021-09-21 02:36:09 | [diff] [blame] | 48 | ASSERT_TRUE(transaction.Begin()); |
| 49 | EXPECT_TRUE(db_.HasActiveTransactions()); |
| 50 | EXPECT_TRUE(transaction.IsActiveForTesting()); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 51 | |
Victor Costan | 628afca | 2021-09-21 02:36:09 | [diff] [blame] | 52 | ASSERT_TRUE(db_.Execute("INSERT INTO foo (a, b) VALUES (1, 2)")); |
| 53 | ASSERT_EQ(1, CountFoo()) << "INSERT did not work as intended"; |
| 54 | |
| 55 | transaction.Commit(); |
| 56 | EXPECT_FALSE(db_.HasActiveTransactions()); |
| 57 | EXPECT_FALSE(transaction.IsActiveForTesting()); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 58 | } |
| 59 | |
Victor Costan | 628afca | 2021-09-21 02:36:09 | [diff] [blame] | 60 | EXPECT_FALSE(db_.HasActiveTransactions()); |
| 61 | EXPECT_EQ(1, CountFoo()) << "Transaction changes not committed"; |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 62 | } |
| 63 | |
Victor Costan | 628afca | 2021-09-21 02:36:09 | [diff] [blame] | 64 | TEST_F(SQLTransactionTest, RollbackOnDestruction) { |
| 65 | EXPECT_FALSE(db_.HasActiveTransactions()); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 66 | |
Victor Costan | 628afca | 2021-09-21 02:36:09 | [diff] [blame] | 67 | { |
| 68 | Transaction transaction(&db_); |
| 69 | EXPECT_FALSE(db_.HasActiveTransactions()); |
| 70 | EXPECT_FALSE(transaction.IsActiveForTesting()); |
| 71 | |
| 72 | ASSERT_TRUE(transaction.Begin()); |
| 73 | EXPECT_TRUE(db_.HasActiveTransactions()); |
| 74 | EXPECT_TRUE(transaction.IsActiveForTesting()); |
| 75 | |
| 76 | ASSERT_TRUE(db_.Execute("INSERT INTO foo (a, b) VALUES (1, 2)")); |
| 77 | ASSERT_EQ(1, CountFoo()) << "INSERT did not work as intended"; |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 78 | } |
| 79 | |
Victor Costan | 628afca | 2021-09-21 02:36:09 | [diff] [blame] | 80 | EXPECT_FALSE(db_.HasActiveTransactions()); |
| 81 | EXPECT_EQ(0, CountFoo()) << "Transaction changes not rolled back"; |
| 82 | } |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 83 | |
Victor Costan | 628afca | 2021-09-21 02:36:09 | [diff] [blame] | 84 | TEST_F(SQLTransactionTest, ExplicitRollback) { |
| 85 | EXPECT_FALSE(db_.HasActiveTransactions()); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 86 | |
Victor Costan | 628afca | 2021-09-21 02:36:09 | [diff] [blame] | 87 | { |
| 88 | Transaction transaction(&db_); |
| 89 | EXPECT_FALSE(db_.HasActiveTransactions()); |
| 90 | EXPECT_FALSE(transaction.IsActiveForTesting()); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 91 | |
Victor Costan | 628afca | 2021-09-21 02:36:09 | [diff] [blame] | 92 | ASSERT_TRUE(transaction.Begin()); |
| 93 | EXPECT_TRUE(db_.HasActiveTransactions()); |
| 94 | EXPECT_TRUE(transaction.IsActiveForTesting()); |
| 95 | |
| 96 | ASSERT_TRUE(db_.Execute("INSERT INTO foo (a, b) VALUES (1, 2)")); |
| 97 | ASSERT_EQ(1, CountFoo()) << "INSERT did not work as intended"; |
| 98 | |
| 99 | transaction.Rollback(); |
| 100 | EXPECT_FALSE(db_.HasActiveTransactions()); |
| 101 | EXPECT_FALSE(transaction.IsActiveForTesting()); |
| 102 | EXPECT_EQ(0, CountFoo()) << "Transaction changes not rolled back"; |
| 103 | } |
| 104 | |
| 105 | EXPECT_FALSE(db_.HasActiveTransactions()); |
| 106 | EXPECT_EQ(0, CountFoo()) << "Transaction changes not rolled back"; |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | // Rolling back any part of a transaction should roll back all of them. |
| 110 | TEST_F(SQLTransactionTest, NestedRollback) { |
Victor Costan | 628afca | 2021-09-21 02:36:09 | [diff] [blame] | 111 | EXPECT_FALSE(db_.HasActiveTransactions()); |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 112 | EXPECT_EQ(0, db_.transaction_nesting()); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 113 | |
| 114 | // Outermost transaction. |
| 115 | { |
Victor Costan | 628afca | 2021-09-21 02:36:09 | [diff] [blame] | 116 | Transaction outer_txn(&db_); |
| 117 | EXPECT_FALSE(db_.HasActiveTransactions()); |
| 118 | EXPECT_EQ(0, db_.transaction_nesting()); |
| 119 | |
| 120 | ASSERT_TRUE(outer_txn.Begin()); |
| 121 | EXPECT_TRUE(db_.HasActiveTransactions()); |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 122 | EXPECT_EQ(1, db_.transaction_nesting()); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 123 | |
Victor Costan | 628afca | 2021-09-21 02:36:09 | [diff] [blame] | 124 | // First inner transaction is committed. |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 125 | { |
Victor Costan | 628afca | 2021-09-21 02:36:09 | [diff] [blame] | 126 | Transaction committed_inner_txn(&db_); |
| 127 | EXPECT_TRUE(db_.HasActiveTransactions()); |
| 128 | EXPECT_EQ(1, db_.transaction_nesting()); |
| 129 | |
| 130 | ASSERT_TRUE(committed_inner_txn.Begin()); |
| 131 | EXPECT_TRUE(db_.HasActiveTransactions()); |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 132 | EXPECT_EQ(2, db_.transaction_nesting()); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 133 | |
Victor Costan | 628afca | 2021-09-21 02:36:09 | [diff] [blame] | 134 | ASSERT_TRUE(db_.Execute("INSERT INTO foo (a, b) VALUES (1, 2)")); |
| 135 | ASSERT_EQ(1, CountFoo()) << "INSERT did not work as intended"; |
| 136 | |
| 137 | committed_inner_txn.Commit(); |
| 138 | EXPECT_TRUE(db_.HasActiveTransactions()); |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 139 | EXPECT_EQ(1, db_.transaction_nesting()); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 140 | } |
| 141 | |
Victor Costan | 628afca | 2021-09-21 02:36:09 | [diff] [blame] | 142 | EXPECT_TRUE(db_.HasActiveTransactions()); |
| 143 | EXPECT_EQ(1, db_.transaction_nesting()); |
| 144 | EXPECT_EQ(1, CountFoo()) << "First inner transaction did not commit"; |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 145 | |
Victor Costan | 628afca | 2021-09-21 02:36:09 | [diff] [blame] | 146 | // Second inner transaction is rolled back. |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 147 | { |
Victor Costan | 628afca | 2021-09-21 02:36:09 | [diff] [blame] | 148 | Transaction rolled_back_inner_txn(&db_); |
| 149 | EXPECT_TRUE(db_.HasActiveTransactions()); |
| 150 | EXPECT_EQ(1, db_.transaction_nesting()); |
| 151 | |
| 152 | ASSERT_TRUE(rolled_back_inner_txn.Begin()); |
| 153 | EXPECT_TRUE(db_.HasActiveTransactions()); |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 154 | EXPECT_EQ(2, db_.transaction_nesting()); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 155 | |
Victor Costan | 628afca | 2021-09-21 02:36:09 | [diff] [blame] | 156 | ASSERT_TRUE(db_.Execute("INSERT INTO foo (a, b) VALUES (2, 3)")); |
| 157 | ASSERT_EQ(2, CountFoo()) << "INSERT did not work as intended"; |
| 158 | |
| 159 | rolled_back_inner_txn.Rollback(); |
| 160 | EXPECT_TRUE(db_.HasActiveTransactions()); |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 161 | EXPECT_EQ(1, db_.transaction_nesting()); |
Victor Costan | 628afca | 2021-09-21 02:36:09 | [diff] [blame] | 162 | EXPECT_EQ(2, CountFoo()) |
| 163 | << "Nested transaction rollback deferred to top-level transaction"; |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 164 | } |
| 165 | |
Victor Costan | 628afca | 2021-09-21 02:36:09 | [diff] [blame] | 166 | EXPECT_TRUE(db_.HasActiveTransactions()); |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 167 | EXPECT_EQ(1, db_.transaction_nesting()); |
Victor Costan | 628afca | 2021-09-21 02:36:09 | [diff] [blame] | 168 | EXPECT_EQ(2, CountFoo()) |
| 169 | << "Nested transaction rollback deferred to top-level transaction"; |
| 170 | |
| 171 | // Third inner transaction fails in Begin(), because a nested transaction |
| 172 | // has already been rolled back. |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 173 | { |
Victor Costan | 628afca | 2021-09-21 02:36:09 | [diff] [blame] | 174 | Transaction failed_inner_txn(&db_); |
| 175 | EXPECT_TRUE(db_.HasActiveTransactions()); |
| 176 | EXPECT_EQ(1, db_.transaction_nesting()); |
| 177 | |
| 178 | EXPECT_FALSE(failed_inner_txn.Begin()); |
| 179 | EXPECT_TRUE(db_.HasActiveTransactions()); |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 180 | EXPECT_EQ(1, db_.transaction_nesting()); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 181 | } |
| 182 | } |
Victor Costan | 628afca | 2021-09-21 02:36:09 | [diff] [blame] | 183 | |
| 184 | EXPECT_FALSE(db_.HasActiveTransactions()); |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 185 | EXPECT_EQ(0, db_.transaction_nesting()); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 186 | EXPECT_EQ(0, CountFoo()); |
| 187 | } |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 188 | |
| 189 | } // namespace |
| 190 | |
| 191 | } // namespace sql |