Home | History | Annotate | Download | only in sql
      1 // Copyright (c) 2011 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.
      4 
      5 #include <string>
      6 
      7 #include "app/sql/connection.h"
      8 #include "app/sql/statement.h"
      9 #include "base/file_path.h"
     10 #include "base/file_util.h"
     11 #include "base/path_service.h"
     12 #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 
     17 namespace {
     18 
     19 
     20 class StatementErrorHandler : public sql::ErrorDelegate {
     21  public:
     22   StatementErrorHandler() : error_(SQLITE_OK) {}
     23 
     24   virtual int OnError(int error, sql::Connection* connection,
     25                       sql::Statement* stmt) {
     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;
     30   }
     31 
     32   int error() const { return error_; }
     33 
     34   void reset_error() {
     35     sql_text_.clear();
     36     error_ = SQLITE_OK;
     37   }
     38 
     39   const char* sql_statement() const { return sql_text_.c_str(); }
     40 
     41  private:
     42   int error_;
     43   std::string sql_text_;
     44 };
     45 
     46 class SQLiteFeaturesTest : public testing::Test {
     47  public:
     48   SQLiteFeaturesTest() : error_handler_(new StatementErrorHandler) {}
     49 
     50   void SetUp() {
     51     ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &path_));
     52     path_ = path_.AppendASCII("SQLStatementTest.db");
     53     file_util::Delete(path_, false);
     54     ASSERT_TRUE(db_.Open(path_));
     55     // The |error_handler_| will be called if any sqlite statement operation
     56     // returns an error code.
     57     db_.set_error_delegate(error_handler_);
     58   }
     59 
     60   void TearDown() {
     61     // If any error happened the original sql statement can be found in
     62     // error_handler_->sql_statement().
     63     EXPECT_EQ(SQLITE_OK, error_handler_->error());
     64     db_.Close();
     65     // If this fails something is going on with cleanup and later tests may
     66     // fail, so we want to identify problems right away.
     67     ASSERT_TRUE(file_util::Delete(path_, false));
     68   }
     69 
     70   sql::Connection& db() { return db_; }
     71 
     72   int sqlite_error() const { return error_handler_->error(); }
     73   void reset_error() const { error_handler_->reset_error(); }
     74 
     75  private:
     76   FilePath path_;
     77   sql::Connection db_;
     78   scoped_refptr<StatementErrorHandler> error_handler_;
     79 };
     80 
     81 // Do not include fts1 support, it is not useful, and nobody is
     82 // looking at it.
     83 TEST_F(SQLiteFeaturesTest, NoFTS1) {
     84   ASSERT_FALSE(db().Execute("CREATE VIRTUAL TABLE foo USING fts1(x)"));
     85 }
     86 
     87 // fts2 is used for older history files, so we're signed on for
     88 // keeping our version up-to-date.
     89 // TODO(shess): Think up a crazy way to get out from having to support
     90 // this forever.
     91 TEST_F(SQLiteFeaturesTest, FTS2) {
     92   ASSERT_TRUE(db().Execute("CREATE VIRTUAL TABLE foo USING fts2(x)"));
     93 }
     94 
     95 // fts3 is used for current history files, and also for WebDatabase.
     96 TEST_F(SQLiteFeaturesTest, FTS3) {
     97   ASSERT_TRUE(db().Execute("CREATE VIRTUAL TABLE foo USING fts3(x)"));
     98 }
     99 
    100 }  // namespace
    101