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
3 // found in the LICENSE file.
5 #include "base/file_util.h"
6 #include "base/files/scoped_temp_dir.h"
7 #include "sql/connection.h"
8 #include "sql/meta_table.h"
9 #include "sql/statement.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "third_party/sqlite/sqlite3.h"
13 class SQLConnectionTest
: public testing::Test
{
15 SQLConnectionTest() {}
18 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
19 ASSERT_TRUE(db_
.Open(db_path()));
26 sql::Connection
& db() { return db_
; }
29 return temp_dir_
.path().AppendASCII("SQLConnectionTest.db");
33 base::ScopedTempDir temp_dir_
;
37 TEST_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());
42 // Invalid statement should fail.
43 ASSERT_EQ(SQLITE_ERROR
,
44 db().ExecuteAndReturnErrorCode("CREATE TAB foo (a, b"));
45 EXPECT_EQ(SQLITE_ERROR
, db().GetErrorCode());
48 TEST_F(SQLConnectionTest
, ExecuteWithErrorCode
) {
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)"));
58 TEST_F(SQLConnectionTest
, CachedStatement
) {
59 sql::StatementID
id1("foo", 12);
61 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)"));
62 ASSERT_TRUE(db().Execute("INSERT INTO foo(a, b) VALUES (12, 13)"));
64 // Create a new cached statement.
66 sql::Statement
s(db().GetCachedStatement(id1
, "SELECT a FROM foo"));
67 ASSERT_TRUE(s
.is_valid());
69 ASSERT_TRUE(s
.Step());
70 EXPECT_EQ(12, s
.ColumnInt(0));
73 // The statement should be cached still.
74 EXPECT_TRUE(db().HasCachedStatement(id1
));
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("));
80 ASSERT_TRUE(s
.is_valid());
82 ASSERT_TRUE(s
.Step());
83 EXPECT_EQ(12, s
.ColumnInt(0));
86 // Make sure other statements aren't marked as cached.
87 EXPECT_FALSE(db().HasCachedStatement(SQL_FROM_HERE
));
90 TEST_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"));
96 TEST_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"));
102 // Should be case sensitive.
103 EXPECT_FALSE(db().DoesTableExist("FOO"));
105 // Test DoesColumnExist.
106 EXPECT_FALSE(db().DoesColumnExist("foo", "bar"));
107 EXPECT_TRUE(db().DoesColumnExist("foo", "a"));
109 // Testing for a column on a nonexistent table.
110 EXPECT_FALSE(db().DoesColumnExist("bar", "b"));
113 TEST_F(SQLConnectionTest
, GetLastInsertRowId
) {
114 ASSERT_TRUE(db().Execute("CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"));
116 ASSERT_TRUE(db().Execute("INSERT INTO foo (value) VALUES (12)"));
118 // Last insert row ID should be valid.
119 int64 row
= db().GetLastInsertRowId();
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=?"));
125 ASSERT_TRUE(s
.Step());
126 EXPECT_EQ(12, s
.ColumnInt(0));
129 TEST_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());
138 // Test that sql::Connection::Raze() results in a database without the
139 // tables from the original database.
140 TEST_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)"));
145 int pragma_auto_vacuum
= 0;
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);
153 // If auto_vacuum is set, there's an extra page to maintain a freelist.
154 const int kExpectedPageCount
= 2 + pragma_auto_vacuum
;
157 sql::Statement
s(db().GetUniqueStatement("PRAGMA page_count"));
158 ASSERT_TRUE(s
.Step());
159 EXPECT_EQ(kExpectedPageCount
, s
.ColumnInt(0));
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));
168 // Table "foo" is stored in the last page of the file.
169 EXPECT_EQ(kExpectedPageCount
, s
.ColumnInt(3));
170 EXPECT_EQ(kCreateSql
, s
.ColumnString(4));
173 ASSERT_TRUE(db().Raze());
176 sql::Statement
s(db().GetUniqueStatement("PRAGMA page_count"));
177 ASSERT_TRUE(s
.Step());
178 EXPECT_EQ(1, s
.ColumnInt(0));
182 sql::Statement
s(db().GetUniqueStatement("SELECT * FROM sqlite_master"));
183 ASSERT_FALSE(s
.Step());
187 sql::Statement
s(db().GetUniqueStatement("PRAGMA auto_vacuum"));
188 ASSERT_TRUE(s
.Step());
189 // The new database has the same auto_vacuum as a fresh database.
190 EXPECT_EQ(pragma_auto_vacuum
, s
.ColumnInt(0));
194 // Test that Raze() maintains page_size.
195 TEST_F(SQLConnectionTest
, RazePageSize
) {
196 // Fetch the default page size and double it for use in this test.
197 // Scoped to release statement before Close().
198 int default_page_size
= 0;
200 sql::Statement
s(db().GetUniqueStatement("PRAGMA page_size"));
201 ASSERT_TRUE(s
.Step());
202 default_page_size
= s
.ColumnInt(0);
204 ASSERT_GT(default_page_size
, 0);
205 const int kPageSize
= 2 * default_page_size
;
207 // Re-open the database to allow setting the page size.
209 db().set_page_size(kPageSize
);
210 ASSERT_TRUE(db().Open(db_path()));
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));
217 // After raze, page_size should still match the indicated value.
218 ASSERT_TRUE(db().Raze());
220 ASSERT_TRUE(s
.Step());
221 ASSERT_EQ(kPageSize
, s
.ColumnInt(0));
224 // Test that Raze() results are seen in other connections.
225 TEST_F(SQLConnectionTest
, RazeMultiple
) {
226 const char* kCreateSql
= "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)";
227 ASSERT_TRUE(db().Execute(kCreateSql
));
229 sql::Connection other_db
;
230 ASSERT_TRUE(other_db
.Open(db_path()));
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.
239 ASSERT_TRUE(db().Raze());
241 // The second connection sees the updated database.
243 ASSERT_TRUE(s
.Step());
244 ASSERT_EQ(0, s
.ColumnInt(0));
247 TEST_F(SQLConnectionTest
, RazeLocked
) {
248 const char* kCreateSql
= "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)";
249 ASSERT_TRUE(db().Execute(kCreateSql
));
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
));
260 ASSERT_FALSE(db().Raze());
262 // Works after COMMIT.
263 ASSERT_TRUE(other_db
.CommitTransaction());
264 ASSERT_TRUE(db().Raze());
266 // Re-create the database.
267 ASSERT_TRUE(db().Execute(kCreateSql
));
268 ASSERT_TRUE(db().Execute(kInsertSql
));
270 // An unfinished read transaction in the other connection also
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());
277 // Complete the statement unlocks the database.
278 ASSERT_FALSE(s
.Step());
279 ASSERT_TRUE(db().Raze());
282 #if defined(OS_ANDROID)
283 TEST_F(SQLConnectionTest
, SetTempDirForSQL
) {
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
291 ASSERT_TRUE(meta_table
.Init(&db(), 4, 4));
295 // 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().