OLD | NEW |
---|---|
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "app/sql/connection.h" | 5 #include "app/sql/connection.h" |
6 #include "app/sql/statement.h" | 6 #include "app/sql/statement.h" |
7 #include "app/sql/transaction.h" | 7 #include "app/sql/transaction.h" |
8 #include "base/file_path.h" | |
9 #include "base/file_util.h" | 8 #include "base/file_util.h" |
10 #include "base/path_service.h" | 9 #include "base/memory/scoped_temp_dir.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
12 #include "third_party/sqlite/sqlite3.h" | 11 #include "third_party/sqlite/sqlite3.h" |
13 | 12 |
14 class SQLTransactionTest : public testing::Test { | 13 class SQLTransactionTest : public testing::Test { |
15 public: | 14 public: |
16 SQLTransactionTest() {} | 15 SQLTransactionTest() {} |
17 | 16 |
18 void SetUp() { | 17 void SetUp() { |
19 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &path_)); | 18 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
20 path_ = path_.AppendASCII("SQLStatementTest.db"); | 19 ASSERT_TRUE(db_.Open( |
21 file_util::Delete(path_, false); | 20 temp_dir_.path().AppendASCII("SQLTransactionTest.db"))); |
22 ASSERT_TRUE(db_.Open(path_)); | |
23 | 21 |
24 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)")); | 22 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)")); |
25 } | 23 } |
26 | 24 |
27 void TearDown() { | 25 void TearDown() { |
28 db_.Close(); | 26 db_.Close(); |
29 | 27 |
30 // If this fails something is going on with cleanup and later tests may | 28 // If this fails something is going on with cleanup and later tests may |
Paweł Hajdan Jr.
2011/04/08 15:58:17
Can be removed.
Mike West
2011/04/11 14:47:04
Done.
| |
31 // fail, so we want to identify problems right away. | 29 // fail, so we want to identify problems right away. |
32 ASSERT_TRUE(file_util::Delete(path_, false)); | 30 ASSERT_TRUE(file_util::Delete( |
31 temp_dir_.path().AppendASCII("SQLTransactionTest.db"), false)); | |
33 } | 32 } |
34 | 33 |
35 sql::Connection& db() { return db_; } | 34 sql::Connection& db() { return db_; } |
36 | 35 |
37 // Returns the number of rows in table "foo". | 36 // Returns the number of rows in table "foo". |
38 int CountFoo() { | 37 int CountFoo() { |
39 sql::Statement count(db().GetUniqueStatement("SELECT count(*) FROM foo")); | 38 sql::Statement count(db().GetUniqueStatement("SELECT count(*) FROM foo")); |
40 count.Step(); | 39 count.Step(); |
41 return count.ColumnInt(0); | 40 return count.ColumnInt(0); |
42 } | 41 } |
43 | 42 |
44 private: | 43 private: |
45 FilePath path_; | 44 ScopedTempDir temp_dir_; |
46 sql::Connection db_; | 45 sql::Connection db_; |
47 }; | 46 }; |
48 | 47 |
49 TEST_F(SQLTransactionTest, Commit) { | 48 TEST_F(SQLTransactionTest, Commit) { |
50 { | 49 { |
51 sql::Transaction t(&db()); | 50 sql::Transaction t(&db()); |
52 EXPECT_FALSE(t.is_open()); | 51 EXPECT_FALSE(t.is_open()); |
53 EXPECT_TRUE(t.Begin()); | 52 EXPECT_TRUE(t.Begin()); |
54 EXPECT_TRUE(t.is_open()); | 53 EXPECT_TRUE(t.is_open()); |
55 | 54 |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
130 EXPECT_EQ(1, db().transaction_nesting()); | 129 EXPECT_EQ(1, db().transaction_nesting()); |
131 { | 130 { |
132 sql::Transaction inner3(&db()); | 131 sql::Transaction inner3(&db()); |
133 EXPECT_FALSE(inner3.Begin()); | 132 EXPECT_FALSE(inner3.Begin()); |
134 EXPECT_EQ(1, db().transaction_nesting()); | 133 EXPECT_EQ(1, db().transaction_nesting()); |
135 } | 134 } |
136 } | 135 } |
137 EXPECT_EQ(0, db().transaction_nesting()); | 136 EXPECT_EQ(0, db().transaction_nesting()); |
138 EXPECT_EQ(0, CountFoo()); | 137 EXPECT_EQ(0, CountFoo()); |
139 } | 138 } |
OLD | NEW |