From 1cfe0c6d29fc0aac1d667bae369518356c357ff4 Mon Sep 17 00:00:00 2001 From: Vince Buffalo Date: Mon, 21 Aug 2023 21:42:14 -0700 Subject: [PATCH] new tests, fixed add/update issue --- src/lib/data.rs | 42 +++++++++++++++++++++++++++++++++++++++--- tests/test_project.rs | 35 ++++++++++++++++++++++++++++++++--- 2 files changed, 71 insertions(+), 6 deletions(-) diff --git a/src/lib/data.rs b/src/lib/data.rs index 9a3dba4..43feeda 100644 --- a/src/lib/data.rs +++ b/src/lib/data.rs @@ -318,6 +318,9 @@ impl MergedFile { impl DataFile { pub fn new(path: String, path_context: &Path) -> Result { let full_path = path_context.join(&path); + if !full_path.exists() { + return Err(anyhow!("File '{}' does not exist.", path)) + } let md5 = match compute_md5(&full_path)? { Some(md5) => md5, None => return Err(anyhow!("Could not compute MD5 as file does not exist")), @@ -482,8 +485,15 @@ impl DataCollection { } pub fn register(&mut self, data_file: DataFile) -> Result<()> { - self.files.insert(data_file.path.clone(), data_file); - Ok(()) + let path = data_file.path.clone(); + if self.files.contains_key(&path) { + Err(anyhow!("File '{}' is already registered in the data manifest. \ + If you wish to update the MD5 or metadata, use: sdf update FILE", + &path)) + } else { + self.files.insert(path, data_file); + Ok(()) + } } pub fn update(&mut self, filename: Option<&String>, path_context: &Path) -> Result<()> { @@ -642,7 +652,7 @@ impl DataCollection { } } - pb.finish_with_message("Fetching completed!"); + pb.finish_with_message("Fetching completed."); Ok(all_remote_files) } // Merge all local and remote files. @@ -984,3 +994,29 @@ impl DataCollection { } } + + +#[cfg(test)] +mod tests { + use super::DataFile; + use std::path::Path; + use anyhow::{anyhow,Result}; + + #[tokio::test] + async fn test_datafile_new_with_nonexistent_path() { + let nonexistent_path = "some/nonexistent/path".to_string(); + let path_context = Path::new(""); + + let result = DataFile::new(nonexistent_path, &path_context); + match result { + Ok(_) => assert!(false, "Expected an error, but got Ok"), + Err(err) => { + assert!(err.to_string().contains("does not exist"), + "Unexpected error: {:?}", err); + } + }; + } + + + +} diff --git a/tests/test_project.rs b/tests/test_project.rs index fd2d524..b9d4fbc 100644 --- a/tests/test_project.rs +++ b/tests/test_project.rs @@ -13,7 +13,7 @@ mod tests { use super::setup; use super::get_statuses; use super::generate_random_tsv; - use std::path::PathBuf; + use std::path::{Path,PathBuf}; use scidataflow::lib::data::LocalStatusCode; #[test] @@ -96,15 +96,44 @@ mod tests { let updated_status = updated_status_option.unwrap().clone(); assert_eq!(updated_status, LocalStatusCode::Modified); - // Now, let's re-add the file and make sure the status goes back to current. + // Now, let's update these files let re_add_files = vec![file_to_check.to_string_lossy().to_string()]; - let _ = fixture.project.add(&re_add_files); + for file in &re_add_files { + let result = fixture.project.update(Some(&file)); + assert!(result.is_ok(), "re-adding raised Error!"); + } + + // and make sure the status goes back to current. let readd_statuses = get_statuses_map(&mut fixture, &path_context).await; let readd_status_option = readd_statuses.get(&file_to_check).unwrap().local_status.clone(); let readd_status = readd_status_option.unwrap().clone(); assert_eq!(readd_status, LocalStatusCode::Current); } + #[tokio::test] + async fn test_add_already_added_error() { + let mut fixture = setup(true); + + if let Some(files) = &fixture.env.files { + for file in files { + let mut file_list = Vec::new(); + file_list.push(file.path.clone()); + let result = fixture.project.add(&file_list); + + // check that we get + match result { + Ok(_) => assert!(false, "Expected an error, but got Ok"), + Err(err) => { + assert!(err.to_string().contains("already registered"), + "Unexpected error: {:?}", err); + } + }; + + } + } + } + + }