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

Unified Diff: sql/sqlite_features_unittest.cc

Issue 856343002: Add SQLite feature test to verify working foreign key support. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Reduce chance of false positives. Created 5 years, 11 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sql/sqlite_features_unittest.cc
diff --git a/sql/sqlite_features_unittest.cc b/sql/sqlite_features_unittest.cc
index 1aff80e9bbac20d11dfe40c9e90e0021166de30c..9acd37873d105764732a3f450cba07c5cbdacc15 100644
--- a/sql/sqlite_features_unittest.cc
+++ b/sql/sqlite_features_unittest.cc
@@ -9,6 +9,7 @@
#include "base/files/scoped_temp_dir.h"
#include "sql/connection.h"
#include "sql/statement.h"
+#include "sql/test/test_helpers.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/sqlite/sqlite3.h"
@@ -40,10 +41,16 @@ class SQLiteFeaturesTest : public testing::Test {
void TearDown() override {
// If any error happened the original sql statement can be found in
// |sql_text_|.
- EXPECT_EQ(SQLITE_OK, error_);
+ EXPECT_EQ(SQLITE_OK, error_) << sql_text_;
db_.Close();
}
+ void VerifyAndClearLastError(int expected_error) {
+ EXPECT_EQ(expected_error, error_);
+ error_ = SQLITE_OK;
+ sql_text_.clear();
+ }
+
sql::Connection& db() { return db_; }
private:
@@ -112,4 +119,35 @@ TEST_F(SQLiteFeaturesTest, FTS3_Prefix) {
}
#endif
+// Ensure that our SQLite version has working foreign key support with cascade
+// delete support.
+TEST_F(SQLiteFeaturesTest, ForeignKeySupport) {
+ ASSERT_TRUE(db().Execute("PRAGMA foreign_keys=1"));
+ ASSERT_TRUE(db().Execute("CREATE TABLE parents (id INTEGER PRIMARY KEY)"));
+ ASSERT_TRUE(db().Execute(
+ "CREATE TABLE children ("
+ " id INTEGER PRIMARY KEY,"
+ " pid INTEGER NOT NULL REFERENCES parents(id) ON DELETE CASCADE)"));
+
+ // Inserting without a matching parent should fail with constraint violation.
+ EXPECT_FALSE(db().Execute("INSERT INTO children VALUES (10, 1)"));
+ VerifyAndClearLastError(SQLITE_CONSTRAINT);
+
+ size_t rows;
+ EXPECT_TRUE(sql::test::CountTableRows(&db(), "children", &rows));
+ EXPECT_EQ(0u, rows);
+
+ // Inserting with a matching parent should work.
+ ASSERT_TRUE(db().Execute("INSERT INTO parents VALUES (1)"));
+ EXPECT_TRUE(db().Execute("INSERT INTO children VALUES (11, 1)"));
+ EXPECT_TRUE(db().Execute("INSERT INTO children VALUES (12, 1)"));
+ EXPECT_TRUE(sql::test::CountTableRows(&db(), "children", &rows));
+ EXPECT_EQ(2u, rows);
+
+ // Deleting the parent should cascade, i.e., delete the children as well.
+ ASSERT_TRUE(db().Execute("DELETE FROM parents"));
+ EXPECT_TRUE(sql::test::CountTableRows(&db(), "children", &rows));
+ EXPECT_EQ(0u, rows);
+}
+
} // namespace
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698