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

Side by Side Diff: app/sql/statement_unittest.cc

Issue 223003: Add basic sqlite error handling to the new sqlite handlers... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 11 years, 2 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 | Annotate | Revision Log
« no previous file with comments | « app/sql/statement.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) 2009 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2009 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 <string>
6
5 #include "app/sql/connection.h" 7 #include "app/sql/connection.h"
6 #include "app/sql/statement.h" 8 #include "app/sql/statement.h"
7 #include "base/file_path.h" 9 #include "base/file_path.h"
8 #include "base/file_util.h" 10 #include "base/file_util.h"
9 #include "base/path_service.h" 11 #include "base/path_service.h"
10 #include "testing/gtest/include/gtest/gtest.h" 12 #include "testing/gtest/include/gtest/gtest.h"
11 #include "third_party/sqlite/preprocessed/sqlite3.h" 13 #include "third_party/sqlite/preprocessed/sqlite3.h"
12 14
15 class StatementErrorHandler : public sql::ErrorDelegate {
16 public:
17 StatementErrorHandler() : error_(SQLITE_OK) {}
18
19 virtual int OnError(int error, sql::Connection* connection,
20 sql::Statement* stmt) {
21 error_ = error;
22 const char* sql_txt = stmt->GetSQLStatement();
23 sql_text_ = sql_txt ? sql_txt : "no statement available";
24 return error;
25 }
26
27 int error() const { return error_; }
28
29 void reset_error() {
30 sql_text_.clear();
31 error_ = SQLITE_OK;
32 }
33
34 const char* sql_statement() const { return sql_text_.c_str(); }
35
36 private:
37 int error_;
38 std::string sql_text_;
39 };
40
13 class SQLStatementTest : public testing::Test { 41 class SQLStatementTest : public testing::Test {
14 public: 42 public:
15 SQLStatementTest() {} 43 SQLStatementTest() : error_handler_(new StatementErrorHandler) {}
16 44
17 void SetUp() { 45 void SetUp() {
18 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &path_)); 46 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &path_));
19 path_ = path_.AppendASCII("SQLStatementTest.db"); 47 path_ = path_.AppendASCII("SQLStatementTest.db");
20 file_util::Delete(path_, false); 48 file_util::Delete(path_, false);
21 ASSERT_TRUE(db_.Init(path_)); 49 ASSERT_TRUE(db_.Init(path_));
50 // The |error_handler_| will be called if any sqlite statement operation
51 // returns an error code.
52 db_.set_error_delegate(error_handler_);
22 } 53 }
23 54
24 void TearDown() { 55 void TearDown() {
56 // If any error happened the original sql statement can be found in
57 // error_handler_->sql_statement().
58 EXPECT_EQ(SQLITE_OK, error_handler_->error());
25 db_.Close(); 59 db_.Close();
26
27 // If this fails something is going on with cleanup and later tests may 60 // If this fails something is going on with cleanup and later tests may
28 // fail, so we want to identify problems right away. 61 // fail, so we want to identify problems right away.
29 ASSERT_TRUE(file_util::Delete(path_, false)); 62 ASSERT_TRUE(file_util::Delete(path_, false));
30 } 63 }
31 64
32 sql::Connection& db() { return db_; } 65 sql::Connection& db() { return db_; }
33 66
67 int sqlite_error() const { return error_handler_->error(); }
68 void reset_error() const { error_handler_->reset_error(); }
69
34 private: 70 private:
35 FilePath path_; 71 FilePath path_;
36 sql::Connection db_; 72 sql::Connection db_;
73 scoped_refptr<StatementErrorHandler> error_handler_;
37 }; 74 };
38 75
39 TEST_F(SQLStatementTest, Assign) { 76 TEST_F(SQLStatementTest, Assign) {
40 sql::Statement s; 77 sql::Statement s;
41 EXPECT_FALSE(s); // bool conversion operator. 78 EXPECT_FALSE(s); // bool conversion operator.
42 EXPECT_TRUE(!s); // ! operator. 79 EXPECT_TRUE(!s); // ! operator.
43 EXPECT_FALSE(s.is_valid()); 80 EXPECT_FALSE(s.is_valid());
44 81
45 s.Assign(db().GetUniqueStatement("CREATE TABLE foo (a, b)")); 82 s.Assign(db().GetUniqueStatement("CREATE TABLE foo (a, b)"));
46 EXPECT_TRUE(s); 83 EXPECT_TRUE(s);
(...skipping 24 matching lines...) Expand all
71 EXPECT_FALSE(s.Succeeded()); 108 EXPECT_FALSE(s.Succeeded());
72 109
73 // Binding and stepping should produce one row. 110 // Binding and stepping should produce one row.
74 s.BindInt(0, 3); 111 s.BindInt(0, 3);
75 EXPECT_TRUE(s.Step()); 112 EXPECT_TRUE(s.Step());
76 EXPECT_TRUE(s.Succeeded()); 113 EXPECT_TRUE(s.Succeeded());
77 EXPECT_EQ(12, s.ColumnInt(0)); 114 EXPECT_EQ(12, s.ColumnInt(0));
78 EXPECT_FALSE(s.Step()); 115 EXPECT_FALSE(s.Step());
79 EXPECT_TRUE(s.Succeeded()); 116 EXPECT_TRUE(s.Succeeded());
80 } 117 }
118
119 TEST_F(SQLStatementTest, BasicErrorCallback) {
120 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a INTEGER PRIMARY KEY, b)"));
121 EXPECT_EQ(SQLITE_OK, sqlite_error());
122 // Insert in the foo table the primary key. It is an error to insert
123 // something other than an number. This error causes the error callback
124 // handler to be called with SQLITE_MISMATCH as error code.
125 sql::Statement s(db().GetUniqueStatement("INSERT INTO foo (a) VALUES (?)"));
126 EXPECT_TRUE(s.is_valid());
127 s.BindCString(0, "bad bad");
128 EXPECT_FALSE(s.Run());
129 EXPECT_EQ(SQLITE_MISMATCH, sqlite_error());
130 reset_error();
131 }
OLDNEW
« no previous file with comments | « app/sql/statement.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698