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

Side by Side 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: add tests 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 unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/leveldb_proto/proto_database_impl.h" 5 #include "components/leveldb_proto/proto_database_impl.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <map> 9 #include <map>
10 #include <memory> 10 #include <memory>
(...skipping 24 matching lines...) Expand all
35 35
36 typedef std::map<std::string, TestProto> EntryMap; 36 typedef std::map<std::string, TestProto> EntryMap;
37 37
38 const char kTestLevelDBClientName[] = "Test"; 38 const char kTestLevelDBClientName[] = "Test";
39 39
40 class MockDB : public LevelDB { 40 class MockDB : public LevelDB {
41 public: 41 public:
42 MOCK_METHOD1(Init, bool(const base::FilePath&)); 42 MOCK_METHOD1(Init, bool(const base::FilePath&));
43 MOCK_METHOD2(Save, bool(const KeyValueVector&, const KeyVector&)); 43 MOCK_METHOD2(Save, bool(const KeyValueVector&, const KeyVector&));
44 MOCK_METHOD1(Load, bool(std::vector<std::string>*)); 44 MOCK_METHOD1(Load, bool(std::vector<std::string>*));
45 MOCK_METHOD2(Get, bool(const std::string&, std::string*));
45 46
46 MockDB() : LevelDB(kTestLevelDBClientName) { 47 MockDB() : LevelDB(kTestLevelDBClientName) {}
47 ON_CALL(*this, Init(_)).WillByDefault(Return(true));
48 ON_CALL(*this, Save(_, _)).WillByDefault(Return(true));
49 ON_CALL(*this, Load(_)).WillByDefault(Return(true));
Marc Treib 2016/06/15 12:35:59 All these default actions weren't actually needed
50 }
51 }; 48 };
52 49
53 class MockDatabaseCaller { 50 class MockDatabaseCaller {
54 public: 51 public:
55 MOCK_METHOD1(InitCallback, void(bool)); 52 MOCK_METHOD1(InitCallback, void(bool));
56 MOCK_METHOD1(DestroyCallback, void(bool)); 53 MOCK_METHOD1(DestroyCallback, void(bool));
57 MOCK_METHOD1(SaveCallback, void(bool)); 54 MOCK_METHOD1(SaveCallback, void(bool));
58 void LoadCallback(bool success, 55 void LoadCallback(bool success,
59 std::unique_ptr<std::vector<TestProto>> entries) { 56 std::unique_ptr<std::vector<TestProto>> entries) {
60 LoadCallback1(success, entries.get()); 57 LoadCallback1(success, entries.get());
61 } 58 }
62 MOCK_METHOD2(LoadCallback1, void(bool, std::vector<TestProto>*)); 59 MOCK_METHOD2(LoadCallback1, void(bool, std::vector<TestProto>*));
60 void GetCallback(bool success,
61 std::unique_ptr<TestProto> entry) {
62 GetCallback1(success, entry.get());
63 }
64 MOCK_METHOD2(GetCallback1, void(bool, TestProto*));
63 }; 65 };
64 66
65 } // namespace 67 } // namespace
66 68
67 EntryMap GetSmallModel() { 69 EntryMap GetSmallModel() {
68 EntryMap model; 70 EntryMap model;
69 71
70 model["0"].set_id("0"); 72 model["0"].set_id("0");
71 model["0"].set_data("http://foo.com/1"); 73 model["0"].set_data("http://foo.com/1");
72 74
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
195 base::Bind(&MockDatabaseCaller::InitCallback, base::Unretained(&caller))); 197 base::Bind(&MockDatabaseCaller::InitCallback, base::Unretained(&caller)));
196 198
197 EXPECT_CALL(*mock_db, Load(_)).WillOnce(Return(false)); 199 EXPECT_CALL(*mock_db, Load(_)).WillOnce(Return(false));
198 EXPECT_CALL(caller, LoadCallback1(false, _)); 200 EXPECT_CALL(caller, LoadCallback1(false, _));
199 db_->LoadEntries( 201 db_->LoadEntries(
200 base::Bind(&MockDatabaseCaller::LoadCallback, base::Unretained(&caller))); 202 base::Bind(&MockDatabaseCaller::LoadCallback, base::Unretained(&caller)));
201 203
202 base::RunLoop().RunUntilIdle(); 204 base::RunLoop().RunUntilIdle();
203 } 205 }
204 206
207 ACTION_P(SetGetEntry, model) {
208 const std::string& key = arg0;
209 std::string* output = arg1;
210 auto it = model.find(key);
211 if (it == model.end())
212 return false;
213 *output = it->second.SerializeAsString();
214 return true;
215 }
216
217 ACTION_P(VerifyGetEntry, expected) {
218 TestProto* actual = arg1;
219 EXPECT_EQ(expected.SerializeAsString(), actual->SerializeAsString());
220 }
221
222 TEST_F(ProtoDatabaseImplTest, TestDBGetSuccess) {
223 base::FilePath path(FILE_PATH_LITERAL("/fake/path"));
224
225 MockDB* mock_db = new MockDB();
226 MockDatabaseCaller caller;
227 EntryMap model = GetSmallModel();
228
229 EXPECT_CALL(*mock_db, Init(_));
230 EXPECT_CALL(caller, InitCallback(_));
231 db_->InitWithDatabase(
232 base::WrapUnique(mock_db), path,
233 base::Bind(&MockDatabaseCaller::InitCallback, base::Unretained(&caller)));
234
235 std::string key("1");
236 ASSERT_TRUE(model.count(key));
237 EXPECT_CALL(*mock_db, Get(key, _)).WillOnce(SetGetEntry(model));
238 EXPECT_CALL(caller, GetCallback1(true, _))
239 .WillOnce(VerifyGetEntry(model[key]));
240 db_->GetEntry(
241 key,
242 base::Bind(&MockDatabaseCaller::GetCallback, base::Unretained(&caller)));
243
244 base::RunLoop().RunUntilIdle();
245 }
246
247 TEST_F(ProtoDatabaseImplTest, TestDBGetFailure) {
248 base::FilePath path(FILE_PATH_LITERAL("/fake/path"));
249
250 MockDB* mock_db = new MockDB();
251 MockDatabaseCaller caller;
252 EntryMap model = GetSmallModel();
253
254 EXPECT_CALL(*mock_db, Init(_));
255 EXPECT_CALL(caller, InitCallback(_));
256 db_->InitWithDatabase(
257 base::WrapUnique(mock_db), path,
258 base::Bind(&MockDatabaseCaller::InitCallback, base::Unretained(&caller)));
259
260 std::string key("does_not_exist");
261 ASSERT_FALSE(model.count(key));
262 EXPECT_CALL(*mock_db, Get(key, _)).WillOnce(Return(false));
263 EXPECT_CALL(caller, GetCallback1(false, nullptr));
264 db_->GetEntry(
265 key,
266 base::Bind(&MockDatabaseCaller::GetCallback, base::Unretained(&caller)));
267
268 base::RunLoop().RunUntilIdle();
269 }
270
205 ACTION_P(VerifyUpdateEntries, expected) { 271 ACTION_P(VerifyUpdateEntries, expected) {
206 const KeyValueVector actual = arg0; 272 const KeyValueVector actual = arg0;
207 // Create a vector of TestProto from |actual| to reuse the comparison 273 // Create a vector of TestProto from |actual| to reuse the comparison
208 // function. 274 // function.
209 std::vector<TestProto> extracted_entries; 275 std::vector<TestProto> extracted_entries;
210 for (const auto& pair : actual) { 276 for (const auto& pair : actual) {
211 TestProto entry; 277 TestProto entry;
212 if (!entry.ParseFromString(pair.second)) { 278 if (!entry.ParseFromString(pair.second)) {
213 ADD_FAILURE() << "Unable to deserialize the protobuf"; 279 ADD_FAILURE() << "Unable to deserialize the protobuf";
214 return false; 280 return false;
(...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after
473 539
474 ASSERT_TRUE(db->Save(save_entries, remove_keys)); 540 ASSERT_TRUE(db->Save(save_entries, remove_keys));
475 541
476 std::vector<std::string> second_load_entries; 542 std::vector<std::string> second_load_entries;
477 543
478 ASSERT_TRUE(db->Load(&second_load_entries)); 544 ASSERT_TRUE(db->Load(&second_load_entries));
479 EXPECT_EQ(1u, second_load_entries.size()); 545 EXPECT_EQ(1u, second_load_entries.size());
480 } 546 }
481 547
482 } // namespace leveldb_proto 548 } // namespace leveldb_proto
OLDNEW
« 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