Skip to content

Commit

Permalink
updating tests so they pass
Browse files Browse the repository at this point in the history
  • Loading branch information
robinsteuteville committed Jan 23, 2024
1 parent 6086641 commit 4c4f1d9
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 38 deletions.
26 changes: 8 additions & 18 deletions rust/fastsim-core/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,13 +360,17 @@ mod tests {
use super::*;

#[test]
fn test_from_url() {
let vehicle = crate::vehicle::RustVehicle::from_url("https://raw.githubusercontent.com/NREL/fastsim-vehicles/main/public/1110_2022_Tesla_Model_Y_RWD_opt45017.yaml").unwrap();
fn test_from_cache() {
let test_path = "1110_2022_Tesla_Model_Y_RWD_opt45017_from_cache.yaml";
let comparison_vehicle =
crate::vehicle::RustVehicle::from_resource("1110_2022_Tesla_Model_Y_RWD_opt45017.yaml")
.unwrap();
println!("{}", vehicle.to_yaml().unwrap());
assert_eq!(vehicle, comparison_vehicle);
crate::vehicle::RustVehicle::to_cache(&comparison_vehicle, test_path).unwrap();
let vehicle = crate::vehicle::RustVehicle::from_cache(test_path).unwrap();
assert_eq!(comparison_vehicle, vehicle);
let full_file_path = Path::new("vehicles").join(test_path);
let path_including_directory = path_to_cache().unwrap().join(full_file_path);
std::fs::remove_file(path_including_directory).unwrap();
}

#[test]
Expand All @@ -387,18 +391,4 @@ mod tests {
assert_eq!(comparison_vehicle, vehicle_b);
std::fs::remove_file(file_path).unwrap();
}

#[test]
fn test_from_cache() {
let test_path = "1110_2022_Tesla_Model_Y_RWD_opt45017.yaml";
let comparison_vehicle =
crate::vehicle::RustVehicle::from_resource("1110_2022_Tesla_Model_Y_RWD_opt45017.yaml")
.unwrap();
crate::vehicle::RustVehicle::to_cache(&comparison_vehicle, test_path).unwrap();
let vehicle = crate::vehicle::RustVehicle::from_cache(test_path).unwrap();
assert_eq!(comparison_vehicle, vehicle);
let full_file_path = Path::new("vehicles").join(test_path);
let path_including_directory = path_to_cache().unwrap().join(full_file_path);
std::fs::remove_file(path_including_directory).unwrap();
}
}
40 changes: 20 additions & 20 deletions rust/fastsim-core/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -536,22 +536,20 @@ pub fn path_to_cache() -> anyhow::Result<PathBuf> {

/// Deletes FASTSim data directory, clearing its contents. If subpath is
/// provided, will only delete the subdirectory pointed to by the subpath,
/// rather than deleting the whole data directory.
/// rather than deleting the whole data directory. If the subpath is an empty
/// string, deletes the entire FASTSim directory.
/// USE WITH CAUTION, as this function deletes ALL objects stored in the FASTSim
/// data directory or provided subdirectory.
/// # Arguments
/// - subpath (Option): Subpath to a subdirectory within the FASTSim data
/// directory. If None, the function will deleter the whole FASTSim data
/// - subpath: Subpath to a subdirectory within the FASTSim data directory. If
/// an empty string, the function will delete the whole FASTSim data
/// directory, clearing all its contents.
/// Note: it is not possible to delete single files using this function, only
/// directories. If a single file needs deleting, the path_to_cache() function
/// can be used to find the FASTSim data directory location. The file can then
/// be found and manually deleted.
pub fn clear_cache(subpath: Option<String>) -> anyhow::Result<()> {
let path = match subpath {
Some(subpath) => path_to_cache()?.join(subpath),
None => path_to_cache()?,
};
pub fn clear_cache<P: AsRef<Path>>(subpath: P) -> anyhow::Result<()> {
let path = path_to_cache()?.join(subpath);
Ok(std::fs::remove_dir_all(path)?)
}

Expand Down Expand Up @@ -811,17 +809,19 @@ mod tests {

#[test]
fn test_clear_cache() {
// let temp_sub_dir = tempfile::TempDir::new_in(path_to_cache().unwrap()).unwrap();
// let sub_dir_path = temp_sub_dir.path().to_str().unwrap();
// url_to_cache("https://raw.githubusercontent.com/NREL/fastsim-vehicles/main/public/1110_2022_Tesla_Model_Y_RWD_opt45017.yaml", "vehicles").unwrap();
// clear_cache(Some(sub_dir_path.to_string())).unwrap();
// let still_exists = std::fs::metadata(sub_dir_path).is_ok();
// assert_eq!(still_exists, false);
// let path_to_vehicle = path_to_cache().unwrap().join("vehicles/https://raw.githubusercontent.com/NREL/fastsim-vehicles/main/public/1110_2022_Tesla_Model_Y_RWD_opt45017.yaml");
// let vehicle_still_exists = std::fs::metadata(path_to_vehicle).is_ok();
// assert_eq!(vehicle_still_exists, true);
// // delete vehicle from subdir (for first attept, use clear_cache to clear entire cache -- but get rid of this once tested)
// std::fs::remove_file(path_to_vehicle).unwrap();
// // test to see if an empty string will clear entire cache
let temp_sub_dir = tempfile::TempDir::new_in(create_project_subdir("").unwrap()).unwrap();
let sub_dir_path = temp_sub_dir.path().to_str().unwrap();
let still_exists_before = std::fs::metadata(sub_dir_path).is_ok();
assert_eq!(still_exists_before, true);
url_to_cache("https://raw.githubusercontent.com/NREL/fastsim-vehicles/main/public/1110_2022_Tesla_Model_Y_RWD_opt45017.yaml", "").unwrap();
clear_cache(sub_dir_path).unwrap();
let still_exists = std::fs::metadata(sub_dir_path).is_ok();
assert_eq!(still_exists, false);
let path_to_vehicle = path_to_cache()
.unwrap()
.join("1110_2022_Tesla_Model_Y_RWD_opt45017.yaml");
let vehicle_still_exists = std::fs::metadata(&path_to_vehicle).is_ok();
assert_eq!(vehicle_still_exists, true);
std::fs::remove_file(path_to_vehicle).unwrap();
}
}
3 changes: 3 additions & 0 deletions rust/fastsim-core/src/vehicle_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2125,6 +2125,9 @@ mod vehicle_utils_tests {
.is_empty());
}

// NOTE: this test does not seem to reliably pass. Sometimes the function
// will give a 403 error and sometimes it will succeed -- I don't think
// there's any way to ensure the function succeeds 100% of the time.
#[test]
fn test_fetch_github_list() {
let list = fetch_github_list(Some(
Expand Down

0 comments on commit 4c4f1d9

Please sign in to comment.