blob: 5f0488001cc968007a0f4b9989e434e4d2577168 [file] [log] [blame]
[email protected]e5ffd0e42009-09-11 21:30:561// Copyright (c) 2009 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
[email protected]faa604e2009-09-25 22:38:595#include <string>
6
[email protected]e5ffd0e42009-09-11 21:30:567#include "app/sql/connection.h"
8#include "app/sql/statement.h"
9#include "base/file_path.h"
10#include "base/file_util.h"
11#include "base/path_service.h"
12#include "testing/gtest/include/gtest/gtest.h"
13#include "third_party/sqlite/preprocessed/sqlite3.h"
14
[email protected]faa604e2009-09-25 22:38:5915class StatementErrorHandler : public sql::ErrorDelegate {
16 public:
17 StatementErrorHandler() : error_(SQLITE_OK) {}
18
19 virtual int OnError(int error, sql::Connection* connection,
20 sql::Statement* stmt) {
21 error_ = error;
22 const char* sql_txt = stmt->GetSQLStatement();
23 sql_text_ = sql_txt ? sql_txt : "no statement available";
24 return error;
25 }
26
27 int error() const { return error_; }
28
29 void reset_error() {
30 sql_text_.clear();
31 error_ = SQLITE_OK;
32 }
33
34 const char* sql_statement() const { return sql_text_.c_str(); }
35
36 private:
37 int error_;
38 std::string sql_text_;
39};
40
[email protected]e5ffd0e42009-09-11 21:30:5641class SQLStatementTest : public testing::Test {
42 public:
[email protected]faa604e2009-09-25 22:38:5943 SQLStatementTest() : error_handler_(new StatementErrorHandler) {}
[email protected]e5ffd0e42009-09-11 21:30:5644
45 void SetUp() {
46 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &path_));
47 path_ = path_.AppendASCII("SQLStatementTest.db");
48 file_util::Delete(path_, false);
49 ASSERT_TRUE(db_.Init(path_));
[email protected]faa604e2009-09-25 22:38:5950 // The |error_handler_| will be called if any sqlite statement operation
51 // returns an error code.
52 db_.set_error_delegate(error_handler_);
[email protected]e5ffd0e42009-09-11 21:30:5653 }
54
55 void TearDown() {
[email protected]faa604e2009-09-25 22:38:5956 // If any error happened the original sql statement can be found in
57 // error_handler_->sql_statement().
58 EXPECT_EQ(SQLITE_OK, error_handler_->error());
[email protected]e5ffd0e42009-09-11 21:30:5659 db_.Close();
[email protected]e5ffd0e42009-09-11 21:30:5660 // If this fails something is going on with cleanup and later tests may
61 // fail, so we want to identify problems right away.
62 ASSERT_TRUE(file_util::Delete(path_, false));
63 }
64
65 sql::Connection& db() { return db_; }
66
[email protected]faa604e2009-09-25 22:38:5967 int sqlite_error() const { return error_handler_->error(); }
68 void reset_error() const { error_handler_->reset_error(); }
69
[email protected]e5ffd0e42009-09-11 21:30:5670 private:
71 FilePath path_;
72 sql::Connection db_;
[email protected]faa604e2009-09-25 22:38:5973 scoped_refptr<StatementErrorHandler> error_handler_;
[email protected]e5ffd0e42009-09-11 21:30:5674};
75
76TEST_F(SQLStatementTest, Assign) {
77 sql::Statement s;
78 EXPECT_FALSE(s); // bool conversion operator.
79 EXPECT_TRUE(!s); // ! operator.
80 EXPECT_FALSE(s.is_valid());
81
82 s.Assign(db().GetUniqueStatement("CREATE TABLE foo (a, b)"));
83 EXPECT_TRUE(s);
84 EXPECT_FALSE(!s);
85 EXPECT_TRUE(s.is_valid());
86}
87
88TEST_F(SQLStatementTest, Run) {
89 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)"));
90 ASSERT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (3, 12)"));
91
92 sql::Statement s(db().GetUniqueStatement("SELECT b FROM foo WHERE a=?"));
93 EXPECT_FALSE(s.Succeeded());
94
95 // Stepping it won't work since we haven't bound the value.
96 EXPECT_FALSE(s.Step());
97
98 // Run should fail since this produces output, and we should use Step(). This
99 // gets a bit wonky since sqlite says this is OK so succeeded is set.
100 s.Reset();
101 s.BindInt(0, 3);
102 EXPECT_FALSE(s.Run());
103 EXPECT_EQ(SQLITE_ROW, db().GetErrorCode());
104 EXPECT_TRUE(s.Succeeded());
105
106 // Resetting it should put it back to the previous state (not runnable).
107 s.Reset();
108 EXPECT_FALSE(s.Succeeded());
109
110 // Binding and stepping should produce one row.
111 s.BindInt(0, 3);
112 EXPECT_TRUE(s.Step());
113 EXPECT_TRUE(s.Succeeded());
114 EXPECT_EQ(12, s.ColumnInt(0));
115 EXPECT_FALSE(s.Step());
116 EXPECT_TRUE(s.Succeeded());
117}
[email protected]faa604e2009-09-25 22:38:59118
119TEST_F(SQLStatementTest, BasicErrorCallback) {
120 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a INTEGER PRIMARY KEY, b)"));
121 EXPECT_EQ(SQLITE_OK, sqlite_error());
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
124 // handler to be called with SQLITE_MISMATCH as error code.
125 sql::Statement s(db().GetUniqueStatement("INSERT INTO foo (a) VALUES (?)"));
126 EXPECT_TRUE(s.is_valid());
127 s.BindCString(0, "bad bad");
128 EXPECT_FALSE(s.Run());
129 EXPECT_EQ(SQLITE_MISMATCH, sqlite_error());
130 reset_error();
131}