[M85 Migration] Add an evas gl option for rotation
[platform/framework/web/chromium-efl.git] / sql / transaction_unittest.cc
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.
4
5 #include "sql/transaction.h"
6 #include "base/files/file_util.h"
7 #include "base/files/scoped_temp_dir.h"
8 #include "sql/database.h"
9 #include "sql/statement.h"
10 #include "sql/test/sql_test_base.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "third_party/sqlite/sqlite3.h"
13
14 class SQLTransactionTest : public sql::SQLTestBase {
15  public:
16   void SetUp() override {
17     SQLTestBase::SetUp();
18
19     ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)"));
20   }
21
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);
27   }
28 };
29
30 TEST_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
46 TEST_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.
75 TEST_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 }