Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions editoast/editoast_models/src/train_schedule_exception.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,33 @@ pub struct TrainScheduleException {
pub change_groups: TrainScheduleExceptionChangeGroups,
}

#[cfg(any(test, feature = "testing"))]
impl TrainScheduleException {
pub fn fixture_created(key: &str, occurrence_index: Option<i64>) -> Self {
Self {
id: 1,
disabled: false,
key: Some(key.into()),
occurrence_index,
timetable_id: 1,
train_schedule_id: 1,
change_groups: TrainScheduleExceptionChangeGroups::fixture_created(),
}
}

pub fn fixture_modified(key: &str, occurrence_index: i64) -> Self {
Self {
id: 1,
disabled: false,
key: Some(key.into()),
occurrence_index: Some(occurrence_index),
timetable_id: 1,
train_schedule_id: 1,
change_groups: TrainScheduleExceptionChangeGroups::fixture_modified(),
}
}
}

impl From<TrainScheduleException> for schemas::TrainScheduleException {
fn from(train_schedule_exception: TrainScheduleException) -> Self {
Self {
Expand Down
6 changes: 3 additions & 3 deletions editoast/schemas/src/train_schedule_exception.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ pub struct TrainScheduleExceptionChangeGroups {

#[cfg(any(test, feature = "testing"))]
impl TrainScheduleExceptionChangeGroups {
pub fn fake_created() -> Self {
pub fn fixture_created() -> Self {
use std::str::FromStr;

use chrono::DateTime;
Expand Down Expand Up @@ -165,8 +165,8 @@ impl TrainScheduleExceptionChangeGroups {
}
}

pub fn fake_modified() -> Self {
let mut created = Self::fake_created();
pub fn fixture_modified() -> Self {
let mut created = Self::fixture_created();
created.start_time = None;
created.train_name = Some(TrainNameChangeGroup {
value: "modified_exception_train_name".to_string(),
Expand Down
24 changes: 0 additions & 24 deletions editoast/src/models/fixtures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ use schemas::fixtures::simple_created_exception_with_change_groups;
use schemas::fixtures::simple_modified_exception_with_change_groups;
use schemas::infra::InfraObject;
use schemas::infra::RailJson;
use schemas::paced_train::ExceptionType;
use schemas::paced_train::Paced;
use schemas::paced_train::PacedTrainException;
use schemas::paced_train::TrainNameChangeGroup;
Expand Down Expand Up @@ -108,29 +107,6 @@ async fn link_train_schedule_set_to_timetable(
.expect("Failed to link train schedule set to timetable")
}

pub fn create_created_exception_with_change_groups(key: &str) -> PacedTrainException {
PacedTrainException {
id: None,
key: key.into(),
exception_type: ExceptionType::Created {},
disabled: false,
change_groups: TrainScheduleExceptionChangeGroups::fake_created(),
}
}

pub fn create_modified_exception_with_change_groups(
key: &str,
occurrence_index: usize,
) -> PacedTrainException {
let mut exception = create_created_exception_with_change_groups(key);
exception.exception_type = ExceptionType::Modified { occurrence_index };
exception.change_groups.start_time = None;
exception.change_groups.train_name = Some(TrainNameChangeGroup {
value: "modified_exception_train_name".to_string(),
});
exception
}

pub fn simple_paced_train_base() -> TrainSchedule {
TrainSchedule {
train_occurrence: schemas::TrainOccurrence::fake(),
Expand Down
56 changes: 27 additions & 29 deletions editoast/src/models/train_schedule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -538,8 +538,6 @@ mod tests {
use std::str::FromStr;

use crate::models::TrainSchedule;
use crate::models::fixtures::create_created_exception_with_change_groups;
use crate::models::fixtures::create_modified_exception_with_change_groups;
use crate::models::fixtures::create_timetable;
use crate::models::fixtures::simple_paced_train_changeset;
use crate::models::fixtures::simple_sub_category;
Expand All @@ -554,15 +552,14 @@ mod tests {
use pretty_assertions::assert_eq;
use rstest::rstest;
use schemas::TrainScheduleExceptionChangeGroups;
use schemas::paced_train::PacedTrainException;
use schemas::paced_train::RollingStockCategoryChangeGroup;
use schemas::paced_train::StartTimeChangeGroup;
use schemas::train_schedule::Comfort;
use schemas::train_schedule::Distribution;
use schemas::train_schedule::Margins;
use schemas::train_schedule::TrainScheduleOptions;

pub fn create_paced_train(exceptions: Vec<PacedTrainException>) -> TrainSchedule {
pub fn create_paced_train() -> TrainSchedule {
TrainSchedule {
id: 1,
train_schedule_set_id: 1,
Expand All @@ -588,23 +585,26 @@ mod tests {
time_window: chrono::Duration::try_hours(2),
interval: chrono::Duration::try_minutes(30),
sub_category: None,
exceptions,
exceptions: vec![],
}
}

#[tokio::test]
async fn paced_train_main_category_apply_exception() {
let mut exception = create_created_exception_with_change_groups("key_1");

exception.change_groups.rolling_stock_category = Some(RollingStockCategoryChangeGroup {
value: Some(schemas::rolling_stock::TrainCategory::Main {
main_category: schemas::rolling_stock::TrainMainCategory::FastFreightTrain,
let mut exception = TrainScheduleException::fixture_created("key_1", None);
exception.change_groups = TrainScheduleExceptionChangeGroups {
rolling_stock_category: Some(RollingStockCategoryChangeGroup {
value: Some(schemas::rolling_stock::TrainCategory::Main {
main_category: schemas::rolling_stock::TrainMainCategory::FastFreightTrain,
}),
}),
});
..Default::default()
};

// The paced train has HighSpeedTrain
let paced_train = create_paced_train(vec![exception.clone()]);
let paced_train_exception = paced_train.apply_exception(&exception);
let paced_train = create_paced_train();

let paced_train_exception = paced_train.apply_train_schedule_exception(&exception.into());

// Check if it get replaced by exception category
assert_eq!(
Expand All @@ -617,12 +617,13 @@ mod tests {

#[rstest]
#[tokio::test]
#[case::created(create_created_exception_with_change_groups("key_1"))]
#[case::created(TrainScheduleException::fixture_created("key_1", None))]
#[tokio::test]
#[case::modified(create_modified_exception_with_change_groups("key_2", 0))]
async fn paced_train_apply_exception(#[case] exception: PacedTrainException) {
let paced_train = create_paced_train(vec![exception.clone()]);
let paced_train_exception = paced_train.apply_exception(&exception);
#[case::modified(TrainScheduleException::fixture_created("key_2", Some(0)))]
async fn paced_train_apply_exception(#[case] exception: TrainScheduleException) {
let exception: schemas::TrainScheduleException = exception.into();
let paced_train = create_paced_train();
let paced_train_exception = paced_train.apply_train_schedule_exception(&exception);

assert_eq!(
paced_train_exception.train_name,
Expand Down Expand Up @@ -707,16 +708,13 @@ mod tests {

#[tokio::test]
async fn num_base_occurrences_without_exceptions() {
let paced_train = create_paced_train(vec![]);
let paced_train = create_paced_train();
assert_eq!(paced_train.num_base_occurrences(), 4);
}

#[tokio::test]
async fn num_base_occurrences_with_exceptions() {
let paced_train = create_paced_train(vec![
create_created_exception_with_change_groups("key_2"),
create_modified_exception_with_change_groups("key_1", 0),
]);
let paced_train = create_paced_train();
assert_eq!(paced_train.num_base_occurrences(), 4);
}

Expand All @@ -729,7 +727,7 @@ mod tests {
key: Some("key_1".into()),
occurrence_index: Some(1),
disabled: false,
change_groups: TrainScheduleExceptionChangeGroups::fake_modified(),
change_groups: TrainScheduleExceptionChangeGroups::fixture_modified(),
}
.into();

Expand All @@ -740,7 +738,7 @@ mod tests {
key: Some("key_2".into()),
occurrence_index: None,
disabled: false,
change_groups: TrainScheduleExceptionChangeGroups::fake_created(),
change_groups: TrainScheduleExceptionChangeGroups::fixture_created(),
}
.into();
let exception_3: schemas::TrainScheduleException = TrainScheduleException {
Expand All @@ -750,11 +748,11 @@ mod tests {
key: Some("key_3".into()),
occurrence_index: Some(0),
disabled: true,
change_groups: TrainScheduleExceptionChangeGroups::fake_modified(),
change_groups: TrainScheduleExceptionChangeGroups::fixture_modified(),
}
.into();

let paced_train = create_paced_train(vec![]);
let paced_train = create_paced_train();
let exceptions: Vec<schemas::TrainScheduleException> = vec![
exception_1.clone(),
exception_2.clone(),
Expand Down Expand Up @@ -806,7 +804,7 @@ mod tests {

#[tokio::test]
async fn iter_occurrences_with_modified_start_time_exception() {
let mut exception_1 = create_modified_exception_with_change_groups("key_1", 1);
let mut exception_1 = TrainScheduleException::fixture_modified("key_1", 1);
exception_1.change_groups.start_time = Some(StartTimeChangeGroup {
value: DateTime::<Utc>::from_str("2025-05-15T14:31:00+02:00").unwrap(),
});
Expand All @@ -821,7 +819,7 @@ mod tests {
}
.into();

let paced_train = create_paced_train(vec![]);
let paced_train = create_paced_train();
let occurrences: Vec<_> = paced_train
.iter_occurrences_v2(std::slice::from_ref(&exception_1))
.collect();
Expand Down
3 changes: 1 addition & 2 deletions editoast/src/views/timetable/paced_train.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1718,7 +1718,6 @@ mod tests {

use crate::error::InternalError;
use crate::models;
use crate::models::fixtures::create_created_exception_with_change_groups;
use crate::models::fixtures::create_fast_rolling_stock;
use crate::models::fixtures::create_paced_train_with_exceptions;
use crate::models::fixtures::create_simple_paced_train;
Expand Down Expand Up @@ -2066,7 +2065,7 @@ mod tests {
create_fast_rolling_stock(&mut db_pool.get_ok(), "simulation_rolling_stock").await;
let (timetable, train_schedule_set) =
create_timetable_with_train_schedule_set(&mut db_pool.get_ok()).await;
let exception = create_created_exception_with_change_groups("created_exception_key");
let exception = TrainScheduleException::fixture_created("created_exception_key", None);

let train_schedule_base = TrainSchedule {
train_occurrence: TrainOccurrence {
Expand Down
Loading