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

Side by Side Diff: sql/connection_unittest.cc

Issue 19281002: [sql] Scoped recovery framework. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 5 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
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/bind.h" 5 #include "base/bind.h"
6 #include "base/file_util.h" 6 #include "base/file_util.h"
7 #include "base/files/scoped_temp_dir.h" 7 #include "base/files/scoped_temp_dir.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "sql/connection.h" 9 #include "sql/connection.h"
10 #include "sql/meta_table.h" 10 #include "sql/meta_table.h"
(...skipping 644 matching lines...) Expand 10 before | Expand all | Expand 10 after
655 // of journal_mode PERSIST. 655 // of journal_mode PERSIST.
656 base::FilePath journal(db_path().value() + FILE_PATH_LITERAL("-journal")); 656 base::FilePath journal(db_path().value() + FILE_PATH_LITERAL("-journal"));
657 ASSERT_TRUE(base::PathExists(db_path())); 657 ASSERT_TRUE(base::PathExists(db_path()));
658 ASSERT_TRUE(base::PathExists(journal)); 658 ASSERT_TRUE(base::PathExists(journal));
659 659
660 sql::Connection::Delete(db_path()); 660 sql::Connection::Delete(db_path());
661 EXPECT_FALSE(base::PathExists(db_path())); 661 EXPECT_FALSE(base::PathExists(db_path()));
662 EXPECT_FALSE(base::PathExists(journal)); 662 EXPECT_FALSE(base::PathExists(journal));
663 } 663 }
664 664
665 // Test that errors start happening once Poison() is called.
666 TEST_F(SQLConnectionTest, Poison) {
667 EXPECT_TRUE(db().Execute("CREATE TABLE x (x)"));
668
669 // Before the Poison() call, things generally work.
670 EXPECT_TRUE(db().IsSQLValid("INSERT INTO x VALUES ('x')"));
671 EXPECT_TRUE(db().Execute("INSERT INTO x VALUES ('x')"));
672 {
673 sql::Statement s(db().GetUniqueStatement("SELECT COUNT(*) FROM x"));
674 ASSERT_TRUE(s.is_valid());
675 ASSERT_TRUE(s.Step());
676 }
677
678 // Get a statement which is valid before and will exist across Poison().
679 sql::Statement valid_statement(
680 db().GetUniqueStatement("SELECT COUNT(*) FROM sqlite_master"));
681 ASSERT_TRUE(valid_statement.is_valid());
682 ASSERT_TRUE(valid_statement.Step());
683 valid_statement.Reset(true);
684
685 db().Poison();
686
687 // After the Poison() call, things fail.
688 EXPECT_FALSE(db().IsSQLValid("INSERT INTO x VALUES ('x')"));
689 EXPECT_FALSE(db().Execute("INSERT INTO x VALUES ('x')"));
690 {
691 sql::Statement s(db().GetUniqueStatement("SELECT COUNT(*) FROM x"));
692 ASSERT_FALSE(s.is_valid());
693 ASSERT_FALSE(s.Step());
694 }
695
696 // The existing statement has become invalid.
697 ASSERT_FALSE(valid_statement.is_valid());
698 ASSERT_FALSE(valid_statement.Step());
699 }
700
701 // Test attaching and detaching databases from the connection.
702 TEST_F(SQLConnectionTest, Attach) {
703 EXPECT_TRUE(db().Execute("CREATE TABLE foo (a, b)"));
704
705 // Create a database to attach to.
706 base::FilePath attach_path =
707 db_path().DirName().AppendASCII("SQLConnectionAttach.db");
708 const char kAttachmentPoint[] = "other";
709 {
710 sql::Connection other_db;
711 ASSERT_TRUE(other_db.Open(attach_path));
712 EXPECT_TRUE(other_db.Execute("CREATE TABLE bar (a, b)"));
713 EXPECT_TRUE(other_db.Execute("INSERT INTO bar VALUES ('hello', 'world')"));
714 }
715
716 // Cannot see the attached database, yet.
717 EXPECT_FALSE(db().IsSQLValid("SELECT count(*) from other.bar"));
718
719 // Attach fails in a transaction.
720 EXPECT_TRUE(db().BeginTransaction());
721 {
722 sql::ScopedErrorIgnorer ignore_errors;
723 ignore_errors.IgnoreError(SQLITE_ERROR);
724 EXPECT_FALSE(db().AttachDatabase(attach_path, kAttachmentPoint));
725 ASSERT_TRUE(ignore_errors.CheckIgnoredErrors());
726 }
727
728 // Attach succeeds when the transaction is closed.
729 db().RollbackTransaction();
730 EXPECT_TRUE(db().AttachDatabase(attach_path, kAttachmentPoint));
731 EXPECT_TRUE(db().IsSQLValid("SELECT count(*) from other.bar"));
732
733 // Queries can touch both databases.
734 EXPECT_TRUE(db().Execute("INSERT INTO foo SELECT a, b FROM other.bar"));
735 {
736 sql::Statement s(db().GetUniqueStatement("SELECT COUNT(*) FROM foo"));
737 ASSERT_TRUE(s.Step());
738 EXPECT_EQ(1, s.ColumnInt(0));
739 }
740
741 // Detach also fails in a transaction.
742 EXPECT_TRUE(db().BeginTransaction());
743 {
744 sql::ScopedErrorIgnorer ignore_errors;
745 ignore_errors.IgnoreError(SQLITE_ERROR);
746 EXPECT_FALSE(db().DetachDatabase(kAttachmentPoint));
747 EXPECT_TRUE(db().IsSQLValid("SELECT count(*) from other.bar"));
748 ASSERT_TRUE(ignore_errors.CheckIgnoredErrors());
749 }
750
751 // Detach succeeds outside of a transaction.
752 db().RollbackTransaction();
753 EXPECT_TRUE(db().DetachDatabase(kAttachmentPoint));
754
755 EXPECT_FALSE(db().IsSQLValid("SELECT count(*) from other.bar"));
756 }
757
665 } // namespace 758 } // namespace
OLDNEW
« sql/connection.h ('K') | « sql/connection.cc ('k') | sql/recovery.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698