[email protected] | 44ad7d90 | 2012-03-23 00:09:05 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 5 | #include "base/file_util.h" |
[email protected] | e078590 | 2011-05-19 23:34:17 | [diff] [blame] | 6 | #include "base/scoped_temp_dir.h" |
[email protected] | f0a54b2 | 2011-07-19 18:40:21 | [diff] [blame] | 7 | #include "sql/connection.h" |
| 8 | #include "sql/statement.h" |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 9 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | e33cba4 | 2010-08-18 23:37:03 | [diff] [blame] | 10 | #include "third_party/sqlite/sqlite3.h" |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 11 | |
| 12 | class SQLConnectionTest : public testing::Test { |
| 13 | public: |
| 14 | SQLConnectionTest() {} |
| 15 | |
| 16 | void SetUp() { |
[email protected] | 3a305db | 2011-04-12 13:40:53 | [diff] [blame] | 17 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
[email protected] | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame^] | 18 | ASSERT_TRUE(db_.Open(db_path())); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 19 | } |
| 20 | |
| 21 | void TearDown() { |
| 22 | db_.Close(); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | sql::Connection& db() { return db_; } |
| 26 | |
[email protected] | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame^] | 27 | FilePath db_path() { |
| 28 | return temp_dir_.path().AppendASCII("SQLConnectionTest.db"); |
| 29 | } |
| 30 | |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 31 | private: |
[email protected] | 3a305db | 2011-04-12 13:40:53 | [diff] [blame] | 32 | ScopedTempDir temp_dir_; |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 33 | sql::Connection db_; |
| 34 | }; |
| 35 | |
| 36 | TEST_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] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 42 | ASSERT_EQ(SQLITE_ERROR, |
| 43 | db().ExecuteAndReturnErrorCode("CREATE TAB foo (a, b")); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 44 | EXPECT_EQ(SQLITE_ERROR, db().GetErrorCode()); |
| 45 | } |
| 46 | |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 47 | TEST_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] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 57 | TEST_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] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 66 | ASSERT_TRUE(s.is_valid()); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 67 | |
| 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] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 79 | ASSERT_TRUE(s.is_valid()); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 80 | |
| 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] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 89 | TEST_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] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 95 | TEST_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] | e7afe245 | 2010-08-22 16:19:13 | [diff] [blame] | 108 | // Testing for a column on a nonexistent table. |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 109 | EXPECT_FALSE(db().DoesColumnExist("bar", "b")); |
| 110 | } |
| 111 | |
| 112 | TEST_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] | 44ad7d90 | 2012-03-23 00:09:05 | [diff] [blame] | 127 | |
| 128 | TEST_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] | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame^] | 136 | |
| 137 | // Test that sql::Connection::Raze() results in a database without the |
| 138 | // tables from the original database. |
| 139 | TEST_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. |
| 175 | TEST_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. |
| 204 | TEST_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 | |
| 226 | TEST_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(). |