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

Unified Diff: components/leveldb_proto/core/proto_database_impl_unittest.cc

Issue 330833002: Extract protobuf database into a new 'leveldb_proto' component (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: components/ OWNERS Created 6 years, 6 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
Index: components/leveldb_proto/core/proto_database_impl_unittest.cc
diff --git a/components/dom_distiller/core/dom_distiller_database_unittest.cc b/components/leveldb_proto/core/proto_database_impl_unittest.cc
similarity index 61%
rename from components/dom_distiller/core/dom_distiller_database_unittest.cc
rename to components/leveldb_proto/core/proto_database_impl_unittest.cc
index 54c9043d440961f0ec561d976542481584eca0a6..8512f5081a32fefc03f22fdf5a55079352a720bc 100644
--- a/components/dom_distiller/core/dom_distiller_database_unittest.cc
+++ b/components/leveldb_proto/core/proto_database_impl_unittest.cc
@@ -1,8 +1,8 @@
-// Copyright 2013 The Chromium Authors. All rights reserved.
+// Copyright 2014 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 "components/dom_distiller/core/dom_distiller_database.h"
+#include "components/leveldb_proto/core/proto_database_impl.h"
#include <map>
@@ -11,7 +11,7 @@
#include "base/files/scoped_temp_dir.h"
#include "base/run_loop.h"
#include "base/threading/thread.h"
-#include "components/dom_distiller/core/article_entry.h"
+#include "components/leveldb_proto/testing/proto/test.pb.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
@@ -21,17 +21,18 @@ using testing::Invoke;
using testing::Return;
using testing::_;
-namespace dom_distiller {
+namespace leveldb_proto {
namespace {
-typedef std::map<std::string, ArticleEntry> EntryMap;
+typedef std::map<std::string, TestProto> EntryMap;
-class MockDB : public DomDistillerDatabase::Database {
+class MockDB : public ProtoDatabaseImpl<TestProto>::Database {
public:
MOCK_METHOD1(Init, bool(const base::FilePath&));
- MOCK_METHOD2(Save, bool(const EntryVector&, const EntryVector&));
- MOCK_METHOD1(Load, bool(EntryVector*));
+ MOCK_METHOD2(Save, bool(const ProtoDatabase<TestProto>::KeyEntryVector&,
+ const std::vector<std::string>&));
+ MOCK_METHOD1(Load, bool(std::vector<TestProto>*));
MockDB() {
ON_CALL(*this, Init(_)).WillByDefault(Return(true));
@@ -39,17 +40,17 @@ class MockDB : public DomDistillerDatabase::Database {
ON_CALL(*this, Load(_)).WillByDefault(Return(true));
}
- bool LoadEntries(EntryVector* entries);
+ bool LoadEntries(std::vector<TestProto>* entries);
};
class MockDatabaseCaller {
public:
MOCK_METHOD1(InitCallback, void(bool));
MOCK_METHOD1(SaveCallback, void(bool));
- void LoadCallback(bool success, scoped_ptr<EntryVector> entries) {
+ void LoadCallback(bool success, scoped_ptr<std::vector<TestProto>> entries) {
LoadCallback1(success, entries.get());
}
- MOCK_METHOD2(LoadCallback1, void(bool, EntryVector*));
+ MOCK_METHOD2(LoadCallback1, void(bool, std::vector<TestProto>*));
};
} // namespace
@@ -57,25 +58,23 @@ class MockDatabaseCaller {
EntryMap GetSmallModel() {
EntryMap model;
- model["key0"].set_entry_id("key0");
- model["key0"].add_pages()->set_url("http://foo.com/1");
- model["key0"].add_pages()->set_url("http://foo.com/2");
- model["key0"].add_pages()->set_url("http://foo.com/3");
+ model["0"].set_id("0");
+ model["0"].set_data("http://foo.com/1");
- model["key1"].set_entry_id("key1");
- model["key1"].add_pages()->set_url("http://bar.com/all");
+ model["1"].set_id("1");
+ model["1"].set_data("http://bar.com/all");
- model["key2"].set_entry_id("key2");
- model["key2"].add_pages()->set_url("http://baz.com/1");
+ model["2"].set_id("2");
+ model["2"].set_data("http://baz.com/1");
return model;
}
-void ExpectEntryPointersEquals(EntryMap expected, const EntryVector& actual) {
+void ExpectEntryPointersEquals(EntryMap expected,
+ const std::vector<TestProto>& actual) {
EXPECT_EQ(expected.size(), actual.size());
for (size_t i = 0; i < actual.size(); i++) {
- EntryMap::iterator expected_it =
- expected.find(std::string(actual[i].entry_id()));
+ EntryMap::iterator expected_it = expected.find(actual[i].id());
EXPECT_TRUE(expected_it != expected.end());
std::string serialized_expected = expected_it->second.SerializeAsString();
std::string serialized_actual = actual[i].SerializeAsString();
@@ -84,11 +83,12 @@ void ExpectEntryPointersEquals(EntryMap expected, const EntryVector& actual) {
}
}
-class DomDistillerDatabaseTest : public testing::Test {
+class ProtoDatabaseImplTest : public testing::Test {
public:
virtual void SetUp() {
main_loop_.reset(new MessageLoop());
- db_.reset(new DomDistillerDatabase(main_loop_->message_loop_proxy()));
+ db_.reset(
+ new ProtoDatabaseImpl<TestProto>(main_loop_->message_loop_proxy()));
}
virtual void TearDown() {
@@ -97,13 +97,13 @@ class DomDistillerDatabaseTest : public testing::Test {
main_loop_.reset();
}
- scoped_ptr<DomDistillerDatabase> db_;
+ scoped_ptr<ProtoDatabaseImpl<TestProto>> db_;
scoped_ptr<MessageLoop> main_loop_;
};
-// Test that DomDistillerDatabase calls Init on the underlying database and that
+// Test that ProtoDatabaseImpl calls Init on the underlying database and that
// the caller's InitCallback is called with the correct value.
-TEST_F(DomDistillerDatabaseTest, TestDBInitSuccess) {
+TEST_F(ProtoDatabaseImplTest, TestDBInitSuccess) {
base::FilePath path(FILE_PATH_LITERAL("/fake/path"));
MockDB* mock_db = new MockDB();
@@ -113,14 +113,14 @@ TEST_F(DomDistillerDatabaseTest, TestDBInitSuccess) {
EXPECT_CALL(caller, InitCallback(true));
db_->InitWithDatabase(
- scoped_ptr<DomDistillerDatabase::Database>(mock_db),
+ scoped_ptr<ProtoDatabaseImpl<TestProto>::Database>(mock_db),
base::FilePath(path),
base::Bind(&MockDatabaseCaller::InitCallback, base::Unretained(&caller)));
base::RunLoop().RunUntilIdle();
}
-TEST_F(DomDistillerDatabaseTest, TestDBInitFailure) {
+TEST_F(ProtoDatabaseImplTest, TestDBInitFailure) {
base::FilePath path(FILE_PATH_LITERAL("/fake/path"));
MockDB* mock_db = new MockDB();
@@ -130,7 +130,7 @@ TEST_F(DomDistillerDatabaseTest, TestDBInitFailure) {
EXPECT_CALL(caller, InitCallback(false));
db_->InitWithDatabase(
- scoped_ptr<DomDistillerDatabase::Database>(mock_db),
+ scoped_ptr<ProtoDatabaseImpl<TestProto>::Database>(mock_db),
base::FilePath(path),
base::Bind(&MockDatabaseCaller::InitCallback, base::Unretained(&caller)));
@@ -138,7 +138,7 @@ TEST_F(DomDistillerDatabaseTest, TestDBInitFailure) {
}
ACTION_P(AppendLoadEntries, model) {
- EntryVector* output = arg0;
+ std::vector<TestProto>* output = arg0;
for (EntryMap::const_iterator it = model.begin(); it != model.end(); ++it) {
output->push_back(it->second);
}
@@ -146,15 +146,15 @@ ACTION_P(AppendLoadEntries, model) {
}
ACTION_P(VerifyLoadEntries, expected) {
- EntryVector* actual = arg1;
+ std::vector<TestProto>* actual = arg1;
ExpectEntryPointersEquals(expected, *actual);
}
-// Test that DomDistillerDatabase calls Load on the underlying database and that
+// Test that ProtoDatabaseImpl calls Load on the underlying database and that
// the caller's LoadCallback is called with the correct success value. Also
// confirms that on success, the expected entries are passed to the caller's
// LoadCallback.
-TEST_F(DomDistillerDatabaseTest, TestDBLoadSuccess) {
+TEST_F(ProtoDatabaseImplTest, TestDBLoadSuccess) {
base::FilePath path(FILE_PATH_LITERAL("/fake/path"));
MockDB* mock_db = new MockDB();
@@ -164,7 +164,7 @@ TEST_F(DomDistillerDatabaseTest, TestDBLoadSuccess) {
EXPECT_CALL(*mock_db, Init(_));
EXPECT_CALL(caller, InitCallback(_));
db_->InitWithDatabase(
- scoped_ptr<DomDistillerDatabase::Database>(mock_db),
+ scoped_ptr<ProtoDatabaseImpl<TestProto>::Database>(mock_db),
base::FilePath(path),
base::Bind(&MockDatabaseCaller::InitCallback, base::Unretained(&caller)));
@@ -177,7 +177,7 @@ TEST_F(DomDistillerDatabaseTest, TestDBLoadSuccess) {
base::RunLoop().RunUntilIdle();
}
-TEST_F(DomDistillerDatabaseTest, TestDBLoadFailure) {
+TEST_F(ProtoDatabaseImplTest, TestDBLoadFailure) {
base::FilePath path(FILE_PATH_LITERAL("/fake/path"));
MockDB* mock_db = new MockDB();
@@ -186,7 +186,7 @@ TEST_F(DomDistillerDatabaseTest, TestDBLoadFailure) {
EXPECT_CALL(*mock_db, Init(_));
EXPECT_CALL(caller, InitCallback(_));
db_->InitWithDatabase(
- scoped_ptr<DomDistillerDatabase::Database>(mock_db),
+ scoped_ptr<ProtoDatabaseImpl<TestProto>::Database>(mock_db),
base::FilePath(path),
base::Bind(&MockDatabaseCaller::InitCallback, base::Unretained(&caller)));
@@ -199,15 +199,23 @@ TEST_F(DomDistillerDatabaseTest, TestDBLoadFailure) {
}
ACTION_P(VerifyUpdateEntries, expected) {
- const EntryVector& actual = arg0;
- ExpectEntryPointersEquals(expected, actual);
+ const ProtoDatabase<TestProto>::KeyEntryVector& actual = arg0;
+ // Create a vector of TestProto from |actual| to reuse the comparison
+ // function.
+ std::vector<TestProto> extracted_entries;
+ for (ProtoDatabase<TestProto>::KeyEntryVector::const_iterator it =
+ actual.begin();
+ it != actual.end(); ++it) {
+ extracted_entries.push_back(it->second);
+ }
+ ExpectEntryPointersEquals(expected, extracted_entries);
return true;
}
-// Test that DomDistillerDatabase calls Save on the underlying database with the
+// Test that ProtoDatabaseImpl calls Save on the underlying database with the
// correct entries to save and that the caller's SaveCallback is called with the
// correct success value.
-TEST_F(DomDistillerDatabaseTest, TestDBSaveSuccess) {
+TEST_F(ProtoDatabaseImplTest, TestDBSaveSuccess) {
base::FilePath path(FILE_PATH_LITERAL("/fake/path"));
MockDB* mock_db = new MockDB();
@@ -217,61 +225,57 @@ TEST_F(DomDistillerDatabaseTest, TestDBSaveSuccess) {
EXPECT_CALL(*mock_db, Init(_));
EXPECT_CALL(caller, InitCallback(_));
db_->InitWithDatabase(
- scoped_ptr<DomDistillerDatabase::Database>(mock_db),
+ scoped_ptr<ProtoDatabaseImpl<TestProto>::Database>(mock_db),
base::FilePath(path),
base::Bind(&MockDatabaseCaller::InitCallback, base::Unretained(&caller)));
- scoped_ptr<EntryVector> entries(new EntryVector());
+ scoped_ptr<ProtoDatabase<TestProto>::KeyEntryVector> entries(
+ new ProtoDatabase<TestProto>::KeyEntryVector());
for (EntryMap::iterator it = model.begin(); it != model.end(); ++it) {
- entries->push_back(it->second);
+ entries->push_back(std::make_pair(it->second.id(), it->second));
}
- scoped_ptr<EntryVector> entries_to_remove(new EntryVector());
+ scoped_ptr<std::vector<std::string>> keys_to_remove(
+ new std::vector<std::string>());
EXPECT_CALL(*mock_db, Save(_, _)).WillOnce(VerifyUpdateEntries(model));
EXPECT_CALL(caller, SaveCallback(true));
db_->UpdateEntries(
- entries.Pass(),
- entries_to_remove.Pass(),
+ entries.Pass(), keys_to_remove.Pass(),
base::Bind(&MockDatabaseCaller::SaveCallback, base::Unretained(&caller)));
base::RunLoop().RunUntilIdle();
}
-TEST_F(DomDistillerDatabaseTest, TestDBSaveFailure) {
+TEST_F(ProtoDatabaseImplTest, TestDBSaveFailure) {
base::FilePath path(FILE_PATH_LITERAL("/fake/path"));
MockDB* mock_db = new MockDB();
MockDatabaseCaller caller;
- scoped_ptr<EntryVector> entries(new EntryVector());
- scoped_ptr<EntryVector> entries_to_remove(new EntryVector());
+ scoped_ptr<ProtoDatabase<TestProto>::KeyEntryVector> entries(
+ new ProtoDatabase<TestProto>::KeyEntryVector());
+ scoped_ptr<std::vector<std::string>> keys_to_remove(
+ new std::vector<std::string>());
EXPECT_CALL(*mock_db, Init(_));
EXPECT_CALL(caller, InitCallback(_));
db_->InitWithDatabase(
- scoped_ptr<DomDistillerDatabase::Database>(mock_db),
+ scoped_ptr<ProtoDatabaseImpl<TestProto>::Database>(mock_db),
base::FilePath(path),
base::Bind(&MockDatabaseCaller::InitCallback, base::Unretained(&caller)));
EXPECT_CALL(*mock_db, Save(_, _)).WillOnce(Return(false));
EXPECT_CALL(caller, SaveCallback(false));
db_->UpdateEntries(
- entries.Pass(),
- entries_to_remove.Pass(),
+ entries.Pass(), keys_to_remove.Pass(),
base::Bind(&MockDatabaseCaller::SaveCallback, base::Unretained(&caller)));
base::RunLoop().RunUntilIdle();
}
-ACTION_P(VerifyRemoveEntries, expected) {
- const EntryVector& actual = arg1;
- ExpectEntryPointersEquals(expected, actual);
- return true;
-}
-
-// Test that DomDistillerDatabase calls Save on the underlying database with the
+// Test that ProtoDatabaseImpl calls Save on the underlying database with the
// correct entries to delete and that the caller's SaveCallback is called with
// the correct success value.
-TEST_F(DomDistillerDatabaseTest, TestDBRemoveSuccess) {
+TEST_F(ProtoDatabaseImplTest, TestDBRemoveSuccess) {
base::FilePath path(FILE_PATH_LITERAL("/fake/path"));
MockDB* mock_db = new MockDB();
@@ -281,55 +285,57 @@ TEST_F(DomDistillerDatabaseTest, TestDBRemoveSuccess) {
EXPECT_CALL(*mock_db, Init(_));
EXPECT_CALL(caller, InitCallback(_));
db_->InitWithDatabase(
- scoped_ptr<DomDistillerDatabase::Database>(mock_db),
+ scoped_ptr<ProtoDatabaseImpl<TestProto>::Database>(mock_db),
base::FilePath(path),
base::Bind(&MockDatabaseCaller::InitCallback, base::Unretained(&caller)));
- scoped_ptr<EntryVector> entries(new EntryVector());
- scoped_ptr<EntryVector> entries_to_remove(new EntryVector());
+ scoped_ptr<ProtoDatabase<TestProto>::KeyEntryVector> entries(
+ new ProtoDatabase<TestProto>::KeyEntryVector());
+ scoped_ptr<std::vector<std::string>> keys_to_remove(
+ new std::vector<std::string>());
for (EntryMap::iterator it = model.begin(); it != model.end(); ++it) {
- entries_to_remove->push_back(it->second);
+ keys_to_remove->push_back(it->second.id());
}
- EXPECT_CALL(*mock_db, Save(_, _)).WillOnce(VerifyRemoveEntries(model));
+ std::vector<std::string> keys_copy(*keys_to_remove.get());
+ EXPECT_CALL(*mock_db, Save(_, keys_copy)).WillOnce(Return(true));
EXPECT_CALL(caller, SaveCallback(true));
db_->UpdateEntries(
- entries.Pass(),
- entries_to_remove.Pass(),
+ entries.Pass(), keys_to_remove.Pass(),
base::Bind(&MockDatabaseCaller::SaveCallback, base::Unretained(&caller)));
base::RunLoop().RunUntilIdle();
}
-TEST_F(DomDistillerDatabaseTest, TestDBRemoveFailure) {
+TEST_F(ProtoDatabaseImplTest, TestDBRemoveFailure) {
base::FilePath path(FILE_PATH_LITERAL("/fake/path"));
MockDB* mock_db = new MockDB();
MockDatabaseCaller caller;
- scoped_ptr<EntryVector> entries(new EntryVector());
- scoped_ptr<EntryVector> entries_to_remove(new EntryVector());
+ scoped_ptr<ProtoDatabase<TestProto>::KeyEntryVector> entries(
+ new ProtoDatabase<TestProto>::KeyEntryVector());
+ scoped_ptr<std::vector<std::string>> keys_to_remove(
+ new std::vector<std::string>());
EXPECT_CALL(*mock_db, Init(_));
EXPECT_CALL(caller, InitCallback(_));
db_->InitWithDatabase(
- scoped_ptr<DomDistillerDatabase::Database>(mock_db),
+ scoped_ptr<ProtoDatabaseImpl<TestProto>::Database>(mock_db),
base::FilePath(path),
base::Bind(&MockDatabaseCaller::InitCallback, base::Unretained(&caller)));
EXPECT_CALL(*mock_db, Save(_, _)).WillOnce(Return(false));
EXPECT_CALL(caller, SaveCallback(false));
db_->UpdateEntries(
- entries.Pass(),
- entries_to_remove.Pass(),
+ entries.Pass(), keys_to_remove.Pass(),
base::Bind(&MockDatabaseCaller::SaveCallback, base::Unretained(&caller)));
base::RunLoop().RunUntilIdle();
}
-
// This tests that normal usage of the real database does not cause any
// threading violations.
-TEST(DomDistillerDatabaseThreadingTest, TestDBDestruction) {
+TEST(ProtoDatabaseImplThreadingTest, TestDBDestruction) {
base::MessageLoop main_loop;
ScopedTempDir temp_dir;
@@ -338,14 +344,13 @@ TEST(DomDistillerDatabaseThreadingTest, TestDBDestruction) {
base::Thread db_thread("dbthread");
ASSERT_TRUE(db_thread.Start());
- scoped_ptr<DomDistillerDatabase> db(
- new DomDistillerDatabase(db_thread.message_loop_proxy()));
+ scoped_ptr<ProtoDatabaseImpl<TestProto>> db(
+ new ProtoDatabaseImpl<TestProto>(db_thread.message_loop_proxy()));
MockDatabaseCaller caller;
EXPECT_CALL(caller, InitCallback(_));
- db->Init(
- temp_dir.path(),
- base::Bind(&MockDatabaseCaller::InitCallback, base::Unretained(&caller)));
+ db->Init(temp_dir.path(), base::Bind(&MockDatabaseCaller::InitCallback,
+ base::Unretained(&caller)));
db.reset();
@@ -363,21 +368,22 @@ void TestLevelDBSaveAndLoad(bool close_after_save) {
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
EntryMap model = GetSmallModel();
- EntryVector save_entries;
- EntryVector load_entries;
- EntryVector remove_entries;
+
+ ProtoDatabase<TestProto>::KeyEntryVector save_entries;
+ std::vector<TestProto> load_entries;
+ std::vector<std::string> remove_keys;
for (EntryMap::iterator it = model.begin(); it != model.end(); ++it) {
- save_entries.push_back(it->second);
+ save_entries.push_back(std::make_pair(it->second.id(), it->second));
}
- scoped_ptr<DomDistillerDatabase::LevelDB> db(
- new DomDistillerDatabase::LevelDB());
+ scoped_ptr<ProtoDatabaseImpl<TestProto>::LevelDB> db(
+ new ProtoDatabaseImpl<TestProto>::LevelDB());
EXPECT_TRUE(db->Init(temp_dir.path()));
- EXPECT_TRUE(db->Save(save_entries, remove_entries));
+ EXPECT_TRUE(db->Save(save_entries, remove_keys));
if (close_after_save) {
- db.reset(new DomDistillerDatabase::LevelDB());
+ db.reset(new ProtoDatabaseImpl<TestProto>::LevelDB());
EXPECT_TRUE(db->Init(temp_dir.path()));
}
@@ -386,12 +392,12 @@ void TestLevelDBSaveAndLoad(bool close_after_save) {
ExpectEntryPointersEquals(model, load_entries);
}
-TEST(DomDistillerDatabaseLevelDBTest, TestDBSaveAndLoad) {
+TEST(ProtoDatabaseImplLevelDBTest, TestDBSaveAndLoad) {
TestLevelDBSaveAndLoad(false);
}
-TEST(DomDistillerDatabaseLevelDBTest, TestDBCloseAndReopen) {
+TEST(ProtoDatabaseImplLevelDBTest, TestDBCloseAndReopen) {
TestLevelDBSaveAndLoad(true);
}
-} // namespace dom_distiller
+} // namespace leveldb_proto

Powered by Google App Engine
This is Rietveld 408576698