Blink roll: 152400:152445.
[chromium-blink-merge.git] / sql / statement_unittest.cc
blob3d32862c79276730c4333a58c71f1b2c208d5f77
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.
5 #include <string>
7 #include "base/bind.h"
8 #include "base/file_util.h"
9 #include "base/files/scoped_temp_dir.h"
10 #include "sql/connection.h"
11 #include "sql/statement.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "third_party/sqlite/sqlite3.h"
15 namespace {
17 void 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";
24 class SQLStatementTest : public testing::Test {
25 public:
26 SQLStatementTest() : error_(SQLITE_OK) {}
28 virtual void SetUp() {
29 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
30 ASSERT_TRUE(db_.Open(temp_dir_.path().AppendASCII("SQLStatementTest.db")));
31 // The error delegate will set |error_| and |sql_text_| when any sqlite
32 // statement operation returns an error code.
33 db_.set_error_callback(base::Bind(&CaptureErrorCallback,
34 &error_, &sql_text_));
37 virtual void TearDown() {
38 // If any error happened the original sql statement can be found in
39 // |sql_text_|.
40 EXPECT_EQ(SQLITE_OK, error_);
41 db_.Close();
44 sql::Connection& db() { return db_; }
46 int sqlite_error() const { return error_; }
48 void ResetError() {
49 error_ = SQLITE_OK;
50 sql_text_.clear();
53 private:
54 base::ScopedTempDir temp_dir_;
55 sql::Connection db_;
57 // The error code of the most recent error.
58 int error_;
59 // Original statement which caused the error.
60 std::string sql_text_;
63 } // namespace
65 TEST_F(SQLStatementTest, Assign) {
66 sql::Statement s;
67 EXPECT_FALSE(s.is_valid());
69 s.Assign(db().GetUniqueStatement("CREATE TABLE foo (a, b)"));
70 EXPECT_TRUE(s.is_valid());
73 TEST_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)"));
77 sql::Statement s(db().GetUniqueStatement("SELECT b FROM foo WHERE a=?"));
78 EXPECT_FALSE(s.Succeeded());
80 // Stepping it won't work since we haven't bound the value.
81 EXPECT_FALSE(s.Step());
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.
85 s.Reset(true);
86 s.BindInt(0, 3);
87 EXPECT_FALSE(s.Run());
88 EXPECT_EQ(SQLITE_ROW, db().GetErrorCode());
89 EXPECT_TRUE(s.Succeeded());
91 // Resetting it should put it back to the previous state (not runnable).
92 s.Reset(true);
93 EXPECT_FALSE(s.Succeeded());
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());
104 TEST_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());
115 ResetError();
118 TEST_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)"));
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());
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());
136 s.Reset(true);
137 ASSERT_FALSE(s.Step());