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" |
(...skipping 27 matching lines...) Expand all Loading... |
38 &error_, &sql_text_)); | 38 &error_, &sql_text_)); |
39 } | 39 } |
40 | 40 |
41 void TearDown() override { | 41 void TearDown() override { |
42 // If any error happened the original sql statement can be found in | 42 // If any error happened the original sql statement can be found in |
43 // |sql_text_|. | 43 // |sql_text_|. |
44 EXPECT_EQ(SQLITE_OK, error_) << sql_text_; | 44 EXPECT_EQ(SQLITE_OK, error_) << sql_text_; |
45 db_.Close(); | 45 db_.Close(); |
46 } | 46 } |
47 | 47 |
48 void VerifyAndClearLastError(int expected_error) { | |
49 EXPECT_EQ(expected_error, error_); | |
50 error_ = SQLITE_OK; | |
51 sql_text_.clear(); | |
52 } | |
53 | |
54 sql::Connection& db() { return db_; } | 48 sql::Connection& db() { return db_; } |
| 49 int error() { return error_; } |
55 | 50 |
56 private: | 51 private: |
57 base::ScopedTempDir temp_dir_; | 52 base::ScopedTempDir temp_dir_; |
58 sql::Connection db_; | 53 sql::Connection db_; |
59 | 54 |
60 // The error code of the most recent error. | 55 // The error code of the most recent error. |
61 int error_; | 56 int error_; |
62 // Original statement which has caused the error. | 57 // Original statement which has caused the error. |
63 std::string sql_text_; | 58 std::string sql_text_; |
64 }; | 59 }; |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 // delete support. | 135 // delete support. |
141 TEST_F(SQLiteFeaturesTest, ForeignKeySupport) { | 136 TEST_F(SQLiteFeaturesTest, ForeignKeySupport) { |
142 ASSERT_TRUE(db().Execute("PRAGMA foreign_keys=1")); | 137 ASSERT_TRUE(db().Execute("PRAGMA foreign_keys=1")); |
143 ASSERT_TRUE(db().Execute("CREATE TABLE parents (id INTEGER PRIMARY KEY)")); | 138 ASSERT_TRUE(db().Execute("CREATE TABLE parents (id INTEGER PRIMARY KEY)")); |
144 ASSERT_TRUE(db().Execute( | 139 ASSERT_TRUE(db().Execute( |
145 "CREATE TABLE children (" | 140 "CREATE TABLE children (" |
146 " id INTEGER PRIMARY KEY," | 141 " id INTEGER PRIMARY KEY," |
147 " pid INTEGER NOT NULL REFERENCES parents(id) ON DELETE CASCADE)")); | 142 " pid INTEGER NOT NULL REFERENCES parents(id) ON DELETE CASCADE)")); |
148 | 143 |
149 // Inserting without a matching parent should fail with constraint violation. | 144 // Inserting without a matching parent should fail with constraint violation. |
150 EXPECT_FALSE(db().Execute("INSERT INTO children VALUES (10, 1)")); | 145 // Mask off any extended error codes for USE_SYSTEM_SQLITE. |
151 VerifyAndClearLastError(SQLITE_CONSTRAINT); | 146 int insertErr = db().ExecuteAndReturnErrorCode( |
| 147 "INSERT INTO children VALUES (10, 1)"); |
| 148 EXPECT_EQ(SQLITE_CONSTRAINT, (insertErr&0xff)); |
152 | 149 |
153 size_t rows; | 150 size_t rows; |
154 EXPECT_TRUE(sql::test::CountTableRows(&db(), "children", &rows)); | 151 EXPECT_TRUE(sql::test::CountTableRows(&db(), "children", &rows)); |
155 EXPECT_EQ(0u, rows); | 152 EXPECT_EQ(0u, rows); |
156 | 153 |
157 // Inserting with a matching parent should work. | 154 // Inserting with a matching parent should work. |
158 ASSERT_TRUE(db().Execute("INSERT INTO parents VALUES (1)")); | 155 ASSERT_TRUE(db().Execute("INSERT INTO parents VALUES (1)")); |
159 EXPECT_TRUE(db().Execute("INSERT INTO children VALUES (11, 1)")); | 156 EXPECT_TRUE(db().Execute("INSERT INTO children VALUES (11, 1)")); |
160 EXPECT_TRUE(db().Execute("INSERT INTO children VALUES (12, 1)")); | 157 EXPECT_TRUE(db().Execute("INSERT INTO children VALUES (12, 1)")); |
161 EXPECT_TRUE(sql::test::CountTableRows(&db(), "children", &rows)); | 158 EXPECT_TRUE(sql::test::CountTableRows(&db(), "children", &rows)); |
162 EXPECT_EQ(2u, rows); | 159 EXPECT_EQ(2u, rows); |
163 | 160 |
164 // Deleting the parent should cascade, i.e., delete the children as well. | 161 // Deleting the parent should cascade, i.e., delete the children as well. |
165 ASSERT_TRUE(db().Execute("DELETE FROM parents")); | 162 ASSERT_TRUE(db().Execute("DELETE FROM parents")); |
166 EXPECT_TRUE(sql::test::CountTableRows(&db(), "children", &rows)); | 163 EXPECT_TRUE(sql::test::CountTableRows(&db(), "children", &rows)); |
167 EXPECT_EQ(0u, rows); | 164 EXPECT_EQ(0u, rows); |
168 } | 165 } |
169 | 166 |
170 } // namespace | 167 } // namespace |
OLD | NEW |