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