[Telemetry] Add SurfaceFlingerStats to timeline_based_measurement
[chromium-blink-merge.git] / sql / transaction_unittest.cc
blob83d41259b2400380e98587266dc80a26eb1a8c94
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/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 {
14 public:
15 void SetUp() override {
16 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
17 ASSERT_TRUE(db_.Open(
18 temp_dir_.path().AppendASCII("SQLTransactionTest.db")));
20 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)"));
23 void TearDown() override { db_.Close(); }
25 sql::Connection& db() { return db_; }
27 // Returns the number of rows in table "foo".
28 int CountFoo() {
29 sql::Statement count(db().GetUniqueStatement("SELECT count(*) FROM foo"));
30 count.Step();
31 return count.ColumnInt(0);
34 private:
35 base::ScopedTempDir temp_dir_;
36 sql::Connection db_;
39 TEST_F(SQLTransactionTest, Commit) {
41 sql::Transaction t(&db());
42 EXPECT_FALSE(t.is_open());
43 EXPECT_TRUE(t.Begin());
44 EXPECT_TRUE(t.is_open());
46 EXPECT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (1, 2)"));
48 t.Commit();
49 EXPECT_FALSE(t.is_open());
52 EXPECT_EQ(1, CountFoo());
55 TEST_F(SQLTransactionTest, Rollback) {
56 // Test some basic initialization, and that rollback runs when you exit the
57 // scope.
59 sql::Transaction t(&db());
60 EXPECT_FALSE(t.is_open());
61 EXPECT_TRUE(t.Begin());
62 EXPECT_TRUE(t.is_open());
64 EXPECT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (1, 2)"));
67 // Nothing should have been committed since it was implicitly rolled back.
68 EXPECT_EQ(0, CountFoo());
70 // Test explicit rollback.
71 sql::Transaction t2(&db());
72 EXPECT_FALSE(t2.is_open());
73 EXPECT_TRUE(t2.Begin());
75 EXPECT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (1, 2)"));
76 t2.Rollback();
77 EXPECT_FALSE(t2.is_open());
79 // Nothing should have been committed since it was explicitly rolled back.
80 EXPECT_EQ(0, CountFoo());
83 // Rolling back any part of a transaction should roll back all of them.
84 TEST_F(SQLTransactionTest, NestedRollback) {
85 EXPECT_EQ(0, db().transaction_nesting());
87 // Outermost transaction.
89 sql::Transaction outer(&db());
90 EXPECT_TRUE(outer.Begin());
91 EXPECT_EQ(1, db().transaction_nesting());
93 // The first inner one gets committed.
95 sql::Transaction inner1(&db());
96 EXPECT_TRUE(inner1.Begin());
97 EXPECT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (1, 2)"));
98 EXPECT_EQ(2, db().transaction_nesting());
100 inner1.Commit();
101 EXPECT_EQ(1, db().transaction_nesting());
104 // One row should have gotten inserted.
105 EXPECT_EQ(1, CountFoo());
107 // The second inner one gets rolled back.
109 sql::Transaction inner2(&db());
110 EXPECT_TRUE(inner2.Begin());
111 EXPECT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (1, 2)"));
112 EXPECT_EQ(2, db().transaction_nesting());
114 inner2.Rollback();
115 EXPECT_EQ(1, db().transaction_nesting());
118 // A third inner one will fail in Begin since one has already been rolled
119 // back.
120 EXPECT_EQ(1, db().transaction_nesting());
122 sql::Transaction inner3(&db());
123 EXPECT_FALSE(inner3.Begin());
124 EXPECT_EQ(1, db().transaction_nesting());
127 EXPECT_EQ(0, db().transaction_nesting());
128 EXPECT_EQ(0, CountFoo());