[email protected] | 389e0a4 | 2012-04-25 21:36:41 | [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] | faa604e | 2009-09-25 22:38:59 | [diff] [blame] | 5 | #include <string> |
| 6 | |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 7 | #include "base/file_util.h" |
[email protected] | e078590 | 2011-05-19 23:34:17 | [diff] [blame] | 8 | #include "base/scoped_temp_dir.h" |
[email protected] | f0a54b2 | 2011-07-19 18:40:21 | [diff] [blame] | 9 | #include "sql/connection.h" |
| 10 | #include "sql/statement.h" |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 11 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | e33cba4 | 2010-08-18 23:37:03 | [diff] [blame] | 12 | #include "third_party/sqlite/sqlite3.h" |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 13 | |
[email protected] | faa604e | 2009-09-25 22:38:59 | [diff] [blame] | 14 | class StatementErrorHandler : public sql::ErrorDelegate { |
| 15 | public: |
| 16 | StatementErrorHandler() : error_(SQLITE_OK) {} |
| 17 | |
| 18 | virtual int OnError(int error, sql::Connection* connection, |
| 19 | sql::Statement* stmt) { |
| 20 | error_ = error; |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 21 | const char* sql_txt = stmt ? stmt->GetSQLStatement() : NULL; |
[email protected] | faa604e | 2009-09-25 22:38:59 | [diff] [blame] | 22 | sql_text_ = sql_txt ? sql_txt : "no statement available"; |
| 23 | return error; |
| 24 | } |
| 25 | |
| 26 | int error() const { return error_; } |
| 27 | |
| 28 | void reset_error() { |
| 29 | sql_text_.clear(); |
| 30 | error_ = SQLITE_OK; |
| 31 | } |
| 32 | |
| 33 | const char* sql_statement() const { return sql_text_.c_str(); } |
| 34 | |
| 35 | private: |
| 36 | int error_; |
| 37 | std::string sql_text_; |
| 38 | }; |
| 39 | |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 40 | class SQLStatementTest : public testing::Test { |
| 41 | public: |
[email protected] | faa604e | 2009-09-25 22:38:59 | [diff] [blame] | 42 | SQLStatementTest() : error_handler_(new StatementErrorHandler) {} |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 43 | |
| 44 | void SetUp() { |
[email protected] | 3a305db | 2011-04-12 13:40:53 | [diff] [blame] | 45 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 46 | ASSERT_TRUE(db_.Open(temp_dir_.path().AppendASCII("SQLStatementTest.db"))); |
| 47 | |
[email protected] | faa604e | 2009-09-25 22:38:59 | [diff] [blame] | 48 | // The |error_handler_| will be called if any sqlite statement operation |
| 49 | // returns an error code. |
| 50 | db_.set_error_delegate(error_handler_); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | void TearDown() { |
[email protected] | faa604e | 2009-09-25 22:38:59 | [diff] [blame] | 54 | // If any error happened the original sql statement can be found in |
| 55 | // error_handler_->sql_statement(). |
| 56 | EXPECT_EQ(SQLITE_OK, error_handler_->error()); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 57 | db_.Close(); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | sql::Connection& db() { return db_; } |
| 61 | |
[email protected] | faa604e | 2009-09-25 22:38:59 | [diff] [blame] | 62 | int sqlite_error() const { return error_handler_->error(); } |
| 63 | void reset_error() const { error_handler_->reset_error(); } |
| 64 | |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 65 | private: |
[email protected] | 3a305db | 2011-04-12 13:40:53 | [diff] [blame] | 66 | ScopedTempDir temp_dir_; |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 67 | sql::Connection db_; |
[email protected] | faa604e | 2009-09-25 22:38:59 | [diff] [blame] | 68 | scoped_refptr<StatementErrorHandler> error_handler_; |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 69 | }; |
| 70 | |
| 71 | TEST_F(SQLStatementTest, Assign) { |
| 72 | sql::Statement s; |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 73 | EXPECT_FALSE(s.is_valid()); |
| 74 | |
| 75 | s.Assign(db().GetUniqueStatement("CREATE TABLE foo (a, b)")); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 76 | EXPECT_TRUE(s.is_valid()); |
| 77 | } |
| 78 | |
| 79 | TEST_F(SQLStatementTest, Run) { |
| 80 | ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)")); |
| 81 | ASSERT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (3, 12)")); |
| 82 | |
| 83 | sql::Statement s(db().GetUniqueStatement("SELECT b FROM foo WHERE a=?")); |
| 84 | EXPECT_FALSE(s.Succeeded()); |
| 85 | |
| 86 | // Stepping it won't work since we haven't bound the value. |
| 87 | EXPECT_FALSE(s.Step()); |
| 88 | |
| 89 | // Run should fail since this produces output, and we should use Step(). This |
| 90 | // gets a bit wonky since sqlite says this is OK so succeeded is set. |
[email protected] | 389e0a4 | 2012-04-25 21:36:41 | [diff] [blame^] | 91 | s.Reset(true); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 92 | s.BindInt(0, 3); |
| 93 | EXPECT_FALSE(s.Run()); |
| 94 | EXPECT_EQ(SQLITE_ROW, db().GetErrorCode()); |
| 95 | EXPECT_TRUE(s.Succeeded()); |
| 96 | |
| 97 | // Resetting it should put it back to the previous state (not runnable). |
[email protected] | 389e0a4 | 2012-04-25 21:36:41 | [diff] [blame^] | 98 | s.Reset(true); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 99 | EXPECT_FALSE(s.Succeeded()); |
| 100 | |
| 101 | // Binding and stepping should produce one row. |
| 102 | s.BindInt(0, 3); |
| 103 | EXPECT_TRUE(s.Step()); |
| 104 | EXPECT_TRUE(s.Succeeded()); |
| 105 | EXPECT_EQ(12, s.ColumnInt(0)); |
| 106 | EXPECT_FALSE(s.Step()); |
| 107 | EXPECT_TRUE(s.Succeeded()); |
| 108 | } |
[email protected] | faa604e | 2009-09-25 22:38:59 | [diff] [blame] | 109 | |
| 110 | TEST_F(SQLStatementTest, BasicErrorCallback) { |
| 111 | ASSERT_TRUE(db().Execute("CREATE TABLE foo (a INTEGER PRIMARY KEY, b)")); |
| 112 | EXPECT_EQ(SQLITE_OK, sqlite_error()); |
| 113 | // Insert in the foo table the primary key. It is an error to insert |
| 114 | // something other than an number. This error causes the error callback |
| 115 | // handler to be called with SQLITE_MISMATCH as error code. |
| 116 | sql::Statement s(db().GetUniqueStatement("INSERT INTO foo (a) VALUES (?)")); |
| 117 | EXPECT_TRUE(s.is_valid()); |
| 118 | s.BindCString(0, "bad bad"); |
| 119 | EXPECT_FALSE(s.Run()); |
| 120 | EXPECT_EQ(SQLITE_MISMATCH, sqlite_error()); |
| 121 | reset_error(); |
| 122 | } |
[email protected] | 389e0a4 | 2012-04-25 21:36:41 | [diff] [blame^] | 123 | |
| 124 | TEST_F(SQLStatementTest, Reset) { |
| 125 | ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)")); |
| 126 | ASSERT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (3, 12)")); |
| 127 | ASSERT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (4, 13)")); |
| 128 | |
| 129 | sql::Statement s(db().GetUniqueStatement( |
| 130 | "SELECT b FROM foo WHERE a = ? ")); |
| 131 | s.BindInt(0, 3); |
| 132 | ASSERT_TRUE(s.Step()); |
| 133 | EXPECT_EQ(12, s.ColumnInt(0)); |
| 134 | ASSERT_FALSE(s.Step()); |
| 135 | |
| 136 | s.Reset(false); |
| 137 | // Verify that we can get all rows again. |
| 138 | ASSERT_TRUE(s.Step()); |
| 139 | EXPECT_EQ(12, s.ColumnInt(0)); |
| 140 | EXPECT_FALSE(s.Step()); |
| 141 | |
| 142 | s.Reset(true); |
| 143 | ASSERT_FALSE(s.Step()); |
| 144 | } |