Updating trunk VERSION from 1129.0 to 1130.0
[chromium-blink-merge.git] / sql / sqlite_features_unittest.cc
blob2ae8dd7bd5cc27b5567dfdfe3b7aa640d27ba2f2
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/file_path.h"
8 #include "base/file_util.h"
9 #include "base/path_service.h"
10 #include "sql/connection.h"
11 #include "sql/statement.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "third_party/sqlite/sqlite3.h"
15 // Test that certain features are/are-not enabled in our SQLite.
17 namespace {
20 class StatementErrorHandler : public sql::ErrorDelegate {
21 public:
22 StatementErrorHandler() : error_(SQLITE_OK) {}
24 virtual int OnError(int error, sql::Connection* connection,
25 sql::Statement* stmt) OVERRIDE {
26 error_ = error;
27 const char* sql_txt = stmt ? stmt->GetSQLStatement() : NULL;
28 sql_text_ = sql_txt ? sql_txt : "no statement available";
29 return error;
32 int error() const { return error_; }
34 void reset_error() {
35 sql_text_.clear();
36 error_ = SQLITE_OK;
39 const char* sql_statement() const { return sql_text_.c_str(); }
41 protected:
42 virtual ~StatementErrorHandler() {}
44 private:
45 int error_;
46 std::string sql_text_;
49 class SQLiteFeaturesTest : public testing::Test {
50 public:
51 SQLiteFeaturesTest() : error_handler_(new StatementErrorHandler) {}
53 void SetUp() {
54 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &path_));
55 path_ = path_.AppendASCII("SQLStatementTest.db");
56 file_util::Delete(path_, false);
57 ASSERT_TRUE(db_.Open(path_));
58 // The |error_handler_| will be called if any sqlite statement operation
59 // returns an error code.
60 db_.set_error_delegate(error_handler_);
63 void TearDown() {
64 // If any error happened the original sql statement can be found in
65 // error_handler_->sql_statement().
66 EXPECT_EQ(SQLITE_OK, error_handler_->error());
67 db_.Close();
68 // If this fails something is going on with cleanup and later tests may
69 // fail, so we want to identify problems right away.
70 ASSERT_TRUE(file_util::Delete(path_, false));
73 sql::Connection& db() { return db_; }
75 int sqlite_error() const { return error_handler_->error(); }
76 void reset_error() const { error_handler_->reset_error(); }
78 private:
79 FilePath path_;
80 sql::Connection db_;
81 scoped_refptr<StatementErrorHandler> error_handler_;
84 // Do not include fts1 support, it is not useful, and nobody is
85 // looking at it.
86 TEST_F(SQLiteFeaturesTest, NoFTS1) {
87 ASSERT_EQ(SQLITE_ERROR, db().ExecuteAndReturnErrorCode(
88 "CREATE VIRTUAL TABLE foo USING fts1(x)"));
91 // fts2 is used for older history files, so we're signed on for
92 // keeping our version up-to-date.
93 // TODO(shess): Think up a crazy way to get out from having to support
94 // this forever.
95 TEST_F(SQLiteFeaturesTest, FTS2) {
96 ASSERT_TRUE(db().Execute("CREATE VIRTUAL TABLE foo USING fts2(x)"));
99 // fts3 is used for current history files, and also for WebDatabase.
100 TEST_F(SQLiteFeaturesTest, FTS3) {
101 ASSERT_TRUE(db().Execute("CREATE VIRTUAL TABLE foo USING fts3(x)"));
104 } // namespace