Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(184)

Side by Side Diff: sql/statement_unittest.cc

Issue 17726002: [sql] Callback and scoped-ignore tests for sql::Statement. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Add test for whether curried values stay live. Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <string> 5 #include <string>
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/file_util.h" 8 #include "base/file_util.h"
9 #include "base/files/scoped_temp_dir.h" 9 #include "base/files/scoped_temp_dir.h"
10 #include "sql/connection.h" 10 #include "sql/connection.h"
11 #include "sql/statement.h" 11 #include "sql/statement.h"
12 #include "sql/test/error_callback_support.h"
13 #include "sql/test/scoped_error_ignorer.h"
12 #include "testing/gtest/include/gtest/gtest.h" 14 #include "testing/gtest/include/gtest/gtest.h"
13 #include "third_party/sqlite/sqlite3.h" 15 #include "third_party/sqlite/sqlite3.h"
14 16
15 namespace { 17 namespace {
16 18
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";
22 }
23
24 class SQLStatementTest : public testing::Test { 19 class SQLStatementTest : public testing::Test {
25 public: 20 public:
26 SQLStatementTest() : error_(SQLITE_OK) {}
27
28 virtual void SetUp() { 21 virtual void SetUp() {
29 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 22 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
30 ASSERT_TRUE(db_.Open(temp_dir_.path().AppendASCII("SQLStatementTest.db"))); 23 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_));
35 } 24 }
36 25
37 virtual void TearDown() { 26 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(); 27 db_.Close();
42 } 28 }
43 29
44 sql::Connection& db() { return db_; } 30 sql::Connection& db() { return db_; }
45 31
46 int sqlite_error() const { return error_; }
47
48 void ResetError() {
49 error_ = SQLITE_OK;
50 sql_text_.clear();
51 }
52
53 private: 32 private:
54 base::ScopedTempDir temp_dir_; 33 base::ScopedTempDir temp_dir_;
55 sql::Connection db_; 34 sql::Connection db_;
56
57 // The error code of the most recent error.
58 int error_;
59 // Original statement which caused the error.
60 std::string sql_text_;
61 }; 35 };
62 36
63 } // namespace 37 } // namespace
64 38
65 TEST_F(SQLStatementTest, Assign) { 39 TEST_F(SQLStatementTest, Assign) {
66 sql::Statement s; 40 sql::Statement s;
67 EXPECT_FALSE(s.is_valid()); 41 EXPECT_FALSE(s.is_valid());
68 42
69 s.Assign(db().GetUniqueStatement("CREATE TABLE foo (a, b)")); 43 s.Assign(db().GetUniqueStatement("CREATE TABLE foo (a, b)"));
70 EXPECT_TRUE(s.is_valid()); 44 EXPECT_TRUE(s.is_valid());
(...skipping 23 matching lines...) Expand all
94 68
95 // Binding and stepping should produce one row. 69 // Binding and stepping should produce one row.
96 s.BindInt(0, 3); 70 s.BindInt(0, 3);
97 EXPECT_TRUE(s.Step()); 71 EXPECT_TRUE(s.Step());
98 EXPECT_TRUE(s.Succeeded()); 72 EXPECT_TRUE(s.Succeeded());
99 EXPECT_EQ(12, s.ColumnInt(0)); 73 EXPECT_EQ(12, s.ColumnInt(0));
100 EXPECT_FALSE(s.Step()); 74 EXPECT_FALSE(s.Step());
101 EXPECT_TRUE(s.Succeeded()); 75 EXPECT_TRUE(s.Succeeded());
102 } 76 }
103 77
104 TEST_F(SQLStatementTest, BasicErrorCallback) { 78 // Error callback called for error running a statement.
79 TEST_F(SQLStatementTest, ErrorCallback) {
105 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a INTEGER PRIMARY KEY, b)")); 80 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a INTEGER PRIMARY KEY, b)"));
106 EXPECT_EQ(SQLITE_OK, sqlite_error()); 81
82 int error = SQLITE_OK;
83 sql::ScopedErrorCallback sec(
84 &db(), base::Bind(&sql::CaptureErrorCallback, &error));
85
107 // Insert in the foo table the primary key. It is an error to insert 86 // 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 87 // something other than an number. This error causes the error callback
109 // handler to be called with SQLITE_MISMATCH as error code. 88 // handler to be called with SQLITE_MISMATCH as error code.
110 sql::Statement s(db().GetUniqueStatement("INSERT INTO foo (a) VALUES (?)")); 89 sql::Statement s(db().GetUniqueStatement("INSERT INTO foo (a) VALUES (?)"));
111 EXPECT_TRUE(s.is_valid()); 90 EXPECT_TRUE(s.is_valid());
112 s.BindCString(0, "bad bad"); 91 s.BindCString(0, "bad bad");
113 EXPECT_FALSE(s.Run()); 92 EXPECT_FALSE(s.Run());
114 EXPECT_EQ(SQLITE_MISMATCH, sqlite_error()); 93 EXPECT_EQ(SQLITE_MISMATCH, error);
115 ResetError(); 94 }
95
96 // Error ignorer works for error running a statement.
97 TEST_F(SQLStatementTest, ScopedIgnoreError) {
98 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a INTEGER PRIMARY KEY, b)"));
99
100 sql::Statement s(db().GetUniqueStatement("INSERT INTO foo (a) VALUES (?)"));
101 EXPECT_TRUE(s.is_valid());
102
103 sql::ScopedErrorIgnorer ignore_errors;
104 ignore_errors.IgnoreError(SQLITE_MISMATCH);
105 s.BindCString(0, "bad bad");
106 ASSERT_FALSE(s.Run());
107 ASSERT_TRUE(ignore_errors.CheckIgnoredErrors());
116 } 108 }
117 109
118 TEST_F(SQLStatementTest, Reset) { 110 TEST_F(SQLStatementTest, Reset) {
119 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)")); 111 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)"));
120 ASSERT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (3, 12)")); 112 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)")); 113 ASSERT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (4, 13)"));
122 114
123 sql::Statement s(db().GetUniqueStatement( 115 sql::Statement s(db().GetUniqueStatement(
124 "SELECT b FROM foo WHERE a = ? ")); 116 "SELECT b FROM foo WHERE a = ? "));
125 s.BindInt(0, 3); 117 s.BindInt(0, 3);
126 ASSERT_TRUE(s.Step()); 118 ASSERT_TRUE(s.Step());
127 EXPECT_EQ(12, s.ColumnInt(0)); 119 EXPECT_EQ(12, s.ColumnInt(0));
128 ASSERT_FALSE(s.Step()); 120 ASSERT_FALSE(s.Step());
129 121
130 s.Reset(false); 122 s.Reset(false);
131 // Verify that we can get all rows again. 123 // Verify that we can get all rows again.
132 ASSERT_TRUE(s.Step()); 124 ASSERT_TRUE(s.Step());
133 EXPECT_EQ(12, s.ColumnInt(0)); 125 EXPECT_EQ(12, s.ColumnInt(0));
134 EXPECT_FALSE(s.Step()); 126 EXPECT_FALSE(s.Step());
135 127
136 s.Reset(true); 128 s.Reset(true);
137 ASSERT_FALSE(s.Step()); 129 ASSERT_FALSE(s.Step());
138 } 130 }
OLDNEW
« sql/connection.cc ('K') | « sql/sql.gyp ('k') | sql/test/error_callback_support.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698