blob: 773de5bcf5a3a75a308490f8d54f4e567baa7aed [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) {
[email protected]e73e89222012-07-13 18:55:22176 // Fetch the default page size and double it for use in this test.
[email protected]8e0c01282012-04-06 19:36:49177 // Scoped to release statement before Close().
[email protected]e73e89222012-07-13 18:55:22178 int default_page_size = 0;
[email protected]8e0c01282012-04-06 19:36:49179 {
180 sql::Statement s(db().GetUniqueStatement("PRAGMA page_size"));
181 ASSERT_TRUE(s.Step());
[email protected]e73e89222012-07-13 18:55:22182 default_page_size = s.ColumnInt(0);
[email protected]8e0c01282012-04-06 19:36:49183 }
[email protected]e73e89222012-07-13 18:55:22184 ASSERT_GT(default_page_size, 0);
185 const int kPageSize = 2 * default_page_size;
[email protected]8e0c01282012-04-06 19:36:49186
187 // Re-open the database to allow setting the page size.
188 db().Close();
189 db().set_page_size(kPageSize);
190 ASSERT_TRUE(db().Open(db_path()));
191
192 // page_size should match the indicated value.
193 sql::Statement s(db().GetUniqueStatement("PRAGMA page_size"));
194 ASSERT_TRUE(s.Step());
195 ASSERT_EQ(kPageSize, s.ColumnInt(0));
196
197 // After raze, page_size should still match the indicated value.
198 ASSERT_TRUE(db().Raze());
[email protected]389e0a42012-04-25 21:36:41199 s.Reset(true);
[email protected]8e0c01282012-04-06 19:36:49200 ASSERT_TRUE(s.Step());
201 ASSERT_EQ(kPageSize, s.ColumnInt(0));
202}
203
204// Test that Raze() results are seen in other connections.
205TEST_F(SQLConnectionTest, RazeMultiple) {
206 const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)";
207 ASSERT_TRUE(db().Execute(kCreateSql));
208
209 sql::Connection other_db;
210 ASSERT_TRUE(other_db.Open(db_path()));
211
212 // Check that the second connection sees the table.
213 const char *kTablesQuery = "SELECT COUNT(*) FROM sqlite_master";
214 sql::Statement s(other_db.GetUniqueStatement(kTablesQuery));
215 ASSERT_TRUE(s.Step());
216 ASSERT_EQ(1, s.ColumnInt(0));
217 ASSERT_FALSE(s.Step()); // Releases the shared lock.
218
219 ASSERT_TRUE(db().Raze());
220
221 // The second connection sees the updated database.
[email protected]389e0a42012-04-25 21:36:41222 s.Reset(true);
[email protected]8e0c01282012-04-06 19:36:49223 ASSERT_TRUE(s.Step());
224 ASSERT_EQ(0, s.ColumnInt(0));
225}
226
227TEST_F(SQLConnectionTest, RazeLocked) {
228 const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)";
229 ASSERT_TRUE(db().Execute(kCreateSql));
230
231 // Open a transaction and write some data in a second connection.
232 // This will acquire a PENDING or EXCLUSIVE transaction, which will
233 // cause the raze to fail.
234 sql::Connection other_db;
235 ASSERT_TRUE(other_db.Open(db_path()));
236 ASSERT_TRUE(other_db.BeginTransaction());
237 const char* kInsertSql = "INSERT INTO foo VALUES (1, 'data')";
238 ASSERT_TRUE(other_db.Execute(kInsertSql));
239
240 ASSERT_FALSE(db().Raze());
241
242 // Works after COMMIT.
243 ASSERT_TRUE(other_db.CommitTransaction());
244 ASSERT_TRUE(db().Raze());
245
246 // Re-create the database.
247 ASSERT_TRUE(db().Execute(kCreateSql));
248 ASSERT_TRUE(db().Execute(kInsertSql));
249
250 // An unfinished read transaction in the other connection also
251 // blocks raze.
252 const char *kQuery = "SELECT COUNT(*) FROM foo";
253 sql::Statement s(other_db.GetUniqueStatement(kQuery));
254 ASSERT_TRUE(s.Step());
255 ASSERT_FALSE(db().Raze());
256
257 // Complete the statement unlocks the database.
258 ASSERT_FALSE(s.Step());
259 ASSERT_TRUE(db().Raze());
260}
261
262// TODO(shess): Spin up a background thread to hold other_db, to more
263// closely match real life. That would also allow testing
264// RazeWithTimeout().