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

Unified 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: Created 7 years, 4 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | sql/sql.gyp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sql/meta_table_unittest.cc
diff --git a/sql/meta_table_unittest.cc b/sql/meta_table_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..03d71129a4d397f3fecd979bdb91827c196b8951
--- /dev/null
+++ b/sql/meta_table_unittest.cc
@@ -0,0 +1,224 @@
+// Copyright (c) 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "sql/meta_table.h"
+
+#include "base/files/file_path.h"
+#include "base/files/scoped_temp_dir.h"
+#include "sql/connection.h"
+#include "sql/statement.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace {
+
+class SQLMetaTableTest : public testing::Test {
+ public:
+ SQLMetaTableTest() {}
Greg Billock 2013/08/29 18:05:40 needed?
Scott Hess - ex-Googler 2013/08/29 23:32:26 You're right. Also removing in the place I did th
+
+ virtual void SetUp() {
+ ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
+ ASSERT_TRUE(db_.Open(db_path()));
+ }
+
+ virtual void TearDown() {
+ db_.Close();
+ }
+
+ sql::Connection& db() { return db_; }
+
+ base::FilePath db_path() {
Greg Billock 2013/08/29 18:05:40 private? or just build in SetUp?
Scott Hess - ex-Googler 2013/08/29 23:32:26 Sure. And connection_unittest.cc while I'm there.
Greg Billock 2013/09/06 16:02:09 I meant just build db_path_ in SetUp and don't hav
Scott Hess - ex-Googler 2013/09/06 16:28:28 Done. Didn't even notice that :-).
+ return temp_dir_.path().AppendASCII("SQLMetaTableTest.db");
+ }
+
+ private:
+ base::ScopedTempDir temp_dir_;
+ sql::Connection db_;
Scott Hess - ex-Googler 2013/08/29 23:32:26 Hmm. Also, seems iffy to have temp_dir_ destructe
Greg Billock 2013/09/06 16:02:09 This was right, no? Destruction is in reverse orde
Scott Hess - ex-Googler 2013/09/06 16:28:28 Darn it. You are correct, and I know this, but it
Greg Billock 2013/09/06 23:19:34 :-) I feel your pain. :-)
+};
+
+TEST_F(SQLMetaTableTest, DoesTableExist) {
+ EXPECT_FALSE(sql::MetaTable::DoesTableExist(&db()));
+
+ {
+ sql::MetaTable meta_table;
+ EXPECT_TRUE(meta_table.Init(&db(), 1, 1));
+ }
+
+ EXPECT_TRUE(sql::MetaTable::DoesTableExist(&db()));
+}
+
+TEST_F(SQLMetaTableTest, VersionNumber) {
+ const int kVersionFirst = 2, kCompatVersionFirst = 1;
Greg Billock 2013/08/29 18:05:40 one per line (and below)
Scott Hess - ex-Googler 2013/08/29 23:32:26 Done.
+ const int kVersionSecond = 4, kCompatVersionSecond = 3;
+ const int kVersionThird = 6, kCompatVersionThird = 5;
+
+ // First Init() sets the version info as expected.
+ {
+ sql::MetaTable meta_table;
+ EXPECT_TRUE(meta_table.Init(&db(), kVersionFirst, kCompatVersionFirst));
+ EXPECT_EQ(kVersionFirst, meta_table.GetVersionNumber());
+ EXPECT_EQ(kCompatVersionFirst, meta_table.GetCompatibleVersionNumber());
+ }
+
+ // Second Init() does not change the version info.
+ {
+ sql::MetaTable meta_table;
+ EXPECT_TRUE(meta_table.Init(&db(), kVersionSecond, kCompatVersionSecond));
Greg Billock 2013/08/29 18:05:40 This sure looks like it is changing the version in
Scott Hess - ex-Googler 2013/08/29 23:32:26 I believe that the idea is that on first run, the
+ EXPECT_EQ(kVersionFirst, meta_table.GetVersionNumber());
+ EXPECT_EQ(kCompatVersionFirst, meta_table.GetCompatibleVersionNumber());
+
+ meta_table.SetVersionNumber(kVersionSecond);
+ meta_table.SetCompatibleVersionNumber(kCompatVersionSecond);
+ }
+
+ // Version info from Set*() calls is seen.
+ {
+ sql::MetaTable meta_table;
+ EXPECT_TRUE(meta_table.Init(&db(), kVersionThird, kCompatVersionThird));
+ EXPECT_EQ(kVersionSecond, meta_table.GetVersionNumber());
+ EXPECT_EQ(kCompatVersionSecond, meta_table.GetCompatibleVersionNumber());
+ }
+}
+
+TEST_F(SQLMetaTableTest, StringValue) {
+ const char kKey[] = "String Key";
+ const std::string kFirstValue("First Value"), kSecondValue("Second Value");
+
+ // Initially, the value isn't there until set.
+ {
+ sql::MetaTable meta_table;
+ EXPECT_TRUE(meta_table.Init(&db(), 1, 1));
+
+ std::string value;
+ EXPECT_FALSE(meta_table.GetValue(kKey, &value));
+
+ EXPECT_TRUE(meta_table.SetValue(kKey, kFirstValue));
+ EXPECT_TRUE(meta_table.GetValue(kKey, &value));
+ EXPECT_EQ(kFirstValue, value);
+ }
+
+ // Value is persistent across different instances.
+ {
+ sql::MetaTable meta_table;
+ EXPECT_TRUE(meta_table.Init(&db(), 1, 1));
+
+ std::string value;
+ EXPECT_TRUE(meta_table.GetValue(kKey, &value));
+ EXPECT_EQ(kFirstValue, value);
+
+ EXPECT_TRUE(meta_table.SetValue(kKey, kSecondValue));
+ }
+
+ // Existing value was successfully changed.
+ {
+ sql::MetaTable meta_table;
+ EXPECT_TRUE(meta_table.Init(&db(), 1, 1));
+
+ std::string value;
+ EXPECT_TRUE(meta_table.GetValue(kKey, &value));
+ EXPECT_EQ(kSecondValue, value);
+ }
+}
+
+TEST_F(SQLMetaTableTest, IntValue) {
+ const char kKey[] = "Int Key";
+ const int kFirstValue = 17, kSecondValue = 23;
+
+ // Initially, the value isn't there until set.
+ {
+ sql::MetaTable meta_table;
+ EXPECT_TRUE(meta_table.Init(&db(), 1, 1));
+
+ int value;
+ EXPECT_FALSE(meta_table.GetValue(kKey, &value));
+
+ EXPECT_TRUE(meta_table.SetValue(kKey, kFirstValue));
+ EXPECT_TRUE(meta_table.GetValue(kKey, &value));
+ EXPECT_EQ(kFirstValue, value);
+ }
+
+ // Value is persistent across different instances.
+ {
+ sql::MetaTable meta_table;
+ EXPECT_TRUE(meta_table.Init(&db(), 1, 1));
+
+ int value;
+ EXPECT_TRUE(meta_table.GetValue(kKey, &value));
+ EXPECT_EQ(kFirstValue, value);
+
+ EXPECT_TRUE(meta_table.SetValue(kKey, kSecondValue));
+ }
+
+ // Existing value was successfully changed.
+ {
+ sql::MetaTable meta_table;
+ EXPECT_TRUE(meta_table.Init(&db(), 1, 1));
+
+ int value;
+ EXPECT_TRUE(meta_table.GetValue(kKey, &value));
+ EXPECT_EQ(kSecondValue, value);
+ }
+}
+
+TEST_F(SQLMetaTableTest, Int64Value) {
+ const char kKey[] = "Int Key";
+ const int64 kFirstValue = 5000000017LLU, kSecondValue = 5000000023LLU;
+
+ // Initially, the value isn't there until set.
+ {
+ sql::MetaTable meta_table;
+ EXPECT_TRUE(meta_table.Init(&db(), 1, 1));
+
+ int64 value;
+ EXPECT_FALSE(meta_table.GetValue(kKey, &value));
+
+ EXPECT_TRUE(meta_table.SetValue(kKey, kFirstValue));
+ EXPECT_TRUE(meta_table.GetValue(kKey, &value));
+ EXPECT_EQ(kFirstValue, value);
+ }
+
+ // Value is persistent across different instances.
+ {
+ sql::MetaTable meta_table;
+ EXPECT_TRUE(meta_table.Init(&db(), 1, 1));
+
+ int64 value;
+ EXPECT_TRUE(meta_table.GetValue(kKey, &value));
+ EXPECT_EQ(kFirstValue, value);
+
+ EXPECT_TRUE(meta_table.SetValue(kKey, kSecondValue));
+ }
+
+ // Existing value was successfully changed.
+ {
+ sql::MetaTable meta_table;
+ EXPECT_TRUE(meta_table.Init(&db(), 1, 1));
+
+ int64 value;
+ EXPECT_TRUE(meta_table.GetValue(kKey, &value));
+ EXPECT_EQ(kSecondValue, value);
+ }
+}
+
+TEST_F(SQLMetaTableTest, DeleteKey) {
+ const char kKey[] = "String Key";
+ const std::string kValue("String Value");
+
+ sql::MetaTable meta_table;
+ EXPECT_TRUE(meta_table.Init(&db(), 1, 1));
+
+ // Value isn't present.
+ std::string value;
+ EXPECT_FALSE(meta_table.GetValue(kKey, &value));
+
+ // Now value is present.
+ EXPECT_TRUE(meta_table.SetValue(kKey, kValue));
+ EXPECT_TRUE(meta_table.GetValue(kKey, &value));
+ EXPECT_EQ(kValue, value);
+
+ // After delete value isn't present.
+ EXPECT_TRUE(meta_table.DeleteKey(kKey));
+ EXPECT_FALSE(meta_table.GetValue(kKey, &value));
+}
+
+} // namespace
« no previous file with comments | « no previous file | sql/sql.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698