OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 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 "sql/meta_table.h" |
| 6 |
| 7 #include "base/files/file_path.h" |
| 8 #include "base/files/scoped_temp_dir.h" |
| 9 #include "sql/connection.h" |
| 10 #include "sql/statement.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 |
| 13 namespace { |
| 14 |
| 15 class SQLMetaTableTest : public testing::Test { |
| 16 public: |
| 17 virtual void SetUp() { |
| 18 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 19 db_path_ = temp_dir_.path().AppendASCII("SQLMetaTableTest.db"); |
| 20 ASSERT_TRUE(db_.Open(db_path_)); |
| 21 } |
| 22 |
| 23 virtual void TearDown() { |
| 24 db_.Close(); |
| 25 } |
| 26 |
| 27 sql::Connection& db() { return db_; } |
| 28 const base::FilePath& db_path() { return db_path_; } |
| 29 |
| 30 private: |
| 31 base::FilePath db_path_; |
| 32 sql::Connection db_; |
| 33 base::ScopedTempDir temp_dir_; |
| 34 }; |
| 35 |
| 36 TEST_F(SQLMetaTableTest, DoesTableExist) { |
| 37 EXPECT_FALSE(sql::MetaTable::DoesTableExist(&db())); |
| 38 |
| 39 { |
| 40 sql::MetaTable meta_table; |
| 41 EXPECT_TRUE(meta_table.Init(&db(), 1, 1)); |
| 42 } |
| 43 |
| 44 EXPECT_TRUE(sql::MetaTable::DoesTableExist(&db())); |
| 45 } |
| 46 |
| 47 TEST_F(SQLMetaTableTest, VersionNumber) { |
| 48 // Compatibility versions one less than the main versions to make |
| 49 // sure the values aren't being crossed with each other. |
| 50 const int kVersionFirst = 2; |
| 51 const int kCompatVersionFirst = kVersionFirst - 1; |
| 52 const int kVersionSecond = 4; |
| 53 const int kCompatVersionSecond = kVersionSecond - 1; |
| 54 const int kVersionThird = 6; |
| 55 const int kCompatVersionThird = kVersionThird - 1; |
| 56 |
| 57 // First Init() sets the version info as expected. |
| 58 { |
| 59 sql::MetaTable meta_table; |
| 60 EXPECT_TRUE(meta_table.Init(&db(), kVersionFirst, kCompatVersionFirst)); |
| 61 EXPECT_EQ(kVersionFirst, meta_table.GetVersionNumber()); |
| 62 EXPECT_EQ(kCompatVersionFirst, meta_table.GetCompatibleVersionNumber()); |
| 63 } |
| 64 |
| 65 // Second Init() does not change the version info. |
| 66 { |
| 67 sql::MetaTable meta_table; |
| 68 EXPECT_TRUE(meta_table.Init(&db(), kVersionSecond, kCompatVersionSecond)); |
| 69 EXPECT_EQ(kVersionFirst, meta_table.GetVersionNumber()); |
| 70 EXPECT_EQ(kCompatVersionFirst, meta_table.GetCompatibleVersionNumber()); |
| 71 |
| 72 meta_table.SetVersionNumber(kVersionSecond); |
| 73 meta_table.SetCompatibleVersionNumber(kCompatVersionSecond); |
| 74 } |
| 75 |
| 76 // Version info from Set*() calls is seen. |
| 77 { |
| 78 sql::MetaTable meta_table; |
| 79 EXPECT_TRUE(meta_table.Init(&db(), kVersionThird, kCompatVersionThird)); |
| 80 EXPECT_EQ(kVersionSecond, meta_table.GetVersionNumber()); |
| 81 EXPECT_EQ(kCompatVersionSecond, meta_table.GetCompatibleVersionNumber()); |
| 82 } |
| 83 } |
| 84 |
| 85 TEST_F(SQLMetaTableTest, StringValue) { |
| 86 const char kKey[] = "String Key"; |
| 87 const std::string kFirstValue("First Value"); |
| 88 const std::string kSecondValue("Second Value"); |
| 89 |
| 90 // Initially, the value isn't there until set. |
| 91 { |
| 92 sql::MetaTable meta_table; |
| 93 EXPECT_TRUE(meta_table.Init(&db(), 1, 1)); |
| 94 |
| 95 std::string value; |
| 96 EXPECT_FALSE(meta_table.GetValue(kKey, &value)); |
| 97 |
| 98 EXPECT_TRUE(meta_table.SetValue(kKey, kFirstValue)); |
| 99 EXPECT_TRUE(meta_table.GetValue(kKey, &value)); |
| 100 EXPECT_EQ(kFirstValue, value); |
| 101 } |
| 102 |
| 103 // Value is persistent across different instances. |
| 104 { |
| 105 sql::MetaTable meta_table; |
| 106 EXPECT_TRUE(meta_table.Init(&db(), 1, 1)); |
| 107 |
| 108 std::string value; |
| 109 EXPECT_TRUE(meta_table.GetValue(kKey, &value)); |
| 110 EXPECT_EQ(kFirstValue, value); |
| 111 |
| 112 EXPECT_TRUE(meta_table.SetValue(kKey, kSecondValue)); |
| 113 } |
| 114 |
| 115 // Existing value was successfully changed. |
| 116 { |
| 117 sql::MetaTable meta_table; |
| 118 EXPECT_TRUE(meta_table.Init(&db(), 1, 1)); |
| 119 |
| 120 std::string value; |
| 121 EXPECT_TRUE(meta_table.GetValue(kKey, &value)); |
| 122 EXPECT_EQ(kSecondValue, value); |
| 123 } |
| 124 } |
| 125 |
| 126 TEST_F(SQLMetaTableTest, IntValue) { |
| 127 const char kKey[] = "Int Key"; |
| 128 const int kFirstValue = 17; |
| 129 const int kSecondValue = 23; |
| 130 |
| 131 // Initially, the value isn't there until set. |
| 132 { |
| 133 sql::MetaTable meta_table; |
| 134 EXPECT_TRUE(meta_table.Init(&db(), 1, 1)); |
| 135 |
| 136 int value; |
| 137 EXPECT_FALSE(meta_table.GetValue(kKey, &value)); |
| 138 |
| 139 EXPECT_TRUE(meta_table.SetValue(kKey, kFirstValue)); |
| 140 EXPECT_TRUE(meta_table.GetValue(kKey, &value)); |
| 141 EXPECT_EQ(kFirstValue, value); |
| 142 } |
| 143 |
| 144 // Value is persistent across different instances. |
| 145 { |
| 146 sql::MetaTable meta_table; |
| 147 EXPECT_TRUE(meta_table.Init(&db(), 1, 1)); |
| 148 |
| 149 int value; |
| 150 EXPECT_TRUE(meta_table.GetValue(kKey, &value)); |
| 151 EXPECT_EQ(kFirstValue, value); |
| 152 |
| 153 EXPECT_TRUE(meta_table.SetValue(kKey, kSecondValue)); |
| 154 } |
| 155 |
| 156 // Existing value was successfully changed. |
| 157 { |
| 158 sql::MetaTable meta_table; |
| 159 EXPECT_TRUE(meta_table.Init(&db(), 1, 1)); |
| 160 |
| 161 int value; |
| 162 EXPECT_TRUE(meta_table.GetValue(kKey, &value)); |
| 163 EXPECT_EQ(kSecondValue, value); |
| 164 } |
| 165 } |
| 166 |
| 167 TEST_F(SQLMetaTableTest, Int64Value) { |
| 168 const char kKey[] = "Int Key"; |
| 169 const int64 kFirstValue = 5000000017LLU; |
| 170 const int64 kSecondValue = 5000000023LLU; |
| 171 |
| 172 // Initially, the value isn't there until set. |
| 173 { |
| 174 sql::MetaTable meta_table; |
| 175 EXPECT_TRUE(meta_table.Init(&db(), 1, 1)); |
| 176 |
| 177 int64 value; |
| 178 EXPECT_FALSE(meta_table.GetValue(kKey, &value)); |
| 179 |
| 180 EXPECT_TRUE(meta_table.SetValue(kKey, kFirstValue)); |
| 181 EXPECT_TRUE(meta_table.GetValue(kKey, &value)); |
| 182 EXPECT_EQ(kFirstValue, value); |
| 183 } |
| 184 |
| 185 // Value is persistent across different instances. |
| 186 { |
| 187 sql::MetaTable meta_table; |
| 188 EXPECT_TRUE(meta_table.Init(&db(), 1, 1)); |
| 189 |
| 190 int64 value; |
| 191 EXPECT_TRUE(meta_table.GetValue(kKey, &value)); |
| 192 EXPECT_EQ(kFirstValue, value); |
| 193 |
| 194 EXPECT_TRUE(meta_table.SetValue(kKey, kSecondValue)); |
| 195 } |
| 196 |
| 197 // Existing value was successfully changed. |
| 198 { |
| 199 sql::MetaTable meta_table; |
| 200 EXPECT_TRUE(meta_table.Init(&db(), 1, 1)); |
| 201 |
| 202 int64 value; |
| 203 EXPECT_TRUE(meta_table.GetValue(kKey, &value)); |
| 204 EXPECT_EQ(kSecondValue, value); |
| 205 } |
| 206 } |
| 207 |
| 208 TEST_F(SQLMetaTableTest, DeleteKey) { |
| 209 const char kKey[] = "String Key"; |
| 210 const std::string kValue("String Value"); |
| 211 |
| 212 sql::MetaTable meta_table; |
| 213 EXPECT_TRUE(meta_table.Init(&db(), 1, 1)); |
| 214 |
| 215 // Value isn't present. |
| 216 std::string value; |
| 217 EXPECT_FALSE(meta_table.GetValue(kKey, &value)); |
| 218 |
| 219 // Now value is present. |
| 220 EXPECT_TRUE(meta_table.SetValue(kKey, kValue)); |
| 221 EXPECT_TRUE(meta_table.GetValue(kKey, &value)); |
| 222 EXPECT_EQ(kValue, value); |
| 223 |
| 224 // After delete value isn't present. |
| 225 EXPECT_TRUE(meta_table.DeleteKey(kKey)); |
| 226 EXPECT_FALSE(meta_table.GetValue(kKey, &value)); |
| 227 } |
| 228 |
| 229 } // namespace |
OLD | NEW |