blob: 1aff80e9bbac20d11dfe40c9e90e0021166de30c [file] [log] [blame]
[email protected]60e60dd2012-04-28 08:16:041// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]67361b32011-04-12 20:13:062// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <string>
6
[email protected]526b4662013-06-14 04:09:127#include "base/bind.h"
thestig22dfc4012014-09-05 08:29:448#include "base/files/file_util.h"
[email protected]ea1a3f62012-11-16 20:34:239#include "base/files/scoped_temp_dir.h"
[email protected]f0a54b22011-07-19 18:40:2110#include "sql/connection.h"
11#include "sql/statement.h"
[email protected]67361b32011-04-12 20:13:0612#include "testing/gtest/include/gtest/gtest.h"
13#include "third_party/sqlite/sqlite3.h"
14
15// Test that certain features are/are-not enabled in our SQLite.
16
17namespace {
18
[email protected]526b4662013-06-14 04:09:1219void CaptureErrorCallback(int* error_pointer, std::string* sql_text,
20 int error, sql::Statement* stmt) {
21 *error_pointer = error;
22 const char* text = stmt ? stmt->GetSQLStatement() : NULL;
23 *sql_text = text ? text : "no statement available";
24}
[email protected]67361b32011-04-12 20:13:0625
26class SQLiteFeaturesTest : public testing::Test {
27 public:
[email protected]49dc4f22012-10-17 17:41:1628 SQLiteFeaturesTest() : error_(SQLITE_OK) {}
[email protected]67361b32011-04-12 20:13:0629
dcheng1b3b125e2014-12-22 23:00:2430 void SetUp() override {
[email protected]99034682012-06-13 03:31:1631 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
32 ASSERT_TRUE(db_.Open(temp_dir_.path().AppendASCII("SQLStatementTest.db")));
33
[email protected]49dc4f22012-10-17 17:41:1634 // The error delegate will set |error_| and |sql_text_| when any sqlite
35 // statement operation returns an error code.
[email protected]526b4662013-06-14 04:09:1236 db_.set_error_callback(base::Bind(&CaptureErrorCallback,
37 &error_, &sql_text_));
[email protected]67361b32011-04-12 20:13:0638 }
39
dcheng1b3b125e2014-12-22 23:00:2440 void TearDown() override {
[email protected]67361b32011-04-12 20:13:0641 // If any error happened the original sql statement can be found in
[email protected]49dc4f22012-10-17 17:41:1642 // |sql_text_|.
43 EXPECT_EQ(SQLITE_OK, error_);
[email protected]67361b32011-04-12 20:13:0644 db_.Close();
[email protected]67361b32011-04-12 20:13:0645 }
46
47 sql::Connection& db() { return db_; }
48
[email protected]67361b32011-04-12 20:13:0649 private:
[email protected]ea1a3f62012-11-16 20:34:2350 base::ScopedTempDir temp_dir_;
[email protected]67361b32011-04-12 20:13:0651 sql::Connection db_;
[email protected]49dc4f22012-10-17 17:41:1652
53 // The error code of the most recent error.
54 int error_;
55 // Original statement which has caused the error.
56 std::string sql_text_;
[email protected]67361b32011-04-12 20:13:0657};
58
59// Do not include fts1 support, it is not useful, and nobody is
60// looking at it.
61TEST_F(SQLiteFeaturesTest, NoFTS1) {
[email protected]eff1fa522011-12-12 23:50:5962 ASSERT_EQ(SQLITE_ERROR, db().ExecuteAndReturnErrorCode(
63 "CREATE VIRTUAL TABLE foo USING fts1(x)"));
[email protected]67361b32011-04-12 20:13:0664}
65
the_jkffdda022014-10-03 11:36:1966#if defined(SQLITE_ENABLE_FTS2)
[email protected]e73e89222012-07-13 18:55:2267// fts2 is used for older history files, so we're signed on for keeping our
the_jkffdda022014-10-03 11:36:1968// version up-to-date.
[email protected]67361b32011-04-12 20:13:0669// TODO(shess): Think up a crazy way to get out from having to support
70// this forever.
71TEST_F(SQLiteFeaturesTest, FTS2) {
72 ASSERT_TRUE(db().Execute("CREATE VIRTUAL TABLE foo USING fts2(x)"));
73}
shess355d9a1e2015-01-10 00:42:2974
75// A standard SQLite will not include our patch. This includes iOS.
76#if !defined(USE_SYSTEM_SQLITE)
77// Chromium fts2 was patched to treat "foo*" as a prefix search, though the icu
78// tokenizer will return it as two tokens {"foo", "*"}.
79TEST_F(SQLiteFeaturesTest, FTS2_Prefix) {
80 const char kCreateSql[] =
81 "CREATE VIRTUAL TABLE foo USING fts2(x, tokenize icu)";
82 ASSERT_TRUE(db().Execute(kCreateSql));
83
84 ASSERT_TRUE(db().Execute("INSERT INTO foo (x) VALUES ('test')"));
85
86 sql::Statement s(db().GetUniqueStatement(
87 "SELECT x FROM foo WHERE x MATCH 'te*'"));
88 ASSERT_TRUE(s.Step());
89 EXPECT_EQ("test", s.ColumnString(0));
90}
91#endif
[email protected]e73e89222012-07-13 18:55:2292#endif
[email protected]67361b32011-04-12 20:13:0693
94// fts3 is used for current history files, and also for WebDatabase.
95TEST_F(SQLiteFeaturesTest, FTS3) {
96 ASSERT_TRUE(db().Execute("CREATE VIRTUAL TABLE foo USING fts3(x)"));
97}
98
shess355d9a1e2015-01-10 00:42:2999#if !defined(USE_SYSTEM_SQLITE)
100// Test that fts3 doesn't need fts2's patch (see above).
101TEST_F(SQLiteFeaturesTest, FTS3_Prefix) {
102 const char kCreateSql[] =
103 "CREATE VIRTUAL TABLE foo USING fts3(x, tokenize icu)";
104 ASSERT_TRUE(db().Execute(kCreateSql));
105
106 ASSERT_TRUE(db().Execute("INSERT INTO foo (x) VALUES ('test')"));
107
108 sql::Statement s(db().GetUniqueStatement(
109 "SELECT x FROM foo WHERE x MATCH 'te*'"));
110 ASSERT_TRUE(s.Step());
111 EXPECT_EQ("test", s.ColumnString(0));
112}
113#endif
114
[email protected]67361b32011-04-12 20:13:06115} // namespace