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

Unified Diff: components/leveldb_proto/proto_database_impl_unittest.cc

Issue 2049573003: Implement ProtoDatabase::GetEntry (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: not found vs error Created 4 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
« no previous file with comments | « components/leveldb_proto/proto_database_impl.h ('k') | components/leveldb_proto/testing/fake_db.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: components/leveldb_proto/proto_database_impl_unittest.cc
diff --git a/components/leveldb_proto/proto_database_impl_unittest.cc b/components/leveldb_proto/proto_database_impl_unittest.cc
index 79fa13172506463ac73c81aefea9e8c8e1e736c8..c3dc76b4ce9ef6d245ee62841f81f68ca6fcab16 100644
--- a/components/leveldb_proto/proto_database_impl_unittest.cc
+++ b/components/leveldb_proto/proto_database_impl_unittest.cc
@@ -42,12 +42,9 @@ class MockDB : public LevelDB {
MOCK_METHOD1(Init, bool(const base::FilePath&));
MOCK_METHOD2(Save, bool(const KeyValueVector&, const KeyVector&));
MOCK_METHOD1(Load, bool(std::vector<std::string>*));
+ MOCK_METHOD3(Get, bool(const std::string&, bool*, std::string*));
- MockDB() : LevelDB(kTestLevelDBClientName) {
- ON_CALL(*this, Init(_)).WillByDefault(Return(true));
- ON_CALL(*this, Save(_, _)).WillByDefault(Return(true));
- ON_CALL(*this, Load(_)).WillByDefault(Return(true));
- }
+ MockDB() : LevelDB(kTestLevelDBClientName) {}
};
class MockDatabaseCaller {
@@ -60,6 +57,10 @@ class MockDatabaseCaller {
LoadCallback1(success, entries.get());
}
MOCK_METHOD2(LoadCallback1, void(bool, std::vector<TestProto>*));
+ void GetCallback(bool success, std::unique_ptr<TestProto> entry) {
+ GetCallback1(success, entry.get());
+ }
+ MOCK_METHOD2(GetCallback1, void(bool, TestProto*));
};
} // namespace
@@ -202,6 +203,95 @@ TEST_F(ProtoDatabaseImplTest, TestDBLoadFailure) {
base::RunLoop().RunUntilIdle();
}
+ACTION_P(SetGetEntry, model) {
+ const std::string& key = arg0;
+ bool* found = arg1;
+ std::string* output = arg2;
+ auto it = model.find(key);
+ if (it == model.end()) {
+ *found = false;
+ } else {
+ *found = true;
+ *output = it->second.SerializeAsString();
+ }
+ return true;
+}
+
+ACTION_P(VerifyGetEntry, expected) {
+ TestProto* actual = arg1;
+ EXPECT_EQ(expected.SerializeAsString(), actual->SerializeAsString());
+}
+
+TEST_F(ProtoDatabaseImplTest, TestDBGetSuccess) {
+ base::FilePath path(FILE_PATH_LITERAL("/fake/path"));
+
+ MockDB* mock_db = new MockDB();
+ MockDatabaseCaller caller;
+ EntryMap model = GetSmallModel();
+
+ EXPECT_CALL(*mock_db, Init(_));
+ EXPECT_CALL(caller, InitCallback(_));
+ db_->InitWithDatabase(
+ base::WrapUnique(mock_db), path,
+ base::Bind(&MockDatabaseCaller::InitCallback, base::Unretained(&caller)));
+
+ std::string key("1");
+ ASSERT_TRUE(model.count(key));
+ EXPECT_CALL(*mock_db, Get(key, _, _)).WillOnce(SetGetEntry(model));
+ EXPECT_CALL(caller, GetCallback1(true, _))
+ .WillOnce(VerifyGetEntry(model[key]));
+ db_->GetEntry(key, base::Bind(&MockDatabaseCaller::GetCallback,
+ base::Unretained(&caller)));
+
+ base::RunLoop().RunUntilIdle();
+}
+
+TEST_F(ProtoDatabaseImplTest, TestDBGetNotFound) {
+ base::FilePath path(FILE_PATH_LITERAL("/fake/path"));
+
+ MockDB* mock_db = new MockDB();
+ MockDatabaseCaller caller;
+ EntryMap model = GetSmallModel();
+
+ EXPECT_CALL(*mock_db, Init(_));
+ EXPECT_CALL(caller, InitCallback(_));
+ db_->InitWithDatabase(
+ base::WrapUnique(mock_db), path,
+ base::Bind(&MockDatabaseCaller::InitCallback, base::Unretained(&caller)));
+
+ std::string key("does_not_exist");
+ ASSERT_FALSE(model.count(key));
+ EXPECT_CALL(*mock_db, Get(key, _, _)).WillOnce(SetGetEntry(model));
+ EXPECT_CALL(caller, GetCallback1(true, nullptr));
+ db_->GetEntry(key, base::Bind(&MockDatabaseCaller::GetCallback,
+ base::Unretained(&caller)));
+
+ base::RunLoop().RunUntilIdle();
+}
+
+TEST_F(ProtoDatabaseImplTest, TestDBGetFailure) {
+ base::FilePath path(FILE_PATH_LITERAL("/fake/path"));
+
+ MockDB* mock_db = new MockDB();
+ MockDatabaseCaller caller;
+ EntryMap model = GetSmallModel();
+
+ EXPECT_CALL(*mock_db, Init(_));
+ EXPECT_CALL(caller, InitCallback(_));
+ db_->InitWithDatabase(
+ base::WrapUnique(mock_db), path,
+ base::Bind(&MockDatabaseCaller::InitCallback, base::Unretained(&caller)));
+
+ std::string key("does_not_exist");
+ ASSERT_FALSE(model.count(key));
+ EXPECT_CALL(*mock_db, Get(key, _, _)).WillOnce(Return(false));
+ EXPECT_CALL(caller, GetCallback1(false, nullptr));
+ db_->GetEntry(key, base::Bind(&MockDatabaseCaller::GetCallback,
+ base::Unretained(&caller)));
+
+ base::RunLoop().RunUntilIdle();
+}
+
ACTION_P(VerifyUpdateEntries, expected) {
const KeyValueVector actual = arg0;
// Create a vector of TestProto from |actual| to reuse the comparison
« no previous file with comments | « components/leveldb_proto/proto_database_impl.h ('k') | components/leveldb_proto/testing/fake_db.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698