Skip to content
This repository has been archived by the owner on Sep 16, 2024. It is now read-only.

#41: Make unit tests of update_one and update_all method #71

Closed
wants to merge 3 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 95 additions & 13 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,21 +374,23 @@ impl ResourceIndex {
self.forget_path(canonical_path, old_id)
}
Ok(new_entry) => {

// valid resource exists by the path
let curr_entry = &self.path2id[canonical_path];

if curr_entry.id == new_entry.id {
// in rare cases we are here due to hash collision

if curr_entry.modified == new_entry.modified {
log::warn!("path {:?} was modified but not its content", &canonical_path);
// in rare cases we are here due to hash collision
funnyVOLT marked this conversation as resolved.
Show resolved Hide resolved
if let Some(curr_entry) =
self.path2id.get(canonical_path)
{
if curr_entry.id == new_entry.id {
// in rare cases we are here due to hash collision
if curr_entry.modified == new_entry.modified {
log::warn!("path {:?} was modified but not its content", &canonical_path);
}
// the caller must have ensured that the path was
// updated
return Err(ArklibError::Collision(
"New content has the same id".into(),
));
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be worth to write something into the log in else branch.


// the caller must have ensured that the path was
// updated
return Err(ArklibError::Collision(
"New content has the same id".into(),
));
}

// new resource exists by the path
Expand Down Expand Up @@ -984,4 +986,84 @@ mod tests {
assert!(new2 > old2);
assert!(new2 > new1);
}

#[test]
fn should_correctly_update_one_resource() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test case is not correct. See test case fn should_not_update_nonexistent_path.

run_test_and_clean_up(|path| {
create_file_at(path.clone(), Some(FILE_SIZE_2), Some(FILE_NAME_2));
let mut actual = ResourceIndex::build(path.clone());

println!("==🌹🌹=update_one unit tests start==");

for (key, value) in &actual.path2id {
println!(
"===❤❤========update_one_new_entry Resource ID: {}",
&actual.path2id[key].id
);
}

create_file_at(path.clone(), Some(FILE_SIZE_1), Some(FILE_NAME_1));
let mut file_path = path.clone();
file_path.push(FILE_NAME_1);
let old_id = ResourceId {
data_size: FILE_SIZE_1,
crc32: CRC32_1,
};
let path_buf: CanonicalPathBuf =
CanonicalPathBuf::canonicalize(&file_path).unwrap();
let update = actual
.update_one(&path_buf.clone(), old_id)
.expect("Should update one resource correctly");

for (key, value) in &actual.path2id {
println!(
"===😂😂========update_after_entry Resource ID: {}",
&actual.path2id[key].id
);
}

println!(
"===✔✔==added=:{} deleted={}",update.added.len().to_string(),update.deleted.len().to_string()
);

assert_eq!(update.added.len(), 1);
assert_eq!(actual.path2id.len(), 2);
})
}

#[test]
fn should_correctly_update_all_resource() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test case is a duplicate. See fn should_update_resource_index_adding_1_additional_file_successfully.

run_test_and_clean_up(|path| {
create_file_at(path.clone(), Some(FILE_SIZE_2), Some(FILE_NAME_2));
let mut actual = ResourceIndex::build(path.clone());

println!("==🌹🌹=update_all unit tests start==");
for (key, value) in &actual.path2id {
println!(
"===❤❤========update_one_new_entry Resource ID: {}",
&actual.path2id[key].id
);
}

create_file_at(path.clone(), Some(FILE_SIZE_1), Some(FILE_NAME_1));

let update = actual
.update_all()
.expect("Should update index correctly");

for (key, value) in &actual.path2id {
println!(
"===😂😂========update_after_entry Resource ID: {}",
&actual.path2id[key].id
);
}

println!(
"===✔✔==added=:{} deleted={}",update.added.len().to_string(),update.deleted.len().to_string()
);

assert_eq!(update.deleted.len(), 0);
assert_eq!(update.added.len(), 1);
})
}
}