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 } | |
Scott Hess - ex-Googler
2012/08/05 15:34:46
2 is a valid auto_vacuum result. I don't know how
Yaron
2012/08/06 17:23:43
Done.
| |
151 | |
152 // If auto_vacuum is set, there's an extra page to maintain a freelist. | |
153 const int kExpectedPageCount = 2 + pragma_auto_vacuum; | |
154 | |
145 { | 155 { |
146 sql::Statement s(db().GetUniqueStatement("PRAGMA page_count")); | 156 sql::Statement s(db().GetUniqueStatement("PRAGMA page_count")); |
147 ASSERT_TRUE(s.Step()); | 157 ASSERT_TRUE(s.Step()); |
148 EXPECT_EQ(2, s.ColumnInt(0)); | 158 EXPECT_EQ(kExpectedPageCount, s.ColumnInt(0)); |
149 } | 159 } |
150 | 160 |
151 { | 161 { |
152 sql::Statement s(db().GetUniqueStatement("SELECT * FROM sqlite_master")); | 162 sql::Statement s(db().GetUniqueStatement("SELECT * FROM sqlite_master")); |
153 ASSERT_TRUE(s.Step()); | 163 ASSERT_TRUE(s.Step()); |
154 EXPECT_EQ("table", s.ColumnString(0)); | 164 EXPECT_EQ("table", s.ColumnString(0)); |
155 EXPECT_EQ("foo", s.ColumnString(1)); | 165 EXPECT_EQ("foo", s.ColumnString(1)); |
156 EXPECT_EQ("foo", s.ColumnString(2)); | 166 EXPECT_EQ("foo", s.ColumnString(2)); |
157 EXPECT_EQ(2, s.ColumnInt(3)); | 167 // Table "foo" is stored in the last page of the file. |
168 EXPECT_EQ(kExpectedPageCount, s.ColumnInt(3)); | |
158 EXPECT_EQ(kCreateSql, s.ColumnString(4)); | 169 EXPECT_EQ(kCreateSql, s.ColumnString(4)); |
159 } | 170 } |
160 | 171 |
161 ASSERT_TRUE(db().Raze()); | 172 ASSERT_TRUE(db().Raze()); |
162 | 173 |
163 { | 174 { |
164 sql::Statement s(db().GetUniqueStatement("PRAGMA page_count")); | 175 sql::Statement s(db().GetUniqueStatement("PRAGMA page_count")); |
165 ASSERT_TRUE(s.Step()); | 176 ASSERT_TRUE(s.Step()); |
166 EXPECT_EQ(1, s.ColumnInt(0)); | 177 EXPECT_EQ(1, s.ColumnInt(0)); |
167 } | 178 } |
168 | 179 |
169 { | 180 { |
170 sql::Statement s(db().GetUniqueStatement("SELECT * FROM sqlite_master")); | 181 sql::Statement s(db().GetUniqueStatement("SELECT * FROM sqlite_master")); |
171 ASSERT_FALSE(s.Step()); | 182 ASSERT_FALSE(s.Step()); |
172 } | 183 } |
184 | |
185 { | |
186 sql::Statement s(db().GetUniqueStatement("PRAGMA auto_vacuum")); | |
187 ASSERT_TRUE(s.Step()); | |
188 // auto_vacuum must be preserved across a Raze. | |
189 EXPECT_EQ(pragma_auto_vacuum, s.ColumnInt(0)); | |
190 } | |
173 } | 191 } |
174 | 192 |
175 // Test that Raze() maintains page_size. | 193 // Test that Raze() maintains page_size. |
176 TEST_F(SQLConnectionTest, RazePageSize) { | 194 TEST_F(SQLConnectionTest, RazePageSize) { |
177 // Fetch the default page size and double it for use in this test. | 195 // Fetch the default page size and double it for use in this test. |
178 // Scoped to release statement before Close(). | 196 // Scoped to release statement before Close(). |
179 int default_page_size = 0; | 197 int default_page_size = 0; |
180 { | 198 { |
181 sql::Statement s(db().GetUniqueStatement("PRAGMA page_size")); | 199 sql::Statement s(db().GetUniqueStatement("PRAGMA page_size")); |
182 ASSERT_TRUE(s.Step()); | 200 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 | 287 // Otherwise, sqlite3 doesn't find the correct directory to store |
270 // temporary files and will report the error 'unable to open | 288 // temporary files and will report the error 'unable to open |
271 // database file'. | 289 // database file'. |
272 ASSERT_TRUE(meta_table.Init(&db(), 4, 4)); | 290 ASSERT_TRUE(meta_table.Init(&db(), 4, 4)); |
273 } | 291 } |
274 #endif | 292 #endif |
275 | 293 |
276 // TODO(shess): Spin up a background thread to hold other_db, to more | 294 // TODO(shess): Spin up a background thread to hold other_db, to more |
277 // closely match real life. That would also allow testing | 295 // closely match real life. That would also allow testing |
278 // RazeWithTimeout(). | 296 // RazeWithTimeout(). |
OLD | NEW |