Versions should increase

This commit is contained in:
Cyryl Płotnicki 2019-09-23 16:30:40 +01:00
parent fce433553f
commit 2fac1b2d77
3 changed files with 33 additions and 9 deletions

View file

@ -99,7 +99,6 @@ impl Index {
) )
}; };
println!("remember {:?}", item);
self.items_by_file_id.insert(item.id.clone(), item.clone()); self.items_by_file_id.insert(item.id.clone(), item.clone());
self.newest_items_by_source_path self.newest_items_by_source_path
.insert(original_source_path.to_string_lossy().to_string(), item.clone()); .insert(original_source_path.to_string_lossy().to_string(), item.clone());

View file

@ -7,6 +7,7 @@ use walkdir::WalkDir;
use crate::error::BakareError; use crate::error::BakareError;
use crate::repository::{ItemId, Repository}; use crate::repository::{ItemId, Repository};
use crate::repository_item::RepositoryItem;
use crate::source::TempSource; use crate::source::TempSource;
use crate::{backup, restore}; use crate::{backup, restore};
@ -62,15 +63,14 @@ pub fn assert_restored_from_version_has_contents(
assert_target_file_contents(&restored_file_path, old_contents) assert_target_file_contents(&restored_file_path, old_contents)
} }
pub fn item_id(repository_path: &Path, source_file_full_path: &Path) -> Result<ItemId, BakareError> { pub fn newest_item(repository_path: &Path, source_file_full_path: &Path) -> Result<RepositoryItem, BakareError> {
let id = { let item = {
let reading_repository = Repository::open(repository_path)?; let reading_repository = Repository::open(repository_path)?;
let item = reading_repository.newest_item_by_source_path(&source_file_full_path)?; let item = reading_repository.newest_item_by_source_path(&source_file_full_path)?;
assert!(item.is_some()); assert!(item.is_some());
let item = item.unwrap(); item.unwrap()
item.id().clone()
}; };
Ok(id) Ok(item)
} }
pub fn restore_all_from_reloaded_repository(repository_path: &Path, restore_target: &Path) -> Result<(), BakareError> { pub fn restore_all_from_reloaded_repository(repository_path: &Path, restore_target: &Path) -> Result<(), BakareError> {

View file

@ -47,12 +47,37 @@ fn restore_older_version_of_file() -> Result<(), BakareError> {
backup_file_with_contents(&source, &repository_path, source_file_relative_path, old_contents)?; backup_file_with_contents(&source, &repository_path, source_file_relative_path, old_contents)?;
let old_version = item_id(&repository_path, &source_file_full_path)?; let old_item = newest_item(&repository_path, &source_file_full_path)?;
let old_id = old_item.id();
let new_contents = "totally new contents"; let new_contents = "totally new contents";
backup_file_with_contents(&source, &repository_path, source_file_relative_path, new_contents)?; backup_file_with_contents(&source, &repository_path, source_file_relative_path, new_contents)?;
assert_restored_from_version_has_contents(&repository_path, &source_file_full_path, old_contents, &old_version) assert_restored_from_version_has_contents(&repository_path, &source_file_full_path, old_contents, &old_id)
}
#[test]
fn newer_version_should_be_greater_than_earlier_version() -> Result<(), BakareError> {
let source = TempSource::new()?;
let repository_path = tempdir()?.into_path();
Repository::init(repository_path.as_path())?;
let source_file_relative_path = "some path";
let source_file_full_path = source.file_path(source_file_relative_path);
backup_file_with_contents(&source, &repository_path, source_file_relative_path, "old")?;
let old_item = newest_item(&repository_path, &source_file_full_path)?;
let old_version = old_item.version();
backup_file_with_contents(&source, &repository_path, source_file_relative_path, "new")?;
let new_item = newest_item(&repository_path, &source_file_full_path)?;
let new_version = new_item.version();
assert!(new_version > old_version);
Ok(())
} }
#[test] #[test]
@ -84,8 +109,8 @@ fn forbid_backup_of_paths_within_repository() -> Result<(), BakareError> {
Ok(()) Ok(())
} }
// TODO: test concurrent writes
// TODO: deduplicate data // TODO: deduplicate data
// TODO: test that index is stored separately from data // TODO: test that index is stored separately from data
// TODO: index corruption // TODO: index corruption
// TODO: newer version should be greater than older version // TODO: newer version should be greater than older version
// TODO: split version into file id and version