Roll src/third_party/WebKit 18ea028:62591a8 (svn 201988:201989)
[chromium-blink-merge.git] / sql / meta_table_unittest.cc
blob13d0b5d2a1dbaa387404361f735812b01e48341b
1 // Copyright 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.
5 #include "sql/meta_table.h"
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 "sql/test/sql_test_base.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 namespace {
16 using SQLMetaTableTest = sql::SQLTestBase;
18 TEST_F(SQLMetaTableTest, DoesTableExist) {
19 EXPECT_FALSE(sql::MetaTable::DoesTableExist(&db()));
22 sql::MetaTable meta_table;
23 EXPECT_TRUE(meta_table.Init(&db(), 1, 1));
26 EXPECT_TRUE(sql::MetaTable::DoesTableExist(&db()));
29 TEST_F(SQLMetaTableTest, RazeIfDeprecated) {
30 const int kDeprecatedVersion = 1;
31 const int kVersion = 2;
33 // Setup a current database.
35 sql::MetaTable meta_table;
36 EXPECT_TRUE(meta_table.Init(&db(), kVersion, kVersion));
37 EXPECT_TRUE(db().Execute("CREATE TABLE t(c)"));
38 EXPECT_TRUE(db().DoesTableExist("t"));
41 // Table should should still exist if the database version is new enough.
42 sql::MetaTable::RazeIfDeprecated(&db(), kDeprecatedVersion);
43 EXPECT_TRUE(db().DoesTableExist("t"));
45 // TODO(shess): It may make sense to Raze() if meta isn't present or
46 // version isn't present. See meta_table.h TODO on RazeIfDeprecated().
48 // Table should still exist if the version is not available.
49 EXPECT_TRUE(db().Execute("DELETE FROM meta WHERE key = 'version'"));
51 sql::MetaTable meta_table;
52 EXPECT_TRUE(meta_table.Init(&db(), kVersion, kVersion));
53 EXPECT_EQ(0, meta_table.GetVersionNumber());
55 sql::MetaTable::RazeIfDeprecated(&db(), kDeprecatedVersion);
56 EXPECT_TRUE(db().DoesTableExist("t"));
58 // Table should still exist if meta table is missing.
59 EXPECT_TRUE(db().Execute("DROP TABLE meta"));
60 sql::MetaTable::RazeIfDeprecated(&db(), kDeprecatedVersion);
61 EXPECT_TRUE(db().DoesTableExist("t"));
63 // Setup meta with deprecated version.
65 sql::MetaTable meta_table;
66 EXPECT_TRUE(meta_table.Init(&db(), kDeprecatedVersion, kDeprecatedVersion));
69 // Deprecation check should remove the table.
70 EXPECT_TRUE(db().DoesTableExist("t"));
71 sql::MetaTable::RazeIfDeprecated(&db(), kDeprecatedVersion);
72 EXPECT_FALSE(sql::MetaTable::DoesTableExist(&db()));
73 EXPECT_FALSE(db().DoesTableExist("t"));
76 TEST_F(SQLMetaTableTest, VersionNumber) {
77 // Compatibility versions one less than the main versions to make
78 // sure the values aren't being crossed with each other.
79 const int kVersionFirst = 2;
80 const int kCompatVersionFirst = kVersionFirst - 1;
81 const int kVersionSecond = 4;
82 const int kCompatVersionSecond = kVersionSecond - 1;
83 const int kVersionThird = 6;
84 const int kCompatVersionThird = kVersionThird - 1;
86 // First Init() sets the version info as expected.
88 sql::MetaTable meta_table;
89 EXPECT_TRUE(meta_table.Init(&db(), kVersionFirst, kCompatVersionFirst));
90 EXPECT_EQ(kVersionFirst, meta_table.GetVersionNumber());
91 EXPECT_EQ(kCompatVersionFirst, meta_table.GetCompatibleVersionNumber());
94 // Second Init() does not change the version info.
96 sql::MetaTable meta_table;
97 EXPECT_TRUE(meta_table.Init(&db(), kVersionSecond, kCompatVersionSecond));
98 EXPECT_EQ(kVersionFirst, meta_table.GetVersionNumber());
99 EXPECT_EQ(kCompatVersionFirst, meta_table.GetCompatibleVersionNumber());
101 meta_table.SetVersionNumber(kVersionSecond);
102 meta_table.SetCompatibleVersionNumber(kCompatVersionSecond);
105 // Version info from Set*() calls is seen.
107 sql::MetaTable meta_table;
108 EXPECT_TRUE(meta_table.Init(&db(), kVersionThird, kCompatVersionThird));
109 EXPECT_EQ(kVersionSecond, meta_table.GetVersionNumber());
110 EXPECT_EQ(kCompatVersionSecond, meta_table.GetCompatibleVersionNumber());
114 TEST_F(SQLMetaTableTest, StringValue) {
115 const char kKey[] = "String Key";
116 const std::string kFirstValue("First Value");
117 const std::string kSecondValue("Second Value");
119 // Initially, the value isn't there until set.
121 sql::MetaTable meta_table;
122 EXPECT_TRUE(meta_table.Init(&db(), 1, 1));
124 std::string value;
125 EXPECT_FALSE(meta_table.GetValue(kKey, &value));
127 EXPECT_TRUE(meta_table.SetValue(kKey, kFirstValue));
128 EXPECT_TRUE(meta_table.GetValue(kKey, &value));
129 EXPECT_EQ(kFirstValue, value);
132 // Value is persistent across different instances.
134 sql::MetaTable meta_table;
135 EXPECT_TRUE(meta_table.Init(&db(), 1, 1));
137 std::string value;
138 EXPECT_TRUE(meta_table.GetValue(kKey, &value));
139 EXPECT_EQ(kFirstValue, value);
141 EXPECT_TRUE(meta_table.SetValue(kKey, kSecondValue));
144 // Existing value was successfully changed.
146 sql::MetaTable meta_table;
147 EXPECT_TRUE(meta_table.Init(&db(), 1, 1));
149 std::string value;
150 EXPECT_TRUE(meta_table.GetValue(kKey, &value));
151 EXPECT_EQ(kSecondValue, value);
155 TEST_F(SQLMetaTableTest, IntValue) {
156 const char kKey[] = "Int Key";
157 const int kFirstValue = 17;
158 const int kSecondValue = 23;
160 // Initially, the value isn't there until set.
162 sql::MetaTable meta_table;
163 EXPECT_TRUE(meta_table.Init(&db(), 1, 1));
165 int value;
166 EXPECT_FALSE(meta_table.GetValue(kKey, &value));
168 EXPECT_TRUE(meta_table.SetValue(kKey, kFirstValue));
169 EXPECT_TRUE(meta_table.GetValue(kKey, &value));
170 EXPECT_EQ(kFirstValue, value);
173 // Value is persistent across different instances.
175 sql::MetaTable meta_table;
176 EXPECT_TRUE(meta_table.Init(&db(), 1, 1));
178 int value;
179 EXPECT_TRUE(meta_table.GetValue(kKey, &value));
180 EXPECT_EQ(kFirstValue, value);
182 EXPECT_TRUE(meta_table.SetValue(kKey, kSecondValue));
185 // Existing value was successfully changed.
187 sql::MetaTable meta_table;
188 EXPECT_TRUE(meta_table.Init(&db(), 1, 1));
190 int value;
191 EXPECT_TRUE(meta_table.GetValue(kKey, &value));
192 EXPECT_EQ(kSecondValue, value);
196 TEST_F(SQLMetaTableTest, Int64Value) {
197 const char kKey[] = "Int Key";
198 const int64_t kFirstValue = 5000000017LL;
199 const int64_t kSecondValue = 5000000023LL;
201 // Initially, the value isn't there until set.
203 sql::MetaTable meta_table;
204 EXPECT_TRUE(meta_table.Init(&db(), 1, 1));
206 int64_t value;
207 EXPECT_FALSE(meta_table.GetValue(kKey, &value));
209 EXPECT_TRUE(meta_table.SetValue(kKey, kFirstValue));
210 EXPECT_TRUE(meta_table.GetValue(kKey, &value));
211 EXPECT_EQ(kFirstValue, value);
214 // Value is persistent across different instances.
216 sql::MetaTable meta_table;
217 EXPECT_TRUE(meta_table.Init(&db(), 1, 1));
219 int64_t value;
220 EXPECT_TRUE(meta_table.GetValue(kKey, &value));
221 EXPECT_EQ(kFirstValue, value);
223 EXPECT_TRUE(meta_table.SetValue(kKey, kSecondValue));
226 // Existing value was successfully changed.
228 sql::MetaTable meta_table;
229 EXPECT_TRUE(meta_table.Init(&db(), 1, 1));
231 int64_t value;
232 EXPECT_TRUE(meta_table.GetValue(kKey, &value));
233 EXPECT_EQ(kSecondValue, value);
237 TEST_F(SQLMetaTableTest, DeleteKey) {
238 const char kKey[] = "String Key";
239 const std::string kValue("String Value");
241 sql::MetaTable meta_table;
242 EXPECT_TRUE(meta_table.Init(&db(), 1, 1));
244 // Value isn't present.
245 std::string value;
246 EXPECT_FALSE(meta_table.GetValue(kKey, &value));
248 // Now value is present.
249 EXPECT_TRUE(meta_table.SetValue(kKey, kValue));
250 EXPECT_TRUE(meta_table.GetValue(kKey, &value));
251 EXPECT_EQ(kValue, value);
253 // After delete value isn't present.
254 EXPECT_TRUE(meta_table.DeleteKey(kKey));
255 EXPECT_FALSE(meta_table.GetValue(kKey, &value));
258 } // namespace