[M108 Migration][HBBTV] Implement ewk_context_register_jsplugin_mime_types API
[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   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   MetaTable::RazeIfIncompatible(&db_, kWrittenVersion + 1,
79                                 /*current_version=*/kWrittenVersion + 1);
80   EXPECT_FALSE(db_.DoesTableExist("data"));
81 }
82
83 TEST_F(SQLMetaTableTest, RazeIfIncompatibleRazesIncompatiblyNewTables) {
84   constexpr int kCompatibleVersion = 2;
85   constexpr int kWrittenVersion = 3;
86
87   // Setup a current database.
88   {
89     MetaTable meta_table;
90     EXPECT_TRUE(meta_table.Init(&db_, kWrittenVersion, kCompatibleVersion));
91     EXPECT_TRUE(db_.Execute("CREATE TABLE data(id INTEGER PRIMARY KEY)"));
92     ASSERT_TRUE(db_.DoesTableExist("data"));
93   }
94
95   // The table should have been cleared, since the least version compatible with
96   // the written database is greater than the current version.
97   MetaTable::RazeIfIncompatible(&db_, MetaTable::kNoLowestSupportedVersion,
98                                 /*current_version=*/kCompatibleVersion - 1);
99   EXPECT_FALSE(db_.DoesTableExist("data"));
100 }
101
102 TEST_F(SQLMetaTableTest, RazeIfIncompatibleDoesntRazeWhenItShouldnt) {
103   constexpr int kVersion = 2;
104
105   {
106     MetaTable meta_table;
107     EXPECT_TRUE(
108         meta_table.Init(&db_, kVersion, /*compatible_version=*/kVersion - 1));
109     EXPECT_TRUE(db_.Execute("CREATE TABLE data(id INTEGER PRIMARY KEY)"));
110     EXPECT_TRUE(db_.DoesTableExist("data"));
111   }
112
113   MetaTable::RazeIfIncompatible(&db_, kVersion,
114                                 /*current_version=*/kVersion);
115   EXPECT_TRUE(db_.DoesTableExist("data"))
116       << "Table should still exist if the database version is exactly right.";
117
118   MetaTable::RazeIfIncompatible(&db_, kVersion - 1,
119                                 /*current_version=*/kVersion);
120   EXPECT_TRUE(db_.DoesTableExist("data"))
121       << "... or if the lower bound is less than the actual version";
122
123   MetaTable::RazeIfIncompatible(&db_, MetaTable::kNoLowestSupportedVersion,
124                                 /*current_version=*/kVersion);
125   EXPECT_TRUE(db_.DoesTableExist("data"))
126       << "... or if the lower bound is not set";
127
128   MetaTable::RazeIfIncompatible(&db_, MetaTable::kNoLowestSupportedVersion,
129                                 /*current_version=*/kVersion - 1);
130   EXPECT_TRUE(db_.DoesTableExist("data"))
131       << "... even if the current version exactly matches the written "
132          "database's least compatible version.";
133 }
134
135 TEST_F(SQLMetaTableTest, VersionNumber) {
136   // Compatibility versions one less than the main versions to make
137   // sure the values aren't being crossed with each other.
138   constexpr int kVersionFirst = 2;
139   constexpr int kCompatVersionFirst = kVersionFirst - 1;
140   constexpr int kVersionSecond = 4;
141   constexpr int kCompatVersionSecond = kVersionSecond - 1;
142   constexpr int kVersionThird = 6;
143   constexpr int kCompatVersionThird = kVersionThird - 1;
144
145   // First Init() sets the version info as expected.
146   {
147     MetaTable meta_table;
148     EXPECT_TRUE(meta_table.Init(&db_, kVersionFirst, kCompatVersionFirst));
149     EXPECT_EQ(kVersionFirst, meta_table.GetVersionNumber());
150     EXPECT_EQ(kCompatVersionFirst, meta_table.GetCompatibleVersionNumber());
151   }
152
153   // Second Init() does not change the version info.
154   {
155     MetaTable meta_table;
156     EXPECT_TRUE(meta_table.Init(&db_, kVersionSecond, kCompatVersionSecond));
157     EXPECT_EQ(kVersionFirst, meta_table.GetVersionNumber());
158     EXPECT_EQ(kCompatVersionFirst, meta_table.GetCompatibleVersionNumber());
159
160     meta_table.SetVersionNumber(kVersionSecond);
161     meta_table.SetCompatibleVersionNumber(kCompatVersionSecond);
162   }
163
164   // Version info from Set*() calls is seen.
165   {
166     MetaTable meta_table;
167     EXPECT_TRUE(meta_table.Init(&db_, kVersionThird, kCompatVersionThird));
168     EXPECT_EQ(kVersionSecond, meta_table.GetVersionNumber());
169     EXPECT_EQ(kCompatVersionSecond, meta_table.GetCompatibleVersionNumber());
170   }
171 }
172
173 TEST_F(SQLMetaTableTest, StringValue) {
174   static const char kKey[] = "String Key";
175   const std::string kFirstValue("First Value");
176   const std::string kSecondValue("Second Value");
177
178   // Initially, the value isn't there until set.
179   {
180     MetaTable meta_table;
181     EXPECT_TRUE(meta_table.Init(&db_, 1, 1));
182
183     std::string value;
184     EXPECT_FALSE(meta_table.GetValue(kKey, &value));
185
186     EXPECT_TRUE(meta_table.SetValue(kKey, kFirstValue));
187     EXPECT_TRUE(meta_table.GetValue(kKey, &value));
188     EXPECT_EQ(kFirstValue, value);
189   }
190
191   // Value is persistent across different instances.
192   {
193     MetaTable meta_table;
194     EXPECT_TRUE(meta_table.Init(&db_, 1, 1));
195
196     std::string value;
197     EXPECT_TRUE(meta_table.GetValue(kKey, &value));
198     EXPECT_EQ(kFirstValue, value);
199
200     EXPECT_TRUE(meta_table.SetValue(kKey, kSecondValue));
201   }
202
203   // Existing value was successfully changed.
204   {
205     MetaTable meta_table;
206     EXPECT_TRUE(meta_table.Init(&db_, 1, 1));
207
208     std::string value;
209     EXPECT_TRUE(meta_table.GetValue(kKey, &value));
210     EXPECT_EQ(kSecondValue, value);
211   }
212 }
213
214 TEST_F(SQLMetaTableTest, IntValue) {
215   static const char kKey[] = "Int Key";
216   constexpr int kFirstValue = 17;
217   constexpr int kSecondValue = 23;
218
219   // Initially, the value isn't there until set.
220   {
221     MetaTable meta_table;
222     EXPECT_TRUE(meta_table.Init(&db_, 1, 1));
223
224     int value;
225     EXPECT_FALSE(meta_table.GetValue(kKey, &value));
226
227     EXPECT_TRUE(meta_table.SetValue(kKey, kFirstValue));
228     EXPECT_TRUE(meta_table.GetValue(kKey, &value));
229     EXPECT_EQ(kFirstValue, value);
230   }
231
232   // Value is persistent across different instances.
233   {
234     MetaTable meta_table;
235     EXPECT_TRUE(meta_table.Init(&db_, 1, 1));
236
237     int value;
238     EXPECT_TRUE(meta_table.GetValue(kKey, &value));
239     EXPECT_EQ(kFirstValue, value);
240
241     EXPECT_TRUE(meta_table.SetValue(kKey, kSecondValue));
242   }
243
244   // Existing value was successfully changed.
245   {
246     MetaTable meta_table;
247     EXPECT_TRUE(meta_table.Init(&db_, 1, 1));
248
249     int value;
250     EXPECT_TRUE(meta_table.GetValue(kKey, &value));
251     EXPECT_EQ(kSecondValue, value);
252   }
253 }
254
255 TEST_F(SQLMetaTableTest, Int64Value) {
256   static const char kKey[] = "Int Key";
257   const int64_t kFirstValue = 5000000017LL;
258   const int64_t kSecondValue = 5000000023LL;
259
260   // Initially, the value isn't there until set.
261   {
262     MetaTable meta_table;
263     EXPECT_TRUE(meta_table.Init(&db_, 1, 1));
264
265     int64_t value;
266     EXPECT_FALSE(meta_table.GetValue(kKey, &value));
267
268     EXPECT_TRUE(meta_table.SetValue(kKey, kFirstValue));
269     EXPECT_TRUE(meta_table.GetValue(kKey, &value));
270     EXPECT_EQ(kFirstValue, value);
271   }
272
273   // Value is persistent across different instances.
274   {
275     MetaTable meta_table;
276     EXPECT_TRUE(meta_table.Init(&db_, 1, 1));
277
278     int64_t value;
279     EXPECT_TRUE(meta_table.GetValue(kKey, &value));
280     EXPECT_EQ(kFirstValue, value);
281
282     EXPECT_TRUE(meta_table.SetValue(kKey, kSecondValue));
283   }
284
285   // Existing value was successfully changed.
286   {
287     MetaTable meta_table;
288     EXPECT_TRUE(meta_table.Init(&db_, 1, 1));
289
290     int64_t value;
291     EXPECT_TRUE(meta_table.GetValue(kKey, &value));
292     EXPECT_EQ(kSecondValue, value);
293   }
294 }
295
296 TEST_F(SQLMetaTableTest, DeleteKey) {
297   static const char kKey[] = "String Key";
298   const std::string kValue("String Value");
299
300   MetaTable meta_table;
301   EXPECT_TRUE(meta_table.Init(&db_, 1, 1));
302
303   // Value isn't present.
304   std::string value;
305   EXPECT_FALSE(meta_table.GetValue(kKey, &value));
306
307   // Now value is present.
308   EXPECT_TRUE(meta_table.SetValue(kKey, kValue));
309   EXPECT_TRUE(meta_table.GetValue(kKey, &value));
310   EXPECT_EQ(kValue, value);
311
312   // After delete value isn't present.
313   EXPECT_TRUE(meta_table.DeleteKey(kKey));
314   EXPECT_FALSE(meta_table.GetValue(kKey, &value));
315 }
316
317 }  // namespace
318
319 }  // namespace sql