| 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 | |
| 30 // If this fails something is going on with cleanup and later tests may | |
| 31 // fail, so we want to identify problems right away. | |
| 32 ASSERT_TRUE(file_util::Delete(path_, false)); | |
| 33 } | 27 } |
| 34 | 28 |
| 35 sql::Connection& db() { return db_; } | 29 sql::Connection& db() { return db_; } |
| 36 | 30 |
| 37 // Returns the number of rows in table "foo". | 31 // Returns the number of rows in table "foo". |
| 38 int CountFoo() { | 32 int CountFoo() { |
| 39 sql::Statement count(db().GetUniqueStatement("SELECT count(*) FROM foo")); | 33 sql::Statement count(db().GetUniqueStatement("SELECT count(*) FROM foo")); |
| 40 count.Step(); | 34 count.Step(); |
| 41 return count.ColumnInt(0); | 35 return count.ColumnInt(0); |
| 42 } | 36 } |
| 43 | 37 |
| 44 private: | 38 private: |
| 45 FilePath path_; | 39 ScopedTempDir temp_dir_; |
| 46 sql::Connection db_; | 40 sql::Connection db_; |
| 47 }; | 41 }; |
| 48 | 42 |
| 49 TEST_F(SQLTransactionTest, Commit) { | 43 TEST_F(SQLTransactionTest, Commit) { |
| 50 { | 44 { |
| 51 sql::Transaction t(&db()); | 45 sql::Transaction t(&db()); |
| 52 EXPECT_FALSE(t.is_open()); | 46 EXPECT_FALSE(t.is_open()); |
| 53 EXPECT_TRUE(t.Begin()); | 47 EXPECT_TRUE(t.Begin()); |
| 54 EXPECT_TRUE(t.is_open()); | 48 EXPECT_TRUE(t.is_open()); |
| 55 | 49 |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 130 EXPECT_EQ(1, db().transaction_nesting()); | 124 EXPECT_EQ(1, db().transaction_nesting()); |
| 131 { | 125 { |
| 132 sql::Transaction inner3(&db()); | 126 sql::Transaction inner3(&db()); |
| 133 EXPECT_FALSE(inner3.Begin()); | 127 EXPECT_FALSE(inner3.Begin()); |
| 134 EXPECT_EQ(1, db().transaction_nesting()); | 128 EXPECT_EQ(1, db().transaction_nesting()); |
| 135 } | 129 } |
| 136 } | 130 } |
| 137 EXPECT_EQ(0, db().transaction_nesting()); | 131 EXPECT_EQ(0, db().transaction_nesting()); |
| 138 EXPECT_EQ(0, CountFoo()); | 132 EXPECT_EQ(0, CountFoo()); |
| 139 } | 133 } |
| OLD | NEW |