Skip to content

Commit 35b927e

Browse files
woshilapinyounesschrifi
authored andcommitted
editoast: move 'simulation_response' one module up
Signed-off-by: Jean SIMARD <[email protected]> Signed-off-by: Youness CHRIFI ALAOUI <[email protected]>
1 parent 3624d5e commit 35b927e

File tree

3 files changed

+82
-45
lines changed

3 files changed

+82
-45
lines changed

editoast/src/views/mod.rs

+42-1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ use crate::client::get_app_version;
7777
use crate::core::mq_client;
7878
use crate::core::pathfinding::PathfindingInputError;
7979
use crate::core::pathfinding::PathfindingNotFound;
80+
use crate::core::simulation::SimulationResponse;
8081
use crate::core::version::CoreVersionRequest;
8182
use crate::core::AsCoreRequest;
8283
use crate::core::CoreClient;
@@ -93,6 +94,7 @@ use crate::map::MapLayers;
9394
use crate::models;
9495
use crate::models::auth::PgAuthDriver;
9596
use crate::valkey_utils::ValkeyConfig;
97+
use crate::views::path::pathfinding::PathfindingFailure;
9698
use crate::ValkeyClient;
9799

98100
crate::routes! {
@@ -172,7 +174,7 @@ pub struct InfraIdQueryParam {
172174

173175
#[derive(Debug, PartialEq, Serialize, Deserialize, ToSchema)]
174176
#[serde(tag = "status", rename_all = "snake_case")]
175-
pub enum SimulationSummaryResult {
177+
enum SimulationSummaryResult {
176178
/// Minimal information on a simulation's result
177179
Success {
178180
/// Length of a path in mm
@@ -201,6 +203,45 @@ pub enum SimulationSummaryResult {
201203
PathfindingInputError(PathfindingInputError),
202204
}
203205

206+
fn simulation_response(sim: SimulationResponse) -> SimulationSummaryResult {
207+
match sim {
208+
SimulationResponse::Success {
209+
final_output,
210+
provisional,
211+
base,
212+
..
213+
} => {
214+
let report = final_output.report_train;
215+
SimulationSummaryResult::Success {
216+
length: *report.positions.last().unwrap(),
217+
time: *report.times.last().unwrap(),
218+
energy_consumption: report.energy_consumption,
219+
path_item_times_final: report.path_item_times.clone(),
220+
path_item_times_provisional: provisional.path_item_times.clone(),
221+
path_item_times_base: base.path_item_times.clone(),
222+
}
223+
}
224+
SimulationResponse::PathfindingFailed { pathfinding_failed } => match pathfinding_failed {
225+
PathfindingFailure::InternalError { core_error } => {
226+
SimulationSummaryResult::PathfindingFailure { core_error }
227+
}
228+
229+
PathfindingFailure::PathfindingInputError(input_error) => {
230+
SimulationSummaryResult::PathfindingInputError(input_error)
231+
}
232+
233+
PathfindingFailure::PathfindingNotFound(not_found) => {
234+
SimulationSummaryResult::PathfindingNotFound(not_found)
235+
}
236+
},
237+
SimulationResponse::SimulationFailed { core_error } => {
238+
SimulationSummaryResult::SimulationFailed {
239+
error_type: core_error.get_type().into(),
240+
}
241+
}
242+
}
243+
}
244+
204245
/// Represents the bundle of information about the issuer of a request
205246
/// that can be extracted form recognized headers.
206247
#[derive(Debug, Clone)]

editoast/src/views/paced_train.rs

+39-4
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use utoipa::ToSchema;
2727

2828
use super::path::pathfinding::PathfindingResult;
2929
use super::projection::ProjectPathForm;
30-
use super::train_schedule::simulation_response;
30+
use super::simulation_response;
3131
use super::train_schedule::train_simulation_batch;
3232
use super::AppState;
3333
use super::AuthenticationExt;
@@ -560,15 +560,15 @@ mod tests {
560560
},
561561
provisional: ReportTrain {
562562
positions: vec![],
563-
times: vec![0, 10],
563+
times: vec![],
564564
speeds: vec![],
565565
energy_consumption: 0.0,
566566
path_item_times: vec![0, 1000, 2000, 3000]
567567
},
568568
final_output: CompleteReportTrain {
569569
report_train: ReportTrain {
570-
positions: vec![],
571-
times: vec![],
570+
positions: vec![0],
571+
times: vec![0],
572572
speeds: vec![],
573573
energy_consumption: 0.0,
574574
path_item_times: vec![0, 1000, 2000, 3000]
@@ -590,6 +590,26 @@ mod tests {
590590
);
591591
}
592592

593+
#[rstest]
594+
async fn paced_train_simulation_not_found() {
595+
let (app, infra_id, _paced_train_id) =
596+
app_infra_id_paced_train_id_for_simulation_tests().await;
597+
let request = app.get(
598+
format!(
599+
"/paced_train/{}/simulation/?infra_id={}",
600+
0, infra_id
601+
)
602+
.as_str(),
603+
);
604+
605+
let response: InternalError = app
606+
.fetch(request)
607+
.assert_status(StatusCode::NOT_FOUND)
608+
.json_into();
609+
610+
assert_eq!(&response.error_type, "editoast:paced_train:NotFound")
611+
}
612+
593613
#[rstest]
594614
async fn paced_train_simulation_summary() {
595615
let (app, infra_id, paced_train_id) =
@@ -613,4 +633,19 @@ mod tests {
613633
}
614634
);
615635
}
636+
637+
async fn paced_train_simulation_summary_not_found() {
638+
let (app, infra_id, _paced_train_id) =
639+
app_infra_id_paced_train_id_for_simulation_tests().await;
640+
let request = app.post("/paced_train/simulation_summary").json(&json!({
641+
"infra_id": infra_id,
642+
"ids": vec![0],
643+
}));
644+
let response: InternalError = app
645+
.fetch(request)
646+
.assert_status(StatusCode::NOT_FOUND)
647+
.json_into();
648+
649+
assert_eq!(&response.error_type, "editoast:paced_train:NotFound")
650+
}
616651
}

editoast/src/views/train_schedule.rs

+1-40
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ use crate::models::prelude::*;
4949
use crate::models::train_schedule::TrainSchedule;
5050
use crate::models::train_schedule::TrainScheduleChangeset;
5151
use crate::views::path::pathfinding::pathfinding_from_train;
52-
use crate::views::path::pathfinding::PathfindingFailure;
5352
use crate::views::path::pathfinding::PathfindingResult;
5453
use crate::views::path::pathfinding_from_train_batch;
5554
use crate::views::path::projection::PathProjection;
@@ -70,6 +69,7 @@ use crate::RollingStockModel;
7069
use crate::ValkeyClient;
7170

7271
use super::projection::ProjectPathForm;
72+
use super::simulation_response;
7373
use super::SimulationSummaryResult;
7474

7575
crate::routes! {
@@ -636,45 +636,6 @@ struct SimulationBatchForm {
636636
ids: HashSet<i64>,
637637
}
638638

639-
pub fn simulation_response(sim: SimulationResponse) -> SimulationSummaryResult {
640-
match sim {
641-
SimulationResponse::Success {
642-
final_output,
643-
provisional,
644-
base,
645-
..
646-
} => {
647-
let report = final_output.report_train;
648-
SimulationSummaryResult::Success {
649-
length: *report.positions.last().unwrap(),
650-
time: *report.times.last().unwrap(),
651-
energy_consumption: report.energy_consumption,
652-
path_item_times_final: report.path_item_times.clone(),
653-
path_item_times_provisional: provisional.path_item_times.clone(),
654-
path_item_times_base: base.path_item_times.clone(),
655-
}
656-
}
657-
SimulationResponse::PathfindingFailed { pathfinding_failed } => match pathfinding_failed {
658-
PathfindingFailure::InternalError { core_error } => {
659-
SimulationSummaryResult::PathfindingFailure { core_error }
660-
}
661-
662-
PathfindingFailure::PathfindingInputError(input_error) => {
663-
SimulationSummaryResult::PathfindingInputError(input_error)
664-
}
665-
666-
PathfindingFailure::PathfindingNotFound(not_found) => {
667-
SimulationSummaryResult::PathfindingNotFound(not_found)
668-
}
669-
},
670-
SimulationResponse::SimulationFailed { core_error } => {
671-
SimulationSummaryResult::SimulationFailed {
672-
error_type: core_error.get_type().into(),
673-
}
674-
}
675-
}
676-
}
677-
678639
/// Associate each train id with its simulation summary response
679640
/// If the simulation fails, it associates the reason: pathfinding failed or running time failed
680641
#[utoipa::path(

0 commit comments

Comments
 (0)