android: Refactor function name template in JNI generator.
[chromium-blink-merge.git] / sql / sqlite_features_unittest.cc
blob2be1fd2a77f6ab967ad5e195292e55da346ea784
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.
5 #include <string>
7 #include "base/bind.h"
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.
18 namespace {
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 {
28 public:
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
43 // |sql_text_|.
44 EXPECT_EQ(SQLITE_OK, error_) << sql_text_;
45 db_.Close();
48 sql::Connection& db() { return db_; }
49 int error() { return error_; }
51 private:
52 base::ScopedTempDir temp_dir_;
53 sql::Connection db_;
55 // The error code of the most recent error.
56 int error_;
57 // Original statement which has caused the error.
58 std::string sql_text_;
61 // Do not include fts1 support, it is not useful, and nobody is
62 // looking at it.
63 TEST_F(SQLiteFeaturesTest, NoFTS1) {
64 ASSERT_EQ(SQLITE_ERROR, db().ExecuteAndReturnErrorCode(
65 "CREATE VIRTUAL TABLE foo USING fts1(x)"));
68 #if defined(SQLITE_ENABLE_FTS2)
69 // fts2 is used for older history files, so we're signed on for keeping our
70 // version up-to-date.
71 // TODO(shess): Think up a crazy way to get out from having to support
72 // this forever.
73 TEST_F(SQLiteFeaturesTest, FTS2) {
74 ASSERT_TRUE(db().Execute("CREATE VIRTUAL TABLE foo USING fts2(x)"));
77 // A standard SQLite will not include our patch. This includes iOS.
78 #if !defined(USE_SYSTEM_SQLITE)
79 // Chromium fts2 was patched to treat "foo*" as a prefix search, though the icu
80 // tokenizer will return it as two tokens {"foo", "*"}.
81 TEST_F(SQLiteFeaturesTest, FTS2_Prefix) {
82 const char kCreateSql[] =
83 "CREATE VIRTUAL TABLE foo USING fts2(x, tokenize icu)";
84 ASSERT_TRUE(db().Execute(kCreateSql));
86 ASSERT_TRUE(db().Execute("INSERT INTO foo (x) VALUES ('test')"));
88 sql::Statement s(db().GetUniqueStatement(
89 "SELECT x FROM foo WHERE x MATCH 'te*'"));
90 ASSERT_TRUE(s.Step());
91 EXPECT_EQ("test", s.ColumnString(0));
93 #endif
94 #endif
96 // fts3 is used for current history files, and also for WebDatabase.
97 TEST_F(SQLiteFeaturesTest, FTS3) {
98 ASSERT_TRUE(db().Execute("CREATE VIRTUAL TABLE foo USING fts3(x)"));
101 #if !defined(USE_SYSTEM_SQLITE)
102 // Test that fts3 doesn't need fts2's patch (see above).
103 TEST_F(SQLiteFeaturesTest, FTS3_Prefix) {
104 const char kCreateSql[] =
105 "CREATE VIRTUAL TABLE foo USING fts3(x, tokenize icu)";
106 ASSERT_TRUE(db().Execute(kCreateSql));
108 ASSERT_TRUE(db().Execute("INSERT INTO foo (x) VALUES ('test')"));
110 sql::Statement s(db().GetUniqueStatement(
111 "SELECT x FROM foo WHERE x MATCH 'te*'"));
112 ASSERT_TRUE(s.Step());
113 EXPECT_EQ("test", s.ColumnString(0));
115 #endif
117 #if !defined(USE_SYSTEM_SQLITE)
118 // Verify that Chromium's SQLite is compiled with HAVE_USLEEP defined. With
119 // HAVE_USLEEP, SQLite uses usleep() with millisecond granularity. Otherwise it
120 // uses sleep() with second granularity.
121 TEST_F(SQLiteFeaturesTest, UsesUsleep) {
122 base::TimeTicks before = base::TimeTicks::Now();
123 sqlite3_sleep(1);
124 base::TimeDelta delta = base::TimeTicks::Now() - before;
126 // It is not impossible for this to be over 1000 if things are compiled the
127 // right way. But it is very unlikely, most platforms seem to be around
128 // <TBD>.
129 LOG(ERROR) << "Milliseconds: " << delta.InMilliseconds();
130 EXPECT_LT(delta.InMilliseconds(), 1000);
132 #endif
134 // Ensure that our SQLite version has working foreign key support with cascade
135 // delete support.
136 TEST_F(SQLiteFeaturesTest, ForeignKeySupport) {
137 ASSERT_TRUE(db().Execute("PRAGMA foreign_keys=1"));
138 ASSERT_TRUE(db().Execute("CREATE TABLE parents (id INTEGER PRIMARY KEY)"));
139 ASSERT_TRUE(db().Execute(
140 "CREATE TABLE children ("
141 " id INTEGER PRIMARY KEY,"
142 " pid INTEGER NOT NULL REFERENCES parents(id) ON DELETE CASCADE)"));
144 // Inserting without a matching parent should fail with constraint violation.
145 // Mask off any extended error codes for USE_SYSTEM_SQLITE.
146 int insertErr = db().ExecuteAndReturnErrorCode(
147 "INSERT INTO children VALUES (10, 1)");
148 EXPECT_EQ(SQLITE_CONSTRAINT, (insertErr&0xff));
150 size_t rows;
151 EXPECT_TRUE(sql::test::CountTableRows(&db(), "children", &rows));
152 EXPECT_EQ(0u, rows);
154 // Inserting with a matching parent should work.
155 ASSERT_TRUE(db().Execute("INSERT INTO parents VALUES (1)"));
156 EXPECT_TRUE(db().Execute("INSERT INTO children VALUES (11, 1)"));
157 EXPECT_TRUE(db().Execute("INSERT INTO children VALUES (12, 1)"));
158 EXPECT_TRUE(sql::test::CountTableRows(&db(), "children", &rows));
159 EXPECT_EQ(2u, rows);
161 // Deleting the parent should cascade, i.e., delete the children as well.
162 ASSERT_TRUE(db().Execute("DELETE FROM parents"));
163 EXPECT_TRUE(sql::test::CountTableRows(&db(), "children", &rows));
164 EXPECT_EQ(0u, rows);
167 } // namespace