Skip to content

Commit

Permalink
editoast: increase the availability of the API
Browse files Browse the repository at this point in the history
This is just good practice to make the API takes `&[]` instead of `&Vec<>`.
  • Loading branch information
woshilapin committed Dec 22, 2023
1 parent e10cafa commit a893bce
Show file tree
Hide file tree
Showing 2 changed files with 239 additions and 2 deletions.
239 changes: 238 additions & 1 deletion editoast/src/infra_cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ impl InfraCache {
}

/// Apply an operation to the infra cache
pub fn apply_operations(&mut self, operations: &Vec<CacheOperation>) -> Result<()> {
pub fn apply_operations(&mut self, operations: &[CacheOperation]) -> Result<()> {
for op_res in operations {
match op_res {
CacheOperation::Delete(obj_ref) => self.apply_delete(obj_ref)?,
Expand Down Expand Up @@ -1225,4 +1225,241 @@ pub mod tests {
})
.await;
}

mod getters {
use std::collections::HashMap;

use super::create_track_section_cache;
use crate::{
infra_cache::{
tests::{
create_buffer_stop_cache, create_catenary_cache, create_detector_cache,
create_operational_point_cache, create_route_cache, create_signal_cache,
create_speed_section_cache, create_switch_cache_point,
create_switch_type_cache,
},
InfraCache, InfraCacheEditoastError,
},
schema::{
utils::Identifier, Direction::StartToStop, ObjectType, TrackEndpoint,
Waypoint::BufferStop,
},
};

#[test]
fn track_section() {
const ID: &str = "track_section_id";

let mut infra_cache = InfraCache::default();

assert_eq!(
infra_cache.get_track_section(ID).unwrap_err(),
InfraCacheEditoastError::ObjectNotFound {
obj_type: ObjectType::TrackSection.to_string(),
obj_id: ID.to_string()
}
.into()
);
let track_section = create_track_section_cache(ID, 100.0);
infra_cache.add(track_section.clone()).unwrap();
assert_eq!(infra_cache.get_track_section(ID).unwrap(), &track_section);
}

#[test]
fn signal() {
const ID: &str = "signal_id";

let mut infra_cache = InfraCache::default();

assert_eq!(
infra_cache.get_signal(ID).unwrap_err(),
InfraCacheEditoastError::ObjectNotFound {
obj_type: ObjectType::Signal.to_string(),
obj_id: ID.to_string()
}
.into()
);
let signal = create_signal_cache(ID, "track_section_id", 0.0);
infra_cache.add(signal.clone()).unwrap();
assert_eq!(infra_cache.get_signal(ID).unwrap(), &signal);
}

#[test]
fn speed_section() {
const ID: &str = "speed_section_id";

let mut infra_cache = InfraCache::default();

assert_eq!(
infra_cache.get_speed_section(ID).unwrap_err(),
InfraCacheEditoastError::ObjectNotFound {
obj_type: ObjectType::SpeedSection.to_string(),
obj_id: ID.to_string()
}
.into()
);
let speed_section =
create_speed_section_cache(ID, vec![("track_section_id", 0.0, 100.0)]);
infra_cache.add(speed_section.clone()).unwrap();
assert_eq!(infra_cache.get_speed_section(ID).unwrap(), &speed_section);
}

#[test]
fn detector() {
const ID: &str = "detector_id";

let mut infra_cache = InfraCache::default();

assert_eq!(
infra_cache.get_detector(ID).unwrap_err(),
InfraCacheEditoastError::ObjectNotFound {
obj_type: ObjectType::Detector.to_string(),
obj_id: ID.to_string()
}
.into()
);
let detector = create_detector_cache(ID, "track_section_id", 0.0);
infra_cache.add(detector.clone()).unwrap();
assert_eq!(infra_cache.get_detector(ID).unwrap(), &detector);
}

#[test]
fn switch() {
const ID: &str = "switch_id";

let mut infra_cache = InfraCache::default();

assert_eq!(
infra_cache.get_switch(ID).unwrap_err(),
InfraCacheEditoastError::ObjectNotFound {
obj_type: ObjectType::Switch.to_string(),
obj_id: ID.to_string()
}
.into()
);
let switch = create_switch_cache_point(
ID.to_string(),
("track_section_1_id", TrackEndpoint::default()),
("track_section_2_id", TrackEndpoint::default()),
("track_section_3_id", TrackEndpoint::default()),
"switch_type_id".to_string(),
);

infra_cache.add(switch.clone()).unwrap();
assert_eq!(infra_cache.get_switch(ID).unwrap(), &switch);
}

#[test]
fn switch_type() {
const ID: &str = "switch_type_id";

let mut infra_cache = InfraCache::default();

assert_eq!(
infra_cache.get_switch_type(ID).unwrap_err(),
InfraCacheEditoastError::ObjectNotFound {
obj_type: ObjectType::SwitchType.to_string(),
obj_id: ID.to_string()
}
.into()
);
let switch_type = create_switch_type_cache(ID, vec![], HashMap::default());
infra_cache.add(switch_type.clone()).unwrap();
assert_eq!(infra_cache.get_switch_type(ID).unwrap(), &switch_type);
}

#[test]
fn buffer_stop() {
const ID: &str = "buffer_stop_id";

let mut infra_cache = InfraCache::default();

assert_eq!(
infra_cache.get_buffer_stop(ID).unwrap_err(),
InfraCacheEditoastError::ObjectNotFound {
obj_type: ObjectType::BufferStop.to_string(),
obj_id: ID.to_string()
}
.into()
);
let buffer_stop = create_buffer_stop_cache(ID, "track_section_id", 0.0);

infra_cache.add(buffer_stop.clone()).unwrap();
assert_eq!(infra_cache.get_buffer_stop(ID).unwrap(), &buffer_stop);
}

#[test]
fn route() {
const ID: &str = "route_id";

let mut infra_cache = InfraCache::default();

assert_eq!(
infra_cache.get_route(ID).unwrap_err(),
InfraCacheEditoastError::ObjectNotFound {
obj_type: ObjectType::Route.to_string(),
obj_id: ID.to_string()
}
.into()
);
let route = create_route_cache(
ID,
BufferStop {
id: Identifier::from("buffer_stop_start_id"),
},
StartToStop,
BufferStop {
id: Identifier::from("buffer_stop_end_id"),
},
vec![],
HashMap::default(),
);

infra_cache.add(route.clone()).unwrap();
assert_eq!(infra_cache.get_route(ID).unwrap(), &route);
}

#[test]
fn operational_point() {
const ID: &str = "operational_point_id";

let mut infra_cache = InfraCache::default();

assert_eq!(
infra_cache.get_operational_point(ID).unwrap_err(),
InfraCacheEditoastError::ObjectNotFound {
obj_type: ObjectType::OperationalPoint.to_string(),
obj_id: ID.to_string()
}
.into()
);
let operational_point = create_operational_point_cache(ID, "track_section_id", 0.0);

infra_cache.add(operational_point.clone()).unwrap();
assert_eq!(
infra_cache.get_operational_point(ID).unwrap(),
&operational_point
);
}

#[test]
fn catenary() {
const ID: &str = "catenary_id";

let mut infra_cache = InfraCache::default();

assert_eq!(
infra_cache.get_catenary(ID).unwrap_err(),
InfraCacheEditoastError::ObjectNotFound {
obj_type: ObjectType::Catenary.to_string(),
obj_id: ID.to_string()
}
.into()
);
let catenary = create_catenary_cache(ID, vec![]);

infra_cache.add(catenary.clone()).unwrap();
assert_eq!(infra_cache.get_catenary(ID).unwrap(), &catenary);
}
}
}
2 changes: 1 addition & 1 deletion editoast/src/views/infra/auto_fixes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ async fn fix_infra(infra_cache: &mut InfraCache) -> Result<Vec<Operation>> {
CacheOperation::Delete(delete_operation.clone().into())
}
})
.collect();
.collect::<Vec<_>>();
infra_cache
.apply_operations(&cache_operations)
.map_err(|source| AutoFixesEditoastError::FixTrialFailure { source })?;
Expand Down

0 comments on commit a893bce

Please sign in to comment.