blob: 1d10fe0b1ef20b7da4abcdf22025058c5244280e [file] [log] [blame]
[email protected]3a305db2011-04-12 13:40:531// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]e5ffd0e42009-09-11 21:30:562// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]faa604e2009-09-25 22:38:595#include <string>
6
[email protected]e5ffd0e42009-09-11 21:30:567#include "base/file_util.h"
[email protected]e0785902011-05-19 23:34:178#include "base/scoped_temp_dir.h"
[email protected]f0a54b22011-07-19 18:40:219#include "sql/connection.h"
10#include "sql/statement.h"
[email protected]e5ffd0e42009-09-11 21:30:5611#include "testing/gtest/include/gtest/gtest.h"
[email protected]e33cba42010-08-18 23:37:0312#include "third_party/sqlite/sqlite3.h"
[email protected]e5ffd0e42009-09-11 21:30:5613
[email protected]faa604e2009-09-25 22:38:5914class 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]765b44502009-10-02 05:01:4221 const char* sql_txt = stmt ? stmt->GetSQLStatement() : NULL;
[email protected]faa604e2009-09-25 22:38:5922 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]e5ffd0e42009-09-11 21:30:5640class SQLStatementTest : public testing::Test {
41 public:
[email protected]faa604e2009-09-25 22:38:5942 SQLStatementTest() : error_handler_(new StatementErrorHandler) {}
[email protected]e5ffd0e42009-09-11 21:30:5643
44 void SetUp() {
[email protected]3a305db2011-04-12 13:40:5345 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
46 ASSERT_TRUE(db_.Open(temp_dir_.path().AppendASCII("SQLStatementTest.db")));
47
[email protected]faa604e2009-09-25 22:38:5948 // 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]e5ffd0e42009-09-11 21:30:5651 }
52
53 void TearDown() {
[email protected]faa604e2009-09-25 22:38:5954 // 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]e5ffd0e42009-09-11 21:30:5657 db_.Close();
[email protected]e5ffd0e42009-09-11 21:30:5658 }
59
60 sql::Connection& db() { return db_; }
61
[email protected]faa604e2009-09-25 22:38:5962 int sqlite_error() const { return error_handler_->error(); }
63 void reset_error() const { error_handler_->reset_error(); }
64
[email protected]e5ffd0e42009-09-11 21:30:5665 private:
[email protected]3a305db2011-04-12 13:40:5366 ScopedTempDir temp_dir_;
[email protected]e5ffd0e42009-09-11 21:30:5667 sql::Connection db_;
[email protected]faa604e2009-09-25 22:38:5968 scoped_refptr<StatementErrorHandler> error_handler_;
[email protected]e5ffd0e42009-09-11 21:30:5669};
70
71TEST_F(SQLStatementTest, Assign) {
72 sql::Statement s;
73 EXPECT_FALSE(s); // bool conversion operator.
74 EXPECT_TRUE(!s); // ! operator.
75 EXPECT_FALSE(s.is_valid());
76
77 s.Assign(db().GetUniqueStatement("CREATE TABLE foo (a, b)"));
78 EXPECT_TRUE(s);
79 EXPECT_FALSE(!s);
80 EXPECT_TRUE(s.is_valid());
81}
82
83TEST_F(SQLStatementTest, Run) {
84 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)"));
85 ASSERT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (3, 12)"));
86
87 sql::Statement s(db().GetUniqueStatement("SELECT b FROM foo WHERE a=?"));
88 EXPECT_FALSE(s.Succeeded());
89
90 // Stepping it won't work since we haven't bound the value.
91 EXPECT_FALSE(s.Step());
92
93 // Run should fail since this produces output, and we should use Step(). This
94 // gets a bit wonky since sqlite says this is OK so succeeded is set.
95 s.Reset();
96 s.BindInt(0, 3);
97 EXPECT_FALSE(s.Run());
98 EXPECT_EQ(SQLITE_ROW, db().GetErrorCode());
99 EXPECT_TRUE(s.Succeeded());
100
101 // Resetting it should put it back to the previous state (not runnable).
102 s.Reset();
103 EXPECT_FALSE(s.Succeeded());
104
105 // Binding and stepping should produce one row.
106 s.BindInt(0, 3);
107 EXPECT_TRUE(s.Step());
108 EXPECT_TRUE(s.Succeeded());
109 EXPECT_EQ(12, s.ColumnInt(0));
110 EXPECT_FALSE(s.Step());
111 EXPECT_TRUE(s.Succeeded());
112}
[email protected]faa604e2009-09-25 22:38:59113
114TEST_F(SQLStatementTest, BasicErrorCallback) {
115 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a INTEGER PRIMARY KEY, b)"));
116 EXPECT_EQ(SQLITE_OK, sqlite_error());
117 // Insert in the foo table the primary key. It is an error to insert
118 // something other than an number. This error causes the error callback
119 // handler to be called with SQLITE_MISMATCH as error code.
120 sql::Statement s(db().GetUniqueStatement("INSERT INTO foo (a) VALUES (?)"));
121 EXPECT_TRUE(s.is_valid());
122 s.BindCString(0, "bad bad");
123 EXPECT_FALSE(s.Run());
124 EXPECT_EQ(SQLITE_MISMATCH, sqlite_error());
125 reset_error();
126}