blob: 3d32862c79276730c4333a58c71f1b2c208d5f77 [file] [log] [blame]
[email protected]389e0a42012-04-25 21:36:411// Copyright (c) 2012 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]526b4662013-06-14 04:09:127#include "base/bind.h"
[email protected]e5ffd0e42009-09-11 21:30:568#include "base/file_util.h"
[email protected]ea1a3f62012-11-16 20:34:239#include "base/files/scoped_temp_dir.h"
[email protected]f0a54b22011-07-19 18:40:2110#include "sql/connection.h"
11#include "sql/statement.h"
[email protected]e5ffd0e42009-09-11 21:30:5612#include "testing/gtest/include/gtest/gtest.h"
[email protected]e33cba42010-08-18 23:37:0313#include "third_party/sqlite/sqlite3.h"
[email protected]e5ffd0e42009-09-11 21:30:5614
[email protected]49dc4f22012-10-17 17:41:1615namespace {
16
[email protected]526b4662013-06-14 04:09:1217void CaptureErrorCallback(int* error_pointer, std::string* sql_text,
18 int error, sql::Statement* stmt) {
19 *error_pointer = error;
20 const char* text = stmt ? stmt->GetSQLStatement() : NULL;
21 *sql_text = text ? text : "no statement available";
22}
[email protected]faa604e2009-09-25 22:38:5923
[email protected]e5ffd0e42009-09-11 21:30:5624class SQLStatementTest : public testing::Test {
25 public:
[email protected]49dc4f22012-10-17 17:41:1626 SQLStatementTest() : error_(SQLITE_OK) {}
[email protected]e5ffd0e42009-09-11 21:30:5627
[email protected]3dbbf7d2013-02-06 18:13:5328 virtual void SetUp() {
[email protected]3a305db2011-04-12 13:40:5329 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
30 ASSERT_TRUE(db_.Open(temp_dir_.path().AppendASCII("SQLStatementTest.db")));
[email protected]49dc4f22012-10-17 17:41:1631 // The error delegate will set |error_| and |sql_text_| when any sqlite
32 // statement operation returns an error code.
[email protected]526b4662013-06-14 04:09:1233 db_.set_error_callback(base::Bind(&CaptureErrorCallback,
34 &error_, &sql_text_));
[email protected]e5ffd0e42009-09-11 21:30:5635 }
36
[email protected]3dbbf7d2013-02-06 18:13:5337 virtual void TearDown() {
[email protected]faa604e2009-09-25 22:38:5938 // If any error happened the original sql statement can be found in
[email protected]49dc4f22012-10-17 17:41:1639 // |sql_text_|.
40 EXPECT_EQ(SQLITE_OK, error_);
[email protected]e5ffd0e42009-09-11 21:30:5641 db_.Close();
[email protected]e5ffd0e42009-09-11 21:30:5642 }
43
44 sql::Connection& db() { return db_; }
45
[email protected]49dc4f22012-10-17 17:41:1646 int sqlite_error() const { return error_; }
47
48 void ResetError() {
49 error_ = SQLITE_OK;
50 sql_text_.clear();
51 }
[email protected]faa604e2009-09-25 22:38:5952
[email protected]e5ffd0e42009-09-11 21:30:5653 private:
[email protected]ea1a3f62012-11-16 20:34:2354 base::ScopedTempDir temp_dir_;
[email protected]e5ffd0e42009-09-11 21:30:5655 sql::Connection db_;
[email protected]49dc4f22012-10-17 17:41:1656
57 // The error code of the most recent error.
58 int error_;
59 // Original statement which caused the error.
60 std::string sql_text_;
[email protected]e5ffd0e42009-09-11 21:30:5661};
62
[email protected]49dc4f22012-10-17 17:41:1663} // namespace
64
[email protected]e5ffd0e42009-09-11 21:30:5665TEST_F(SQLStatementTest, Assign) {
66 sql::Statement s;
[email protected]e5ffd0e42009-09-11 21:30:5667 EXPECT_FALSE(s.is_valid());
68
69 s.Assign(db().GetUniqueStatement("CREATE TABLE foo (a, b)"));
[email protected]e5ffd0e42009-09-11 21:30:5670 EXPECT_TRUE(s.is_valid());
71}
72
73TEST_F(SQLStatementTest, Run) {
74 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)"));
75 ASSERT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (3, 12)"));
76
77 sql::Statement s(db().GetUniqueStatement("SELECT b FROM foo WHERE a=?"));
78 EXPECT_FALSE(s.Succeeded());
79
80 // Stepping it won't work since we haven't bound the value.
81 EXPECT_FALSE(s.Step());
82
83 // Run should fail since this produces output, and we should use Step(). This
84 // gets a bit wonky since sqlite says this is OK so succeeded is set.
[email protected]389e0a42012-04-25 21:36:4185 s.Reset(true);
[email protected]e5ffd0e42009-09-11 21:30:5686 s.BindInt(0, 3);
87 EXPECT_FALSE(s.Run());
88 EXPECT_EQ(SQLITE_ROW, db().GetErrorCode());
89 EXPECT_TRUE(s.Succeeded());
90
91 // Resetting it should put it back to the previous state (not runnable).
[email protected]389e0a42012-04-25 21:36:4192 s.Reset(true);
[email protected]e5ffd0e42009-09-11 21:30:5693 EXPECT_FALSE(s.Succeeded());
94
95 // Binding and stepping should produce one row.
96 s.BindInt(0, 3);
97 EXPECT_TRUE(s.Step());
98 EXPECT_TRUE(s.Succeeded());
99 EXPECT_EQ(12, s.ColumnInt(0));
100 EXPECT_FALSE(s.Step());
101 EXPECT_TRUE(s.Succeeded());
102}
[email protected]faa604e2009-09-25 22:38:59103
104TEST_F(SQLStatementTest, BasicErrorCallback) {
105 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a INTEGER PRIMARY KEY, b)"));
106 EXPECT_EQ(SQLITE_OK, sqlite_error());
107 // Insert in the foo table the primary key. It is an error to insert
108 // something other than an number. This error causes the error callback
109 // handler to be called with SQLITE_MISMATCH as error code.
110 sql::Statement s(db().GetUniqueStatement("INSERT INTO foo (a) VALUES (?)"));
111 EXPECT_TRUE(s.is_valid());
112 s.BindCString(0, "bad bad");
113 EXPECT_FALSE(s.Run());
114 EXPECT_EQ(SQLITE_MISMATCH, sqlite_error());
[email protected]49dc4f22012-10-17 17:41:16115 ResetError();
[email protected]faa604e2009-09-25 22:38:59116}
[email protected]389e0a42012-04-25 21:36:41117
118TEST_F(SQLStatementTest, Reset) {
119 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)"));
120 ASSERT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (3, 12)"));
121 ASSERT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (4, 13)"));
122
123 sql::Statement s(db().GetUniqueStatement(
124 "SELECT b FROM foo WHERE a = ? "));
125 s.BindInt(0, 3);
126 ASSERT_TRUE(s.Step());
127 EXPECT_EQ(12, s.ColumnInt(0));
128 ASSERT_FALSE(s.Step());
129
130 s.Reset(false);
131 // Verify that we can get all rows again.
132 ASSERT_TRUE(s.Step());
133 EXPECT_EQ(12, s.ColumnInt(0));
134 EXPECT_FALSE(s.Step());
135
136 s.Reset(true);
137 ASSERT_FALSE(s.Step());
138}