proto_table_manager_unittest.cc 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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/sqlite_proto/proto_table_manager.h"
  5. #include <vector>
  6. #include "base/memory/scoped_refptr.h"
  7. #include "base/strings/strcat.h"
  8. #include "base/test/task_environment.h"
  9. #include "base/threading/thread_task_runner_handle.h"
  10. #include "components/sqlite_proto/key_value_data.h"
  11. #include "components/sqlite_proto/key_value_table.h"
  12. #include "components/sqlite_proto/test_proto.pb.h"
  13. #include "sql/database.h"
  14. #include "sql/meta_table.h"
  15. #include "testing/gmock/include/gmock/gmock.h"
  16. #include "testing/gtest/include/gtest/gtest.h"
  17. namespace sqlite_proto {
  18. namespace {
  19. MATCHER_P(EqualsProto,
  20. message,
  21. "Match a proto Message equal to the matcher's argument.") {
  22. std::string expected_serialized, actual_serialized;
  23. message.SerializeToString(&expected_serialized);
  24. arg.SerializeToString(&actual_serialized);
  25. return expected_serialized == actual_serialized;
  26. }
  27. constexpr char kTableName[] = "my_table";
  28. } // namespace
  29. TEST(ProtoTableTest, PutReinitializeAndGet) {
  30. // In order to test ProtoTableManager is correctly
  31. // initializing the underlying database's tables:
  32. // - create a database and a ProtoTableManager on it;
  33. // - store some data; and
  34. // - construct a new ProtoTableManager to read from the
  35. // existing database state.
  36. base::test::TaskEnvironment env;
  37. sql::Database db;
  38. CHECK(db.OpenInMemory());
  39. auto manager = base::MakeRefCounted<ProtoTableManager>(
  40. base::ThreadTaskRunnerHandle::Get());
  41. manager->InitializeOnDbSequence(&db, std::vector<std::string>{kTableName},
  42. /*schema_version=*/1);
  43. KeyValueTable<TestProto> table(kTableName);
  44. TestProto first_entry, second_entry;
  45. first_entry.set_value(1);
  46. second_entry.set_value(1);
  47. {
  48. KeyValueData<TestProto> data(manager, &table,
  49. /*max_num_entries=*/absl::nullopt,
  50. /*flush_delay=*/base::TimeDelta());
  51. // In these tests, we're using the current thread as the DB sequence.
  52. data.InitializeOnDBSequence();
  53. data.UpdateData("a", first_entry);
  54. data.UpdateData("b", second_entry);
  55. env.RunUntilIdle();
  56. }
  57. manager = base::MakeRefCounted<ProtoTableManager>(
  58. base::ThreadTaskRunnerHandle::Get());
  59. manager->InitializeOnDbSequence(&db, std::vector<std::string>{kTableName},
  60. /*schema_version=*/1);
  61. {
  62. KeyValueData<TestProto> data(manager, &table,
  63. /*max_num_entries=*/absl::nullopt,
  64. /*flush_delay=*/base::TimeDelta());
  65. data.InitializeOnDBSequence();
  66. TestProto result;
  67. ASSERT_TRUE(data.TryGetData("a", &result));
  68. EXPECT_THAT(result, EqualsProto(first_entry));
  69. ASSERT_TRUE(data.TryGetData("b", &result));
  70. EXPECT_THAT(result, EqualsProto(second_entry));
  71. }
  72. }
  73. TEST(ProtoTableTest, ReinitializingWithDifferentVersionClearsTables) {
  74. // In order to test ProtoTableManager is correctly
  75. // initializing the underlying database's tables:
  76. // - create a database and a ProtoTableManager on it;
  77. // - store some data; and
  78. // - construct a new ProtoTableManager to read from the
  79. // existing database state.
  80. base::test::TaskEnvironment env;
  81. sql::Database db;
  82. CHECK(db.OpenInMemory());
  83. constexpr int kInitialVersion = 1;
  84. auto manager = base::MakeRefCounted<ProtoTableManager>(
  85. base::ThreadTaskRunnerHandle::Get());
  86. manager->InitializeOnDbSequence(&db, std::vector<std::string>{kTableName},
  87. /*schema_version=*/kInitialVersion);
  88. KeyValueTable<TestProto> table(kTableName);
  89. TestProto first_entry, second_entry;
  90. first_entry.set_value(1);
  91. second_entry.set_value(1);
  92. {
  93. KeyValueData<TestProto> data(manager, &table,
  94. /*max_num_entries=*/absl::nullopt,
  95. /*flush_delay=*/base::TimeDelta());
  96. // In these tests, we're using the current thread as the DB sequence.
  97. data.InitializeOnDBSequence();
  98. data.UpdateData("a", first_entry);
  99. data.UpdateData("b", second_entry);
  100. env.RunUntilIdle();
  101. }
  102. manager = base::MakeRefCounted<ProtoTableManager>(
  103. base::ThreadTaskRunnerHandle::Get());
  104. manager->InitializeOnDbSequence(&db, std::vector<std::string>{kTableName},
  105. /*schema_version=*/kInitialVersion + 1);
  106. {
  107. KeyValueData<TestProto> data(manager, &table,
  108. /*max_num_entries=*/absl::nullopt,
  109. /*flush_delay=*/base::TimeDelta());
  110. data.InitializeOnDBSequence();
  111. TestProto result;
  112. EXPECT_FALSE(data.TryGetData("a", &result));
  113. EXPECT_FALSE(data.TryGetData("b", &result));
  114. }
  115. }
  116. TEST(ProtoTableTest, InitializingWithoutWrittenVersionClearsTables) {
  117. // Check that, when reinitializing the database when the most recent write
  118. // occurred before the database started keeping track of versions,
  119. // ProtoTableManager correctly clears the database.
  120. base::test::TaskEnvironment env;
  121. sql::Database db;
  122. CHECK(db.OpenInMemory());
  123. constexpr int kInitialVersion = 1;
  124. auto manager = base::MakeRefCounted<ProtoTableManager>(
  125. base::ThreadTaskRunnerHandle::Get());
  126. manager->InitializeOnDbSequence(&db, std::vector<std::string>{kTableName},
  127. /*schema_version=*/kInitialVersion);
  128. KeyValueTable<TestProto> table(kTableName);
  129. TestProto first_entry, second_entry;
  130. first_entry.set_value(1);
  131. second_entry.set_value(1);
  132. {
  133. KeyValueData<TestProto> data(manager, &table,
  134. /*max_num_entries=*/absl::nullopt,
  135. /*flush_delay=*/base::TimeDelta());
  136. // In these tests, we're using the current thread as the DB sequence.
  137. data.InitializeOnDBSequence();
  138. data.UpdateData("a", first_entry);
  139. data.UpdateData("b", second_entry);
  140. env.RunUntilIdle();
  141. ASSERT_TRUE(sql::MetaTable::DeleteTableForTesting(&db));
  142. env.RunUntilIdle();
  143. }
  144. manager = base::MakeRefCounted<ProtoTableManager>(
  145. base::ThreadTaskRunnerHandle::Get());
  146. manager->InitializeOnDbSequence(&db, std::vector<std::string>{kTableName},
  147. /*schema_version=*/kInitialVersion);
  148. {
  149. KeyValueData<TestProto> data(manager, &table,
  150. /*max_num_entries=*/absl::nullopt,
  151. /*flush_delay=*/base::TimeDelta());
  152. data.InitializeOnDBSequence();
  153. TestProto result;
  154. EXPECT_FALSE(data.TryGetData("a", &result));
  155. EXPECT_FALSE(data.TryGetData("b", &result));
  156. }
  157. }
  158. TEST(ProtoTableTest, LoadingUnexpectedlyLargeVersionClearsTables) {
  159. // Check that, when reinitializing the database and the most recent write
  160. // occurred against a greater version of the database, ProtoTableManager
  161. // correctly clears the database.
  162. base::test::TaskEnvironment env;
  163. sql::Database db;
  164. CHECK(db.OpenInMemory());
  165. constexpr int kInitialVersion = 1;
  166. auto manager = base::MakeRefCounted<ProtoTableManager>(
  167. base::ThreadTaskRunnerHandle::Get());
  168. manager->InitializeOnDbSequence(&db, std::vector<std::string>{kTableName},
  169. /*schema_version=*/kInitialVersion);
  170. KeyValueTable<TestProto> table(kTableName);
  171. TestProto first_entry, second_entry;
  172. first_entry.set_value(1);
  173. second_entry.set_value(1);
  174. {
  175. KeyValueData<TestProto> data(manager, &table,
  176. /*max_num_entries=*/absl::nullopt,
  177. /*flush_delay=*/base::TimeDelta());
  178. // In these tests, we're using the current thread as the DB sequence.
  179. data.InitializeOnDBSequence();
  180. data.UpdateData("a", first_entry);
  181. data.UpdateData("b", second_entry);
  182. env.RunUntilIdle();
  183. // Overwrite the stored version. It's safe to use an instance of
  184. // sql::MetaTable here because all sql::MetaTable instances use the same
  185. // database name ("meta") to manipulate versions.
  186. //
  187. // MetaTable::Init only writes a version if there was no version previously
  188. // written, so it doesn't matter what values the final two arguments have.
  189. // The SetVersionNumber is what actually overwrites the version.
  190. sql::MetaTable meta_helper;
  191. ASSERT_TRUE(meta_helper.Init(&db, 1, 1));
  192. meta_helper.SetVersionNumber(kInitialVersion + 1);
  193. meta_helper.SetCompatibleVersionNumber(kInitialVersion + 1);
  194. env.RunUntilIdle();
  195. }
  196. manager = base::MakeRefCounted<ProtoTableManager>(
  197. base::ThreadTaskRunnerHandle::Get());
  198. manager->InitializeOnDbSequence(&db, std::vector<std::string>{kTableName},
  199. /*schema_version=*/kInitialVersion);
  200. {
  201. KeyValueData<TestProto> data(manager, &table,
  202. /*max_num_entries=*/absl::nullopt,
  203. /*flush_delay=*/base::TimeDelta());
  204. data.InitializeOnDBSequence();
  205. TestProto result;
  206. EXPECT_FALSE(data.TryGetData("a", &result));
  207. EXPECT_FALSE(data.TryGetData("b", &result));
  208. }
  209. }
  210. } // namespace sqlite_proto