Feat: runtime profile override with getenv
[platform/framework/web/chromium-efl.git] / sql / meta_table_unittest.cc
1 // Copyright 2013 The Chromium Authors
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 <stdint.h>
8
9 #include "base/files/file_path.h"
10 #include "base/files/scoped_temp_dir.h"
11 #include "sql/database.h"
12 #include "sql/statement.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 namespace sql {
16
17 namespace {
18
19 class SQLMetaTableTest : public testing::Test {
20  public:
21   ~SQLMetaTableTest() override = default;
22
23   void SetUp() override {
24     ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
25     ASSERT_TRUE(
26         db_.Open(temp_dir_.GetPath().AppendASCII("meta_table_test.sqlite")));
27   }
28
29  protected:
30   base::ScopedTempDir temp_dir_;
31   Database db_;
32 };
33
34 TEST_F(SQLMetaTableTest, DoesTableExist) {
35   EXPECT_FALSE(MetaTable::DoesTableExist(&db_));
36
37   {
38     MetaTable meta_table;
39     EXPECT_TRUE(meta_table.Init(&db_, 1, 1));
40   }
41
42   EXPECT_TRUE(MetaTable::DoesTableExist(&db_));
43 }
44
45 TEST_F(SQLMetaTableTest, DeleteTableForTesting) {
46   MetaTable meta_table;
47   EXPECT_TRUE(meta_table.Init(&db_, 1, 1));
48
49   EXPECT_TRUE(MetaTable::DeleteTableForTesting(&db_));
50   EXPECT_FALSE(MetaTable::DoesTableExist(&db_));
51 }
52
53 TEST_F(SQLMetaTableTest, RazeIfIncompatiblePreservesDatabasesWithoutMetadata) {
54   EXPECT_TRUE(db_.Execute("CREATE TABLE data(id INTEGER PRIMARY KEY)"));
55   ASSERT_TRUE(db_.DoesTableExist("data"));
56
57   // The table should not have been cleared, since the database does not have a
58   // metadata table.
59   EXPECT_TRUE(MetaTable::RazeIfIncompatible(&db_, 1,
60                                             /*current_version=*/1));
61   EXPECT_TRUE(db_.DoesTableExist("data"));
62 }
63
64 TEST_F(SQLMetaTableTest, RazeIfIncompatibleRazesIncompatiblyOldTables) {
65   constexpr int kWrittenVersion = 1;
66   constexpr int kCompatibleVersion = 1;
67
68   // Setup a current database.
69   {
70     MetaTable meta_table;
71     EXPECT_TRUE(meta_table.Init(&db_, kWrittenVersion, kCompatibleVersion));
72     EXPECT_TRUE(db_.Execute("CREATE TABLE data(id INTEGER PRIMARY KEY)"));
73     ASSERT_TRUE(db_.DoesTableExist("data"));
74   }
75
76   // The table should have been cleared, since the least version compatible with
77   // the written database is greater than the current version.
78   EXPECT_TRUE(
79       MetaTable::RazeIfIncompatible(&db_, kWrittenVersion + 1,
80                                     /*current_version=*/kWrittenVersion + 1));
81   EXPECT_FALSE(db_.DoesTableExist("data"));
82 }
83
84 TEST_F(SQLMetaTableTest, RazeIfIncompatibleRazesIncompatiblyNewTables) {
85   constexpr int kCompatibleVersion = 2;
86   constexpr int kWrittenVersion = 3;
87
88   // Setup a current database.
89   {
90     MetaTable meta_table;
91     EXPECT_TRUE(meta_table.Init(&db_, kWrittenVersion, kCompatibleVersion));
92     EXPECT_TRUE(db_.Execute("CREATE TABLE data(id INTEGER PRIMARY KEY)"));
93     ASSERT_TRUE(db_.DoesTableExist("data"));
94   }
95
96   // The table should have been cleared, since the least version compatible with
97   // the written database is greater than the current version.
98   EXPECT_TRUE(MetaTable::RazeIfIncompatible(
99       &db_, MetaTable::kNoLowestSupportedVersion,
100       /*current_version=*/kCompatibleVersion - 1));
101   EXPECT_FALSE(db_.DoesTableExist("data"));
102 }
103
104 TEST_F(SQLMetaTableTest, RazeIfIncompatibleDoesntRazeWhenItShouldnt) {
105   constexpr int kVersion = 2;
106
107   {
108     MetaTable meta_table;
109     EXPECT_TRUE(
110         meta_table.Init(&db_, kVersion, /*compatible_version=*/kVersion - 1));
111     EXPECT_TRUE(db_.Execute("CREATE TABLE data(id INTEGER PRIMARY KEY)"));
112     EXPECT_TRUE(db_.DoesTableExist("data"));
113   }
114
115   EXPECT_TRUE(MetaTable::RazeIfIncompatible(&db_, kVersion,
116                                             /*current_version=*/kVersion));
117   EXPECT_TRUE(db_.DoesTableExist("data"))
118       << "Table should still exist if the database version is exactly right.";
119
120   EXPECT_TRUE(MetaTable::RazeIfIncompatible(&db_, kVersion - 1,
121                                             /*current_version=*/kVersion));
122   EXPECT_TRUE(db_.DoesTableExist("data"))
123       << "... or if the lower bound is less than the actual version";
124
125   EXPECT_TRUE(
126       MetaTable::RazeIfIncompatible(&db_, MetaTable::kNoLowestSupportedVersion,
127                                     /*current_version=*/kVersion));
128   EXPECT_TRUE(db_.DoesTableExist("data"))
129       << "... or if the lower bound is not set";
130
131   EXPECT_TRUE(
132       MetaTable::RazeIfIncompatible(&db_, MetaTable::kNoLowestSupportedVersion,
133                                     /*current_version=*/kVersion - 1));
134   EXPECT_TRUE(db_.DoesTableExist("data"))
135       << "... even if the current version exactly matches the written "
136          "database's least compatible version.";
137 }
138
139 TEST_F(SQLMetaTableTest, VersionNumber) {
140   // Compatibility versions one less than the main versions to make
141   // sure the values aren't being crossed with each other.
142   constexpr int kVersionFirst = 2;
143   constexpr int kCompatVersionFirst = kVersionFirst - 1;
144   constexpr int kVersionSecond = 4;
145   constexpr int kCompatVersionSecond = kVersionSecond - 1;
146   constexpr int kVersionThird = 6;
147   constexpr int kCompatVersionThird = kVersionThird - 1;
148
149   // First Init() sets the version info as expected.
150   {
151     MetaTable meta_table;
152     EXPECT_TRUE(meta_table.Init(&db_, kVersionFirst, kCompatVersionFirst));
153     EXPECT_EQ(kVersionFirst, meta_table.GetVersionNumber());
154     EXPECT_EQ(kCompatVersionFirst, meta_table.GetCompatibleVersionNumber());
155   }
156
157   // Second Init() does not change the version info.
158   {
159     MetaTable meta_table;
160     EXPECT_TRUE(meta_table.Init(&db_, kVersionSecond, kCompatVersionSecond));
161     EXPECT_EQ(kVersionFirst, meta_table.GetVersionNumber());
162     EXPECT_EQ(kCompatVersionFirst, meta_table.GetCompatibleVersionNumber());
163
164     EXPECT_TRUE(meta_table.SetVersionNumber(kVersionSecond));
165     EXPECT_TRUE(meta_table.SetCompatibleVersionNumber(kCompatVersionSecond));
166   }
167
168   // Version info from Set*() calls is seen.
169   {
170     MetaTable meta_table;
171     EXPECT_TRUE(meta_table.Init(&db_, kVersionThird, kCompatVersionThird));
172     EXPECT_EQ(kVersionSecond, meta_table.GetVersionNumber());
173     EXPECT_EQ(kCompatVersionSecond, meta_table.GetCompatibleVersionNumber());
174   }
175 }
176
177 TEST_F(SQLMetaTableTest, StringValue) {
178   static const char kKey[] = "String Key";
179   const std::string kFirstValue("First Value");
180   const std::string kSecondValue("Second Value");
181
182   // Initially, the value isn't there until set.
183   {
184     MetaTable meta_table;
185     EXPECT_TRUE(meta_table.Init(&db_, 1, 1));
186
187     std::string value;
188     EXPECT_FALSE(meta_table.GetValue(kKey, &value));
189
190     EXPECT_TRUE(meta_table.SetValue(kKey, kFirstValue));
191     EXPECT_TRUE(meta_table.GetValue(kKey, &value));
192     EXPECT_EQ(kFirstValue, value);
193   }
194
195   // Value is persistent across different instances.
196   {
197     MetaTable meta_table;
198     EXPECT_TRUE(meta_table.Init(&db_, 1, 1));
199
200     std::string value;
201     EXPECT_TRUE(meta_table.GetValue(kKey, &value));
202     EXPECT_EQ(kFirstValue, value);
203
204     EXPECT_TRUE(meta_table.SetValue(kKey, kSecondValue));
205   }
206
207   // Existing value was successfully changed.
208   {
209     MetaTable meta_table;
210     EXPECT_TRUE(meta_table.Init(&db_, 1, 1));
211
212     std::string value;
213     EXPECT_TRUE(meta_table.GetValue(kKey, &value));
214     EXPECT_EQ(kSecondValue, value);
215   }
216 }
217
218 TEST_F(SQLMetaTableTest, IntValue) {
219   static const char kKey[] = "Int Key";
220   constexpr int kFirstValue = 17;
221   constexpr int kSecondValue = 23;
222
223   // Initially, the value isn't there until set.
224   {
225     MetaTable meta_table;
226     EXPECT_TRUE(meta_table.Init(&db_, 1, 1));
227
228     int value;
229     EXPECT_FALSE(meta_table.GetValue(kKey, &value));
230
231     EXPECT_TRUE(meta_table.SetValue(kKey, kFirstValue));
232     EXPECT_TRUE(meta_table.GetValue(kKey, &value));
233     EXPECT_EQ(kFirstValue, value);
234   }
235
236   // Value is persistent across different instances.
237   {
238     MetaTable meta_table;
239     EXPECT_TRUE(meta_table.Init(&db_, 1, 1));
240
241     int value;
242     EXPECT_TRUE(meta_table.GetValue(kKey, &value));
243     EXPECT_EQ(kFirstValue, value);
244
245     EXPECT_TRUE(meta_table.SetValue(kKey, kSecondValue));
246   }
247
248   // Existing value was successfully changed.
249   {
250     MetaTable meta_table;
251     EXPECT_TRUE(meta_table.Init(&db_, 1, 1));
252
253     int value;
254     EXPECT_TRUE(meta_table.GetValue(kKey, &value));
255     EXPECT_EQ(kSecondValue, value);
256   }
257 }
258
259 TEST_F(SQLMetaTableTest, Int64Value) {
260   static const char kKey[] = "Int Key";
261   const int64_t kFirstValue = 5000000017LL;
262   const int64_t kSecondValue = 5000000023LL;
263
264   // Initially, the value isn't there until set.
265   {
266     MetaTable meta_table;
267     EXPECT_TRUE(meta_table.Init(&db_, 1, 1));
268
269     int64_t value;
270     EXPECT_FALSE(meta_table.GetValue(kKey, &value));
271
272     EXPECT_TRUE(meta_table.SetValue(kKey, kFirstValue));
273     EXPECT_TRUE(meta_table.GetValue(kKey, &value));
274     EXPECT_EQ(kFirstValue, value);
275   }
276
277   // Value is persistent across different instances.
278   {
279     MetaTable meta_table;
280     EXPECT_TRUE(meta_table.Init(&db_, 1, 1));
281
282     int64_t value;
283     EXPECT_TRUE(meta_table.GetValue(kKey, &value));
284     EXPECT_EQ(kFirstValue, value);
285
286     EXPECT_TRUE(meta_table.SetValue(kKey, kSecondValue));
287   }
288
289   // Existing value was successfully changed.
290   {
291     MetaTable meta_table;
292     EXPECT_TRUE(meta_table.Init(&db_, 1, 1));
293
294     int64_t value;
295     EXPECT_TRUE(meta_table.GetValue(kKey, &value));
296     EXPECT_EQ(kSecondValue, value);
297   }
298 }
299
300 TEST_F(SQLMetaTableTest, DeleteKey) {
301   static const char kKey[] = "String Key";
302   const std::string kValue("String Value");
303
304   MetaTable meta_table;
305   EXPECT_TRUE(meta_table.Init(&db_, 1, 1));
306
307   // Value isn't present.
308   std::string value;
309   EXPECT_FALSE(meta_table.GetValue(kKey, &value));
310
311   // Now value is present.
312   EXPECT_TRUE(meta_table.SetValue(kKey, kValue));
313   EXPECT_TRUE(meta_table.GetValue(kKey, &value));
314   EXPECT_EQ(kValue, value);
315
316   // After delete value isn't present.
317   EXPECT_TRUE(meta_table.DeleteKey(kKey));
318   EXPECT_FALSE(meta_table.GetValue(kKey, &value));
319 }
320
321 }  // namespace
322
323 }  // namespace sql