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 22 matching lines...) Expand all Loading... | |
33 | 33 |
34 // The error delegate will set |error_| and |sql_text_| when any sqlite | 34 // The error delegate will set |error_| and |sql_text_| when any sqlite |
35 // statement operation returns an error code. | 35 // statement operation returns an error code. |
36 db_.set_error_callback(base::Bind(&CaptureErrorCallback, | 36 db_.set_error_callback(base::Bind(&CaptureErrorCallback, |
37 &error_, &sql_text_)); | 37 &error_, &sql_text_)); |
38 } | 38 } |
39 | 39 |
40 void TearDown() override { | 40 void TearDown() override { |
41 // If any error happened the original sql statement can be found in | 41 // If any error happened the original sql statement can be found in |
42 // |sql_text_|. | 42 // |sql_text_|. |
43 EXPECT_EQ(SQLITE_OK, error_); | 43 EXPECT_EQ(SQLITE_OK, error_) << sql_text_; |
44 db_.Close(); | 44 db_.Close(); |
45 } | 45 } |
46 | 46 |
47 void VerifyAndClearLastError(int expected_error) { | |
48 EXPECT_EQ(expected_error, error_); | |
49 error_ = SQLITE_OK; | |
50 sql_text_.clear(); | |
51 } | |
52 | |
47 sql::Connection& db() { return db_; } | 53 sql::Connection& db() { return db_; } |
48 | 54 |
49 private: | 55 private: |
50 base::ScopedTempDir temp_dir_; | 56 base::ScopedTempDir temp_dir_; |
51 sql::Connection db_; | 57 sql::Connection db_; |
52 | 58 |
53 // The error code of the most recent error. | 59 // The error code of the most recent error. |
54 int error_; | 60 int error_; |
55 // Original statement which has caused the error. | 61 // Original statement which has caused the error. |
56 std::string sql_text_; | 62 std::string sql_text_; |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
105 | 111 |
106 ASSERT_TRUE(db().Execute("INSERT INTO foo (x) VALUES ('test')")); | 112 ASSERT_TRUE(db().Execute("INSERT INTO foo (x) VALUES ('test')")); |
107 | 113 |
108 sql::Statement s(db().GetUniqueStatement( | 114 sql::Statement s(db().GetUniqueStatement( |
109 "SELECT x FROM foo WHERE x MATCH 'te*'")); | 115 "SELECT x FROM foo WHERE x MATCH 'te*'")); |
110 ASSERT_TRUE(s.Step()); | 116 ASSERT_TRUE(s.Step()); |
111 EXPECT_EQ("test", s.ColumnString(0)); | 117 EXPECT_EQ("test", s.ColumnString(0)); |
112 } | 118 } |
113 #endif | 119 #endif |
114 | 120 |
121 // Ensure that our SQLite version has working foreign key support. | |
122 TEST_F(SQLiteFeaturesTest, ForeignKeySupport) { | |
123 ASSERT_TRUE(db().Execute("PRAGMA foreign_keys=1")); | |
124 ASSERT_TRUE(db().Execute("CREATE TABLE parents (id INTEGER PRIMARY KEY)")); | |
125 ASSERT_TRUE(db().Execute( | |
126 "CREATE TABLE children (" | |
127 " id INTEGER PRIMARY KEY," | |
128 " pid INTEGER NOT NULL REFERENCES parents(id) ON DELETE CASCADE)")); | |
129 | |
130 // Inserting without a matching parent should fail with constraint violation. | |
131 EXPECT_FALSE(db().Execute("INSERT INTO children VALUES (10, 1)")); | |
132 VerifyAndClearLastError(SQLITE_CONSTRAINT); | |
133 | |
134 // Inserting with a matching parent should work. | |
135 ASSERT_TRUE(db().Execute("INSERT INTO parents VALUES (1)")); | |
136 EXPECT_TRUE(db().Execute("INSERT INTO children VALUES (11, 1)")); | |
137 EXPECT_TRUE(db().Execute("INSERT INTO children VALUES (12, 1)")); | |
138 | |
139 // Deleting the parent should cascade, i.e., delete the children as well. | |
140 ASSERT_TRUE(db().Execute("DELETE FROM parents")); | |
141 sql::Statement s(db().GetUniqueStatement("SELECT * FROM children")); | |
142 EXPECT_FALSE(s.Step()); | |
143 EXPECT_TRUE(s.Succeeded()); | |
Scott Hess - ex-Googler
2015/01/21 18:17:02
This may be prone to false positives, since there
engedy
2015/01/21 19:50:22
Do you think doing it with SELECT COUNT(*) would b
Scott Hess - ex-Googler
2015/01/21 20:23:30
Not in-place, because that still wouldn't demonstr
engedy
2015/01/22 09:11:07
Done.
| |
144 } | |
145 | |
115 } // namespace | 146 } // namespace |
OLD | NEW |