tile_store_unittest.cc 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. // Copyright 2020 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "components/query_tiles/internal/tile_store.h"
  5. #include <map>
  6. #include <memory>
  7. #include <string>
  8. #include <utility>
  9. #include "base/memory/raw_ptr.h"
  10. #include "base/test/task_environment.h"
  11. #include "components/leveldb_proto/public/proto_database.h"
  12. #include "components/leveldb_proto/testing/fake_db.h"
  13. #include "components/query_tiles/internal/proto_conversion.h"
  14. #include "components/query_tiles/test/test_utils.h"
  15. #include "testing/gtest/include/gtest/gtest.h"
  16. using leveldb_proto::test::FakeDB;
  17. using InitStatus = leveldb_proto::Enums::InitStatus;
  18. namespace query_tiles {
  19. namespace {
  20. class TileStoreTest : public testing::Test {
  21. public:
  22. using TileGroupProto = query_tiles::proto::TileGroup;
  23. using EntriesMap = std::map<std::string, std::unique_ptr<TileGroup>>;
  24. using ProtoMap = std::map<std::string, TileGroupProto>;
  25. using KeysAndEntries = std::map<std::string, TileGroup>;
  26. using TestEntries = std::vector<TileGroup>;
  27. TileStoreTest() : load_result_(false), db_(nullptr) {}
  28. ~TileStoreTest() override = default;
  29. TileStoreTest(const TileStoreTest& other) = delete;
  30. TileStoreTest& operator=(const TileStoreTest& other) = delete;
  31. protected:
  32. void Init(TestEntries input, InitStatus status) {
  33. CreateTestDbEntries(std::move(input));
  34. auto db = std::make_unique<FakeDB<TileGroupProto, TileGroup>>(&db_entries_);
  35. db_ = db.get();
  36. store_ = std::make_unique<TileStore>(std::move(db));
  37. store_->InitAndLoad(base::BindOnce(&TileStoreTest::OnEntriesLoaded,
  38. base::Unretained(this)));
  39. db_->InitStatusCallback(status);
  40. }
  41. void OnEntriesLoaded(bool success, EntriesMap loaded_entries) {
  42. load_result_ = success;
  43. in_memory_entries_ = std::move(loaded_entries);
  44. }
  45. void CreateTestDbEntries(TestEntries input) {
  46. for (auto& entry : input) {
  47. TileGroupProto proto;
  48. query_tiles::TileGroupToProto(&entry, &proto);
  49. db_entries_.emplace(entry.id, proto);
  50. }
  51. }
  52. // Verifies the entries in the db is |expected|.
  53. void VerifyDataInDb(std::unique_ptr<KeysAndEntries> expected) {
  54. db_->LoadKeysAndEntries(base::BindOnce(&TileStoreTest::OnVerifyDataInDb,
  55. base::Unretained(this),
  56. std::move(expected)));
  57. db_->LoadCallback(true);
  58. }
  59. void OnVerifyDataInDb(std::unique_ptr<KeysAndEntries> expected,
  60. bool success,
  61. std::unique_ptr<KeysAndEntries> loaded_entries) {
  62. EXPECT_TRUE(success);
  63. DCHECK(expected);
  64. DCHECK(loaded_entries);
  65. for (auto it = loaded_entries->begin(); it != loaded_entries->end(); it++) {
  66. EXPECT_NE(expected->count(it->first), 0u);
  67. auto& actual_loaded_group = it->second;
  68. auto& expected_group = expected->at(it->first);
  69. EXPECT_TRUE(
  70. test::AreTileGroupsIdentical(actual_loaded_group, expected_group))
  71. << "\n Actual: " << actual_loaded_group.DebugString()
  72. << "\n Expected: " << expected_group.DebugString();
  73. }
  74. }
  75. bool load_result() const { return load_result_; }
  76. const EntriesMap& in_memory_entries() const { return in_memory_entries_; }
  77. FakeDB<TileGroupProto, TileGroup>* db() { return db_; }
  78. Store<TileGroup>* store() { return store_.get(); }
  79. private:
  80. base::test::TaskEnvironment task_environment_;
  81. bool load_result_;
  82. EntriesMap in_memory_entries_;
  83. ProtoMap db_entries_;
  84. raw_ptr<FakeDB<TileGroupProto, TileGroup>> db_;
  85. std::unique_ptr<Store<TileGroup>> store_;
  86. };
  87. // Test Initializing and loading an empty database .
  88. TEST_F(TileStoreTest, InitSuccessEmptyDb) {
  89. auto test_data = TestEntries();
  90. Init(std::move(test_data), InitStatus::kOK);
  91. db()->LoadCallback(true);
  92. EXPECT_EQ(load_result(), true);
  93. EXPECT_TRUE(in_memory_entries().empty());
  94. }
  95. // Test Initializing and loading a non-empty database.
  96. TEST_F(TileStoreTest, InitSuccessWithData) {
  97. auto test_data = TestEntries();
  98. TileGroup test_group;
  99. test::ResetTestGroup(&test_group);
  100. std::string id = test_group.id;
  101. test_data.emplace_back(std::move(test_group));
  102. Init(std::move(test_data), InitStatus::kOK);
  103. db()->LoadCallback(true);
  104. EXPECT_EQ(load_result(), true);
  105. EXPECT_EQ(in_memory_entries().size(), 1u);
  106. auto actual = in_memory_entries().begin();
  107. EXPECT_EQ(actual->first, id);
  108. EXPECT_EQ(actual->second.get()->id, id);
  109. }
  110. // Test Initializing and loading a non-empty database failed.
  111. TEST_F(TileStoreTest, InitFailedWithData) {
  112. auto test_data = TestEntries();
  113. TileGroup test_group;
  114. test::ResetTestGroup(&test_group);
  115. std::string id = test_group.id;
  116. test_data.emplace_back(std::move(test_group));
  117. Init(std::move(test_data), InitStatus::kOK);
  118. db()->LoadCallback(false);
  119. EXPECT_EQ(load_result(), false);
  120. EXPECT_TRUE(in_memory_entries().empty());
  121. }
  122. // Test adding and updating.
  123. TEST_F(TileStoreTest, AddAndUpdateDataFailed) {
  124. auto test_data = TestEntries();
  125. Init(std::move(test_data), InitStatus::kOK);
  126. db()->LoadCallback(true);
  127. EXPECT_EQ(load_result(), true);
  128. EXPECT_TRUE(in_memory_entries().empty());
  129. // Add an entry failed.
  130. TileGroup test_group;
  131. test_group.id = "test_group_id";
  132. auto test_entry_1 = std::make_unique<Tile>();
  133. test_entry_1->id = "test_entry_id_1";
  134. test_entry_1->display_text = "test_entry_test_display_text";
  135. test_group.tiles.emplace_back(std::move(test_entry_1));
  136. store()->Update(test_group.id, test_group,
  137. base::BindOnce([](bool success) { EXPECT_FALSE(success); }));
  138. db()->UpdateCallback(false);
  139. }
  140. TEST_F(TileStoreTest, AddAndUpdateDataSuccess) {
  141. auto test_data = TestEntries();
  142. Init(std::move(test_data), InitStatus::kOK);
  143. db()->LoadCallback(true);
  144. EXPECT_EQ(load_result(), true);
  145. EXPECT_TRUE(in_memory_entries().empty());
  146. // Add a group successfully.
  147. TileGroup test_group;
  148. test::ResetTestGroup(&test_group);
  149. store()->Update(test_group.id, test_group,
  150. base::BindOnce([](bool success) { EXPECT_TRUE(success); }));
  151. db()->UpdateCallback(true);
  152. auto expected = std::make_unique<KeysAndEntries>();
  153. expected->emplace(test_group.id, std::move(test_group));
  154. VerifyDataInDb(std::move(expected));
  155. }
  156. // Test deleting from db.
  157. TEST_F(TileStoreTest, DeleteSuccess) {
  158. auto test_data = TestEntries();
  159. TileGroup test_group;
  160. test::ResetTestGroup(&test_group);
  161. std::string id = test_group.id;
  162. test_data.emplace_back(std::move(test_group));
  163. Init(std::move(test_data), InitStatus::kOK);
  164. db()->LoadCallback(true);
  165. EXPECT_EQ(load_result(), true);
  166. EXPECT_EQ(in_memory_entries().size(), 1u);
  167. auto actual = in_memory_entries().begin();
  168. EXPECT_EQ(actual->first, id);
  169. EXPECT_EQ(actual->second.get()->id, id);
  170. store()->Delete(id,
  171. base::BindOnce([](bool success) { EXPECT_TRUE(success); }));
  172. db()->UpdateCallback(true);
  173. // No entry is expected in db.
  174. auto expected = std::make_unique<KeysAndEntries>();
  175. VerifyDataInDb(std::move(expected));
  176. }
  177. } // namespace
  178. } // namespace query_tiles