blob: e50043fa940e731b9fe9dde503f5757cc60a2642 [file] [log] [blame]
[email protected]44ad7d902012-03-23 00:09:051// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]e5ffd0e42009-09-11 21:30:562// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]e5ffd0e42009-09-11 21:30:565#include "base/file_util.h"
[email protected]e0785902011-05-19 23:34:176#include "base/scoped_temp_dir.h"
[email protected]f0a54b22011-07-19 18:40:217#include "sql/connection.h"
8#include "sql/statement.h"
[email protected]1348765a2012-07-24 08:25:539#include "sql/meta_table.h"
[email protected]e5ffd0e42009-09-11 21:30:5610#include "testing/gtest/include/gtest/gtest.h"
[email protected]e33cba42010-08-18 23:37:0311#include "third_party/sqlite/sqlite3.h"
[email protected]e5ffd0e42009-09-11 21:30:5612
13class SQLConnectionTest : public testing::Test {
14 public:
15 SQLConnectionTest() {}
16
17 void SetUp() {
[email protected]3a305db2011-04-12 13:40:5318 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
[email protected]8e0c01282012-04-06 19:36:4919 ASSERT_TRUE(db_.Open(db_path()));
[email protected]e5ffd0e42009-09-11 21:30:5620 }
21
22 void TearDown() {
23 db_.Close();
[email protected]e5ffd0e42009-09-11 21:30:5624 }
25
26 sql::Connection& db() { return db_; }
27
[email protected]8e0c01282012-04-06 19:36:4928 FilePath db_path() {
29 return temp_dir_.path().AppendASCII("SQLConnectionTest.db");
30 }
31
[email protected]e5ffd0e42009-09-11 21:30:5632 private:
[email protected]3a305db2011-04-12 13:40:5333 ScopedTempDir temp_dir_;
[email protected]e5ffd0e42009-09-11 21:30:5634 sql::Connection db_;
35};
36
37TEST_F(SQLConnectionTest, Execute) {
38 // Valid statement should return true.
39 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)"));
40 EXPECT_EQ(SQLITE_OK, db().GetErrorCode());
41
42 // Invalid statement should fail.
[email protected]eff1fa522011-12-12 23:50:5943 ASSERT_EQ(SQLITE_ERROR,
44 db().ExecuteAndReturnErrorCode("CREATE TAB foo (a, b"));
[email protected]e5ffd0e42009-09-11 21:30:5645 EXPECT_EQ(SQLITE_ERROR, db().GetErrorCode());
46}
47
[email protected]eff1fa522011-12-12 23:50:5948TEST_F(SQLConnectionTest, ExecuteWithErrorCode) {
49 ASSERT_EQ(SQLITE_OK,
50 db().ExecuteAndReturnErrorCode("CREATE TABLE foo (a, b)"));
51 ASSERT_EQ(SQLITE_ERROR,
52 db().ExecuteAndReturnErrorCode("CREATE TABLE TABLE"));
53 ASSERT_EQ(SQLITE_ERROR,
54 db().ExecuteAndReturnErrorCode(
55 "INSERT INTO foo(a, b) VALUES (1, 2, 3, 4)"));
56}
57
[email protected]e5ffd0e42009-09-11 21:30:5658TEST_F(SQLConnectionTest, CachedStatement) {
59 sql::StatementID id1("foo", 12);
60
61 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)"));
62 ASSERT_TRUE(db().Execute("INSERT INTO foo(a, b) VALUES (12, 13)"));
63
64 // Create a new cached statement.
65 {
66 sql::Statement s(db().GetCachedStatement(id1, "SELECT a FROM foo"));
[email protected]eff1fa522011-12-12 23:50:5967 ASSERT_TRUE(s.is_valid());
[email protected]e5ffd0e42009-09-11 21:30:5668
69 ASSERT_TRUE(s.Step());
70 EXPECT_EQ(12, s.ColumnInt(0));
71 }
72
73 // The statement should be cached still.
74 EXPECT_TRUE(db().HasCachedStatement(id1));
75
76 {
77 // Get the same statement using different SQL. This should ignore our
78 // SQL and use the cached one (so it will be valid).
79 sql::Statement s(db().GetCachedStatement(id1, "something invalid("));
[email protected]eff1fa522011-12-12 23:50:5980 ASSERT_TRUE(s.is_valid());
[email protected]e5ffd0e42009-09-11 21:30:5681
82 ASSERT_TRUE(s.Step());
83 EXPECT_EQ(12, s.ColumnInt(0));
84 }
85
86 // Make sure other statements aren't marked as cached.
87 EXPECT_FALSE(db().HasCachedStatement(SQL_FROM_HERE));
88}
89
[email protected]eff1fa522011-12-12 23:50:5990TEST_F(SQLConnectionTest, IsSQLValidTest) {
91 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)"));
92 ASSERT_TRUE(db().IsSQLValid("SELECT a FROM foo"));
93 ASSERT_FALSE(db().IsSQLValid("SELECT no_exist FROM foo"));
94}
95
[email protected]e5ffd0e42009-09-11 21:30:5696TEST_F(SQLConnectionTest, DoesStuffExist) {
97 // Test DoesTableExist.
98 EXPECT_FALSE(db().DoesTableExist("foo"));
99 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)"));
100 EXPECT_TRUE(db().DoesTableExist("foo"));
101
102 // Should be case sensitive.
103 EXPECT_FALSE(db().DoesTableExist("FOO"));
104
105 // Test DoesColumnExist.
106 EXPECT_FALSE(db().DoesColumnExist("foo", "bar"));
107 EXPECT_TRUE(db().DoesColumnExist("foo", "a"));
108
[email protected]e7afe2452010-08-22 16:19:13109 // Testing for a column on a nonexistent table.
[email protected]e5ffd0e42009-09-11 21:30:56110 EXPECT_FALSE(db().DoesColumnExist("bar", "b"));
111}
112
113TEST_F(SQLConnectionTest, GetLastInsertRowId) {
114 ASSERT_TRUE(db().Execute("CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"));
115
116 ASSERT_TRUE(db().Execute("INSERT INTO foo (value) VALUES (12)"));
117
118 // Last insert row ID should be valid.
119 int64 row = db().GetLastInsertRowId();
120 EXPECT_LT(0, row);
121
122 // It should be the primary key of the row we just inserted.
123 sql::Statement s(db().GetUniqueStatement("SELECT value FROM foo WHERE id=?"));
124 s.BindInt64(0, row);
125 ASSERT_TRUE(s.Step());
126 EXPECT_EQ(12, s.ColumnInt(0));
127}
[email protected]44ad7d902012-03-23 00:09:05128
129TEST_F(SQLConnectionTest, Rollback) {
130 ASSERT_TRUE(db().BeginTransaction());
131 ASSERT_TRUE(db().BeginTransaction());
132 EXPECT_EQ(2, db().transaction_nesting());
133 db().RollbackTransaction();
134 EXPECT_FALSE(db().CommitTransaction());
135 EXPECT_TRUE(db().BeginTransaction());
136}
[email protected]8e0c01282012-04-06 19:36:49137
138// Test that sql::Connection::Raze() results in a database without the
139// tables from the original database.
140TEST_F(SQLConnectionTest, Raze) {
141 const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)";
142 ASSERT_TRUE(db().Execute(kCreateSql));
143 ASSERT_TRUE(db().Execute("INSERT INTO foo (value) VALUES (12)"));
144
[email protected]69c58452012-08-06 19:22:42145 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
[email protected]8e0c01282012-04-06 19:36:49156 {
157 sql::Statement s(db().GetUniqueStatement("PRAGMA page_count"));
158 ASSERT_TRUE(s.Step());
[email protected]69c58452012-08-06 19:22:42159 EXPECT_EQ(kExpectedPageCount, s.ColumnInt(0));
[email protected]8e0c01282012-04-06 19:36:49160 }
161
162 {
163 sql::Statement s(db().GetUniqueStatement("SELECT * FROM sqlite_master"));
164 ASSERT_TRUE(s.Step());
165 EXPECT_EQ("table", s.ColumnString(0));
166 EXPECT_EQ("foo", s.ColumnString(1));
167 EXPECT_EQ("foo", s.ColumnString(2));
[email protected]69c58452012-08-06 19:22:42168 // Table "foo" is stored in the last page of the file.
169 EXPECT_EQ(kExpectedPageCount, s.ColumnInt(3));
[email protected]8e0c01282012-04-06 19:36:49170 EXPECT_EQ(kCreateSql, s.ColumnString(4));
171 }
172
173 ASSERT_TRUE(db().Raze());
174
175 {
176 sql::Statement s(db().GetUniqueStatement("PRAGMA page_count"));
177 ASSERT_TRUE(s.Step());
178 EXPECT_EQ(1, s.ColumnInt(0));
179 }
180
181 {
182 sql::Statement s(db().GetUniqueStatement("SELECT * FROM sqlite_master"));
183 ASSERT_FALSE(s.Step());
184 }
[email protected]69c58452012-08-06 19:22:42185
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 }
[email protected]8e0c01282012-04-06 19:36:49192}
193
194// Test that Raze() maintains page_size.
195TEST_F(SQLConnectionTest, RazePageSize) {
[email protected]e73e89222012-07-13 18:55:22196 // Fetch the default page size and double it for use in this test.
[email protected]8e0c01282012-04-06 19:36:49197 // Scoped to release statement before Close().
[email protected]e73e89222012-07-13 18:55:22198 int default_page_size = 0;
[email protected]8e0c01282012-04-06 19:36:49199 {
200 sql::Statement s(db().GetUniqueStatement("PRAGMA page_size"));
201 ASSERT_TRUE(s.Step());
[email protected]e73e89222012-07-13 18:55:22202 default_page_size = s.ColumnInt(0);
[email protected]8e0c01282012-04-06 19:36:49203 }
[email protected]e73e89222012-07-13 18:55:22204 ASSERT_GT(default_page_size, 0);
205 const int kPageSize = 2 * default_page_size;
[email protected]8e0c01282012-04-06 19:36:49206
207 // Re-open the database to allow setting the page size.
208 db().Close();
209 db().set_page_size(kPageSize);
210 ASSERT_TRUE(db().Open(db_path()));
211
212 // page_size should match the indicated value.
213 sql::Statement s(db().GetUniqueStatement("PRAGMA page_size"));
214 ASSERT_TRUE(s.Step());
215 ASSERT_EQ(kPageSize, s.ColumnInt(0));
216
217 // After raze, page_size should still match the indicated value.
218 ASSERT_TRUE(db().Raze());
[email protected]389e0a42012-04-25 21:36:41219 s.Reset(true);
[email protected]8e0c01282012-04-06 19:36:49220 ASSERT_TRUE(s.Step());
221 ASSERT_EQ(kPageSize, s.ColumnInt(0));
222}
223
224// Test that Raze() results are seen in other connections.
225TEST_F(SQLConnectionTest, RazeMultiple) {
226 const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)";
227 ASSERT_TRUE(db().Execute(kCreateSql));
228
229 sql::Connection other_db;
230 ASSERT_TRUE(other_db.Open(db_path()));
231
232 // Check that the second connection sees the table.
233 const char *kTablesQuery = "SELECT COUNT(*) FROM sqlite_master";
234 sql::Statement s(other_db.GetUniqueStatement(kTablesQuery));
235 ASSERT_TRUE(s.Step());
236 ASSERT_EQ(1, s.ColumnInt(0));
237 ASSERT_FALSE(s.Step()); // Releases the shared lock.
238
239 ASSERT_TRUE(db().Raze());
240
241 // The second connection sees the updated database.
[email protected]389e0a42012-04-25 21:36:41242 s.Reset(true);
[email protected]8e0c01282012-04-06 19:36:49243 ASSERT_TRUE(s.Step());
244 ASSERT_EQ(0, s.ColumnInt(0));
245}
246
247TEST_F(SQLConnectionTest, RazeLocked) {
248 const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)";
249 ASSERT_TRUE(db().Execute(kCreateSql));
250
251 // Open a transaction and write some data in a second connection.
252 // This will acquire a PENDING or EXCLUSIVE transaction, which will
253 // cause the raze to fail.
254 sql::Connection other_db;
255 ASSERT_TRUE(other_db.Open(db_path()));
256 ASSERT_TRUE(other_db.BeginTransaction());
257 const char* kInsertSql = "INSERT INTO foo VALUES (1, 'data')";
258 ASSERT_TRUE(other_db.Execute(kInsertSql));
259
260 ASSERT_FALSE(db().Raze());
261
262 // Works after COMMIT.
263 ASSERT_TRUE(other_db.CommitTransaction());
264 ASSERT_TRUE(db().Raze());
265
266 // Re-create the database.
267 ASSERT_TRUE(db().Execute(kCreateSql));
268 ASSERT_TRUE(db().Execute(kInsertSql));
269
270 // An unfinished read transaction in the other connection also
271 // blocks raze.
272 const char *kQuery = "SELECT COUNT(*) FROM foo";
273 sql::Statement s(other_db.GetUniqueStatement(kQuery));
274 ASSERT_TRUE(s.Step());
275 ASSERT_FALSE(db().Raze());
276
277 // Complete the statement unlocks the database.
278 ASSERT_FALSE(s.Step());
279 ASSERT_TRUE(db().Raze());
280}
281
[email protected]1348765a2012-07-24 08:25:53282#if defined(OS_ANDROID)
283TEST_F(SQLConnectionTest, SetTempDirForSQL) {
284
285 sql::MetaTable meta_table;
286 // Below call needs a temporary directory in sqlite3
287 // On Android, it can pass only when the temporary directory is set.
288 // Otherwise, sqlite3 doesn't find the correct directory to store
289 // temporary files and will report the error 'unable to open
290 // database file'.
291 ASSERT_TRUE(meta_table.Init(&db(), 4, 4));
292}
293#endif
294
[email protected]8e0c01282012-04-06 19:36:49295// TODO(shess): Spin up a background thread to hold other_db, to more
296// closely match real life. That would also allow testing
297// RazeWithTimeout().