Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(287)

Side by Side Diff: sql/connection_unittest.cc

Issue 12096073: Implement sql::Connection::RazeAndClose(). (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Execute() and untracked changes, unit testsing. Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « sql/connection.cc ('k') | sql/statement.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "sql/connection.h" 8 #include "sql/connection.h"
8 #include "sql/meta_table.h" 9 #include "sql/meta_table.h"
9 #include "sql/statement.h" 10 #include "sql/statement.h"
10 #include "testing/gtest/include/gtest/gtest.h" 11 #include "testing/gtest/include/gtest/gtest.h"
11 #include "third_party/sqlite/sqlite3.h" 12 #include "third_party/sqlite/sqlite3.h"
12 13
13 class SQLConnectionTest : public testing::Test { 14 class SQLConnectionTest : public testing::Test {
14 public: 15 public:
15 SQLConnectionTest() {} 16 SQLConnectionTest() {}
16 17
(...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after
272 const char *kQuery = "SELECT COUNT(*) FROM foo"; 273 const char *kQuery = "SELECT COUNT(*) FROM foo";
273 sql::Statement s(other_db.GetUniqueStatement(kQuery)); 274 sql::Statement s(other_db.GetUniqueStatement(kQuery));
274 ASSERT_TRUE(s.Step()); 275 ASSERT_TRUE(s.Step());
275 ASSERT_FALSE(db().Raze()); 276 ASSERT_FALSE(db().Raze());
276 277
277 // Complete the statement unlocks the database. 278 // Complete the statement unlocks the database.
278 ASSERT_FALSE(s.Step()); 279 ASSERT_FALSE(s.Step());
279 ASSERT_TRUE(db().Raze()); 280 ASSERT_TRUE(db().Raze());
280 } 281 }
281 282
283 // Basic test of RazeAndClose() operation.
284 TEST_F(SQLConnectionTest, RazeAndClose) {
285 const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)";
286 const char* kPopulateSql = "INSERT INTO foo (value) VALUES (12)";
287
288 // Test that RazeAndClose() closes the database, and that the
289 // database is empty when re-opened.
290 ASSERT_TRUE(db().Execute(kCreateSql));
291 ASSERT_TRUE(db().Execute(kPopulateSql));
292 ASSERT_TRUE(db().RazeAndClose());
293 ASSERT_FALSE(db().is_open());
pkotwicz 2013/02/06 17:58:57 Nit: Should you call db().Close() here for the sak
Scott Hess - ex-Googler 2013/02/06 19:19:30 Interesting. This code really should break. The
294 ASSERT_TRUE(db().Open(db_path()));
295 {
296 sql::Statement s(db().GetUniqueStatement("SELECT * FROM sqlite_master"));
297 ASSERT_FALSE(s.Step());
298 }
299
300 // Test that RazeAndClose() can break transactions.
301 ASSERT_TRUE(db().Execute(kCreateSql));
302 ASSERT_TRUE(db().Execute(kPopulateSql));
303 ASSERT_TRUE(db().BeginTransaction());
304 ASSERT_TRUE(db().RazeAndClose());
305 ASSERT_FALSE(db().is_open());
306 ASSERT_FALSE(db().CommitTransaction());
307 db().Close();
308 ASSERT_TRUE(db().Open(db_path()));
309 {
310 sql::Statement s(db().GetUniqueStatement("SELECT * FROM sqlite_master"));
311 ASSERT_FALSE(s.Step());
312 }
313 }
314
315 // Test that various operations fail without crashing after
316 // RazeAndClose().
317 TEST_F(SQLConnectionTest, RazeAndCloseDiagnostics) {
318 const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)";
319 const char* kPopulateSql = "INSERT INTO foo (value) VALUES (12)";
320 const char* kSimpleSql = "SELECT 1";
321
322 ASSERT_TRUE(db().Execute(kCreateSql));
323 ASSERT_TRUE(db().Execute(kPopulateSql));
324
325 // Test baseline expectations.
326 db().Preload();
327 ASSERT_TRUE(db().DoesTableExist("foo"));
328 ASSERT_TRUE(db().IsSQLValid(kSimpleSql));
329 ASSERT_EQ(SQLITE_OK, db().ExecuteAndReturnErrorCode(kSimpleSql));
330 ASSERT_TRUE(db().Execute(kSimpleSql));
331 ASSERT_TRUE(db().is_open());
332 {
333 sql::Statement s(db().GetUniqueStatement(kSimpleSql));
334 ASSERT_TRUE(s.Step());
335 }
336 {
337 sql::Statement s(db().GetCachedStatement(SQL_FROM_HERE, kSimpleSql));
338 ASSERT_TRUE(s.Step());
339 }
340 ASSERT_TRUE(db().BeginTransaction());
341 ASSERT_TRUE(db().CommitTransaction());
342 ASSERT_TRUE(db().BeginTransaction());
343 db().RollbackTransaction();
344
345 ASSERT_TRUE(db().RazeAndClose());
346
347 // At this point, they should all fail, but not crash.
348 db().Preload();
349 ASSERT_FALSE(db().DoesTableExist("foo"));
350 ASSERT_FALSE(db().IsSQLValid(kSimpleSql));
351 ASSERT_EQ(SQLITE_ERROR, db().ExecuteAndReturnErrorCode(kSimpleSql));
352 ASSERT_FALSE(db().Execute(kSimpleSql));
353 ASSERT_FALSE(db().is_open());
354 {
355 sql::Statement s(db().GetUniqueStatement(kSimpleSql));
356 ASSERT_FALSE(s.Step());
357 }
358 {
359 sql::Statement s(db().GetCachedStatement(SQL_FROM_HERE, kSimpleSql));
360 ASSERT_FALSE(s.Step());
361 }
362 ASSERT_FALSE(db().BeginTransaction());
363 ASSERT_FALSE(db().CommitTransaction());
364 ASSERT_FALSE(db().BeginTransaction());
365 db().RollbackTransaction();
366
367 // Close normally to reset the poisoned flag.
368 db().Close();
369
370 // Once the real Close() has been called, various calls enforce API
371 // usage by becoming fatal in debug mode. Since DEATH tests are
372 // expensive, just test one of them.
373 if (DLOG_IS_ON(FATAL)) {
374 ASSERT_DEATH({
375 db().IsSQLValid(kSimpleSql);
376 }, "Illegal use of connection without a db");
377 }
378 }
379
380 // TODO(shess): Spin up a background thread to hold other_db, to more
381 // closely match real life. That would also allow testing
382 // RazeWithTimeout().
383
282 #if defined(OS_ANDROID) 384 #if defined(OS_ANDROID)
283 TEST_F(SQLConnectionTest, SetTempDirForSQL) { 385 TEST_F(SQLConnectionTest, SetTempDirForSQL) {
284 386
285 sql::MetaTable meta_table; 387 sql::MetaTable meta_table;
286 // Below call needs a temporary directory in sqlite3 388 // Below call needs a temporary directory in sqlite3
287 // On Android, it can pass only when the temporary directory is set. 389 // On Android, it can pass only when the temporary directory is set.
288 // Otherwise, sqlite3 doesn't find the correct directory to store 390 // Otherwise, sqlite3 doesn't find the correct directory to store
289 // temporary files and will report the error 'unable to open 391 // temporary files and will report the error 'unable to open
290 // database file'. 392 // database file'.
291 ASSERT_TRUE(meta_table.Init(&db(), 4, 4)); 393 ASSERT_TRUE(meta_table.Init(&db(), 4, 4));
292 } 394 }
293 #endif 395 #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().
OLDNEW
« no previous file with comments | « sql/connection.cc ('k') | sql/statement.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698