blob: bcc05f5815b9b236b3c8e09309f76abc9e9e24a0 [file] [log] [blame]
[email protected]3a305db2011-04-12 13:40:531// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]e5ffd0e42009-09-11 21:30:562// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Scott Graham47ed2c32017-09-15 02:17:075#include "sql/transaction.h"
thestig22dfc4012014-09-05 08:29:446#include "base/files/file_util.h"
[email protected]ea1a3f62012-11-16 20:34:237#include "base/files/scoped_temp_dir.h"
Victor Costancfbfa602018-08-01 23:24:468#include "sql/database.h"
[email protected]f0a54b22011-07-19 18:40:219#include "sql/statement.h"
Scott Graham47ed2c32017-09-15 02:17:0710#include "sql/test/sql_test_base.h"
[email protected]e5ffd0e42009-09-11 21:30:5611#include "testing/gtest/include/gtest/gtest.h"
[email protected]e33cba42010-08-18 23:37:0312#include "third_party/sqlite/sqlite3.h"
[email protected]e5ffd0e42009-09-11 21:30:5613
erg102ceb412015-06-20 01:38:1314class SQLTransactionTest : public sql::SQLTestBase {
[email protected]e5ffd0e42009-09-11 21:30:5615 public:
dcheng1b3b125e2014-12-22 23:00:2416 void SetUp() override {
erg102ceb412015-06-20 01:38:1317 SQLTestBase::SetUp();
[email protected]e5ffd0e42009-09-11 21:30:5618
19 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)"));
20 }
21
[email protected]e5ffd0e42009-09-11 21:30:5622 // Returns the number of rows in table "foo".
23 int CountFoo() {
24 sql::Statement count(db().GetUniqueStatement("SELECT count(*) FROM foo"));
25 count.Step();
26 return count.ColumnInt(0);
27 }
[email protected]e5ffd0e42009-09-11 21:30:5628};
29
30TEST_F(SQLTransactionTest, Commit) {
31 {
32 sql::Transaction t(&db());
33 EXPECT_FALSE(t.is_open());
34 EXPECT_TRUE(t.Begin());
35 EXPECT_TRUE(t.is_open());
36
37 EXPECT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (1, 2)"));
38
39 t.Commit();
40 EXPECT_FALSE(t.is_open());
41 }
42
43 EXPECT_EQ(1, CountFoo());
44}
45
46TEST_F(SQLTransactionTest, Rollback) {
47 // Test some basic initialization, and that rollback runs when you exit the
48 // scope.
49 {
50 sql::Transaction t(&db());
51 EXPECT_FALSE(t.is_open());
52 EXPECT_TRUE(t.Begin());
53 EXPECT_TRUE(t.is_open());
54
55 EXPECT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (1, 2)"));
56 }
57
58 // Nothing should have been committed since it was implicitly rolled back.
59 EXPECT_EQ(0, CountFoo());
60
61 // Test explicit rollback.
62 sql::Transaction t2(&db());
63 EXPECT_FALSE(t2.is_open());
64 EXPECT_TRUE(t2.Begin());
65
66 EXPECT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (1, 2)"));
67 t2.Rollback();
68 EXPECT_FALSE(t2.is_open());
69
70 // Nothing should have been committed since it was explicitly rolled back.
71 EXPECT_EQ(0, CountFoo());
72}
73
74// Rolling back any part of a transaction should roll back all of them.
75TEST_F(SQLTransactionTest, NestedRollback) {
76 EXPECT_EQ(0, db().transaction_nesting());
77
78 // Outermost transaction.
79 {
80 sql::Transaction outer(&db());
81 EXPECT_TRUE(outer.Begin());
82 EXPECT_EQ(1, db().transaction_nesting());
83
84 // The first inner one gets committed.
85 {
86 sql::Transaction inner1(&db());
87 EXPECT_TRUE(inner1.Begin());
88 EXPECT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (1, 2)"));
89 EXPECT_EQ(2, db().transaction_nesting());
90
91 inner1.Commit();
92 EXPECT_EQ(1, db().transaction_nesting());
93 }
94
95 // One row should have gotten inserted.
96 EXPECT_EQ(1, CountFoo());
97
98 // The second inner one gets rolled back.
99 {
100 sql::Transaction inner2(&db());
101 EXPECT_TRUE(inner2.Begin());
102 EXPECT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (1, 2)"));
103 EXPECT_EQ(2, db().transaction_nesting());
104
105 inner2.Rollback();
106 EXPECT_EQ(1, db().transaction_nesting());
107 }
108
109 // A third inner one will fail in Begin since one has already been rolled
110 // back.
111 EXPECT_EQ(1, db().transaction_nesting());
112 {
113 sql::Transaction inner3(&db());
114 EXPECT_FALSE(inner3.Begin());
115 EXPECT_EQ(1, db().transaction_nesting());
116 }
117 }
118 EXPECT_EQ(0, db().transaction_nesting());
119 EXPECT_EQ(0, CountFoo());
120}