Skip to content

Commit

Permalink
Enable listing resources for cycle and vehicle
Browse files Browse the repository at this point in the history
Also add python tests to confirm results
  • Loading branch information
Michael O'Keefe authored and Michael O'Keefe committed Aug 7, 2024
1 parent d8f69d2 commit 7d5b7d8
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 13 deletions.
14 changes: 13 additions & 1 deletion python/fastsim/tests/test_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,22 @@

class TestListResources(unittest.TestCase):
def test_list_resources_for_cycle(self):
"check if list_resources works and yields results for Cycle"
"check if list_resources works for RustCycle"
c = cycle.Cycle.from_dict({
"cycSecs": [0.0, 1.0],
"cycMps": [0.0, 0.0]})
rc = c.to_rust()
resources = rc.list_resources()
self.assertTrue(len(resources) > 0)

def test_list_resources_for_vehicles(self):
"check if list_resources works for RustVehicle"
# NOTE: at the time of writing this test,
# there are no vehicle assets in resources.
# Therefore, we expect to get an empty vector.
# If resources are committed, this test should
# fail and we should use the following assert:
# self.assertTrue(len(resources) > 0)
rv = vehicle.Vehicle.from_vehdb(1).to_rust()
resources = rv.list_resources()
self.assertTrue(len(resources) == 0)
30 changes: 18 additions & 12 deletions rust/fastsim-core/src/resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@ pub const RESOURCES_DIR: Dir = include_dir!("$CARGO_MANIFEST_DIR/resources");

/// List the available resources in the resources directory
/// - subdir: &str, a subdirectory to choose from the resources directory
/// if it cannot be resolved, then the top level is used to list resources
/// NOTE: if you want the top level, a good way to get that is to pass "".
/// NOTE: if subdir cannot be resolved, returns an empty list
/// RETURNS: a vector of strings for resources that can be loaded
pub fn list_resources(subdir: &str) -> Vec<String> {
let resources_path = if let Some(rp) = RESOURCES_DIR.get_dir(subdir) {
rp
if subdir.is_empty() {
Vec::<String>::new()
} else if let Some(resources_path) = RESOURCES_DIR.get_dir(subdir) {
let mut file_names: Vec<String> = resources_path
.files()
.filter_map(|entry| entry.path().file_name()?.to_str().map(String::from))
.collect();
file_names.sort();
file_names
} else {
&RESOURCES_DIR
};
let mut file_names: Vec<String> = resources_path
.files()
.filter_map(|entry| entry.path().file_name()?.to_str().map(String::from))
.collect();
file_names.sort();
file_names
Vec::<String>::new()
}
}

#[cfg(test)]
Expand All @@ -31,5 +31,11 @@ mod tests {
let result = list_resources("cycles");
assert!(result.len() == 3);
assert!(result[0] == "HHDDTCruiseSmooth.csv");
// NOTE: at the time of writing this test, there is no
// vehicles subdirectory. The agreed-upon behavior in
// that case is that list_resources should return an
// empty vector of string.
let another_result = list_resources("vehicles");
assert!(another_result.len() == 0);
}
}
6 changes: 6 additions & 0 deletions rust/fastsim-core/src/vehicle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ lazy_static! {
self.clone()
}
#[pyo3(name = "list_resources")]
/// list available vehicle resources
pub fn list_resources_py(&self) -> Vec<String> {
RustVehicle::list_resources()
}
#[staticmethod]
#[pyo3(name = "mock_vehicle")]
fn mock_vehicle_py() -> Self {
Expand Down

0 comments on commit 7d5b7d8

Please sign in to comment.