OLD | NEW |
---|---|
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 <string> | 5 #include <string> |
6 | 6 |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/scoped_temp_dir.h" | 8 #include "base/scoped_temp_dir.h" |
9 #include "sql/connection.h" | 9 #include "sql/connection.h" |
10 #include "sql/statement.h" | 10 #include "sql/statement.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
12 #include "third_party/sqlite/sqlite3.h" | 12 #include "third_party/sqlite/sqlite3.h" |
13 | 13 |
14 class StatementErrorHandler : public sql::ErrorDelegate { | 14 class StatementErrorHandler : public sql::ErrorDelegate { |
15 public: | 15 public: |
16 StatementErrorHandler() : error_(SQLITE_OK) {} | 16 StatementErrorHandler() : error_(SQLITE_OK) {} |
17 | 17 |
18 virtual ~StatementErrorHandler() {} | |
19 | |
18 virtual int OnError(int error, sql::Connection* connection, | 20 virtual int OnError(int error, sql::Connection* connection, |
19 sql::Statement* stmt) OVERRIDE { | 21 sql::Statement* stmt) OVERRIDE { |
20 error_ = error; | 22 error_ = error; |
21 const char* sql_txt = stmt ? stmt->GetSQLStatement() : NULL; | 23 const char* sql_txt = stmt ? stmt->GetSQLStatement() : NULL; |
22 sql_text_ = sql_txt ? sql_txt : "no statement available"; | 24 sql_text_ = sql_txt ? sql_txt : "no statement available"; |
23 return error; | 25 return error; |
24 } | 26 } |
25 | 27 |
26 int error() const { return error_; } | 28 int error() const { return error_; } |
27 | 29 |
28 void reset_error() { | 30 void reset_error() { |
29 sql_text_.clear(); | 31 sql_text_.clear(); |
30 error_ = SQLITE_OK; | 32 error_ = SQLITE_OK; |
31 } | 33 } |
32 | 34 |
33 const char* sql_statement() const { return sql_text_.c_str(); } | 35 const char* sql_statement() const { return sql_text_.c_str(); } |
34 | 36 |
35 protected: | |
36 virtual ~StatementErrorHandler() {} | |
37 | |
38 private: | 37 private: |
39 int error_; | 38 int error_; |
40 std::string sql_text_; | 39 std::string sql_text_; |
40 | |
41 DISALLOW_COPY_AND_ASSIGN(StatementErrorHandler); | |
41 }; | 42 }; |
42 | 43 |
43 class SQLStatementTest : public testing::Test { | 44 class SQLStatementTest : public testing::Test { |
44 public: | 45 public: |
45 SQLStatementTest() : error_handler_(new StatementErrorHandler) {} | 46 SQLStatementTest() : error_handler_(new StatementErrorHandler) {} |
Scott Hess - ex-Googler
2012/10/12 20:04:51
Same thing in this file as the other file.
| |
46 | 47 |
47 void SetUp() { | 48 void SetUp() { |
48 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 49 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
49 ASSERT_TRUE(db_.Open(temp_dir_.path().AppendASCII("SQLStatementTest.db"))); | 50 ASSERT_TRUE(db_.Open(temp_dir_.path().AppendASCII("SQLStatementTest.db"))); |
50 | 51 |
51 // The |error_handler_| will be called if any sqlite statement operation | 52 // The |error_handler_| will be called if any sqlite statement operation |
52 // returns an error code. | 53 // returns an error code. |
53 db_.set_error_delegate(error_handler_); | 54 db_.set_error_delegate(error_handler_); |
54 } | 55 } |
55 | 56 |
56 void TearDown() { | 57 void TearDown() { |
57 // If any error happened the original sql statement can be found in | 58 // If any error happened the original sql statement can be found in |
58 // error_handler_->sql_statement(). | 59 // error_handler_->sql_statement(). |
59 EXPECT_EQ(SQLITE_OK, error_handler_->error()); | 60 EXPECT_EQ(SQLITE_OK, error_handler_->error()); |
60 db_.Close(); | 61 db_.Close(); |
61 } | 62 } |
62 | 63 |
63 sql::Connection& db() { return db_; } | 64 sql::Connection& db() { return db_; } |
64 | 65 |
65 int sqlite_error() const { return error_handler_->error(); } | 66 int sqlite_error() const { return error_handler_->error(); } |
66 void reset_error() const { error_handler_->reset_error(); } | 67 void reset_error() const { error_handler_->reset_error(); } |
67 | 68 |
68 private: | 69 private: |
69 ScopedTempDir temp_dir_; | 70 ScopedTempDir temp_dir_; |
70 sql::Connection db_; | 71 sql::Connection db_; |
71 scoped_refptr<StatementErrorHandler> error_handler_; | 72 StatementErrorHandler* error_handler_; |
72 }; | 73 }; |
73 | 74 |
74 TEST_F(SQLStatementTest, Assign) { | 75 TEST_F(SQLStatementTest, Assign) { |
75 sql::Statement s; | 76 sql::Statement s; |
76 EXPECT_FALSE(s.is_valid()); | 77 EXPECT_FALSE(s.is_valid()); |
77 | 78 |
78 s.Assign(db().GetUniqueStatement("CREATE TABLE foo (a, b)")); | 79 s.Assign(db().GetUniqueStatement("CREATE TABLE foo (a, b)")); |
79 EXPECT_TRUE(s.is_valid()); | 80 EXPECT_TRUE(s.is_valid()); |
80 } | 81 } |
81 | 82 |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
138 | 139 |
139 s.Reset(false); | 140 s.Reset(false); |
140 // Verify that we can get all rows again. | 141 // Verify that we can get all rows again. |
141 ASSERT_TRUE(s.Step()); | 142 ASSERT_TRUE(s.Step()); |
142 EXPECT_EQ(12, s.ColumnInt(0)); | 143 EXPECT_EQ(12, s.ColumnInt(0)); |
143 EXPECT_FALSE(s.Step()); | 144 EXPECT_FALSE(s.Step()); |
144 | 145 |
145 s.Reset(true); | 146 s.Reset(true); |
146 ASSERT_FALSE(s.Step()); | 147 ASSERT_FALSE(s.Step()); |
147 } | 148 } |
OLD | NEW |