Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(60)

Side by Side Diff: sql/meta_table_unittest.cc

Issue 23649002: Unit tests for sql::MetaTable. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Sigh. Portable int64 constants FTW. Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « sql/connection_unittest.cc ('k') | sql/sql.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 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
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"
12
13 namespace {
14
15 class SQLMetaTableTest : public testing::Test {
16 public:
17 virtual void SetUp() {
18 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
19 ASSERT_TRUE(db_.Open(temp_dir_.path().AppendASCII("SQLMetaTableTest.db")));
20 }
21
22 virtual void TearDown() {
23 db_.Close();
24 }
25
26 sql::Connection& db() { return db_; }
27
28 private:
29 base::ScopedTempDir temp_dir_;
30 sql::Connection db_;
31 };
32
33 TEST_F(SQLMetaTableTest, DoesTableExist) {
34 EXPECT_FALSE(sql::MetaTable::DoesTableExist(&db()));
35
36 {
37 sql::MetaTable meta_table;
38 EXPECT_TRUE(meta_table.Init(&db(), 1, 1));
39 }
40
41 EXPECT_TRUE(sql::MetaTable::DoesTableExist(&db()));
42 }
43
44 TEST_F(SQLMetaTableTest, VersionNumber) {
45 // Compatibility versions one less than the main versions to make
46 // sure the values aren't being crossed with each other.
47 const int kVersionFirst = 2;
48 const int kCompatVersionFirst = kVersionFirst - 1;
49 const int kVersionSecond = 4;
50 const int kCompatVersionSecond = kVersionSecond - 1;
51 const int kVersionThird = 6;
52 const int kCompatVersionThird = kVersionThird - 1;
53
54 // First Init() sets the version info as expected.
55 {
56 sql::MetaTable meta_table;
57 EXPECT_TRUE(meta_table.Init(&db(), kVersionFirst, kCompatVersionFirst));
58 EXPECT_EQ(kVersionFirst, meta_table.GetVersionNumber());
59 EXPECT_EQ(kCompatVersionFirst, meta_table.GetCompatibleVersionNumber());
60 }
61
62 // Second Init() does not change the version info.
63 {
64 sql::MetaTable meta_table;
65 EXPECT_TRUE(meta_table.Init(&db(), kVersionSecond, kCompatVersionSecond));
66 EXPECT_EQ(kVersionFirst, meta_table.GetVersionNumber());
67 EXPECT_EQ(kCompatVersionFirst, meta_table.GetCompatibleVersionNumber());
68
69 meta_table.SetVersionNumber(kVersionSecond);
70 meta_table.SetCompatibleVersionNumber(kCompatVersionSecond);
71 }
72
73 // Version info from Set*() calls is seen.
74 {
75 sql::MetaTable meta_table;
76 EXPECT_TRUE(meta_table.Init(&db(), kVersionThird, kCompatVersionThird));
77 EXPECT_EQ(kVersionSecond, meta_table.GetVersionNumber());
78 EXPECT_EQ(kCompatVersionSecond, meta_table.GetCompatibleVersionNumber());
79 }
80 }
81
82 TEST_F(SQLMetaTableTest, StringValue) {
83 const char kKey[] = "String Key";
84 const std::string kFirstValue("First Value");
85 const std::string kSecondValue("Second Value");
86
87 // Initially, the value isn't there until set.
88 {
89 sql::MetaTable meta_table;
90 EXPECT_TRUE(meta_table.Init(&db(), 1, 1));
91
92 std::string value;
93 EXPECT_FALSE(meta_table.GetValue(kKey, &value));
94
95 EXPECT_TRUE(meta_table.SetValue(kKey, kFirstValue));
96 EXPECT_TRUE(meta_table.GetValue(kKey, &value));
97 EXPECT_EQ(kFirstValue, value);
98 }
99
100 // Value is persistent across different instances.
101 {
102 sql::MetaTable meta_table;
103 EXPECT_TRUE(meta_table.Init(&db(), 1, 1));
104
105 std::string value;
106 EXPECT_TRUE(meta_table.GetValue(kKey, &value));
107 EXPECT_EQ(kFirstValue, value);
108
109 EXPECT_TRUE(meta_table.SetValue(kKey, kSecondValue));
110 }
111
112 // Existing value was successfully changed.
113 {
114 sql::MetaTable meta_table;
115 EXPECT_TRUE(meta_table.Init(&db(), 1, 1));
116
117 std::string value;
118 EXPECT_TRUE(meta_table.GetValue(kKey, &value));
119 EXPECT_EQ(kSecondValue, value);
120 }
121 }
122
123 TEST_F(SQLMetaTableTest, IntValue) {
124 const char kKey[] = "Int Key";
125 const int kFirstValue = 17;
126 const int kSecondValue = 23;
127
128 // Initially, the value isn't there until set.
129 {
130 sql::MetaTable meta_table;
131 EXPECT_TRUE(meta_table.Init(&db(), 1, 1));
132
133 int value;
134 EXPECT_FALSE(meta_table.GetValue(kKey, &value));
135
136 EXPECT_TRUE(meta_table.SetValue(kKey, kFirstValue));
137 EXPECT_TRUE(meta_table.GetValue(kKey, &value));
138 EXPECT_EQ(kFirstValue, value);
139 }
140
141 // Value is persistent across different instances.
142 {
143 sql::MetaTable meta_table;
144 EXPECT_TRUE(meta_table.Init(&db(), 1, 1));
145
146 int value;
147 EXPECT_TRUE(meta_table.GetValue(kKey, &value));
148 EXPECT_EQ(kFirstValue, value);
149
150 EXPECT_TRUE(meta_table.SetValue(kKey, kSecondValue));
151 }
152
153 // Existing value was successfully changed.
154 {
155 sql::MetaTable meta_table;
156 EXPECT_TRUE(meta_table.Init(&db(), 1, 1));
157
158 int value;
159 EXPECT_TRUE(meta_table.GetValue(kKey, &value));
160 EXPECT_EQ(kSecondValue, value);
161 }
162 }
163
164 TEST_F(SQLMetaTableTest, Int64Value) {
165 const char kKey[] = "Int Key";
166 const int64 kFirstValue = GG_LONGLONG(5000000017);
167 const int64 kSecondValue = GG_LONGLONG(5000000023);
168
169 // Initially, the value isn't there until set.
170 {
171 sql::MetaTable meta_table;
172 EXPECT_TRUE(meta_table.Init(&db(), 1, 1));
173
174 int64 value;
175 EXPECT_FALSE(meta_table.GetValue(kKey, &value));
176
177 EXPECT_TRUE(meta_table.SetValue(kKey, kFirstValue));
178 EXPECT_TRUE(meta_table.GetValue(kKey, &value));
179 EXPECT_EQ(kFirstValue, value);
180 }
181
182 // Value is persistent across different instances.
183 {
184 sql::MetaTable meta_table;
185 EXPECT_TRUE(meta_table.Init(&db(), 1, 1));
186
187 int64 value;
188 EXPECT_TRUE(meta_table.GetValue(kKey, &value));
189 EXPECT_EQ(kFirstValue, value);
190
191 EXPECT_TRUE(meta_table.SetValue(kKey, kSecondValue));
192 }
193
194 // Existing value was successfully changed.
195 {
196 sql::MetaTable meta_table;
197 EXPECT_TRUE(meta_table.Init(&db(), 1, 1));
198
199 int64 value;
200 EXPECT_TRUE(meta_table.GetValue(kKey, &value));
201 EXPECT_EQ(kSecondValue, value);
202 }
203 }
204
205 TEST_F(SQLMetaTableTest, DeleteKey) {
206 const char kKey[] = "String Key";
207 const std::string kValue("String Value");
208
209 sql::MetaTable meta_table;
210 EXPECT_TRUE(meta_table.Init(&db(), 1, 1));
211
212 // Value isn't present.
213 std::string value;
214 EXPECT_FALSE(meta_table.GetValue(kKey, &value));
215
216 // Now value is present.
217 EXPECT_TRUE(meta_table.SetValue(kKey, kValue));
218 EXPECT_TRUE(meta_table.GetValue(kKey, &value));
219 EXPECT_EQ(kValue, value);
220
221 // After delete value isn't present.
222 EXPECT_TRUE(meta_table.DeleteKey(kKey));
223 EXPECT_FALSE(meta_table.GetValue(kKey, &value));
224 }
225
226 } // namespace
OLDNEW
« no previous file with comments | « sql/connection_unittest.cc ('k') | sql/sql.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698