diff options
author | shess@chromium.org <shess@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-09-07 15:48:36 +0000 |
---|---|---|
committer | shess@chromium.org <shess@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-09-07 15:48:36 +0000 |
commit | 6ed6d14feef570f2b740e898ca640d1d537e4918 (patch) | |
tree | 5222f489f5d41ff02746659d06ba209ba2a9998f /sql | |
parent | e3ed0c0e3bec4ef1de1e616ee71c4a38022bf100 (diff) | |
download | chromium_src-6ed6d14feef570f2b740e898ca640d1d537e4918.zip chromium_src-6ed6d14feef570f2b740e898ca640d1d537e4918.tar.gz chromium_src-6ed6d14feef570f2b740e898ca640d1d537e4918.tar.bz2 |
Unit tests for sql::MetaTable.
BUG=273203
Review URL: https://chromiumcodereview.appspot.com/23649002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@221922 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'sql')
-rw-r--r-- | sql/connection_unittest.cc | 13 | ||||
-rw-r--r-- | sql/meta_table_unittest.cc | 226 | ||||
-rw-r--r-- | sql/sql.gyp | 1 | ||||
-rw-r--r-- | sql/transaction_unittest.cc | 2 |
4 files changed, 232 insertions, 10 deletions
diff --git a/sql/connection_unittest.cc b/sql/connection_unittest.cc index 09a47fb..445db34 100644 --- a/sql/connection_unittest.cc +++ b/sql/connection_unittest.cc @@ -88,11 +88,10 @@ class ScopedUmaskSetter { class SQLConnectionTest : public testing::Test { public: - SQLConnectionTest() {} - virtual void SetUp() { ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); - ASSERT_TRUE(db_.Open(db_path())); + db_path_ = temp_dir_.path().AppendASCII("SQLConnectionTest.db"); + ASSERT_TRUE(db_.Open(db_path_)); } virtual void TearDown() { @@ -100,10 +99,7 @@ class SQLConnectionTest : public testing::Test { } sql::Connection& db() { return db_; } - - base::FilePath db_path() { - return temp_dir_.path().AppendASCII("SQLConnectionTest.db"); - } + const base::FilePath& db_path() { return db_path_; } // Handle errors by blowing away the database. void RazeErrorCallback(int expected_error, int error, sql::Statement* stmt) { @@ -112,8 +108,9 @@ class SQLConnectionTest : public testing::Test { } private: - base::ScopedTempDir temp_dir_; sql::Connection db_; + base::FilePath db_path_; + base::ScopedTempDir temp_dir_; }; TEST_F(SQLConnectionTest, Execute) { diff --git a/sql/meta_table_unittest.cc b/sql/meta_table_unittest.cc new file mode 100644 index 0000000..3fbc499 --- /dev/null +++ b/sql/meta_table_unittest.cc @@ -0,0 +1,226 @@ +// 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/meta_table.h" + +#include "base/files/file_path.h" +#include "base/files/scoped_temp_dir.h" +#include "sql/connection.h" +#include "sql/statement.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace { + +class SQLMetaTableTest : public testing::Test { + public: + virtual void SetUp() { + ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); + ASSERT_TRUE(db_.Open(temp_dir_.path().AppendASCII("SQLMetaTableTest.db"))); + } + + virtual void TearDown() { + db_.Close(); + } + + sql::Connection& db() { return db_; } + + private: + base::ScopedTempDir temp_dir_; + sql::Connection db_; +}; + +TEST_F(SQLMetaTableTest, DoesTableExist) { + EXPECT_FALSE(sql::MetaTable::DoesTableExist(&db())); + + { + sql::MetaTable meta_table; + EXPECT_TRUE(meta_table.Init(&db(), 1, 1)); + } + + EXPECT_TRUE(sql::MetaTable::DoesTableExist(&db())); +} + +TEST_F(SQLMetaTableTest, VersionNumber) { + // Compatibility versions one less than the main versions to make + // sure the values aren't being crossed with each other. + const int kVersionFirst = 2; + const int kCompatVersionFirst = kVersionFirst - 1; + const int kVersionSecond = 4; + const int kCompatVersionSecond = kVersionSecond - 1; + const int kVersionThird = 6; + const int kCompatVersionThird = kVersionThird - 1; + + // First Init() sets the version info as expected. + { + sql::MetaTable meta_table; + EXPECT_TRUE(meta_table.Init(&db(), kVersionFirst, kCompatVersionFirst)); + EXPECT_EQ(kVersionFirst, meta_table.GetVersionNumber()); + EXPECT_EQ(kCompatVersionFirst, meta_table.GetCompatibleVersionNumber()); + } + + // Second Init() does not change the version info. + { + sql::MetaTable meta_table; + EXPECT_TRUE(meta_table.Init(&db(), kVersionSecond, kCompatVersionSecond)); + EXPECT_EQ(kVersionFirst, meta_table.GetVersionNumber()); + EXPECT_EQ(kCompatVersionFirst, meta_table.GetCompatibleVersionNumber()); + + meta_table.SetVersionNumber(kVersionSecond); + meta_table.SetCompatibleVersionNumber(kCompatVersionSecond); + } + + // Version info from Set*() calls is seen. + { + sql::MetaTable meta_table; + EXPECT_TRUE(meta_table.Init(&db(), kVersionThird, kCompatVersionThird)); + EXPECT_EQ(kVersionSecond, meta_table.GetVersionNumber()); + EXPECT_EQ(kCompatVersionSecond, meta_table.GetCompatibleVersionNumber()); + } +} + +TEST_F(SQLMetaTableTest, StringValue) { + const char kKey[] = "String Key"; + const std::string kFirstValue("First Value"); + const std::string kSecondValue("Second Value"); + + // Initially, the value isn't there until set. + { + sql::MetaTable meta_table; + EXPECT_TRUE(meta_table.Init(&db(), 1, 1)); + + std::string value; + EXPECT_FALSE(meta_table.GetValue(kKey, &value)); + + EXPECT_TRUE(meta_table.SetValue(kKey, kFirstValue)); + EXPECT_TRUE(meta_table.GetValue(kKey, &value)); + EXPECT_EQ(kFirstValue, value); + } + + // Value is persistent across different instances. + { + sql::MetaTable meta_table; + EXPECT_TRUE(meta_table.Init(&db(), 1, 1)); + + std::string value; + EXPECT_TRUE(meta_table.GetValue(kKey, &value)); + EXPECT_EQ(kFirstValue, value); + + EXPECT_TRUE(meta_table.SetValue(kKey, kSecondValue)); + } + + // Existing value was successfully changed. + { + sql::MetaTable meta_table; + EXPECT_TRUE(meta_table.Init(&db(), 1, 1)); + + std::string value; + EXPECT_TRUE(meta_table.GetValue(kKey, &value)); + EXPECT_EQ(kSecondValue, value); + } +} + +TEST_F(SQLMetaTableTest, IntValue) { + const char kKey[] = "Int Key"; + const int kFirstValue = 17; + const int kSecondValue = 23; + + // Initially, the value isn't there until set. + { + sql::MetaTable meta_table; + EXPECT_TRUE(meta_table.Init(&db(), 1, 1)); + + int value; + EXPECT_FALSE(meta_table.GetValue(kKey, &value)); + + EXPECT_TRUE(meta_table.SetValue(kKey, kFirstValue)); + EXPECT_TRUE(meta_table.GetValue(kKey, &value)); + EXPECT_EQ(kFirstValue, value); + } + + // Value is persistent across different instances. + { + sql::MetaTable meta_table; + EXPECT_TRUE(meta_table.Init(&db(), 1, 1)); + + int value; + EXPECT_TRUE(meta_table.GetValue(kKey, &value)); + EXPECT_EQ(kFirstValue, value); + + EXPECT_TRUE(meta_table.SetValue(kKey, kSecondValue)); + } + + // Existing value was successfully changed. + { + sql::MetaTable meta_table; + EXPECT_TRUE(meta_table.Init(&db(), 1, 1)); + + int value; + EXPECT_TRUE(meta_table.GetValue(kKey, &value)); + EXPECT_EQ(kSecondValue, value); + } +} + +TEST_F(SQLMetaTableTest, Int64Value) { + const char kKey[] = "Int Key"; + const int64 kFirstValue = GG_LONGLONG(5000000017); + const int64 kSecondValue = GG_LONGLONG(5000000023); + + // Initially, the value isn't there until set. + { + sql::MetaTable meta_table; + EXPECT_TRUE(meta_table.Init(&db(), 1, 1)); + + int64 value; + EXPECT_FALSE(meta_table.GetValue(kKey, &value)); + + EXPECT_TRUE(meta_table.SetValue(kKey, kFirstValue)); + EXPECT_TRUE(meta_table.GetValue(kKey, &value)); + EXPECT_EQ(kFirstValue, value); + } + + // Value is persistent across different instances. + { + sql::MetaTable meta_table; + EXPECT_TRUE(meta_table.Init(&db(), 1, 1)); + + int64 value; + EXPECT_TRUE(meta_table.GetValue(kKey, &value)); + EXPECT_EQ(kFirstValue, value); + + EXPECT_TRUE(meta_table.SetValue(kKey, kSecondValue)); + } + + // Existing value was successfully changed. + { + sql::MetaTable meta_table; + EXPECT_TRUE(meta_table.Init(&db(), 1, 1)); + + int64 value; + EXPECT_TRUE(meta_table.GetValue(kKey, &value)); + EXPECT_EQ(kSecondValue, value); + } +} + +TEST_F(SQLMetaTableTest, DeleteKey) { + const char kKey[] = "String Key"; + const std::string kValue("String Value"); + + sql::MetaTable meta_table; + EXPECT_TRUE(meta_table.Init(&db(), 1, 1)); + + // Value isn't present. + std::string value; + EXPECT_FALSE(meta_table.GetValue(kKey, &value)); + + // Now value is present. + EXPECT_TRUE(meta_table.SetValue(kKey, kValue)); + EXPECT_TRUE(meta_table.GetValue(kKey, &value)); + EXPECT_EQ(kValue, value); + + // After delete value isn't present. + EXPECT_TRUE(meta_table.DeleteKey(kKey)); + EXPECT_FALSE(meta_table.GetValue(kKey, &value)); +} + +} // namespace diff --git a/sql/sql.gyp b/sql/sql.gyp index aa2d642..861a1dd 100644 --- a/sql/sql.gyp +++ b/sql/sql.gyp @@ -85,6 +85,7 @@ ], 'sources': [ 'connection_unittest.cc', + 'meta_table_unittest.cc', 'recovery_unittest.cc', 'sqlite_features_unittest.cc', 'statement_unittest.cc', diff --git a/sql/transaction_unittest.cc b/sql/transaction_unittest.cc index ceaa4db..fe5eee2 100644 --- a/sql/transaction_unittest.cc +++ b/sql/transaction_unittest.cc @@ -12,8 +12,6 @@ class SQLTransactionTest : public testing::Test { public: - SQLTransactionTest() {} - virtual void SetUp() { ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); ASSERT_TRUE(db_.Open( |