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

Unified Diff: chrome/browser/chromeos/drive/drive_resource_metadata_storage_unittest.cc

Issue 14108009: chromeos: Use WriteBatch to update DriveResourceMetadata DB (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rename method Created 7 years, 8 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: chrome/browser/chromeos/drive/drive_resource_metadata_storage_unittest.cc
diff --git a/chrome/browser/chromeos/drive/drive_resource_metadata_storage_unittest.cc b/chrome/browser/chromeos/drive/drive_resource_metadata_storage_unittest.cc
index 04090f20642925d0b4daf69a17ef7560a7f5246a..d8453e5069565dc8f20e17a1fed4290c4b777c24 100644
--- a/chrome/browser/chromeos/drive/drive_resource_metadata_storage_unittest.cc
+++ b/chrome/browser/chromeos/drive/drive_resource_metadata_storage_unittest.cc
@@ -11,6 +11,7 @@
#include "base/files/scoped_temp_dir.h"
#include "chrome/browser/chromeos/drive/drive.pb.h"
#include "testing/gtest/include/gtest/gtest.h"
+#include "third_party/leveldatabase/src/include/leveldb/db.h"
namespace drive {
@@ -48,6 +49,26 @@ class DriveResourceMetadataStorageTest : public testing::Test {
return storage_->CheckValidity();
}
+ // Puts a child entry.
+ void PutChild(const std::string& parent_resource_id,
+ const std::string& child_base_name,
+ const std::string& child_resource_id) {
+ storage_->resource_map_->Put(
+ leveldb::WriteOptions(),
+ DriveResourceMetadataStorage::GetChildEntryKey(parent_resource_id,
+ child_base_name),
+ child_resource_id);
+ }
+
+ // Removes a child entry.
+ void RemoveChild(const std::string& parent_resource_id,
+ const std::string& child_base_name) {
+ storage_->resource_map_->Delete(
+ leveldb::WriteOptions(),
+ DriveResourceMetadataStorage::GetChildEntryKey(parent_resource_id,
+ child_base_name));
+ }
+
base::ScopedTempDir temp_dir_;
scoped_ptr<DriveResourceMetadataStorage> storage_;
};
@@ -61,6 +82,9 @@ TEST_F(DriveResourceMetadataStorageTest, LargestChangestamp) {
TEST_F(DriveResourceMetadataStorageTest, PutEntry) {
const std::string key1 = "abcdefg";
const std::string key2 = "abcd";
+ const std::string key3 = "efgh";
+ const std::string name2 = "ABCD";
+ const std::string name3 = "EFGH";
DriveEntryProto entry1;
entry1.set_resource_id(key1);
@@ -69,7 +93,7 @@ TEST_F(DriveResourceMetadataStorageTest, PutEntry) {
EXPECT_FALSE(storage_->GetEntry(key1));
// Put entry1.
- storage_->PutEntry(entry1);
+ EXPECT_TRUE(storage_->PutEntry(entry1));
// key1 found.
scoped_ptr<DriveEntryProto> result;
@@ -80,8 +104,42 @@ TEST_F(DriveResourceMetadataStorageTest, PutEntry) {
// key2 not found.
EXPECT_FALSE(storage_->GetEntry(key2));
- // Remove key1.
- storage_->RemoveEntry(key1);
+ // Put entry2 as a child of entry1.
+ DriveEntryProto entry2;
+ entry2.set_parent_resource_id(key1);
+ entry2.set_resource_id(key2);
+ entry2.set_base_name(name2);
+ EXPECT_TRUE(storage_->PutEntry(entry2));
+
+ // key2 found.
+ EXPECT_TRUE(storage_->GetEntry(key2));
+ EXPECT_EQ(key2, storage_->GetChild(key1, name2));
+
+ // Put entry3 as a child of entry2.
+ DriveEntryProto entry3;
+ entry3.set_parent_resource_id(key2);
+ entry3.set_resource_id(key3);
+ entry3.set_base_name(name3);
+ EXPECT_TRUE(storage_->PutEntry(entry3));
+
+ // key3 found.
+ EXPECT_TRUE(storage_->GetEntry(key3));
+ EXPECT_EQ(key3, storage_->GetChild(key2, name3));
+
+ // Change entry3's parent to entry1.
+ entry3.set_parent_resource_id(key1);
+ EXPECT_TRUE(storage_->PutEntry(entry3));
+
+ // entry3 is a child of entry1 now.
+ EXPECT_TRUE(storage_->GetChild(key2, name3).empty());
+ EXPECT_EQ(key3, storage_->GetChild(key1, name3));
+
+ // Remove entries.
+ EXPECT_TRUE(storage_->RemoveEntry(key3));
+ EXPECT_FALSE(storage_->GetEntry(key3));
+ EXPECT_TRUE(storage_->RemoveEntry(key2));
+ EXPECT_FALSE(storage_->GetEntry(key2));
+ EXPECT_TRUE(storage_->RemoveEntry(key1));
EXPECT_FALSE(storage_->GetEntry(key1));
}
@@ -100,7 +158,7 @@ TEST_F(DriveResourceMetadataStorageTest, Iterate) {
entries.push_back(entry);
for (size_t i = 0; i < entries.size(); ++i)
- storage_->PutEntry(entries[i]);
+ EXPECT_TRUE(storage_->PutEntry(entries[i]));
// Call Iterate and check the result.
std::map<std::string, DriveEntryProto> result;
@@ -111,49 +169,6 @@ TEST_F(DriveResourceMetadataStorageTest, Iterate) {
EXPECT_EQ(1U, result.count(entries[i].resource_id()));
}
-TEST_F(DriveResourceMetadataStorageTest, PutChild) {
- const std::string parent_id1 = "abcdefg";
- const std::string parent_id2 = "abcd";
- const std::string child_name1 = "WXYZABC";
- const std::string child_name2 = "efgWXYZABC";
- const std::string child_id1 = "qwerty";
- const std::string child_id2 = "asdfgh";
-
- // No child found.
- EXPECT_TRUE(storage_->GetChild(parent_id1, child_name1).empty());
- EXPECT_TRUE(storage_->GetChild(parent_id1, child_name2).empty());
- EXPECT_TRUE(storage_->GetChild(parent_id2, child_name1).empty());
- EXPECT_TRUE(storage_->GetChild(parent_id2, child_name2).empty());
-
- // Put child1 under parent1.
- storage_->PutChild(parent_id1, child_name1, child_id1);
- EXPECT_EQ(child_id1, storage_->GetChild(parent_id1, child_name1));
- EXPECT_TRUE(storage_->GetChild(parent_id1, child_name2).empty());
- EXPECT_TRUE(storage_->GetChild(parent_id2, child_name1).empty());
- EXPECT_TRUE(storage_->GetChild(parent_id2, child_name2).empty());
-
- // Put child2 under parent1.
- storage_->PutChild(parent_id1, child_name2, child_id2);
- EXPECT_EQ(child_id1, storage_->GetChild(parent_id1, child_name1));
- EXPECT_EQ(child_id2, storage_->GetChild(parent_id1, child_name2));
- EXPECT_TRUE(storage_->GetChild(parent_id2, child_name1).empty());
- EXPECT_TRUE(storage_->GetChild(parent_id2, child_name2).empty());
-
- // Remove child1.
- storage_->RemoveChild(parent_id1, child_name1);
- EXPECT_TRUE(storage_->GetChild(parent_id1, child_name1).empty());
- EXPECT_EQ(child_id2, storage_->GetChild(parent_id1, child_name2));
- EXPECT_TRUE(storage_->GetChild(parent_id2, child_name1).empty());
- EXPECT_TRUE(storage_->GetChild(parent_id2, child_name2).empty());
-
- // Remove child2.
- storage_->RemoveChild(parent_id1, child_name2);
- EXPECT_TRUE(storage_->GetChild(parent_id1, child_name1).empty());
- EXPECT_TRUE(storage_->GetChild(parent_id1, child_name2).empty());
- EXPECT_TRUE(storage_->GetChild(parent_id2, child_name1).empty());
- EXPECT_TRUE(storage_->GetChild(parent_id2, child_name2).empty());
-}
-
TEST_F(DriveResourceMetadataStorageTest, GetChildren) {
const std::string parents_id[] = { "mercury", "venus", "mars", "jupiter",
"saturn" };
@@ -174,21 +189,30 @@ TEST_F(DriveResourceMetadataStorageTest, GetChildren) {
children_name_id[4].push_back(std::make_pair("titan", "saturn_vi"));
children_name_id[4].push_back(std::make_pair("iapetus", "saturn_vii"));
+ // Put parents.
+ for (size_t i = 0; i < arraysize(parents_id); ++i) {
+ DriveEntryProto entry;
+ entry.set_resource_id(parents_id[i]);
+ EXPECT_TRUE(storage_->PutEntry(entry));
+ }
+
// Put some data.
- for (size_t i = 0; i != children_name_id.size(); ++i) {
- for (size_t j = 0; j != children_name_id[i].size(); ++j) {
- storage_->PutChild(parents_id[i],
- children_name_id[i][j].first,
- children_name_id[i][j].second);
+ for (size_t i = 0; i < children_name_id.size(); ++i) {
+ for (size_t j = 0; j < children_name_id[i].size(); ++j) {
+ DriveEntryProto entry;
+ entry.set_parent_resource_id(parents_id[i]);
+ entry.set_base_name(children_name_id[i][j].first);
+ entry.set_resource_id(children_name_id[i][j].second);
+ EXPECT_TRUE(storage_->PutEntry(entry));
}
}
// Try to get children.
- for (size_t i = 0; i != children_name_id.size(); ++i) {
+ for (size_t i = 0; i < children_name_id.size(); ++i) {
std::vector<std::string> children;
storage_->GetChildren(parents_id[i], &children);
EXPECT_EQ(children_name_id[i].size(), children.size());
- for (size_t j = 0; j != children_name_id[i].size(); ++j) {
+ for (size_t j = 0; j < children_name_id[i].size(); ++j) {
EXPECT_EQ(1, std::count(children.begin(),
children.end(),
children_name_id[i][j].second));
@@ -209,9 +233,8 @@ TEST_F(DriveResourceMetadataStorageTest, OpenExistingDB) {
entry2.set_base_name(child_name1);
// Put some data.
- storage_->PutEntry(entry1);
- storage_->PutEntry(entry2);
- storage_->PutChild(parent_id1, child_name1, child_id1);
+ EXPECT_TRUE(storage_->PutEntry(entry1));
+ EXPECT_TRUE(storage_->PutEntry(entry2));
// Close DB and reopen.
storage_.reset(new DriveResourceMetadataStorage(temp_dir_.path()));
@@ -241,7 +264,7 @@ TEST_F(DriveResourceMetadataStorageTest, IncompatibleDB) {
// Put some data.
storage_->SetLargestChangestamp(kLargestChangestamp);
- storage_->PutEntry(entry);
+ EXPECT_TRUE(storage_->PutEntry(entry));
EXPECT_TRUE(storage_->GetEntry(key1));
// Set incompatible version and reopen DB.
@@ -279,57 +302,60 @@ TEST_F(DriveResourceMetadataStorageTest, CheckValidity) {
DriveEntryProto entry;
entry.set_resource_id(key1);
entry.set_base_name(name1);
- storage_->PutEntry(entry);
+ EXPECT_TRUE(storage_->PutEntry(entry));
EXPECT_TRUE(CheckValidity());
// Put entry with key2 under key1.
entry.set_resource_id(key2);
entry.set_parent_resource_id(key1);
entry.set_base_name(name2);
- storage_->PutEntry(entry);
+ EXPECT_TRUE(storage_->PutEntry(entry));
+ EXPECT_TRUE(CheckValidity());
+
+ RemoveChild(key1, name2);
EXPECT_FALSE(CheckValidity()); // Missing parent-child relationship.
- // Add missing parent-child relationship between key1 and key2.
- storage_->PutChild(key1, name2, key2);
+ // Add back parent-child relationship between key1 and key2.
+ PutChild(key1, name2, key2);
EXPECT_TRUE(CheckValidity());
// Add parent-child relationship between key2 and key3.
- storage_->PutChild(key2, name3, key3);
+ PutChild(key2, name3, key3);
EXPECT_FALSE(CheckValidity()); // key3 is not stored in the storage.
// Put entry with key3 under key2.
entry.set_resource_id(key3);
entry.set_parent_resource_id(key2);
entry.set_base_name(name3);
- storage_->PutEntry(entry);
+ EXPECT_TRUE(storage_->PutEntry(entry));
EXPECT_TRUE(CheckValidity());
// Parent-child relationship with wrong name.
- storage_->RemoveChild(key2, name3);
+ RemoveChild(key2, name3);
EXPECT_FALSE(CheckValidity());
- storage_->PutChild(key2, name2, key3);
+ PutChild(key2, name2, key3);
EXPECT_FALSE(CheckValidity());
// Fix up the relationship between key2 and key3.
- storage_->RemoveChild(key2, name2);
+ RemoveChild(key2, name2);
EXPECT_FALSE(CheckValidity());
- storage_->PutChild(key2, name3, key3);
+ PutChild(key2, name3, key3);
EXPECT_TRUE(CheckValidity());
// Remove key2.
- storage_->RemoveChild(key1, name2);
+ RemoveChild(key1, name2);
EXPECT_FALSE(CheckValidity());
- storage_->RemoveEntry(key2);
+ EXPECT_TRUE(storage_->RemoveEntry(key2));
EXPECT_FALSE(CheckValidity());
// Remove key3.
- storage_->RemoveChild(key2, name3);
+ RemoveChild(key2, name3);
EXPECT_FALSE(CheckValidity());
- storage_->RemoveEntry(key3);
+ EXPECT_TRUE(storage_->RemoveEntry(key3));
EXPECT_TRUE(CheckValidity());
// Remove key1.
- storage_->RemoveEntry(key1);
+ EXPECT_TRUE(storage_->RemoveEntry(key1));
EXPECT_TRUE(CheckValidity());
}

Powered by Google App Engine
This is Rietveld 408576698