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/bind.h" |
5 #include "base/file_util.h" | 6 #include "base/file_util.h" |
6 #include "base/files/scoped_temp_dir.h" | 7 #include "base/files/scoped_temp_dir.h" |
7 #include "base/logging.h" | 8 #include "base/logging.h" |
8 #include "sql/connection.h" | 9 #include "sql/connection.h" |
9 #include "sql/meta_table.h" | 10 #include "sql/meta_table.h" |
10 #include "sql/statement.h" | 11 #include "sql/statement.h" |
| 12 #include "sql/test/error_callback_support.h" |
11 #include "sql/test/scoped_error_ignorer.h" | 13 #include "sql/test/scoped_error_ignorer.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
13 #include "third_party/sqlite/sqlite3.h" | 15 #include "third_party/sqlite/sqlite3.h" |
14 | 16 |
| 17 namespace { |
| 18 |
15 class SQLConnectionTest : public testing::Test { | 19 class SQLConnectionTest : public testing::Test { |
16 public: | 20 public: |
17 SQLConnectionTest() {} | 21 SQLConnectionTest() {} |
18 | 22 |
19 virtual void SetUp() { | 23 virtual void SetUp() { |
20 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 24 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
21 ASSERT_TRUE(db_.Open(db_path())); | 25 ASSERT_TRUE(db_.Open(db_path())); |
22 } | 26 } |
23 | 27 |
24 virtual void TearDown() { | 28 virtual void TearDown() { |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
143 const char* kCreateSql = "CREATE TABLE foo (id INTEGER UNIQUE)"; | 147 const char* kCreateSql = "CREATE TABLE foo (id INTEGER UNIQUE)"; |
144 ASSERT_TRUE(db().Execute(kCreateSql)); | 148 ASSERT_TRUE(db().Execute(kCreateSql)); |
145 ASSERT_TRUE(db().Execute("INSERT INTO foo (id) VALUES (12)")); | 149 ASSERT_TRUE(db().Execute("INSERT INTO foo (id) VALUES (12)")); |
146 | 150 |
147 sql::ScopedErrorIgnorer ignore_errors; | 151 sql::ScopedErrorIgnorer ignore_errors; |
148 ignore_errors.IgnoreError(SQLITE_CONSTRAINT); | 152 ignore_errors.IgnoreError(SQLITE_CONSTRAINT); |
149 ASSERT_FALSE(db().Execute("INSERT INTO foo (id) VALUES (12)")); | 153 ASSERT_FALSE(db().Execute("INSERT INTO foo (id) VALUES (12)")); |
150 ASSERT_TRUE(ignore_errors.CheckIgnoredErrors()); | 154 ASSERT_TRUE(ignore_errors.CheckIgnoredErrors()); |
151 } | 155 } |
152 | 156 |
| 157 TEST_F(SQLConnectionTest, ErrorCallback) { |
| 158 const char* kCreateSql = "CREATE TABLE foo (id INTEGER UNIQUE)"; |
| 159 ASSERT_TRUE(db().Execute(kCreateSql)); |
| 160 ASSERT_TRUE(db().Execute("INSERT INTO foo (id) VALUES (12)")); |
| 161 |
| 162 int error = SQLITE_OK; |
| 163 sql::ScopedErrorCallback sec( |
| 164 &db(), base::Bind(&sql::CaptureErrorCallback, &error)); |
| 165 |
| 166 // Inserting something other than a number into the primary key |
| 167 // should result in SQLITE_MISMATCH. |
| 168 EXPECT_FALSE(db().Execute("INSERT INTO foo (id) VALUES (12)")); |
| 169 EXPECT_EQ(SQLITE_CONSTRAINT, error); |
| 170 } |
| 171 |
153 // Test that sql::Connection::Raze() results in a database without the | 172 // Test that sql::Connection::Raze() results in a database without the |
154 // tables from the original database. | 173 // tables from the original database. |
155 TEST_F(SQLConnectionTest, Raze) { | 174 TEST_F(SQLConnectionTest, Raze) { |
156 const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; | 175 const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; |
157 ASSERT_TRUE(db().Execute(kCreateSql)); | 176 ASSERT_TRUE(db().Execute(kCreateSql)); |
158 ASSERT_TRUE(db().Execute("INSERT INTO foo (value) VALUES (12)")); | 177 ASSERT_TRUE(db().Execute("INSERT INTO foo (value) VALUES (12)")); |
159 | 178 |
160 int pragma_auto_vacuum = 0; | 179 int pragma_auto_vacuum = 0; |
161 { | 180 { |
162 sql::Statement s(db().GetUniqueStatement("PRAGMA auto_vacuum")); | 181 sql::Statement s(db().GetUniqueStatement("PRAGMA auto_vacuum")); |
(...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
419 // Should have both a main database file and a journal file because | 438 // Should have both a main database file and a journal file because |
420 // of journal_mode PERSIST. | 439 // of journal_mode PERSIST. |
421 base::FilePath journal(db_path().value() + FILE_PATH_LITERAL("-journal")); | 440 base::FilePath journal(db_path().value() + FILE_PATH_LITERAL("-journal")); |
422 ASSERT_TRUE(file_util::PathExists(db_path())); | 441 ASSERT_TRUE(file_util::PathExists(db_path())); |
423 ASSERT_TRUE(file_util::PathExists(journal)); | 442 ASSERT_TRUE(file_util::PathExists(journal)); |
424 | 443 |
425 sql::Connection::Delete(db_path()); | 444 sql::Connection::Delete(db_path()); |
426 EXPECT_FALSE(file_util::PathExists(db_path())); | 445 EXPECT_FALSE(file_util::PathExists(db_path())); |
427 EXPECT_FALSE(file_util::PathExists(journal)); | 446 EXPECT_FALSE(file_util::PathExists(journal)); |
428 } | 447 } |
| 448 |
| 449 } // namespace |
OLD | NEW |