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

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: 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 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_METHOD3(Get, bool(const std::string&, bool*, 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));
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, std::unique_ptr<TestProto> entry) {
61 GetCallback1(success, entry.get());
62 }
63 MOCK_METHOD2(GetCallback1, void(bool, TestProto*));
63 }; 64 };
64 65
65 } // namespace 66 } // namespace
66 67
67 EntryMap GetSmallModel() { 68 EntryMap GetSmallModel() {
68 EntryMap model; 69 EntryMap model;
69 70
70 model["0"].set_id("0"); 71 model["0"].set_id("0");
71 model["0"].set_data("http://foo.com/1"); 72 model["0"].set_data("http://foo.com/1");
72 73
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
195 base::Bind(&MockDatabaseCaller::InitCallback, base::Unretained(&caller))); 196 base::Bind(&MockDatabaseCaller::InitCallback, base::Unretained(&caller)));
196 197
197 EXPECT_CALL(*mock_db, Load(_)).WillOnce(Return(false)); 198 EXPECT_CALL(*mock_db, Load(_)).WillOnce(Return(false));
198 EXPECT_CALL(caller, LoadCallback1(false, _)); 199 EXPECT_CALL(caller, LoadCallback1(false, _));
199 db_->LoadEntries( 200 db_->LoadEntries(
200 base::Bind(&MockDatabaseCaller::LoadCallback, base::Unretained(&caller))); 201 base::Bind(&MockDatabaseCaller::LoadCallback, base::Unretained(&caller)));
201 202
202 base::RunLoop().RunUntilIdle(); 203 base::RunLoop().RunUntilIdle();
203 } 204 }
204 205
206 ACTION_P(SetGetEntry, model) {
207 const std::string& key = arg0;
208 bool* found = arg1;
209 std::string* output = arg2;
210 auto it = model.find(key);
211 if (it == model.end()) {
212 *found = false;
213 } else {
214 *found = true;
215 *output = it->second.SerializeAsString();
216 }
217 return true;
218 }
219
220 ACTION_P(VerifyGetEntry, expected) {
221 TestProto* actual = arg1;
222 EXPECT_EQ(expected.SerializeAsString(), actual->SerializeAsString());
223 }
224
225 TEST_F(ProtoDatabaseImplTest, TestDBGetSuccess) {
226 base::FilePath path(FILE_PATH_LITERAL("/fake/path"));
227
228 MockDB* mock_db = new MockDB();
229 MockDatabaseCaller caller;
230 EntryMap model = GetSmallModel();
231
232 EXPECT_CALL(*mock_db, Init(_));
233 EXPECT_CALL(caller, InitCallback(_));
234 db_->InitWithDatabase(
235 base::WrapUnique(mock_db), path,
236 base::Bind(&MockDatabaseCaller::InitCallback, base::Unretained(&caller)));
237
238 std::string key("1");
239 ASSERT_TRUE(model.count(key));
240 EXPECT_CALL(*mock_db, Get(key, _, _)).WillOnce(SetGetEntry(model));
241 EXPECT_CALL(caller, GetCallback1(true, _))
242 .WillOnce(VerifyGetEntry(model[key]));
243 db_->GetEntry(key, base::Bind(&MockDatabaseCaller::GetCallback,
244 base::Unretained(&caller)));
245
246 base::RunLoop().RunUntilIdle();
247 }
248
249 TEST_F(ProtoDatabaseImplTest, TestDBGetNotFound) {
250 base::FilePath path(FILE_PATH_LITERAL("/fake/path"));
251
252 MockDB* mock_db = new MockDB();
253 MockDatabaseCaller caller;
254 EntryMap model = GetSmallModel();
255
256 EXPECT_CALL(*mock_db, Init(_));
257 EXPECT_CALL(caller, InitCallback(_));
258 db_->InitWithDatabase(
259 base::WrapUnique(mock_db), path,
260 base::Bind(&MockDatabaseCaller::InitCallback, base::Unretained(&caller)));
261
262 std::string key("does_not_exist");
263 ASSERT_FALSE(model.count(key));
264 EXPECT_CALL(*mock_db, Get(key, _, _)).WillOnce(SetGetEntry(model));
265 EXPECT_CALL(caller, GetCallback1(true, nullptr));
266 db_->GetEntry(key, base::Bind(&MockDatabaseCaller::GetCallback,
267 base::Unretained(&caller)));
268
269 base::RunLoop().RunUntilIdle();
270 }
271
272 TEST_F(ProtoDatabaseImplTest, TestDBGetFailure) {
273 base::FilePath path(FILE_PATH_LITERAL("/fake/path"));
274
275 MockDB* mock_db = new MockDB();
276 MockDatabaseCaller caller;
277 EntryMap model = GetSmallModel();
278
279 EXPECT_CALL(*mock_db, Init(_));
280 EXPECT_CALL(caller, InitCallback(_));
281 db_->InitWithDatabase(
282 base::WrapUnique(mock_db), path,
283 base::Bind(&MockDatabaseCaller::InitCallback, base::Unretained(&caller)));
284
285 std::string key("does_not_exist");
286 ASSERT_FALSE(model.count(key));
287 EXPECT_CALL(*mock_db, Get(key, _, _)).WillOnce(Return(false));
288 EXPECT_CALL(caller, GetCallback1(false, nullptr));
289 db_->GetEntry(key, base::Bind(&MockDatabaseCaller::GetCallback,
290 base::Unretained(&caller)));
291
292 base::RunLoop().RunUntilIdle();
293 }
294
205 ACTION_P(VerifyUpdateEntries, expected) { 295 ACTION_P(VerifyUpdateEntries, expected) {
206 const KeyValueVector actual = arg0; 296 const KeyValueVector actual = arg0;
207 // Create a vector of TestProto from |actual| to reuse the comparison 297 // Create a vector of TestProto from |actual| to reuse the comparison
208 // function. 298 // function.
209 std::vector<TestProto> extracted_entries; 299 std::vector<TestProto> extracted_entries;
210 for (const auto& pair : actual) { 300 for (const auto& pair : actual) {
211 TestProto entry; 301 TestProto entry;
212 if (!entry.ParseFromString(pair.second)) { 302 if (!entry.ParseFromString(pair.second)) {
213 ADD_FAILURE() << "Unable to deserialize the protobuf"; 303 ADD_FAILURE() << "Unable to deserialize the protobuf";
214 return false; 304 return false;
(...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after
473 563
474 ASSERT_TRUE(db->Save(save_entries, remove_keys)); 564 ASSERT_TRUE(db->Save(save_entries, remove_keys));
475 565
476 std::vector<std::string> second_load_entries; 566 std::vector<std::string> second_load_entries;
477 567
478 ASSERT_TRUE(db->Load(&second_load_entries)); 568 ASSERT_TRUE(db->Load(&second_load_entries));
479 EXPECT_EQ(1u, second_load_entries.size()); 569 EXPECT_EQ(1u, second_load_entries.size());
480 } 570 }
481 571
482 } // namespace leveldb_proto 572 } // 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