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 "testing/gtest/include/gtest/gtest.h"
15 class SQLMetaTableTest
: public testing::Test
{
17 void SetUp() override
{
18 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
19 ASSERT_TRUE(db_
.Open(temp_dir_
.path().AppendASCII("SQLMetaTableTest.db")));
22 void TearDown() override
{ db_
.Close(); }
24 sql::Connection
& db() { return db_
; }
27 base::ScopedTempDir temp_dir_
;
31 TEST_F(SQLMetaTableTest
, DoesTableExist
) {
32 EXPECT_FALSE(sql::MetaTable::DoesTableExist(&db()));
35 sql::MetaTable meta_table
;
36 EXPECT_TRUE(meta_table
.Init(&db(), 1, 1));
39 EXPECT_TRUE(sql::MetaTable::DoesTableExist(&db()));
42 TEST_F(SQLMetaTableTest
, RazeIfDeprecated
) {
43 const int kDeprecatedVersion
= 1;
44 const int kVersion
= 2;
46 // Setup a current database.
48 sql::MetaTable meta_table
;
49 EXPECT_TRUE(meta_table
.Init(&db(), kVersion
, kVersion
));
50 EXPECT_TRUE(db().Execute("CREATE TABLE t(c)"));
51 EXPECT_TRUE(db().DoesTableExist("t"));
54 // Table should should still exist if the database version is new enough.
55 sql::MetaTable::RazeIfDeprecated(&db(), kDeprecatedVersion
);
56 EXPECT_TRUE(db().DoesTableExist("t"));
58 // TODO(shess): It may make sense to Raze() if meta isn't present or
59 // version isn't present. See meta_table.h TODO on RazeIfDeprecated().
61 // Table should still exist if the version is not available.
62 EXPECT_TRUE(db().Execute("DELETE FROM meta WHERE key = 'version'"));
64 sql::MetaTable meta_table
;
65 EXPECT_TRUE(meta_table
.Init(&db(), kVersion
, kVersion
));
66 EXPECT_EQ(0, meta_table
.GetVersionNumber());
68 sql::MetaTable::RazeIfDeprecated(&db(), kDeprecatedVersion
);
69 EXPECT_TRUE(db().DoesTableExist("t"));
71 // Table should still exist if meta table is missing.
72 EXPECT_TRUE(db().Execute("DROP TABLE meta"));
73 sql::MetaTable::RazeIfDeprecated(&db(), kDeprecatedVersion
);
74 EXPECT_TRUE(db().DoesTableExist("t"));
76 // Setup meta with deprecated version.
78 sql::MetaTable meta_table
;
79 EXPECT_TRUE(meta_table
.Init(&db(), kDeprecatedVersion
, kDeprecatedVersion
));
82 // Deprecation check should remove the table.
83 EXPECT_TRUE(db().DoesTableExist("t"));
84 sql::MetaTable::RazeIfDeprecated(&db(), kDeprecatedVersion
);
85 EXPECT_FALSE(sql::MetaTable::DoesTableExist(&db()));
86 EXPECT_FALSE(db().DoesTableExist("t"));
89 TEST_F(SQLMetaTableTest
, VersionNumber
) {
90 // Compatibility versions one less than the main versions to make
91 // sure the values aren't being crossed with each other.
92 const int kVersionFirst
= 2;
93 const int kCompatVersionFirst
= kVersionFirst
- 1;
94 const int kVersionSecond
= 4;
95 const int kCompatVersionSecond
= kVersionSecond
- 1;
96 const int kVersionThird
= 6;
97 const int kCompatVersionThird
= kVersionThird
- 1;
99 // First Init() sets the version info as expected.
101 sql::MetaTable meta_table
;
102 EXPECT_TRUE(meta_table
.Init(&db(), kVersionFirst
, kCompatVersionFirst
));
103 EXPECT_EQ(kVersionFirst
, meta_table
.GetVersionNumber());
104 EXPECT_EQ(kCompatVersionFirst
, meta_table
.GetCompatibleVersionNumber());
107 // Second Init() does not change the version info.
109 sql::MetaTable meta_table
;
110 EXPECT_TRUE(meta_table
.Init(&db(), kVersionSecond
, kCompatVersionSecond
));
111 EXPECT_EQ(kVersionFirst
, meta_table
.GetVersionNumber());
112 EXPECT_EQ(kCompatVersionFirst
, meta_table
.GetCompatibleVersionNumber());
114 meta_table
.SetVersionNumber(kVersionSecond
);
115 meta_table
.SetCompatibleVersionNumber(kCompatVersionSecond
);
118 // Version info from Set*() calls is seen.
120 sql::MetaTable meta_table
;
121 EXPECT_TRUE(meta_table
.Init(&db(), kVersionThird
, kCompatVersionThird
));
122 EXPECT_EQ(kVersionSecond
, meta_table
.GetVersionNumber());
123 EXPECT_EQ(kCompatVersionSecond
, meta_table
.GetCompatibleVersionNumber());
127 TEST_F(SQLMetaTableTest
, StringValue
) {
128 const char kKey
[] = "String Key";
129 const std::string
kFirstValue("First Value");
130 const std::string
kSecondValue("Second Value");
132 // Initially, the value isn't there until set.
134 sql::MetaTable meta_table
;
135 EXPECT_TRUE(meta_table
.Init(&db(), 1, 1));
138 EXPECT_FALSE(meta_table
.GetValue(kKey
, &value
));
140 EXPECT_TRUE(meta_table
.SetValue(kKey
, kFirstValue
));
141 EXPECT_TRUE(meta_table
.GetValue(kKey
, &value
));
142 EXPECT_EQ(kFirstValue
, value
);
145 // Value is persistent across different instances.
147 sql::MetaTable meta_table
;
148 EXPECT_TRUE(meta_table
.Init(&db(), 1, 1));
151 EXPECT_TRUE(meta_table
.GetValue(kKey
, &value
));
152 EXPECT_EQ(kFirstValue
, value
);
154 EXPECT_TRUE(meta_table
.SetValue(kKey
, kSecondValue
));
157 // Existing value was successfully changed.
159 sql::MetaTable meta_table
;
160 EXPECT_TRUE(meta_table
.Init(&db(), 1, 1));
163 EXPECT_TRUE(meta_table
.GetValue(kKey
, &value
));
164 EXPECT_EQ(kSecondValue
, value
);
168 TEST_F(SQLMetaTableTest
, IntValue
) {
169 const char kKey
[] = "Int Key";
170 const int kFirstValue
= 17;
171 const int kSecondValue
= 23;
173 // Initially, the value isn't there until set.
175 sql::MetaTable meta_table
;
176 EXPECT_TRUE(meta_table
.Init(&db(), 1, 1));
179 EXPECT_FALSE(meta_table
.GetValue(kKey
, &value
));
181 EXPECT_TRUE(meta_table
.SetValue(kKey
, kFirstValue
));
182 EXPECT_TRUE(meta_table
.GetValue(kKey
, &value
));
183 EXPECT_EQ(kFirstValue
, value
);
186 // Value is persistent across different instances.
188 sql::MetaTable meta_table
;
189 EXPECT_TRUE(meta_table
.Init(&db(), 1, 1));
192 EXPECT_TRUE(meta_table
.GetValue(kKey
, &value
));
193 EXPECT_EQ(kFirstValue
, value
);
195 EXPECT_TRUE(meta_table
.SetValue(kKey
, kSecondValue
));
198 // Existing value was successfully changed.
200 sql::MetaTable meta_table
;
201 EXPECT_TRUE(meta_table
.Init(&db(), 1, 1));
204 EXPECT_TRUE(meta_table
.GetValue(kKey
, &value
));
205 EXPECT_EQ(kSecondValue
, value
);
209 TEST_F(SQLMetaTableTest
, Int64Value
) {
210 const char kKey
[] = "Int Key";
211 const int64_t kFirstValue
= 5000000017LL;
212 const int64_t kSecondValue
= 5000000023LL;
214 // Initially, the value isn't there until set.
216 sql::MetaTable meta_table
;
217 EXPECT_TRUE(meta_table
.Init(&db(), 1, 1));
220 EXPECT_FALSE(meta_table
.GetValue(kKey
, &value
));
222 EXPECT_TRUE(meta_table
.SetValue(kKey
, kFirstValue
));
223 EXPECT_TRUE(meta_table
.GetValue(kKey
, &value
));
224 EXPECT_EQ(kFirstValue
, value
);
227 // Value is persistent across different instances.
229 sql::MetaTable meta_table
;
230 EXPECT_TRUE(meta_table
.Init(&db(), 1, 1));
233 EXPECT_TRUE(meta_table
.GetValue(kKey
, &value
));
234 EXPECT_EQ(kFirstValue
, value
);
236 EXPECT_TRUE(meta_table
.SetValue(kKey
, kSecondValue
));
239 // Existing value was successfully changed.
241 sql::MetaTable meta_table
;
242 EXPECT_TRUE(meta_table
.Init(&db(), 1, 1));
245 EXPECT_TRUE(meta_table
.GetValue(kKey
, &value
));
246 EXPECT_EQ(kSecondValue
, value
);
250 TEST_F(SQLMetaTableTest
, DeleteKey
) {
251 const char kKey
[] = "String Key";
252 const std::string
kValue("String Value");
254 sql::MetaTable meta_table
;
255 EXPECT_TRUE(meta_table
.Init(&db(), 1, 1));
257 // Value isn't present.
259 EXPECT_FALSE(meta_table
.GetValue(kKey
, &value
));
261 // Now value is present.
262 EXPECT_TRUE(meta_table
.SetValue(kKey
, kValue
));
263 EXPECT_TRUE(meta_table
.GetValue(kKey
, &value
));
264 EXPECT_EQ(kValue
, value
);
266 // After delete value isn't present.
267 EXPECT_TRUE(meta_table
.DeleteKey(kKey
));
268 EXPECT_FALSE(meta_table
.GetValue(kKey
, &value
));