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

Unified Diff: components/sync/model_impl/model_type_store_backend_unittest.cc

Issue 2426613003: [sync] Add store version to model type store backend (Closed)
Patch Set: [sync] Add store version to model type store backend Created 4 years, 2 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
Index: components/sync/model_impl/model_type_store_backend_unittest.cc
diff --git a/components/sync/model_impl/model_type_store_backend_unittest.cc b/components/sync/model_impl/model_type_store_backend_unittest.cc
index 34f7e734247256ed34f590d0ae91c24252424c2d..a0d285fd9732865d138581b0e1bb039a191a92d1 100644
--- a/components/sync/model_impl/model_type_store_backend_unittest.cc
+++ b/components/sync/model_impl/model_type_store_backend_unittest.cc
@@ -7,10 +7,13 @@
#include <utility>
#include "base/memory/ptr_util.h"
+#include "components/sync/protocol/model_type_store_schema_descriptor.pb.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/leveldatabase/src/include/leveldb/env.h"
#include "third_party/leveldatabase/src/include/leveldb/write_batch.h"
+using sync_pb::ModelTypeStoreSchemaDescriptor;
+
namespace syncer {
class ModelTypeStoreBackendTest : public testing::Test {
@@ -49,6 +52,28 @@ class ModelTypeStoreBackendTest : public testing::Test {
std::string GetBackendPath(scoped_refptr<ModelTypeStoreBackend> backend) {
return backend->path_;
}
+
+ ModelTypeStore::Result Migrate(scoped_refptr<ModelTypeStoreBackend> backend,
+ int64_t current_version,
+ int64_t desired_version) {
+ return backend->Migrate(current_version, desired_version);
+ }
+
+ bool Migrate0To1(scoped_refptr<ModelTypeStoreBackend> backend) {
+ return backend->Migrate0To1();
+ }
+
+ int64_t GetStoreVersion(scoped_refptr<ModelTypeStoreBackend> backend) {
+ return backend->GetStoreVersion();
+ }
+
+ int64_t LatestVersion() {
+ return ModelTypeStoreBackend::kLatestSchemaVersion;
+ }
+
+ const char* SchemaId() {
+ return ModelTypeStoreBackend::kDBSchemaDescriptorRecordId;
+ }
};
// Test that after record is written to backend it can be read back even after
@@ -190,4 +215,33 @@ TEST_F(ModelTypeStoreBackendTest, TwoDifferentBackendTest) {
ASSERT_FALSE(BackendExistsForPath("/test_db2"));
}
+// Test that initializing the database migrates it to the latest schema version.
+TEST_F(ModelTypeStoreBackendTest, MigrateNoSchemaVersionToLatestVersionTest) {
+ scoped_refptr<ModelTypeStoreBackend> backend = GetOrCreateBackend();
+
+ ASSERT_EQ(LatestVersion(), GetStoreVersion(backend));
+}
+
+// Test that the 0 to 1 migration succeeds and sets the schema version to 1.
+TEST_F(ModelTypeStoreBackendTest, Migrate0To1Test) {
+ scoped_refptr<ModelTypeStoreBackend> backend = GetOrCreateBackend();
+
+ std::unique_ptr<leveldb::WriteBatch> write_batch(new leveldb::WriteBatch());
+ write_batch->Delete(SchemaId());
+ ModelTypeStore::Result result =
+ backend->WriteModifications(std::move(write_batch));
+ ASSERT_EQ(ModelTypeStore::Result::SUCCESS, result);
+
+ ASSERT_TRUE(Migrate0To1(backend));
+ ASSERT_EQ(1, GetStoreVersion(backend));
+}
+
+// Test that migration to an unknown version fails
+TEST_F(ModelTypeStoreBackendTest, MigrateWithHigherExistingVersionFails) {
+ scoped_refptr<ModelTypeStoreBackend> backend = GetOrCreateBackend();
+
+ ASSERT_EQ(Migrate(backend, LatestVersion() + 1, LatestVersion()),
+ ModelTypeStore::Result::SCHEMA_VERSION_TOO_HIGH);
+}
+
} // namespace syncer

Powered by Google App Engine
This is Rietveld 408576698