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

Side by Side 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 unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/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/statement.h" 11 #include "sql/statement.h"
12 #include "sql/test/test_helpers.h"
12 #include "testing/gtest/include/gtest/gtest.h" 13 #include "testing/gtest/include/gtest/gtest.h"
13 #include "third_party/sqlite/sqlite3.h" 14 #include "third_party/sqlite/sqlite3.h"
14 15
15 // Test that certain features are/are-not enabled in our SQLite. 16 // Test that certain features are/are-not enabled in our SQLite.
16 17
17 namespace { 18 namespace {
18 19
19 void CaptureErrorCallback(int* error_pointer, std::string* sql_text, 20 void CaptureErrorCallback(int* error_pointer, std::string* sql_text,
20 int error, sql::Statement* stmt) { 21 int error, sql::Statement* stmt) {
21 *error_pointer = error; 22 *error_pointer = error;
(...skipping 11 matching lines...) Expand all
33 34
34 // The error delegate will set |error_| and |sql_text_| when any sqlite 35 // The error delegate will set |error_| and |sql_text_| when any sqlite
35 // statement operation returns an error code. 36 // statement operation returns an error code.
36 db_.set_error_callback(base::Bind(&CaptureErrorCallback, 37 db_.set_error_callback(base::Bind(&CaptureErrorCallback,
37 &error_, &sql_text_)); 38 &error_, &sql_text_));
38 } 39 }
39 40
40 void TearDown() override { 41 void TearDown() override {
41 // 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
42 // |sql_text_|. 43 // |sql_text_|.
43 EXPECT_EQ(SQLITE_OK, error_); 44 EXPECT_EQ(SQLITE_OK, error_) << sql_text_;
44 db_.Close(); 45 db_.Close();
45 } 46 }
46 47
48 void VerifyAndClearLastError(int expected_error) {
49 EXPECT_EQ(expected_error, error_);
50 error_ = SQLITE_OK;
51 sql_text_.clear();
52 }
53
47 sql::Connection& db() { return db_; } 54 sql::Connection& db() { return db_; }
48 55
49 private: 56 private:
50 base::ScopedTempDir temp_dir_; 57 base::ScopedTempDir temp_dir_;
51 sql::Connection db_; 58 sql::Connection db_;
52 59
53 // The error code of the most recent error. 60 // The error code of the most recent error.
54 int error_; 61 int error_;
55 // Original statement which has caused the error. 62 // Original statement which has caused the error.
56 std::string sql_text_; 63 std::string sql_text_;
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
105 112
106 ASSERT_TRUE(db().Execute("INSERT INTO foo (x) VALUES ('test')")); 113 ASSERT_TRUE(db().Execute("INSERT INTO foo (x) VALUES ('test')"));
107 114
108 sql::Statement s(db().GetUniqueStatement( 115 sql::Statement s(db().GetUniqueStatement(
109 "SELECT x FROM foo WHERE x MATCH 'te*'")); 116 "SELECT x FROM foo WHERE x MATCH 'te*'"));
110 ASSERT_TRUE(s.Step()); 117 ASSERT_TRUE(s.Step());
111 EXPECT_EQ("test", s.ColumnString(0)); 118 EXPECT_EQ("test", s.ColumnString(0));
112 } 119 }
113 #endif 120 #endif
114 121
122 // Ensure that our SQLite version has working foreign key support with cascade
123 // delete support.
124 TEST_F(SQLiteFeaturesTest, ForeignKeySupport) {
125 ASSERT_TRUE(db().Execute("PRAGMA foreign_keys=1"));
126 ASSERT_TRUE(db().Execute("CREATE TABLE parents (id INTEGER PRIMARY KEY)"));
127 ASSERT_TRUE(db().Execute(
128 "CREATE TABLE children ("
129 " id INTEGER PRIMARY KEY,"
130 " pid INTEGER NOT NULL REFERENCES parents(id) ON DELETE CASCADE)"));
131
132 // Inserting without a matching parent should fail with constraint violation.
133 EXPECT_FALSE(db().Execute("INSERT INTO children VALUES (10, 1)"));
134 VerifyAndClearLastError(SQLITE_CONSTRAINT);
135
136 size_t rows;
137 EXPECT_TRUE(sql::test::CountTableRows(&db(), "children", &rows));
138 EXPECT_EQ(0u, rows);
139
140 // Inserting with a matching parent should work.
141 ASSERT_TRUE(db().Execute("INSERT INTO parents VALUES (1)"));
142 EXPECT_TRUE(db().Execute("INSERT INTO children VALUES (11, 1)"));
143 EXPECT_TRUE(db().Execute("INSERT INTO children VALUES (12, 1)"));
144 EXPECT_TRUE(sql::test::CountTableRows(&db(), "children", &rows));
145 EXPECT_EQ(2u, rows);
146
147 // Deleting the parent should cascade, i.e., delete the children as well.
148 ASSERT_TRUE(db().Execute("DELETE FROM parents"));
149 EXPECT_TRUE(sql::test::CountTableRows(&db(), "children", &rows));
150 EXPECT_EQ(0u, rows);
151 }
152
115 } // namespace 153 } // namespace
OLDNEW
« 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