1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/file_util.h"
6 #include "base/files/scoped_temp_dir.h"
7 #include "sql/connection.h"
8 #include "sql/statement.h"
9 #include "sql/transaction.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "third_party/sqlite/sqlite3.h"
13 class SQLTransactionTest
: public testing::Test
{
15 SQLTransactionTest() {}
18 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
20 temp_dir_
.path().AppendASCII("SQLTransactionTest.db")));
22 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)"));
29 sql::Connection
& db() { return db_
; }
31 // Returns the number of rows in table "foo".
33 sql::Statement
count(db().GetUniqueStatement("SELECT count(*) FROM foo"));
35 return count
.ColumnInt(0);
39 base::ScopedTempDir temp_dir_
;
43 TEST_F(SQLTransactionTest
, Commit
) {
45 sql::Transaction
t(&db());
46 EXPECT_FALSE(t
.is_open());
47 EXPECT_TRUE(t
.Begin());
48 EXPECT_TRUE(t
.is_open());
50 EXPECT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (1, 2)"));
53 EXPECT_FALSE(t
.is_open());
56 EXPECT_EQ(1, CountFoo());
59 TEST_F(SQLTransactionTest
, Rollback
) {
60 // Test some basic initialization, and that rollback runs when you exit the
63 sql::Transaction
t(&db());
64 EXPECT_FALSE(t
.is_open());
65 EXPECT_TRUE(t
.Begin());
66 EXPECT_TRUE(t
.is_open());
68 EXPECT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (1, 2)"));
71 // Nothing should have been committed since it was implicitly rolled back.
72 EXPECT_EQ(0, CountFoo());
74 // Test explicit rollback.
75 sql::Transaction
t2(&db());
76 EXPECT_FALSE(t2
.is_open());
77 EXPECT_TRUE(t2
.Begin());
79 EXPECT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (1, 2)"));
81 EXPECT_FALSE(t2
.is_open());
83 // Nothing should have been committed since it was explicitly rolled back.
84 EXPECT_EQ(0, CountFoo());
87 // Rolling back any part of a transaction should roll back all of them.
88 TEST_F(SQLTransactionTest
, NestedRollback
) {
89 EXPECT_EQ(0, db().transaction_nesting());
91 // Outermost transaction.
93 sql::Transaction
outer(&db());
94 EXPECT_TRUE(outer
.Begin());
95 EXPECT_EQ(1, db().transaction_nesting());
97 // The first inner one gets committed.
99 sql::Transaction
inner1(&db());
100 EXPECT_TRUE(inner1
.Begin());
101 EXPECT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (1, 2)"));
102 EXPECT_EQ(2, db().transaction_nesting());
105 EXPECT_EQ(1, db().transaction_nesting());
108 // One row should have gotten inserted.
109 EXPECT_EQ(1, CountFoo());
111 // The second inner one gets rolled back.
113 sql::Transaction
inner2(&db());
114 EXPECT_TRUE(inner2
.Begin());
115 EXPECT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (1, 2)"));
116 EXPECT_EQ(2, db().transaction_nesting());
119 EXPECT_EQ(1, db().transaction_nesting());
122 // A third inner one will fail in Begin since one has already been rolled
124 EXPECT_EQ(1, db().transaction_nesting());
126 sql::Transaction
inner3(&db());
127 EXPECT_FALSE(inner3
.Begin());
128 EXPECT_EQ(1, db().transaction_nesting());
131 EXPECT_EQ(0, db().transaction_nesting());
132 EXPECT_EQ(0, CountFoo());