| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "base/file_util.h" | 5 #include "base/file_util.h" |
| 6 #include "base/files/scoped_temp_dir.h" | 6 #include "base/files/scoped_temp_dir.h" |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "sql/connection.h" | 8 #include "sql/connection.h" |
| 9 #include "sql/meta_table.h" | 9 #include "sql/meta_table.h" |
| 10 #include "sql/statement.h" | 10 #include "sql/statement.h" |
| 11 #include "sql/test/scoped_error_ignorer.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 12 #include "third_party/sqlite/sqlite3.h" | 13 #include "third_party/sqlite/sqlite3.h" |
| 13 | 14 |
| 14 class SQLConnectionTest : public testing::Test { | 15 class SQLConnectionTest : public testing::Test { |
| 15 public: | 16 public: |
| 16 SQLConnectionTest() {} | 17 SQLConnectionTest() {} |
| 17 | 18 |
| 18 virtual void SetUp() { | 19 virtual void SetUp() { |
| 19 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 20 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 20 ASSERT_TRUE(db_.Open(db_path())); | 21 ASSERT_TRUE(db_.Open(db_path())); |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 129 | 130 |
| 130 TEST_F(SQLConnectionTest, Rollback) { | 131 TEST_F(SQLConnectionTest, Rollback) { |
| 131 ASSERT_TRUE(db().BeginTransaction()); | 132 ASSERT_TRUE(db().BeginTransaction()); |
| 132 ASSERT_TRUE(db().BeginTransaction()); | 133 ASSERT_TRUE(db().BeginTransaction()); |
| 133 EXPECT_EQ(2, db().transaction_nesting()); | 134 EXPECT_EQ(2, db().transaction_nesting()); |
| 134 db().RollbackTransaction(); | 135 db().RollbackTransaction(); |
| 135 EXPECT_FALSE(db().CommitTransaction()); | 136 EXPECT_FALSE(db().CommitTransaction()); |
| 136 EXPECT_TRUE(db().BeginTransaction()); | 137 EXPECT_TRUE(db().BeginTransaction()); |
| 137 } | 138 } |
| 138 | 139 |
| 140 // Test the scoped error ignorer by attempting to insert a duplicate |
| 141 // value into an index. |
| 142 TEST_F(SQLConnectionTest, ScopedIgnoreError) { |
| 143 const char* kCreateSql = "CREATE TABLE foo (id INTEGER UNIQUE)"; |
| 144 ASSERT_TRUE(db().Execute(kCreateSql)); |
| 145 ASSERT_TRUE(db().Execute("INSERT INTO foo (id) VALUES (12)")); |
| 146 |
| 147 sql::ScopedErrorIgnorer ignore_errors; |
| 148 ignore_errors.IgnoreError(SQLITE_CONSTRAINT); |
| 149 ASSERT_FALSE(db().Execute("INSERT INTO foo (id) VALUES (12)")); |
| 150 ASSERT_TRUE(ignore_errors.CheckIgnoredErrors()); |
| 151 } |
| 152 |
| 139 // Test that sql::Connection::Raze() results in a database without the | 153 // Test that sql::Connection::Raze() results in a database without the |
| 140 // tables from the original database. | 154 // tables from the original database. |
| 141 TEST_F(SQLConnectionTest, Raze) { | 155 TEST_F(SQLConnectionTest, Raze) { |
| 142 const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; | 156 const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; |
| 143 ASSERT_TRUE(db().Execute(kCreateSql)); | 157 ASSERT_TRUE(db().Execute(kCreateSql)); |
| 144 ASSERT_TRUE(db().Execute("INSERT INTO foo (value) VALUES (12)")); | 158 ASSERT_TRUE(db().Execute("INSERT INTO foo (value) VALUES (12)")); |
| 145 | 159 |
| 146 int pragma_auto_vacuum = 0; | 160 int pragma_auto_vacuum = 0; |
| 147 { | 161 { |
| 148 sql::Statement s(db().GetUniqueStatement("PRAGMA auto_vacuum")); | 162 sql::Statement s(db().GetUniqueStatement("PRAGMA auto_vacuum")); |
| (...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 390 | 404 |
| 391 sql::MetaTable meta_table; | 405 sql::MetaTable meta_table; |
| 392 // Below call needs a temporary directory in sqlite3 | 406 // Below call needs a temporary directory in sqlite3 |
| 393 // On Android, it can pass only when the temporary directory is set. | 407 // On Android, it can pass only when the temporary directory is set. |
| 394 // Otherwise, sqlite3 doesn't find the correct directory to store | 408 // Otherwise, sqlite3 doesn't find the correct directory to store |
| 395 // temporary files and will report the error 'unable to open | 409 // temporary files and will report the error 'unable to open |
| 396 // database file'. | 410 // database file'. |
| 397 ASSERT_TRUE(meta_table.Init(&db(), 4, 4)); | 411 ASSERT_TRUE(meta_table.Init(&db(), 4, 4)); |
| 398 } | 412 } |
| 399 #endif | 413 #endif |
| OLD | NEW |