port data weight test to proptest

This commit is contained in:
Cyryl Płotnicki 2020-11-28 12:03:02 +00:00
parent 6629977515
commit 8ea7e3d950
2 changed files with 15 additions and 20 deletions

View file

@ -173,31 +173,26 @@ impl<'a> Repository<'a> {
mod must {
use super::Repository;
use crate::test::source::TempSource;
use anyhow::Result;
use proptest::prelude::*;
use tempfile::tempdir;
proptest! {
#[test]
fn have_size_equal_to_sum_of_sizes_of_backed_up_files() -> Result<()> {
fn have_size_equal_to_sum_of_sizes_of_backed_up_files(file_size1 in 0u64..128, file_size2 in 0u64..128) {
let source = TempSource::new().unwrap();
let repository_path = tempdir().unwrap().into_path();
Repository::init(&repository_path)?;
Repository::init(&repository_path).unwrap();
let file_size1 = 13;
let file_size2 = 27;
let mut backup_repository = Repository::open(&repository_path)?;
let mut backup_repository = Repository::open(&repository_path).unwrap();
source.write_random_bytes_to_file("file1", file_size1)?;
backup_repository.store(&source.file_path("file1"))?;
backup_repository.store(&source.file_path("file1")).unwrap();
source.write_random_bytes_to_file("file2", file_size2)?;
backup_repository.store(&source.file_path("file2"))?;
backup_repository.store(&source.file_path("file2")).unwrap();
assert_eq!((file_size1 + file_size2) as u64, backup_repository.data_weight()?);
assert_eq!(file_size1 + file_size2, backup_repository.data_weight().unwrap());
}
backup_repository.save_index()?;
assert_eq!((file_size1 + file_size2) as u64, backup_repository.data_weight()?);
Ok(())
}
}

View file

@ -25,7 +25,7 @@ impl TempSource {
self.write_bytes_to_file(filename, text.as_bytes())
}
pub fn write_random_bytes_to_file(&self, filename: &str, size: usize) -> Result<(), Error> {
pub fn write_random_bytes_to_file(&self, filename: &str, size: u64) -> Result<(), Error> {
let random_bytes: Vec<u8> = (0..size).map(|_| rand::random::<u8>()).collect();
self.write_bytes_to_file(filename, &random_bytes)?;
Ok(())