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
3 // found in the LICENSE file.
8 #include "base/files/file_util.h"
9 #include "base/files/scoped_temp_dir.h"
10 #include "sql/connection.h"
11 #include "sql/statement.h"
12 #include "sql/test/test_helpers.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "third_party/sqlite/sqlite3.h"
16 // Test that certain features are/are-not enabled in our SQLite.
20 void CaptureErrorCallback(int* error_pointer
, std::string
* sql_text
,
21 int error
, sql::Statement
* stmt
) {
22 *error_pointer
= error
;
23 const char* text
= stmt
? stmt
->GetSQLStatement() : NULL
;
24 *sql_text
= text
? text
: "no statement available";
27 class SQLiteFeaturesTest
: public testing::Test
{
29 SQLiteFeaturesTest() : error_(SQLITE_OK
) {}
31 void SetUp() override
{
32 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
33 ASSERT_TRUE(db_
.Open(temp_dir_
.path().AppendASCII("SQLStatementTest.db")));
35 // The error delegate will set |error_| and |sql_text_| when any sqlite
36 // statement operation returns an error code.
37 db_
.set_error_callback(base::Bind(&CaptureErrorCallback
,
38 &error_
, &sql_text_
));
41 void TearDown() override
{
42 // If any error happened the original sql statement can be found in
44 EXPECT_EQ(SQLITE_OK
, error_
) << sql_text_
;
48 void VerifyAndClearLastError(int expected_error
) {
49 EXPECT_EQ(expected_error
, error_
);
54 sql::Connection
& db() { return db_
; }
57 base::ScopedTempDir temp_dir_
;
60 // The error code of the most recent error.
62 // Original statement which has caused the error.
63 std::string sql_text_
;
66 // Do not include fts1 support, it is not useful, and nobody is
68 TEST_F(SQLiteFeaturesTest
, NoFTS1
) {
69 ASSERT_EQ(SQLITE_ERROR
, db().ExecuteAndReturnErrorCode(
70 "CREATE VIRTUAL TABLE foo USING fts1(x)"));
73 #if defined(SQLITE_ENABLE_FTS2)
74 // fts2 is used for older history files, so we're signed on for keeping our
75 // version up-to-date.
76 // TODO(shess): Think up a crazy way to get out from having to support
78 TEST_F(SQLiteFeaturesTest
, FTS2
) {
79 ASSERT_TRUE(db().Execute("CREATE VIRTUAL TABLE foo USING fts2(x)"));
82 // A standard SQLite will not include our patch. This includes iOS.
83 #if !defined(USE_SYSTEM_SQLITE)
84 // Chromium fts2 was patched to treat "foo*" as a prefix search, though the icu
85 // tokenizer will return it as two tokens {"foo", "*"}.
86 TEST_F(SQLiteFeaturesTest
, FTS2_Prefix
) {
87 const char kCreateSql
[] =
88 "CREATE VIRTUAL TABLE foo USING fts2(x, tokenize icu)";
89 ASSERT_TRUE(db().Execute(kCreateSql
));
91 ASSERT_TRUE(db().Execute("INSERT INTO foo (x) VALUES ('test')"));
93 sql::Statement
s(db().GetUniqueStatement(
94 "SELECT x FROM foo WHERE x MATCH 'te*'"));
95 ASSERT_TRUE(s
.Step());
96 EXPECT_EQ("test", s
.ColumnString(0));
101 // fts3 is used for current history files, and also for WebDatabase.
102 TEST_F(SQLiteFeaturesTest
, FTS3
) {
103 ASSERT_TRUE(db().Execute("CREATE VIRTUAL TABLE foo USING fts3(x)"));
106 #if !defined(USE_SYSTEM_SQLITE)
107 // Test that fts3 doesn't need fts2's patch (see above).
108 TEST_F(SQLiteFeaturesTest
, FTS3_Prefix
) {
109 const char kCreateSql
[] =
110 "CREATE VIRTUAL TABLE foo USING fts3(x, tokenize icu)";
111 ASSERT_TRUE(db().Execute(kCreateSql
));
113 ASSERT_TRUE(db().Execute("INSERT INTO foo (x) VALUES ('test')"));
115 sql::Statement
s(db().GetUniqueStatement(
116 "SELECT x FROM foo WHERE x MATCH 'te*'"));
117 ASSERT_TRUE(s
.Step());
118 EXPECT_EQ("test", s
.ColumnString(0));
122 #if !defined(USE_SYSTEM_SQLITE)
123 // Verify that Chromium's SQLite is compiled with HAVE_USLEEP defined. With
124 // HAVE_USLEEP, SQLite uses usleep() with millisecond granularity. Otherwise it
125 // uses sleep() with second granularity.
126 TEST_F(SQLiteFeaturesTest
, UsesUsleep
) {
127 base::TimeTicks before
= base::TimeTicks::Now();
129 base::TimeDelta delta
= base::TimeTicks::Now() - before
;
131 // It is not impossible for this to be over 1000 if things are compiled the
132 // right way. But it is very unlikely, most platforms seem to be around
134 LOG(ERROR
) << "Milliseconds: " << delta
.InMilliseconds();
135 EXPECT_LT(delta
.InMilliseconds(), 1000);
139 // Ensure that our SQLite version has working foreign key support with cascade
141 TEST_F(SQLiteFeaturesTest
, ForeignKeySupport
) {
142 ASSERT_TRUE(db().Execute("PRAGMA foreign_keys=1"));
143 ASSERT_TRUE(db().Execute("CREATE TABLE parents (id INTEGER PRIMARY KEY)"));
144 ASSERT_TRUE(db().Execute(
145 "CREATE TABLE children ("
146 " id INTEGER PRIMARY KEY,"
147 " pid INTEGER NOT NULL REFERENCES parents(id) ON DELETE CASCADE)"));
149 // Inserting without a matching parent should fail with constraint violation.
150 EXPECT_FALSE(db().Execute("INSERT INTO children VALUES (10, 1)"));
151 VerifyAndClearLastError(SQLITE_CONSTRAINT
);
154 EXPECT_TRUE(sql::test::CountTableRows(&db(), "children", &rows
));
157 // Inserting with a matching parent should work.
158 ASSERT_TRUE(db().Execute("INSERT INTO parents VALUES (1)"));
159 EXPECT_TRUE(db().Execute("INSERT INTO children VALUES (11, 1)"));
160 EXPECT_TRUE(db().Execute("INSERT INTO children VALUES (12, 1)"));
161 EXPECT_TRUE(sql::test::CountTableRows(&db(), "children", &rows
));
164 // Deleting the parent should cascade, i.e., delete the children as well.
165 ASSERT_TRUE(db().Execute("DELETE FROM parents"));
166 EXPECT_TRUE(sql::test::CountTableRows(&db(), "children", &rows
));