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

Side by Side Diff: sql/recovery_unittest.cc

Issue 1832173002: [sql] Database recovery system for Shortcuts. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: iOS SQLite doesn't support column names in view definition. Created 4 years, 9 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
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 "sql/recovery.h" 5 #include "sql/recovery.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <string> 8 #include <string>
9 #include <utility> 9 #include <utility>
10 10
(...skipping 729 matching lines...) Expand 10 before | Expand all | Expand 10 after
740 TEST_F(SQLRecoveryTest, NoMmap) { 740 TEST_F(SQLRecoveryTest, NoMmap) {
741 scoped_ptr<sql::Recovery> recovery = sql::Recovery::Begin(&db(), db_path()); 741 scoped_ptr<sql::Recovery> recovery = sql::Recovery::Begin(&db(), db_path());
742 ASSERT_TRUE(recovery.get()); 742 ASSERT_TRUE(recovery.get());
743 743
744 // In the current implementation, the PRAGMA successfully runs with no result 744 // In the current implementation, the PRAGMA successfully runs with no result
745 // rows. Running with a single result of |0| is also acceptable. 745 // rows. Running with a single result of |0| is also acceptable.
746 sql::Statement s(recovery->db()->GetUniqueStatement("PRAGMA mmap_size")); 746 sql::Statement s(recovery->db()->GetUniqueStatement("PRAGMA mmap_size"));
747 EXPECT_TRUE(!s.Step() || !s.ColumnInt64(0)); 747 EXPECT_TRUE(!s.Step() || !s.ColumnInt64(0));
748 } 748 }
749 749
750 TEST_F(SQLRecoveryTest, RecoverDatabaseOrRaze) {
751 // This table needs sqlite_sequence to work.
Mark P 2016/04/08 20:12:06 ... because of the autoincrement.
Scott Hess - ex-Googler 2016/04/15 00:38:16 Done.
752 ASSERT_TRUE(db().Execute(
753 "CREATE TABLE a (id INTEGER PRIMARY KEY AUTOINCREMENT, v TEXT)"));
Mark P 2016/04/08 20:12:06 Everywhere else in this file uses tables x and y.
Scott Hess - ex-Googler 2016/04/15 00:38:15 Eh, it was just [a]utoincrement and [i]ndex and [v
754 EXPECT_TRUE(db().Execute("INSERT INTO a (v) VALUES ('turtle')"));
755 EXPECT_TRUE(db().Execute("INSERT INTO a (v) VALUES ('truck')"));
756 EXPECT_TRUE(db().Execute("INSERT INTO a (v) VALUES ('trailer')"));
757
758 // This table needs index and a unique index to work.
759 ASSERT_TRUE(db().Execute("CREATE TABLE i (name TEXT, v TEXT)"));
760 ASSERT_TRUE(db().Execute("CREATE UNIQUE INDEX i_name ON i(name)"));
761 ASSERT_TRUE(db().Execute("CREATE INDEX i_v ON i(v)"));
762 EXPECT_TRUE(db().Execute("INSERT INTO i VALUES ('jim', 'telephone')"));
763 EXPECT_TRUE(db().Execute("INSERT INTO i VALUES ('bob', 'truck')"));
764 EXPECT_TRUE(db().Execute("INSERT INTO i VALUES ('dean', 'trailer')"));
765
766 // View which is the intersection of [a.v] and [i.v].
767 ASSERT_TRUE(db().Execute(
768 "CREATE VIEW v AS SELECT a.v FROM a, i WHERE a.v = i.v"));
769
770 // When an element is deleted from [a], trigger a delete on [i]. Between the
771 // BEGIN and END, [old] stands for the deleted rows from [a].
772 ASSERT_TRUE(db().Execute("CREATE TRIGGER t AFTER DELETE ON a "
773 "BEGIN DELETE FROM i WHERE i.v = old.v; END"));
774
775 // Save aside a copy of the original schema and data.
776 const std::string orig_schema(GetSchema(&db()));
777 const char kASql[] = "SELECT * FROM a ORDER BY 1";
778 const char kISql[] = "SELECT * FROM i ORDER BY 1";
779 const std::string orig_data_a(ExecuteWithResults(&db(), kASql, "|", "\n"));
780 const std::string orig_data_i(ExecuteWithResults(&db(), kISql, "|", "\n"));
781
Mark P 2016/04/08 20:12:07 I think this blank line is unnecessary.
Scott Hess - ex-Googler 2016/04/15 00:38:15 Done.
782 const char kVSql[] = "SELECT * FROM v ORDER BY 1";
783 const std::string orig_data_v(ExecuteWithResults(&db(), kVSql, "|", "\n"));
784
Mark P 2016/04/08 20:12:06 ditto
Scott Hess - ex-Googler 2016/04/15 00:38:15 Done.
785 const char kTrivialSql[] = "SELECT COUNT(*) FROM sqlite_master";
786 EXPECT_TRUE(db().IsSQLValid(kTrivialSql));
Mark P 2016/04/08 20:12:06 Comment such as // Verify the database handle is v
Scott Hess - ex-Googler 2016/04/15 00:38:16 Done.
Scott Hess - ex-Googler 2016/04/15 00:38:16 Done.
787
788 sql::Recovery::RecoverDatabaseOrRaze(&db(), db_path());
789
790 // Database handle has been poisoned.
791 EXPECT_FALSE(db().IsSQLValid(kTrivialSql));
792
793 // Since the database was not corrupt, the entire schema and all
794 // data should be recovered.
795 ASSERT_TRUE(Reopen());
Mark P 2016/04/08 20:12:07 This block of tests makes me nervous because you'r
Scott Hess - ex-Googler 2016/04/15 00:38:15 Done. In testing the schema, it seemed like a fai
796 ASSERT_EQ(orig_schema, GetSchema(&db()));
797 EXPECT_EQ(orig_data_a, ExecuteWithResults(&db(), kASql, "|", "\n"));
798 EXPECT_EQ(orig_data_i, ExecuteWithResults(&db(), kISql, "|", "\n"));
799 EXPECT_EQ(orig_data_v, ExecuteWithResults(&db(), kVSql, "|", "\n"));
800
801 // Test that the view works.
Mark P 2016/04/08 20:12:07 I don't understand this block. I don't see how th
Scott Hess - ex-Googler 2016/04/15 00:38:15 It's possible that the block is dumb. It's gone n
802 const char kAWithoutSql[] = "SELECT * FROM a WHERE v<>'truck' ORDER BY 1";
803 const char kIWithoutSql[] = "SELECT * FROM i WHERE v<>'truck' ORDER BY 1";
804 const std::string orig_data_a_without(
805 ExecuteWithResults(&db(), kAWithoutSql, "|", "\n"));
806 const std::string orig_data_i_without(
807 ExecuteWithResults(&db(), kIWithoutSql, "|", "\n"));
808
809 // Test that the trigger works.
810 ASSERT_TRUE(db().Execute("DELETE FROM a WHERE v = 'truck'"));
811 EXPECT_EQ(orig_data_a_without,
812 ExecuteWithResults(&db(), kAWithoutSql, "|", "\n"));
Mark P 2016/04/08 20:12:07 Shouldn't this be kASql, i.e., you're checking tha
Scott Hess - ex-Googler 2016/04/15 00:38:15 Sorry, the above capture from the view _is_ the sa
813 EXPECT_EQ(orig_data_i_without,
814 ExecuteWithResults(&db(), kIWithoutSql, "|", "\n"));
815 }
816
750 } // namespace 817 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698