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 "sql/connection.h" | 7 #include "sql/connection.h" |
8 #include "sql/meta_table.h" | 8 #include "sql/meta_table.h" |
9 #include "sql/statement.h" | 9 #include "sql/statement.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
(...skipping 261 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
272 const char *kQuery = "SELECT COUNT(*) FROM foo"; | 272 const char *kQuery = "SELECT COUNT(*) FROM foo"; |
273 sql::Statement s(other_db.GetUniqueStatement(kQuery)); | 273 sql::Statement s(other_db.GetUniqueStatement(kQuery)); |
274 ASSERT_TRUE(s.Step()); | 274 ASSERT_TRUE(s.Step()); |
275 ASSERT_FALSE(db().Raze()); | 275 ASSERT_FALSE(db().Raze()); |
276 | 276 |
277 // Complete the statement unlocks the database. | 277 // Complete the statement unlocks the database. |
278 ASSERT_FALSE(s.Step()); | 278 ASSERT_FALSE(s.Step()); |
279 ASSERT_TRUE(db().Raze()); | 279 ASSERT_TRUE(db().Raze()); |
280 } | 280 } |
281 | 281 |
| 282 TEST_F(SQLConnectionTest, RazeAndClose) { |
| 283 const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; |
| 284 const char* kPopulateSql = "INSERT INTO foo (value) VALUES (12)"; |
| 285 |
| 286 // Test that RazeAndClose() closes the database, and that the |
| 287 // database is empty when re-opened. |
| 288 ASSERT_TRUE(db().Execute(kCreateSql)); |
| 289 ASSERT_TRUE(db().Execute(kPopulateSql)); |
| 290 ASSERT_TRUE(db().RazeAndClose()); |
| 291 ASSERT_FALSE(db().is_open()); |
| 292 ASSERT_TRUE(db().Open(db_path())); |
| 293 { |
| 294 sql::Statement s(db().GetUniqueStatement("SELECT * FROM sqlite_master")); |
| 295 ASSERT_FALSE(s.Step()); |
| 296 } |
| 297 |
| 298 // Test that RazeAndClose() can break transactions. |
| 299 ASSERT_TRUE(db().Execute(kCreateSql)); |
| 300 ASSERT_TRUE(db().Execute(kPopulateSql)); |
| 301 ASSERT_TRUE(db().BeginTransaction()); |
| 302 ASSERT_TRUE(db().RazeAndClose()); |
| 303 ASSERT_FALSE(db().is_open()); |
| 304 ASSERT_FALSE(db().CommitTransaction()); |
| 305 ASSERT_TRUE(db().Open(db_path())); |
| 306 { |
| 307 sql::Statement s(db().GetUniqueStatement("SELECT * FROM sqlite_master")); |
| 308 ASSERT_FALSE(s.Step()); |
| 309 } |
| 310 |
| 311 // Test that statements start failed after RazeAndClose(). |
| 312 ASSERT_TRUE(db().Execute(kCreateSql)); |
| 313 ASSERT_TRUE(db().Execute(kPopulateSql)); |
| 314 { |
| 315 sql::Statement s(db().GetUniqueStatement("SELECT * FROM foo")); |
| 316 ASSERT_TRUE(db().RazeAndClose()); |
| 317 ASSERT_FALSE(s.Run()); |
| 318 } |
| 319 ASSERT_FALSE(db().is_open()); |
| 320 ASSERT_TRUE(db().Open(db_path())); |
| 321 { |
| 322 sql::Statement s(db().GetUniqueStatement("SELECT * FROM sqlite_master")); |
| 323 ASSERT_FALSE(s.Step()); |
| 324 } |
| 325 } |
| 326 |
| 327 // TODO(shess): Spin up a background thread to hold other_db, to more |
| 328 // closely match real life. That would also allow testing |
| 329 // RazeWithTimeout(). |
| 330 |
282 #if defined(OS_ANDROID) | 331 #if defined(OS_ANDROID) |
283 TEST_F(SQLConnectionTest, SetTempDirForSQL) { | 332 TEST_F(SQLConnectionTest, SetTempDirForSQL) { |
284 | 333 |
285 sql::MetaTable meta_table; | 334 sql::MetaTable meta_table; |
286 // Below call needs a temporary directory in sqlite3 | 335 // Below call needs a temporary directory in sqlite3 |
287 // On Android, it can pass only when the temporary directory is set. | 336 // On Android, it can pass only when the temporary directory is set. |
288 // Otherwise, sqlite3 doesn't find the correct directory to store | 337 // Otherwise, sqlite3 doesn't find the correct directory to store |
289 // temporary files and will report the error 'unable to open | 338 // temporary files and will report the error 'unable to open |
290 // database file'. | 339 // database file'. |
291 ASSERT_TRUE(meta_table.Init(&db(), 4, 4)); | 340 ASSERT_TRUE(meta_table.Init(&db(), 4, 4)); |
292 } | 341 } |
293 #endif | 342 #endif |
294 | |
295 // TODO(shess): Spin up a background thread to hold other_db, to more | |
296 // closely match real life. That would also allow testing | |
297 // RazeWithTimeout(). | |
OLD | NEW |