blob: 565c12fb9ab113c781de7ebe4e29bd3edb530f8a [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
145 {
146 sql::Statement s(db().GetUniqueStatement("PRAGMA page_count"));
147 ASSERT_TRUE(s.Step());
148 EXPECT_EQ(2, s.ColumnInt(0));
149 }
150
151 {
152 sql::Statement s(db().GetUniqueStatement("SELECT * FROM sqlite_master"));
153 ASSERT_TRUE(s.Step());
154 EXPECT_EQ("table", s.ColumnString(0));
155 EXPECT_EQ("foo", s.ColumnString(1));
156 EXPECT_EQ("foo", s.ColumnString(2));
157 EXPECT_EQ(2, s.ColumnInt(3));
158 EXPECT_EQ(kCreateSql, s.ColumnString(4));
159 }
160
161 ASSERT_TRUE(db().Raze());
162
163 {
164 sql::Statement s(db().GetUniqueStatement("PRAGMA page_count"));
165 ASSERT_TRUE(s.Step());
166 EXPECT_EQ(1, s.ColumnInt(0));
167 }
168
169 {
170 sql::Statement s(db().GetUniqueStatement("SELECT * FROM sqlite_master"));
171 ASSERT_FALSE(s.Step());
172 }
173}
174
175// Test that Raze() maintains page_size.
176TEST_F(SQLConnectionTest, RazePageSize) {
[email protected]e73e89222012-07-13 18:55:22177 // Fetch the default page size and double it for use in this test.
[email protected]8e0c01282012-04-06 19:36:49178 // Scoped to release statement before Close().
[email protected]e73e89222012-07-13 18:55:22179 int default_page_size = 0;
[email protected]8e0c01282012-04-06 19:36:49180 {
181 sql::Statement s(db().GetUniqueStatement("PRAGMA page_size"));
182 ASSERT_TRUE(s.Step());
[email protected]e73e89222012-07-13 18:55:22183 default_page_size = s.ColumnInt(0);
[email protected]8e0c01282012-04-06 19:36:49184 }
[email protected]e73e89222012-07-13 18:55:22185 ASSERT_GT(default_page_size, 0);
186 const int kPageSize = 2 * default_page_size;
[email protected]8e0c01282012-04-06 19:36:49187
188 // Re-open the database to allow setting the page size.
189 db().Close();
190 db().set_page_size(kPageSize);
191 ASSERT_TRUE(db().Open(db_path()));
192
193 // page_size should match the indicated value.
194 sql::Statement s(db().GetUniqueStatement("PRAGMA page_size"));
195 ASSERT_TRUE(s.Step());
196 ASSERT_EQ(kPageSize, s.ColumnInt(0));
197
198 // After raze, page_size should still match the indicated value.
199 ASSERT_TRUE(db().Raze());
[email protected]389e0a42012-04-25 21:36:41200 s.Reset(true);
[email protected]8e0c01282012-04-06 19:36:49201 ASSERT_TRUE(s.Step());
202 ASSERT_EQ(kPageSize, s.ColumnInt(0));
203}
204
205// Test that Raze() results are seen in other connections.
206TEST_F(SQLConnectionTest, RazeMultiple) {
207 const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)";
208 ASSERT_TRUE(db().Execute(kCreateSql));
209
210 sql::Connection other_db;
211 ASSERT_TRUE(other_db.Open(db_path()));
212
213 // Check that the second connection sees the table.
214 const char *kTablesQuery = "SELECT COUNT(*) FROM sqlite_master";
215 sql::Statement s(other_db.GetUniqueStatement(kTablesQuery));
216 ASSERT_TRUE(s.Step());
217 ASSERT_EQ(1, s.ColumnInt(0));
218 ASSERT_FALSE(s.Step()); // Releases the shared lock.
219
220 ASSERT_TRUE(db().Raze());
221
222 // The second connection sees the updated database.
[email protected]389e0a42012-04-25 21:36:41223 s.Reset(true);
[email protected]8e0c01282012-04-06 19:36:49224 ASSERT_TRUE(s.Step());
225 ASSERT_EQ(0, s.ColumnInt(0));
226}
227
228TEST_F(SQLConnectionTest, RazeLocked) {
229 const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)";
230 ASSERT_TRUE(db().Execute(kCreateSql));
231
232 // Open a transaction and write some data in a second connection.
233 // This will acquire a PENDING or EXCLUSIVE transaction, which will
234 // cause the raze to fail.
235 sql::Connection other_db;
236 ASSERT_TRUE(other_db.Open(db_path()));
237 ASSERT_TRUE(other_db.BeginTransaction());
238 const char* kInsertSql = "INSERT INTO foo VALUES (1, 'data')";
239 ASSERT_TRUE(other_db.Execute(kInsertSql));
240
241 ASSERT_FALSE(db().Raze());
242
243 // Works after COMMIT.
244 ASSERT_TRUE(other_db.CommitTransaction());
245 ASSERT_TRUE(db().Raze());
246
247 // Re-create the database.
248 ASSERT_TRUE(db().Execute(kCreateSql));
249 ASSERT_TRUE(db().Execute(kInsertSql));
250
251 // An unfinished read transaction in the other connection also
252 // blocks raze.
253 const char *kQuery = "SELECT COUNT(*) FROM foo";
254 sql::Statement s(other_db.GetUniqueStatement(kQuery));
255 ASSERT_TRUE(s.Step());
256 ASSERT_FALSE(db().Raze());
257
258 // Complete the statement unlocks the database.
259 ASSERT_FALSE(s.Step());
260 ASSERT_TRUE(db().Raze());
261}
262
[email protected]1348765a2012-07-24 08:25:53263#if defined(OS_ANDROID)
264TEST_F(SQLConnectionTest, SetTempDirForSQL) {
265
266 sql::MetaTable meta_table;
267 // Below call needs a temporary directory in sqlite3
268 // On Android, it can pass only when the temporary directory is set.
269 // Otherwise, sqlite3 doesn't find the correct directory to store
270 // temporary files and will report the error 'unable to open
271 // database file'.
272 ASSERT_TRUE(meta_table.Init(&db(), 4, 4));
273}
274#endif
275
[email protected]8e0c01282012-04-06 19:36:49276// TODO(shess): Spin up a background thread to hold other_db, to more
277// closely match real life. That would also allow testing
278// RazeWithTimeout().