blob: 928c0ed02da6632afe13b152c0aa96d8ed72e514 [file] [log] [blame]
[email protected]6ed6d14f2013-09-07 15:48:361// 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.
4
5#include "sql/meta_table.h"
6
avi0b519202015-12-21 07:25:197#include <stdint.h>
8
[email protected]6ed6d14f2013-09-07 15:48:369#include "base/files/file_path.h"
10#include "base/files/scoped_temp_dir.h"
Victor Costancfbfa602018-08-01 23:24:4611#include "sql/database.h"
[email protected]6ed6d14f2013-09-07 15:48:3612#include "sql/statement.h"
erg102ceb412015-06-20 01:38:1313#include "sql/test/sql_test_base.h"
[email protected]6ed6d14f2013-09-07 15:48:3614#include "testing/gtest/include/gtest/gtest.h"
15
16namespace {
17
erg102ceb412015-06-20 01:38:1318using SQLMetaTableTest = sql::SQLTestBase;
[email protected]6ed6d14f2013-09-07 15:48:3619
20TEST_F(SQLMetaTableTest, DoesTableExist) {
21 EXPECT_FALSE(sql::MetaTable::DoesTableExist(&db()));
22
23 {
24 sql::MetaTable meta_table;
25 EXPECT_TRUE(meta_table.Init(&db(), 1, 1));
26 }
27
28 EXPECT_TRUE(sql::MetaTable::DoesTableExist(&db()));
29}
30
[email protected]fe4e3de2013-10-08 02:19:1731TEST_F(SQLMetaTableTest, RazeIfDeprecated) {
32 const int kDeprecatedVersion = 1;
33 const int kVersion = 2;
34
35 // Setup a current database.
36 {
37 sql::MetaTable meta_table;
38 EXPECT_TRUE(meta_table.Init(&db(), kVersion, kVersion));
39 EXPECT_TRUE(db().Execute("CREATE TABLE t(c)"));
40 EXPECT_TRUE(db().DoesTableExist("t"));
41 }
42
43 // Table should should still exist if the database version is new enough.
44 sql::MetaTable::RazeIfDeprecated(&db(), kDeprecatedVersion);
45 EXPECT_TRUE(db().DoesTableExist("t"));
46
47 // TODO(shess): It may make sense to Raze() if meta isn't present or
48 // version isn't present. See meta_table.h TODO on RazeIfDeprecated().
49
50 // Table should still exist if the version is not available.
51 EXPECT_TRUE(db().Execute("DELETE FROM meta WHERE key = 'version'"));
52 {
53 sql::MetaTable meta_table;
54 EXPECT_TRUE(meta_table.Init(&db(), kVersion, kVersion));
55 EXPECT_EQ(0, meta_table.GetVersionNumber());
56 }
57 sql::MetaTable::RazeIfDeprecated(&db(), kDeprecatedVersion);
58 EXPECT_TRUE(db().DoesTableExist("t"));
59
60 // Table should still exist if meta table is missing.
61 EXPECT_TRUE(db().Execute("DROP TABLE meta"));
62 sql::MetaTable::RazeIfDeprecated(&db(), kDeprecatedVersion);
63 EXPECT_TRUE(db().DoesTableExist("t"));
64
65 // Setup meta with deprecated version.
66 {
67 sql::MetaTable meta_table;
68 EXPECT_TRUE(meta_table.Init(&db(), kDeprecatedVersion, kDeprecatedVersion));
69 }
70
71 // Deprecation check should remove the table.
72 EXPECT_TRUE(db().DoesTableExist("t"));
73 sql::MetaTable::RazeIfDeprecated(&db(), kDeprecatedVersion);
74 EXPECT_FALSE(sql::MetaTable::DoesTableExist(&db()));
75 EXPECT_FALSE(db().DoesTableExist("t"));
76}
77
[email protected]6ed6d14f2013-09-07 15:48:3678TEST_F(SQLMetaTableTest, VersionNumber) {
79 // Compatibility versions one less than the main versions to make
80 // sure the values aren't being crossed with each other.
81 const int kVersionFirst = 2;
82 const int kCompatVersionFirst = kVersionFirst - 1;
83 const int kVersionSecond = 4;
84 const int kCompatVersionSecond = kVersionSecond - 1;
85 const int kVersionThird = 6;
86 const int kCompatVersionThird = kVersionThird - 1;
87
88 // First Init() sets the version info as expected.
89 {
90 sql::MetaTable meta_table;
91 EXPECT_TRUE(meta_table.Init(&db(), kVersionFirst, kCompatVersionFirst));
92 EXPECT_EQ(kVersionFirst, meta_table.GetVersionNumber());
93 EXPECT_EQ(kCompatVersionFirst, meta_table.GetCompatibleVersionNumber());
94 }
95
96 // Second Init() does not change the version info.
97 {
98 sql::MetaTable meta_table;
99 EXPECT_TRUE(meta_table.Init(&db(), kVersionSecond, kCompatVersionSecond));
100 EXPECT_EQ(kVersionFirst, meta_table.GetVersionNumber());
101 EXPECT_EQ(kCompatVersionFirst, meta_table.GetCompatibleVersionNumber());
102
103 meta_table.SetVersionNumber(kVersionSecond);
104 meta_table.SetCompatibleVersionNumber(kCompatVersionSecond);
105 }
106
107 // Version info from Set*() calls is seen.
108 {
109 sql::MetaTable meta_table;
110 EXPECT_TRUE(meta_table.Init(&db(), kVersionThird, kCompatVersionThird));
111 EXPECT_EQ(kVersionSecond, meta_table.GetVersionNumber());
112 EXPECT_EQ(kCompatVersionSecond, meta_table.GetCompatibleVersionNumber());
113 }
114}
115
116TEST_F(SQLMetaTableTest, StringValue) {
Victor Costan1d868352018-06-26 19:06:48117 static const char kKey[] = "String Key";
[email protected]6ed6d14f2013-09-07 15:48:36118 const std::string kFirstValue("First Value");
119 const std::string kSecondValue("Second Value");
120
121 // Initially, the value isn't there until set.
122 {
123 sql::MetaTable meta_table;
124 EXPECT_TRUE(meta_table.Init(&db(), 1, 1));
125
126 std::string value;
127 EXPECT_FALSE(meta_table.GetValue(kKey, &value));
128
129 EXPECT_TRUE(meta_table.SetValue(kKey, kFirstValue));
130 EXPECT_TRUE(meta_table.GetValue(kKey, &value));
131 EXPECT_EQ(kFirstValue, value);
132 }
133
134 // Value is persistent across different instances.
135 {
136 sql::MetaTable meta_table;
137 EXPECT_TRUE(meta_table.Init(&db(), 1, 1));
138
139 std::string value;
140 EXPECT_TRUE(meta_table.GetValue(kKey, &value));
141 EXPECT_EQ(kFirstValue, value);
142
143 EXPECT_TRUE(meta_table.SetValue(kKey, kSecondValue));
144 }
145
146 // Existing value was successfully changed.
147 {
148 sql::MetaTable meta_table;
149 EXPECT_TRUE(meta_table.Init(&db(), 1, 1));
150
151 std::string value;
152 EXPECT_TRUE(meta_table.GetValue(kKey, &value));
153 EXPECT_EQ(kSecondValue, value);
154 }
155}
156
157TEST_F(SQLMetaTableTest, IntValue) {
Victor Costan1d868352018-06-26 19:06:48158 static const char kKey[] = "Int Key";
[email protected]6ed6d14f2013-09-07 15:48:36159 const int kFirstValue = 17;
160 const int kSecondValue = 23;
161
162 // Initially, the value isn't there until set.
163 {
164 sql::MetaTable meta_table;
165 EXPECT_TRUE(meta_table.Init(&db(), 1, 1));
166
167 int value;
168 EXPECT_FALSE(meta_table.GetValue(kKey, &value));
169
170 EXPECT_TRUE(meta_table.SetValue(kKey, kFirstValue));
171 EXPECT_TRUE(meta_table.GetValue(kKey, &value));
172 EXPECT_EQ(kFirstValue, value);
173 }
174
175 // Value is persistent across different instances.
176 {
177 sql::MetaTable meta_table;
178 EXPECT_TRUE(meta_table.Init(&db(), 1, 1));
179
180 int value;
181 EXPECT_TRUE(meta_table.GetValue(kKey, &value));
182 EXPECT_EQ(kFirstValue, value);
183
184 EXPECT_TRUE(meta_table.SetValue(kKey, kSecondValue));
185 }
186
187 // Existing value was successfully changed.
188 {
189 sql::MetaTable meta_table;
190 EXPECT_TRUE(meta_table.Init(&db(), 1, 1));
191
192 int value;
193 EXPECT_TRUE(meta_table.GetValue(kKey, &value));
194 EXPECT_EQ(kSecondValue, value);
195 }
196}
197
198TEST_F(SQLMetaTableTest, Int64Value) {
Victor Costan1d868352018-06-26 19:06:48199 static const char kKey[] = "Int Key";
tfarina720d4f32015-05-11 22:31:26200 const int64_t kFirstValue = 5000000017LL;
201 const int64_t kSecondValue = 5000000023LL;
[email protected]6ed6d14f2013-09-07 15:48:36202
203 // Initially, the value isn't there until set.
204 {
205 sql::MetaTable meta_table;
206 EXPECT_TRUE(meta_table.Init(&db(), 1, 1));
207
tfarina720d4f32015-05-11 22:31:26208 int64_t value;
[email protected]6ed6d14f2013-09-07 15:48:36209 EXPECT_FALSE(meta_table.GetValue(kKey, &value));
210
211 EXPECT_TRUE(meta_table.SetValue(kKey, kFirstValue));
212 EXPECT_TRUE(meta_table.GetValue(kKey, &value));
213 EXPECT_EQ(kFirstValue, value);
214 }
215
216 // Value is persistent across different instances.
217 {
218 sql::MetaTable meta_table;
219 EXPECT_TRUE(meta_table.Init(&db(), 1, 1));
220
tfarina720d4f32015-05-11 22:31:26221 int64_t value;
[email protected]6ed6d14f2013-09-07 15:48:36222 EXPECT_TRUE(meta_table.GetValue(kKey, &value));
223 EXPECT_EQ(kFirstValue, value);
224
225 EXPECT_TRUE(meta_table.SetValue(kKey, kSecondValue));
226 }
227
228 // Existing value was successfully changed.
229 {
230 sql::MetaTable meta_table;
231 EXPECT_TRUE(meta_table.Init(&db(), 1, 1));
232
tfarina720d4f32015-05-11 22:31:26233 int64_t value;
[email protected]6ed6d14f2013-09-07 15:48:36234 EXPECT_TRUE(meta_table.GetValue(kKey, &value));
235 EXPECT_EQ(kSecondValue, value);
236 }
237}
238
239TEST_F(SQLMetaTableTest, DeleteKey) {
Victor Costan1d868352018-06-26 19:06:48240 static const char kKey[] = "String Key";
[email protected]6ed6d14f2013-09-07 15:48:36241 const std::string kValue("String Value");
242
243 sql::MetaTable meta_table;
244 EXPECT_TRUE(meta_table.Init(&db(), 1, 1));
245
246 // Value isn't present.
247 std::string value;
248 EXPECT_FALSE(meta_table.GetValue(kKey, &value));
249
250 // Now value is present.
251 EXPECT_TRUE(meta_table.SetValue(kKey, kValue));
252 EXPECT_TRUE(meta_table.GetValue(kKey, &value));
253 EXPECT_EQ(kValue, value);
254
255 // After delete value isn't present.
256 EXPECT_TRUE(meta_table.DeleteKey(kKey));
257 EXPECT_FALSE(meta_table.GetValue(kKey, &value));
258}
259
260} // namespace