Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(100)

Side by Side Diff: sql/connection_unittest.cc

Issue 1069313004: [sql] Change DoesStuffExist() to work with ScopedErrorIgnorer. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« sql/connection.cc ('K') | « sql/connection.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/bind.h" 5 #include "base/bind.h"
6 #include "base/files/file_util.h" 6 #include "base/files/file_util.h"
7 #include "base/files/scoped_file.h" 7 #include "base/files/scoped_file.h"
8 #include "base/files/scoped_temp_dir.h" 8 #include "base/files/scoped_temp_dir.h"
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "sql/connection.h" 10 #include "sql/connection.h"
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after
169 TEST_F(SQLConnectionTest, IsSQLValidTest) { 169 TEST_F(SQLConnectionTest, IsSQLValidTest) {
170 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)")); 170 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)"));
171 ASSERT_TRUE(db().IsSQLValid("SELECT a FROM foo")); 171 ASSERT_TRUE(db().IsSQLValid("SELECT a FROM foo"));
172 ASSERT_FALSE(db().IsSQLValid("SELECT no_exist FROM foo")); 172 ASSERT_FALSE(db().IsSQLValid("SELECT no_exist FROM foo"));
173 } 173 }
174 174
175 TEST_F(SQLConnectionTest, DoesStuffExist) { 175 TEST_F(SQLConnectionTest, DoesStuffExist) {
176 // Test DoesTableExist. 176 // Test DoesTableExist.
177 EXPECT_FALSE(db().DoesTableExist("foo")); 177 EXPECT_FALSE(db().DoesTableExist("foo"));
178 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)")); 178 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)"));
179 ASSERT_TRUE(db().Execute("CREATE INDEX foo_a ON foo (a)"));
179 EXPECT_TRUE(db().DoesTableExist("foo")); 180 EXPECT_TRUE(db().DoesTableExist("foo"));
180 181 EXPECT_TRUE(db().DoesIndexExist("foo_a"));
181 // Should be case sensitive.
182 EXPECT_FALSE(db().DoesTableExist("FOO"));
183 182
184 // Test DoesColumnExist. 183 // Test DoesColumnExist.
185 EXPECT_FALSE(db().DoesColumnExist("foo", "bar")); 184 EXPECT_FALSE(db().DoesColumnExist("foo", "bar"));
186 EXPECT_TRUE(db().DoesColumnExist("foo", "a")); 185 EXPECT_TRUE(db().DoesColumnExist("foo", "a"));
187 186
188 // Testing for a column on a nonexistent table. 187 // Testing for a column on a nonexistent table.
189 EXPECT_FALSE(db().DoesColumnExist("bar", "b")); 188 EXPECT_FALSE(db().DoesColumnExist("bar", "b"));
189
190 // Names are not case sensitive.
191 EXPECT_TRUE(db().DoesTableExist("FOO"));
192 EXPECT_TRUE(db().DoesColumnExist("FOO", "A"));
190 } 193 }
191 194
192 TEST_F(SQLConnectionTest, GetLastInsertRowId) { 195 TEST_F(SQLConnectionTest, GetLastInsertRowId) {
193 ASSERT_TRUE(db().Execute("CREATE TABLE foo (id INTEGER PRIMARY KEY, value)")); 196 ASSERT_TRUE(db().Execute("CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"));
194 197
195 ASSERT_TRUE(db().Execute("INSERT INTO foo (value) VALUES (12)")); 198 ASSERT_TRUE(db().Execute("INSERT INTO foo (value) VALUES (12)"));
196 199
197 // Last insert row ID should be valid. 200 // Last insert row ID should be valid.
198 int64 row = db().GetLastInsertRowId(); 201 int64 row = db().GetLastInsertRowId();
199 EXPECT_LT(0, row); 202 EXPECT_LT(0, row);
(...skipping 14 matching lines...) Expand all
214 EXPECT_TRUE(db().BeginTransaction()); 217 EXPECT_TRUE(db().BeginTransaction());
215 } 218 }
216 219
217 // Test the scoped error ignorer by attempting to insert a duplicate 220 // Test the scoped error ignorer by attempting to insert a duplicate
218 // value into an index. 221 // value into an index.
219 TEST_F(SQLConnectionTest, ScopedIgnoreError) { 222 TEST_F(SQLConnectionTest, ScopedIgnoreError) {
220 const char* kCreateSql = "CREATE TABLE foo (id INTEGER UNIQUE)"; 223 const char* kCreateSql = "CREATE TABLE foo (id INTEGER UNIQUE)";
221 ASSERT_TRUE(db().Execute(kCreateSql)); 224 ASSERT_TRUE(db().Execute(kCreateSql));
222 ASSERT_TRUE(db().Execute("INSERT INTO foo (id) VALUES (12)")); 225 ASSERT_TRUE(db().Execute("INSERT INTO foo (id) VALUES (12)"));
223 226
224 sql::ScopedErrorIgnorer ignore_errors; 227 {
225 ignore_errors.IgnoreError(SQLITE_CONSTRAINT); 228 sql::ScopedErrorIgnorer ignore_errors;
226 ASSERT_FALSE(db().Execute("INSERT INTO foo (id) VALUES (12)")); 229 ignore_errors.IgnoreError(SQLITE_CONSTRAINT);
227 ASSERT_TRUE(ignore_errors.CheckIgnoredErrors()); 230 ASSERT_FALSE(db().Execute("INSERT INTO foo (id) VALUES (12)"));
231 ASSERT_TRUE(ignore_errors.CheckIgnoredErrors());
232 }
233 }
234
235 // Test that clients of GetUntrackedStatement() can test corruption-handling
236 // with ScopedErrorIgnorer.
237 TEST_F(SQLConnectionTest, ScopedIgnoreUntracked) {
238 const char* kCreateSql = "CREATE TABLE foo (id INTEGER UNIQUE)";
239 ASSERT_TRUE(db().Execute(kCreateSql));
240 ASSERT_FALSE(db().DoesTableExist("bar"));
241 ASSERT_TRUE(db().DoesTableExist("foo"));
242 ASSERT_TRUE(db().DoesColumnExist("foo", "id"));
243 db().Close();
244
245 // Corrupt the database so that nothing works, including PRAGMAs.
246 ASSERT_TRUE(sql::test::CorruptSizeInHeader(db_path()));
247
248 {
249 sql::ScopedErrorIgnorer ignore_errors;
250 ignore_errors.IgnoreError(SQLITE_CORRUPT);
251 ASSERT_TRUE(db().Open(db_path()));
252 ASSERT_FALSE(db().DoesTableExist("bar"));
253 ASSERT_FALSE(db().DoesTableExist("foo"));
254 ASSERT_FALSE(db().DoesColumnExist("foo", "id"));
255 ASSERT_TRUE(ignore_errors.CheckIgnoredErrors());
256 }
228 } 257 }
229 258
230 TEST_F(SQLConnectionTest, ErrorCallback) { 259 TEST_F(SQLConnectionTest, ErrorCallback) {
231 const char* kCreateSql = "CREATE TABLE foo (id INTEGER UNIQUE)"; 260 const char* kCreateSql = "CREATE TABLE foo (id INTEGER UNIQUE)";
232 ASSERT_TRUE(db().Execute(kCreateSql)); 261 ASSERT_TRUE(db().Execute(kCreateSql));
233 ASSERT_TRUE(db().Execute("INSERT INTO foo (id) VALUES (12)")); 262 ASSERT_TRUE(db().Execute("INSERT INTO foo (id) VALUES (12)"));
234 263
235 int error = SQLITE_OK; 264 int error = SQLITE_OK;
236 { 265 {
237 sql::ScopedErrorCallback sec( 266 sql::ScopedErrorCallback sec(
(...skipping 632 matching lines...) Expand 10 before | Expand all | Expand 10 after
870 EXPECT_LT(1u, messages.size()); 899 EXPECT_LT(1u, messages.size());
871 EXPECT_NE(kOk, messages[0]); 900 EXPECT_NE(kOk, messages[0]);
872 ASSERT_TRUE(ignore_errors.CheckIgnoredErrors()); 901 ASSERT_TRUE(ignore_errors.CheckIgnoredErrors());
873 } 902 }
874 903
875 // TODO(shess): CorruptTableOrIndex could be used to produce a 904 // TODO(shess): CorruptTableOrIndex could be used to produce a
876 // file that would pass the quick check and fail the full check. 905 // file that would pass the quick check and fail the full check.
877 } 906 }
878 907
879 } // namespace 908 } // namespace
OLDNEW
« sql/connection.cc ('K') | « sql/connection.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698