OLD | NEW |
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/files/file_util.h" | 8 #include "base/files/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/correct_sql_test_base.h" | 11 #include "sql/correct_sql_test_base.h" |
12 #include "sql/statement.h" | 12 #include "sql/statement.h" |
13 #include "sql/test/error_callback_support.h" | 13 #include "sql/test/error_callback_support.h" |
14 #include "sql/test/scoped_error_ignorer.h" | 14 #include "sql/test/scoped_error_expecter.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
16 #include "third_party/sqlite/sqlite3.h" | 16 #include "third_party/sqlite/sqlite3.h" |
17 | 17 |
18 namespace { | 18 namespace { |
19 | 19 |
20 using SQLStatementTest = sql::SQLTestBase; | 20 using SQLStatementTest = sql::SQLTestBase; |
21 | 21 |
22 } // namespace | 22 } // namespace |
23 | 23 |
24 TEST_F(SQLStatementTest, Assign) { | 24 TEST_F(SQLStatementTest, Assign) { |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 // Insert in the foo table the primary key. It is an error to insert | 71 // Insert in the foo table the primary key. It is an error to insert |
72 // something other than an number. This error causes the error callback | 72 // something other than an number. This error causes the error callback |
73 // handler to be called with SQLITE_MISMATCH as error code. | 73 // handler to be called with SQLITE_MISMATCH as error code. |
74 sql::Statement s(db().GetUniqueStatement("INSERT INTO foo (a) VALUES (?)")); | 74 sql::Statement s(db().GetUniqueStatement("INSERT INTO foo (a) VALUES (?)")); |
75 EXPECT_TRUE(s.is_valid()); | 75 EXPECT_TRUE(s.is_valid()); |
76 s.BindCString(0, "bad bad"); | 76 s.BindCString(0, "bad bad"); |
77 EXPECT_FALSE(s.Run()); | 77 EXPECT_FALSE(s.Run()); |
78 EXPECT_EQ(SQLITE_MISMATCH, error); | 78 EXPECT_EQ(SQLITE_MISMATCH, error); |
79 } | 79 } |
80 | 80 |
81 // Error ignorer works for error running a statement. | 81 // Error expecter works for error running a statement. |
82 TEST_F(SQLStatementTest, ScopedIgnoreError) { | 82 TEST_F(SQLStatementTest, ScopedIgnoreError) { |
83 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a INTEGER PRIMARY KEY, b)")); | 83 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a INTEGER PRIMARY KEY, b)")); |
84 | 84 |
85 sql::Statement s(db().GetUniqueStatement("INSERT INTO foo (a) VALUES (?)")); | 85 sql::Statement s(db().GetUniqueStatement("INSERT INTO foo (a) VALUES (?)")); |
86 EXPECT_TRUE(s.is_valid()); | 86 EXPECT_TRUE(s.is_valid()); |
87 | 87 |
88 sql::ScopedErrorIgnorer ignore_errors; | 88 { |
89 ignore_errors.IgnoreError(SQLITE_MISMATCH); | 89 sql::test::ScopedErrorExpecter expecter; |
90 s.BindCString(0, "bad bad"); | 90 expecter.ExpectError(SQLITE_MISMATCH); |
91 ASSERT_FALSE(s.Run()); | 91 s.BindCString(0, "bad bad"); |
92 ASSERT_TRUE(ignore_errors.CheckIgnoredErrors()); | 92 ASSERT_FALSE(s.Run()); |
| 93 ASSERT_TRUE(expecter.SawExpectedErrors()); |
| 94 } |
93 } | 95 } |
94 | 96 |
95 TEST_F(SQLStatementTest, Reset) { | 97 TEST_F(SQLStatementTest, Reset) { |
96 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)")); | 98 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)")); |
97 ASSERT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (3, 12)")); | 99 ASSERT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (3, 12)")); |
98 ASSERT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (4, 13)")); | 100 ASSERT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (4, 13)")); |
99 | 101 |
100 sql::Statement s(db().GetUniqueStatement( | 102 sql::Statement s(db().GetUniqueStatement( |
101 "SELECT b FROM foo WHERE a = ? ")); | 103 "SELECT b FROM foo WHERE a = ? ")); |
102 s.BindInt(0, 3); | 104 s.BindInt(0, 3); |
103 ASSERT_TRUE(s.Step()); | 105 ASSERT_TRUE(s.Step()); |
104 EXPECT_EQ(12, s.ColumnInt(0)); | 106 EXPECT_EQ(12, s.ColumnInt(0)); |
105 ASSERT_FALSE(s.Step()); | 107 ASSERT_FALSE(s.Step()); |
106 | 108 |
107 s.Reset(false); | 109 s.Reset(false); |
108 // Verify that we can get all rows again. | 110 // Verify that we can get all rows again. |
109 ASSERT_TRUE(s.Step()); | 111 ASSERT_TRUE(s.Step()); |
110 EXPECT_EQ(12, s.ColumnInt(0)); | 112 EXPECT_EQ(12, s.ColumnInt(0)); |
111 EXPECT_FALSE(s.Step()); | 113 EXPECT_FALSE(s.Step()); |
112 | 114 |
113 s.Reset(true); | 115 s.Reset(true); |
114 ASSERT_FALSE(s.Step()); | 116 ASSERT_FALSE(s.Step()); |
115 } | 117 } |
OLD | NEW |