Skip to content

Commit

Permalink
new tests, fixed add/update issue
Browse files Browse the repository at this point in the history
  • Loading branch information
vsbuffalo committed Aug 22, 2023
1 parent ac0bedb commit 1cfe0c6
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 6 deletions.
42 changes: 39 additions & 3 deletions src/lib/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,9 @@ impl MergedFile {
impl DataFile {
pub fn new(path: String, path_context: &Path) -> Result<DataFile> {
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")),
Expand Down Expand Up @@ -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<()> {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}
};
}



}
35 changes: 32 additions & 3 deletions tests/test_project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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);
}
};

}
}
}


}

0 comments on commit 1cfe0c6

Please sign in to comment.