blob: d9bcb86bc61d61394369506f7d5efeadb89c6c76 [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}
[email protected]e73e89222012-07-13 18:55:2274#endif
[email protected]67361b32011-04-12 20:13:0675
76// fts3 is used for current history files, and also for WebDatabase.
77TEST_F(SQLiteFeaturesTest, FTS3) {
78 ASSERT_TRUE(db().Execute("CREATE VIRTUAL TABLE foo USING fts3(x)"));
79}
80
81} // namespace