diff options
Diffstat (limited to 'sql')
-rw-r--r-- | sql/sql.gyp | 2 | ||||
-rw-r--r-- | sql/test/test_helpers.cc | 75 | ||||
-rw-r--r-- | sql/test/test_helpers.h | 45 |
3 files changed, 122 insertions, 0 deletions
diff --git a/sql/sql.gyp b/sql/sql.gyp index 861a1dd..7add51c 100644 --- a/sql/sql.gyp +++ b/sql/sql.gyp @@ -62,6 +62,8 @@ 'test/error_callback_support.h', 'test/scoped_error_ignorer.cc', 'test/scoped_error_ignorer.h', + 'test/test_helpers.cc', + 'test/test_helpers.h', ], 'include_dirs': [ '..', diff --git a/sql/test/test_helpers.cc b/sql/test/test_helpers.cc new file mode 100644 index 0000000..607f146 --- /dev/null +++ b/sql/test/test_helpers.cc @@ -0,0 +1,75 @@ +// Copyright 2013 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "sql/test/test_helpers.h" + +#include <string> + +#include "base/file_util.h" +#include "sql/connection.h" +#include "sql/statement.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace { + +size_t CountSQLItemsOfType(sql::Connection* db, const char* type) { + const char kTypeSQL[] = "SELECT COUNT(*) FROM sqlite_master WHERE type = ?"; + sql::Statement s(db->GetUniqueStatement(kTypeSQL)); + s.BindCString(0, type); + EXPECT_TRUE(s.Step()); + return s.ColumnInt(0); +} + +} // namespace + +namespace sql { +namespace test { + +size_t CountSQLTables(sql::Connection* db) { + return CountSQLItemsOfType(db, "table"); +} + +size_t CountSQLIndices(sql::Connection* db) { + return CountSQLItemsOfType(db, "index"); +} + +size_t CountTableColumns(sql::Connection* db, const char* table) { + // TODO(shess): sql::Connection::QuoteForSQL() would make sense. + std::string quoted_table; + { + const char kQuoteSQL[] = "SELECT quote(?)"; + sql::Statement s(db->GetUniqueStatement(kQuoteSQL)); + s.BindCString(0, table); + EXPECT_TRUE(s.Step()); + quoted_table = s.ColumnString(0); + } + + std::string sql = "PRAGMA table_info(" + quoted_table + ")"; + sql::Statement s(db->GetUniqueStatement(sql.c_str())); + size_t rows = 0; + while (s.Step()) { + ++rows; + } + EXPECT_TRUE(s.Succeeded()); + return rows; +} + +bool CreateDatabaseFromSQL(const base::FilePath& db_path, + const base::FilePath& sql_path) { + if (base::PathExists(db_path) || !base::PathExists(sql_path)) + return false; + + std::string sql; + if (!base::ReadFileToString(sql_path, &sql)) + return false; + + sql::Connection db; + if (!db.Open(db_path)) + return false; + + return db.Execute(sql.c_str()); +} + +} // namespace test +} // namespace sql diff --git a/sql/test/test_helpers.h b/sql/test/test_helpers.h new file mode 100644 index 0000000..2e01ecd --- /dev/null +++ b/sql/test/test_helpers.h @@ -0,0 +1,45 @@ +// Copyright 2013 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef SQL_TEST_TEST_HELPERS_H_ +#define SQL_TEST_TEST_HELPERS_H_ + +#include "base/basictypes.h" +#include "base/compiler_specific.h" + +// Collection of test-only convenience functions. + +namespace base { +class FilePath; +} + +namespace sql { +class Connection; +} + +namespace sql { +namespace test { + +// Return the number of tables in sqlite_master. +size_t CountSQLTables(sql::Connection* db) WARN_UNUSED_RESULT; + +// Return the number of indices in sqlite_master. +size_t CountSQLIndices(sql::Connection* db) WARN_UNUSED_RESULT; + +// Returns the number of columns in the named table. 0 indicates an +// error (probably no such table). +size_t CountTableColumns(sql::Connection* db, const char* table) + WARN_UNUSED_RESULT; + +// Creates a SQLite database at |db_path| from the sqlite .dump output +// at |sql_path|. Returns false if |db_path| already exists, or if +// sql_path does not exist or cannot be read, or if there is an error +// executing the statements. +bool CreateDatabaseFromSQL(const base::FilePath& db_path, + const base::FilePath& sql_path) WARN_UNUSED_RESULT; + +} // namespace test +} // namespace sql + +#endif // SQL_TEST_TEST_HELPERS_H_ |