OLD | NEW |
---|---|
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "app/sql/connection.h" | 7 #include "app/sql/connection.h" |
8 #include "app/sql/statement.h" | 8 #include "app/sql/statement.h" |
9 #include "base/file_path.h" | |
10 #include "base/file_util.h" | 9 #include "base/file_util.h" |
11 #include "base/path_service.h" | 10 #include "base/memory/scoped_temp_dir.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
13 #include "third_party/sqlite/sqlite3.h" | 12 #include "third_party/sqlite/sqlite3.h" |
14 | 13 |
15 class StatementErrorHandler : public sql::ErrorDelegate { | 14 class StatementErrorHandler : public sql::ErrorDelegate { |
16 public: | 15 public: |
17 StatementErrorHandler() : error_(SQLITE_OK) {} | 16 StatementErrorHandler() : error_(SQLITE_OK) {} |
18 | 17 |
19 virtual int OnError(int error, sql::Connection* connection, | 18 virtual int OnError(int error, sql::Connection* connection, |
20 sql::Statement* stmt) { | 19 sql::Statement* stmt) { |
21 error_ = error; | 20 error_ = error; |
(...skipping 14 matching lines...) Expand all Loading... | |
36 private: | 35 private: |
37 int error_; | 36 int error_; |
38 std::string sql_text_; | 37 std::string sql_text_; |
39 }; | 38 }; |
40 | 39 |
41 class SQLStatementTest : public testing::Test { | 40 class SQLStatementTest : public testing::Test { |
42 public: | 41 public: |
43 SQLStatementTest() : error_handler_(new StatementErrorHandler) {} | 42 SQLStatementTest() : error_handler_(new StatementErrorHandler) {} |
44 | 43 |
45 void SetUp() { | 44 void SetUp() { |
46 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &path_)); | 45 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
47 path_ = path_.AppendASCII("SQLStatementTest.db"); | 46 ASSERT_TRUE(db_.Open(temp_dir_.path().AppendASCII("SQLStatementTest.db"))); |
48 file_util::Delete(path_, false); | 47 |
49 ASSERT_TRUE(db_.Open(path_)); | |
50 // The |error_handler_| will be called if any sqlite statement operation | 48 // The |error_handler_| will be called if any sqlite statement operation |
51 // returns an error code. | 49 // returns an error code. |
52 db_.set_error_delegate(error_handler_); | 50 db_.set_error_delegate(error_handler_); |
53 } | 51 } |
54 | 52 |
55 void TearDown() { | 53 void TearDown() { |
56 // If any error happened the original sql statement can be found in | 54 // If any error happened the original sql statement can be found in |
57 // error_handler_->sql_statement(). | 55 // error_handler_->sql_statement(). |
58 EXPECT_EQ(SQLITE_OK, error_handler_->error()); | 56 EXPECT_EQ(SQLITE_OK, error_handler_->error()); |
59 db_.Close(); | 57 db_.Close(); |
58 | |
60 // If this fails something is going on with cleanup and later tests may | 59 // If this fails something is going on with cleanup and later tests may |
61 // fail, so we want to identify problems right away. | 60 // fail, so we want to identify problems right away. |
62 ASSERT_TRUE(file_util::Delete(path_, false)); | 61 ASSERT_TRUE(file_util::Delete( |
Paweł Hajdan Jr.
2011/04/08 15:58:17
Can be removed too.
Mike West
2011/04/11 14:47:04
Done.
| |
62 temp_dir_.path().AppendASCII("SQLStatementTest.db"), false)); | |
63 } | 63 } |
64 | 64 |
65 sql::Connection& db() { return db_; } | 65 sql::Connection& db() { return db_; } |
66 | 66 |
67 int sqlite_error() const { return error_handler_->error(); } | 67 int sqlite_error() const { return error_handler_->error(); } |
68 void reset_error() const { error_handler_->reset_error(); } | 68 void reset_error() const { error_handler_->reset_error(); } |
69 | 69 |
70 private: | 70 private: |
71 FilePath path_; | 71 ScopedTempDir temp_dir_; |
72 sql::Connection db_; | 72 sql::Connection db_; |
73 scoped_refptr<StatementErrorHandler> error_handler_; | 73 scoped_refptr<StatementErrorHandler> error_handler_; |
74 }; | 74 }; |
75 | 75 |
76 TEST_F(SQLStatementTest, Assign) { | 76 TEST_F(SQLStatementTest, Assign) { |
77 sql::Statement s; | 77 sql::Statement s; |
78 EXPECT_FALSE(s); // bool conversion operator. | 78 EXPECT_FALSE(s); // bool conversion operator. |
79 EXPECT_TRUE(!s); // ! operator. | 79 EXPECT_TRUE(!s); // ! operator. |
80 EXPECT_FALSE(s.is_valid()); | 80 EXPECT_FALSE(s.is_valid()); |
81 | 81 |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
122 // Insert in the foo table the primary key. It is an error to insert | 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 | 123 // something other than an number. This error causes the error callback |
124 // handler to be called with SQLITE_MISMATCH as error code. | 124 // handler to be called with SQLITE_MISMATCH as error code. |
125 sql::Statement s(db().GetUniqueStatement("INSERT INTO foo (a) VALUES (?)")); | 125 sql::Statement s(db().GetUniqueStatement("INSERT INTO foo (a) VALUES (?)")); |
126 EXPECT_TRUE(s.is_valid()); | 126 EXPECT_TRUE(s.is_valid()); |
127 s.BindCString(0, "bad bad"); | 127 s.BindCString(0, "bad bad"); |
128 EXPECT_FALSE(s.Run()); | 128 EXPECT_FALSE(s.Run()); |
129 EXPECT_EQ(SQLITE_MISMATCH, sqlite_error()); | 129 EXPECT_EQ(SQLITE_MISMATCH, sqlite_error()); |
130 reset_error(); | 130 reset_error(); |
131 } | 131 } |
OLD | NEW |