Make UsbEventRouter depend explicitly on EventRouter.
[chromium-blink-merge.git] / sql / transaction_unittest.cc
blob179adcf3e55316a49ffb0d72d52a17c9c8021e8e
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/files/file_util.h"
6 #include "base/files/scoped_temp_dir.h"
7 #include "sql/connection.h"
8 #include "sql/correct_sql_test_base.h"
9 #include "sql/statement.h"
10 #include "sql/transaction.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "third_party/sqlite/sqlite3.h"
14 class SQLTransactionTest : public sql::SQLTestBase {
15 public:
16 void SetUp() override {
17 SQLTestBase::SetUp();
19 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)"));
22 // 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);
30 TEST_F(SQLTransactionTest, Commit) {
32 sql::Transaction t(&db());
33 EXPECT_FALSE(t.is_open());
34 EXPECT_TRUE(t.Begin());
35 EXPECT_TRUE(t.is_open());
37 EXPECT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (1, 2)"));
39 t.Commit();
40 EXPECT_FALSE(t.is_open());
43 EXPECT_EQ(1, CountFoo());
46 TEST_F(SQLTransactionTest, Rollback) {
47 // Test some basic initialization, and that rollback runs when you exit the
48 // scope.
50 sql::Transaction t(&db());
51 EXPECT_FALSE(t.is_open());
52 EXPECT_TRUE(t.Begin());
53 EXPECT_TRUE(t.is_open());
55 EXPECT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (1, 2)"));
58 // Nothing should have been committed since it was implicitly rolled back.
59 EXPECT_EQ(0, CountFoo());
61 // Test explicit rollback.
62 sql::Transaction t2(&db());
63 EXPECT_FALSE(t2.is_open());
64 EXPECT_TRUE(t2.Begin());
66 EXPECT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (1, 2)"));
67 t2.Rollback();
68 EXPECT_FALSE(t2.is_open());
70 // Nothing should have been committed since it was explicitly rolled back.
71 EXPECT_EQ(0, CountFoo());
74 // Rolling back any part of a transaction should roll back all of them.
75 TEST_F(SQLTransactionTest, NestedRollback) {
76 EXPECT_EQ(0, db().transaction_nesting());
78 // Outermost transaction.
80 sql::Transaction outer(&db());
81 EXPECT_TRUE(outer.Begin());
82 EXPECT_EQ(1, db().transaction_nesting());
84 // The first inner one gets committed.
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());
91 inner1.Commit();
92 EXPECT_EQ(1, db().transaction_nesting());
95 // One row should have gotten inserted.
96 EXPECT_EQ(1, CountFoo());
98 // The second inner one gets rolled back.
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());
105 inner2.Rollback();
106 EXPECT_EQ(1, db().transaction_nesting());
109 // A third inner one will fail in Begin since one has already been rolled
110 // back.
111 EXPECT_EQ(1, db().transaction_nesting());
113 sql::Transaction inner3(&db());
114 EXPECT_FALSE(inner3.Begin());
115 EXPECT_EQ(1, db().transaction_nesting());
118 EXPECT_EQ(0, db().transaction_nesting());
119 EXPECT_EQ(0, CountFoo());