OLD | NEW |
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 <string> | 5 #include <string> |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
10 #include "base/files/scoped_temp_dir.h" | 10 #include "base/files/scoped_temp_dir.h" |
11 #include "base/path_service.h" | 11 #include "base/path_service.h" |
12 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
13 #include "sql/connection.h" | 13 #include "sql/connection.h" |
14 #include "sql/meta_table.h" | 14 #include "sql/meta_table.h" |
15 #include "sql/recovery.h" | 15 #include "sql/recovery.h" |
16 #include "sql/statement.h" | 16 #include "sql/statement.h" |
17 #include "sql/test/paths.h" | 17 #include "sql/test/paths.h" |
18 #include "sql/test/scoped_error_ignorer.h" | 18 #include "sql/test/scoped_error_ignorer.h" |
| 19 #include "sql/test/sql_test_base.h" |
19 #include "sql/test/test_helpers.h" | 20 #include "sql/test/test_helpers.h" |
20 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
21 #include "third_party/sqlite/sqlite3.h" | 22 #include "third_party/sqlite/sqlite3.h" |
22 | 23 |
23 namespace { | 24 namespace { |
24 | 25 |
25 // Execute |sql|, and stringify the results with |column_sep| between | 26 // Execute |sql|, and stringify the results with |column_sep| between |
26 // columns and |row_sep| between rows. | 27 // columns and |row_sep| between rows. |
27 // TODO(shess): Promote this to a central testing helper. | 28 // TODO(shess): Promote this to a central testing helper. |
28 std::string ExecuteWithResults(sql::Connection* db, | 29 std::string ExecuteWithResults(sql::Connection* db, |
(...skipping 25 matching lines...) Expand all Loading... |
54 // Dump consistent human-readable representation of the database | 55 // Dump consistent human-readable representation of the database |
55 // schema. For tables or indices, this will contain the sql command | 56 // schema. For tables or indices, this will contain the sql command |
56 // to create the table or index. For certain automatic SQLite | 57 // to create the table or index. For certain automatic SQLite |
57 // structures with no sql, the name is used. | 58 // structures with no sql, the name is used. |
58 std::string GetSchema(sql::Connection* db) { | 59 std::string GetSchema(sql::Connection* db) { |
59 const char kSql[] = | 60 const char kSql[] = |
60 "SELECT COALESCE(sql, name) FROM sqlite_master ORDER BY 1"; | 61 "SELECT COALESCE(sql, name) FROM sqlite_master ORDER BY 1"; |
61 return ExecuteWithResults(db, kSql, "|", "\n"); | 62 return ExecuteWithResults(db, kSql, "|", "\n"); |
62 } | 63 } |
63 | 64 |
64 class SQLRecoveryTest : public testing::Test { | 65 using SQLRecoveryTest = sql::SQLTestBase; |
65 public: | |
66 SQLRecoveryTest() {} | |
67 | |
68 void SetUp() override { | |
69 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | |
70 ASSERT_TRUE(db_.Open(db_path())); | |
71 } | |
72 | |
73 void TearDown() override { db_.Close(); } | |
74 | |
75 sql::Connection& db() { return db_; } | |
76 | |
77 base::FilePath db_path() { | |
78 return temp_dir_.path().AppendASCII("SQLRecoveryTest.db"); | |
79 } | |
80 | |
81 bool Reopen() { | |
82 db_.Close(); | |
83 return db_.Open(db_path()); | |
84 } | |
85 | |
86 private: | |
87 base::ScopedTempDir temp_dir_; | |
88 sql::Connection db_; | |
89 }; | |
90 | 66 |
91 TEST_F(SQLRecoveryTest, RecoverBasic) { | 67 TEST_F(SQLRecoveryTest, RecoverBasic) { |
92 const char kCreateSql[] = "CREATE TABLE x (t TEXT)"; | 68 const char kCreateSql[] = "CREATE TABLE x (t TEXT)"; |
93 const char kInsertSql[] = "INSERT INTO x VALUES ('This is a test')"; | 69 const char kInsertSql[] = "INSERT INTO x VALUES ('This is a test')"; |
94 ASSERT_TRUE(db().Execute(kCreateSql)); | 70 ASSERT_TRUE(db().Execute(kCreateSql)); |
95 ASSERT_TRUE(db().Execute(kInsertSql)); | 71 ASSERT_TRUE(db().Execute(kInsertSql)); |
96 ASSERT_EQ("CREATE TABLE x (t TEXT)", GetSchema(&db())); | 72 ASSERT_EQ("CREATE TABLE x (t TEXT)", GetSchema(&db())); |
97 | 73 |
98 // If the Recovery handle goes out of scope without being | 74 // If the Recovery handle goes out of scope without being |
99 // Recovered(), the database is razed. | 75 // Recovered(), the database is razed. |
(...skipping 610 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
710 EXPECT_TRUE(recovery->AutoRecoverTable("x", 0, &rows)); | 686 EXPECT_TRUE(recovery->AutoRecoverTable("x", 0, &rows)); |
711 EXPECT_EQ(43u, rows); | 687 EXPECT_EQ(43u, rows); |
712 | 688 |
713 // Successfully recovered. | 689 // Successfully recovered. |
714 EXPECT_TRUE(sql::Recovery::Recovered(recovery.Pass())); | 690 EXPECT_TRUE(sql::Recovery::Recovered(recovery.Pass())); |
715 } | 691 } |
716 } | 692 } |
717 #endif // !defined(USE_SYSTEM_SQLITE) | 693 #endif // !defined(USE_SYSTEM_SQLITE) |
718 | 694 |
719 } // namespace | 695 } // namespace |
OLD | NEW |