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/scoped_temp_dir.h" | 6 #include "base/scoped_temp_dir.h" |
7 #include "sql/connection.h" | 7 #include "sql/connection.h" |
8 #include "sql/statement.h" | 8 #include "sql/statement.h" |
9 #include "sql/meta_table.h" | 9 #include "sql/meta_table.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
135 EXPECT_TRUE(db().BeginTransaction()); | 135 EXPECT_TRUE(db().BeginTransaction()); |
136 } | 136 } |
137 | 137 |
138 // Test that sql::Connection::Raze() results in a database without the | 138 // Test that sql::Connection::Raze() results in a database without the |
139 // tables from the original database. | 139 // tables from the original database. |
140 TEST_F(SQLConnectionTest, Raze) { | 140 TEST_F(SQLConnectionTest, Raze) { |
141 const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; | 141 const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; |
142 ASSERT_TRUE(db().Execute(kCreateSql)); | 142 ASSERT_TRUE(db().Execute(kCreateSql)); |
143 ASSERT_TRUE(db().Execute("INSERT INTO foo (value) VALUES (12)")); | 143 ASSERT_TRUE(db().Execute("INSERT INTO foo (value) VALUES (12)")); |
144 | 144 |
| 145 int pragma_auto_vacuum = 0; |
| 146 { |
| 147 sql::Statement s(db().GetUniqueStatement("PRAGMA auto_vacuum")); |
| 148 ASSERT_TRUE(s.Step()); |
| 149 pragma_auto_vacuum = s.ColumnInt(0); |
| 150 ASSERT_TRUE(pragma_auto_vacuum == 0 || pragma_auto_vacuum == 1); |
| 151 } |
| 152 |
| 153 // If auto_vacuum is set, there's an extra page to maintain a freelist. |
| 154 const int kExpectedPageCount = 2 + pragma_auto_vacuum; |
| 155 |
145 { | 156 { |
146 sql::Statement s(db().GetUniqueStatement("PRAGMA page_count")); | 157 sql::Statement s(db().GetUniqueStatement("PRAGMA page_count")); |
147 ASSERT_TRUE(s.Step()); | 158 ASSERT_TRUE(s.Step()); |
148 EXPECT_EQ(2, s.ColumnInt(0)); | 159 EXPECT_EQ(kExpectedPageCount, s.ColumnInt(0)); |
149 } | 160 } |
150 | 161 |
151 { | 162 { |
152 sql::Statement s(db().GetUniqueStatement("SELECT * FROM sqlite_master")); | 163 sql::Statement s(db().GetUniqueStatement("SELECT * FROM sqlite_master")); |
153 ASSERT_TRUE(s.Step()); | 164 ASSERT_TRUE(s.Step()); |
154 EXPECT_EQ("table", s.ColumnString(0)); | 165 EXPECT_EQ("table", s.ColumnString(0)); |
155 EXPECT_EQ("foo", s.ColumnString(1)); | 166 EXPECT_EQ("foo", s.ColumnString(1)); |
156 EXPECT_EQ("foo", s.ColumnString(2)); | 167 EXPECT_EQ("foo", s.ColumnString(2)); |
157 EXPECT_EQ(2, s.ColumnInt(3)); | 168 // Table "foo" is stored in the last page of the file. |
| 169 EXPECT_EQ(kExpectedPageCount, s.ColumnInt(3)); |
158 EXPECT_EQ(kCreateSql, s.ColumnString(4)); | 170 EXPECT_EQ(kCreateSql, s.ColumnString(4)); |
159 } | 171 } |
160 | 172 |
161 ASSERT_TRUE(db().Raze()); | 173 ASSERT_TRUE(db().Raze()); |
162 | 174 |
163 { | 175 { |
164 sql::Statement s(db().GetUniqueStatement("PRAGMA page_count")); | 176 sql::Statement s(db().GetUniqueStatement("PRAGMA page_count")); |
165 ASSERT_TRUE(s.Step()); | 177 ASSERT_TRUE(s.Step()); |
166 EXPECT_EQ(1, s.ColumnInt(0)); | 178 EXPECT_EQ(1, s.ColumnInt(0)); |
167 } | 179 } |
168 | 180 |
169 { | 181 { |
170 sql::Statement s(db().GetUniqueStatement("SELECT * FROM sqlite_master")); | 182 sql::Statement s(db().GetUniqueStatement("SELECT * FROM sqlite_master")); |
171 ASSERT_FALSE(s.Step()); | 183 ASSERT_FALSE(s.Step()); |
172 } | 184 } |
| 185 |
| 186 { |
| 187 sql::Statement s(db().GetUniqueStatement("PRAGMA auto_vacuum")); |
| 188 ASSERT_TRUE(s.Step()); |
| 189 // auto_vacuum must be preserved across a Raze. |
| 190 EXPECT_EQ(pragma_auto_vacuum, s.ColumnInt(0)); |
| 191 } |
173 } | 192 } |
174 | 193 |
175 // Test that Raze() maintains page_size. | 194 // Test that Raze() maintains page_size. |
176 TEST_F(SQLConnectionTest, RazePageSize) { | 195 TEST_F(SQLConnectionTest, RazePageSize) { |
177 // Fetch the default page size and double it for use in this test. | 196 // Fetch the default page size and double it for use in this test. |
178 // Scoped to release statement before Close(). | 197 // Scoped to release statement before Close(). |
179 int default_page_size = 0; | 198 int default_page_size = 0; |
180 { | 199 { |
181 sql::Statement s(db().GetUniqueStatement("PRAGMA page_size")); | 200 sql::Statement s(db().GetUniqueStatement("PRAGMA page_size")); |
182 ASSERT_TRUE(s.Step()); | 201 ASSERT_TRUE(s.Step()); |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
269 // Otherwise, sqlite3 doesn't find the correct directory to store | 288 // Otherwise, sqlite3 doesn't find the correct directory to store |
270 // temporary files and will report the error 'unable to open | 289 // temporary files and will report the error 'unable to open |
271 // database file'. | 290 // database file'. |
272 ASSERT_TRUE(meta_table.Init(&db(), 4, 4)); | 291 ASSERT_TRUE(meta_table.Init(&db(), 4, 4)); |
273 } | 292 } |
274 #endif | 293 #endif |
275 | 294 |
276 // TODO(shess): Spin up a background thread to hold other_db, to more | 295 // TODO(shess): Spin up a background thread to hold other_db, to more |
277 // closely match real life. That would also allow testing | 296 // closely match real life. That would also allow testing |
278 // RazeWithTimeout(). | 297 // RazeWithTimeout(). |
OLD | NEW |