blob: 96cc25b45aa120c84515042247cbbf3076c2b098 [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]e5ffd0e42009-09-11 21:30:569#include "testing/gtest/include/gtest/gtest.h"
[email protected]e33cba42010-08-18 23:37:0310#include "third_party/sqlite/sqlite3.h"
[email protected]e5ffd0e42009-09-11 21:30:5611
12class SQLConnectionTest : public testing::Test {
13 public:
14 SQLConnectionTest() {}
15
16 void SetUp() {
[email protected]3a305db2011-04-12 13:40:5317 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
[email protected]8e0c01282012-04-06 19:36:4918 ASSERT_TRUE(db_.Open(db_path()));
[email protected]e5ffd0e42009-09-11 21:30:5619 }
20
21 void TearDown() {
22 db_.Close();
[email protected]e5ffd0e42009-09-11 21:30:5623 }
24
25 sql::Connection& db() { return db_; }
26
[email protected]8e0c01282012-04-06 19:36:4927 FilePath db_path() {
28 return temp_dir_.path().AppendASCII("SQLConnectionTest.db");
29 }
30
[email protected]e5ffd0e42009-09-11 21:30:5631 private:
[email protected]3a305db2011-04-12 13:40:5332 ScopedTempDir temp_dir_;
[email protected]e5ffd0e42009-09-11 21:30:5633 sql::Connection db_;
34};
35
36TEST_F(SQLConnectionTest, Execute) {
37 // Valid statement should return true.
38 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)"));
39 EXPECT_EQ(SQLITE_OK, db().GetErrorCode());
40
41 // Invalid statement should fail.
[email protected]eff1fa522011-12-12 23:50:5942 ASSERT_EQ(SQLITE_ERROR,
43 db().ExecuteAndReturnErrorCode("CREATE TAB foo (a, b"));
[email protected]e5ffd0e42009-09-11 21:30:5644 EXPECT_EQ(SQLITE_ERROR, db().GetErrorCode());
45}
46
[email protected]eff1fa522011-12-12 23:50:5947TEST_F(SQLConnectionTest, ExecuteWithErrorCode) {
48 ASSERT_EQ(SQLITE_OK,
49 db().ExecuteAndReturnErrorCode("CREATE TABLE foo (a, b)"));
50 ASSERT_EQ(SQLITE_ERROR,
51 db().ExecuteAndReturnErrorCode("CREATE TABLE TABLE"));
52 ASSERT_EQ(SQLITE_ERROR,
53 db().ExecuteAndReturnErrorCode(
54 "INSERT INTO foo(a, b) VALUES (1, 2, 3, 4)"));
55}
56
[email protected]e5ffd0e42009-09-11 21:30:5657TEST_F(SQLConnectionTest, CachedStatement) {
58 sql::StatementID id1("foo", 12);
59
60 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)"));
61 ASSERT_TRUE(db().Execute("INSERT INTO foo(a, b) VALUES (12, 13)"));
62
63 // Create a new cached statement.
64 {
65 sql::Statement s(db().GetCachedStatement(id1, "SELECT a FROM foo"));
[email protected]eff1fa522011-12-12 23:50:5966 ASSERT_TRUE(s.is_valid());
[email protected]e5ffd0e42009-09-11 21:30:5667
68 ASSERT_TRUE(s.Step());
69 EXPECT_EQ(12, s.ColumnInt(0));
70 }
71
72 // The statement should be cached still.
73 EXPECT_TRUE(db().HasCachedStatement(id1));
74
75 {
76 // Get the same statement using different SQL. This should ignore our
77 // SQL and use the cached one (so it will be valid).
78 sql::Statement s(db().GetCachedStatement(id1, "something invalid("));
[email protected]eff1fa522011-12-12 23:50:5979 ASSERT_TRUE(s.is_valid());
[email protected]e5ffd0e42009-09-11 21:30:5680
81 ASSERT_TRUE(s.Step());
82 EXPECT_EQ(12, s.ColumnInt(0));
83 }
84
85 // Make sure other statements aren't marked as cached.
86 EXPECT_FALSE(db().HasCachedStatement(SQL_FROM_HERE));
87}
88
[email protected]eff1fa522011-12-12 23:50:5989TEST_F(SQLConnectionTest, IsSQLValidTest) {
90 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)"));
91 ASSERT_TRUE(db().IsSQLValid("SELECT a FROM foo"));
92 ASSERT_FALSE(db().IsSQLValid("SELECT no_exist FROM foo"));
93}
94
[email protected]e5ffd0e42009-09-11 21:30:5695TEST_F(SQLConnectionTest, DoesStuffExist) {
96 // Test DoesTableExist.
97 EXPECT_FALSE(db().DoesTableExist("foo"));
98 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)"));
99 EXPECT_TRUE(db().DoesTableExist("foo"));
100
101 // Should be case sensitive.
102 EXPECT_FALSE(db().DoesTableExist("FOO"));
103
104 // Test DoesColumnExist.
105 EXPECT_FALSE(db().DoesColumnExist("foo", "bar"));
106 EXPECT_TRUE(db().DoesColumnExist("foo", "a"));
107
[email protected]e7afe2452010-08-22 16:19:13108 // Testing for a column on a nonexistent table.
[email protected]e5ffd0e42009-09-11 21:30:56109 EXPECT_FALSE(db().DoesColumnExist("bar", "b"));
110}
111
112TEST_F(SQLConnectionTest, GetLastInsertRowId) {
113 ASSERT_TRUE(db().Execute("CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"));
114
115 ASSERT_TRUE(db().Execute("INSERT INTO foo (value) VALUES (12)"));
116
117 // Last insert row ID should be valid.
118 int64 row = db().GetLastInsertRowId();
119 EXPECT_LT(0, row);
120
121 // It should be the primary key of the row we just inserted.
122 sql::Statement s(db().GetUniqueStatement("SELECT value FROM foo WHERE id=?"));
123 s.BindInt64(0, row);
124 ASSERT_TRUE(s.Step());
125 EXPECT_EQ(12, s.ColumnInt(0));
126}
[email protected]44ad7d902012-03-23 00:09:05127
128TEST_F(SQLConnectionTest, Rollback) {
129 ASSERT_TRUE(db().BeginTransaction());
130 ASSERT_TRUE(db().BeginTransaction());
131 EXPECT_EQ(2, db().transaction_nesting());
132 db().RollbackTransaction();
133 EXPECT_FALSE(db().CommitTransaction());
134 EXPECT_TRUE(db().BeginTransaction());
135}
[email protected]8e0c01282012-04-06 19:36:49136
137// Test that sql::Connection::Raze() results in a database without the
138// tables from the original database.
139TEST_F(SQLConnectionTest, Raze) {
140 const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)";
141 ASSERT_TRUE(db().Execute(kCreateSql));
142 ASSERT_TRUE(db().Execute("INSERT INTO foo (value) VALUES (12)"));
143
144 {
145 sql::Statement s(db().GetUniqueStatement("PRAGMA page_count"));
146 ASSERT_TRUE(s.Step());
147 EXPECT_EQ(2, s.ColumnInt(0));
148 }
149
150 {
151 sql::Statement s(db().GetUniqueStatement("SELECT * FROM sqlite_master"));
152 ASSERT_TRUE(s.Step());
153 EXPECT_EQ("table", s.ColumnString(0));
154 EXPECT_EQ("foo", s.ColumnString(1));
155 EXPECT_EQ("foo", s.ColumnString(2));
156 EXPECT_EQ(2, s.ColumnInt(3));
157 EXPECT_EQ(kCreateSql, s.ColumnString(4));
158 }
159
160 ASSERT_TRUE(db().Raze());
161
162 {
163 sql::Statement s(db().GetUniqueStatement("PRAGMA page_count"));
164 ASSERT_TRUE(s.Step());
165 EXPECT_EQ(1, s.ColumnInt(0));
166 }
167
168 {
169 sql::Statement s(db().GetUniqueStatement("SELECT * FROM sqlite_master"));
170 ASSERT_FALSE(s.Step());
171 }
172}
173
174// Test that Raze() maintains page_size.
175TEST_F(SQLConnectionTest, RazePageSize) {
176 const int kPageSize = 4096;
177
178 // Make sure that the default size isn't already |kPageSize|.
179 // Scoped to release statement before Close().
180 {
181 sql::Statement s(db().GetUniqueStatement("PRAGMA page_size"));
182 ASSERT_TRUE(s.Step());
183 ASSERT_NE(kPageSize, s.ColumnInt(0));
184 }
185
186 // Re-open the database to allow setting the page size.
187 db().Close();
188 db().set_page_size(kPageSize);
189 ASSERT_TRUE(db().Open(db_path()));
190
191 // page_size should match the indicated value.
192 sql::Statement s(db().GetUniqueStatement("PRAGMA page_size"));
193 ASSERT_TRUE(s.Step());
194 ASSERT_EQ(kPageSize, s.ColumnInt(0));
195
196 // After raze, page_size should still match the indicated value.
197 ASSERT_TRUE(db().Raze());
198 s.Reset();
199 ASSERT_TRUE(s.Step());
200 ASSERT_EQ(kPageSize, s.ColumnInt(0));
201}
202
203// Test that Raze() results are seen in other connections.
204TEST_F(SQLConnectionTest, RazeMultiple) {
205 const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)";
206 ASSERT_TRUE(db().Execute(kCreateSql));
207
208 sql::Connection other_db;
209 ASSERT_TRUE(other_db.Open(db_path()));
210
211 // Check that the second connection sees the table.
212 const char *kTablesQuery = "SELECT COUNT(*) FROM sqlite_master";
213 sql::Statement s(other_db.GetUniqueStatement(kTablesQuery));
214 ASSERT_TRUE(s.Step());
215 ASSERT_EQ(1, s.ColumnInt(0));
216 ASSERT_FALSE(s.Step()); // Releases the shared lock.
217
218 ASSERT_TRUE(db().Raze());
219
220 // The second connection sees the updated database.
221 s.Reset();
222 ASSERT_TRUE(s.Step());
223 ASSERT_EQ(0, s.ColumnInt(0));
224}
225
226TEST_F(SQLConnectionTest, RazeLocked) {
227 const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)";
228 ASSERT_TRUE(db().Execute(kCreateSql));
229
230 // Open a transaction and write some data in a second connection.
231 // This will acquire a PENDING or EXCLUSIVE transaction, which will
232 // cause the raze to fail.
233 sql::Connection other_db;
234 ASSERT_TRUE(other_db.Open(db_path()));
235 ASSERT_TRUE(other_db.BeginTransaction());
236 const char* kInsertSql = "INSERT INTO foo VALUES (1, 'data')";
237 ASSERT_TRUE(other_db.Execute(kInsertSql));
238
239 ASSERT_FALSE(db().Raze());
240
241 // Works after COMMIT.
242 ASSERT_TRUE(other_db.CommitTransaction());
243 ASSERT_TRUE(db().Raze());
244
245 // Re-create the database.
246 ASSERT_TRUE(db().Execute(kCreateSql));
247 ASSERT_TRUE(db().Execute(kInsertSql));
248
249 // An unfinished read transaction in the other connection also
250 // blocks raze.
251 const char *kQuery = "SELECT COUNT(*) FROM foo";
252 sql::Statement s(other_db.GetUniqueStatement(kQuery));
253 ASSERT_TRUE(s.Step());
254 ASSERT_FALSE(db().Raze());
255
256 // Complete the statement unlocks the database.
257 ASSERT_FALSE(s.Step());
258 ASSERT_TRUE(db().Raze());
259}
260
261// TODO(shess): Spin up a background thread to hold other_db, to more
262// closely match real life. That would also allow testing
263// RazeWithTimeout().