Skip to content

Commit a60e00c

Browse files
committed
editoast: refactor train schedule exceptions fixtures
1 parent 302e525 commit a60e00c

File tree

5 files changed

+55
-55
lines changed

5 files changed

+55
-55
lines changed

editoast/editoast_models/src/train_schedule_exception.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,33 @@ pub struct TrainScheduleException {
2525
pub change_groups: TrainScheduleExceptionChangeGroups,
2626
}
2727

28+
#[cfg(any(test, feature = "testing"))]
29+
impl TrainScheduleException {
30+
pub fn fake(key: &str, occurrence_index: Option<i64>) -> Self {
31+
Self {
32+
id: 1,
33+
disabled: false,
34+
key: Some(key.into()),
35+
occurrence_index,
36+
timetable_id: 1,
37+
train_schedule_id: 1,
38+
change_groups: TrainScheduleExceptionChangeGroups::fake(),
39+
}
40+
}
41+
42+
pub fn fake_modified(key: &str, occurrence_index: i64) -> Self {
43+
Self {
44+
id: 1,
45+
disabled: false,
46+
key: Some(key.into()),
47+
occurrence_index: Some(occurrence_index),
48+
timetable_id: 1,
49+
train_schedule_id: 1,
50+
change_groups: TrainScheduleExceptionChangeGroups::fake_modified(),
51+
}
52+
}
53+
}
54+
2855
impl From<TrainScheduleException> for schemas::TrainScheduleException {
2956
fn from(train_schedule_exception: TrainScheduleException) -> Self {
3057
Self {

editoast/schemas/src/train_schedule_exception.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ pub struct TrainScheduleExceptionChangeGroups {
5858

5959
#[cfg(any(test, feature = "testing"))]
6060
impl TrainScheduleExceptionChangeGroups {
61-
pub fn fake_created() -> Self {
61+
pub fn fake() -> Self {
6262
use std::str::FromStr;
6363

6464
use chrono::DateTime;
@@ -166,7 +166,7 @@ impl TrainScheduleExceptionChangeGroups {
166166
}
167167

168168
pub fn fake_modified() -> Self {
169-
let mut created = Self::fake_created();
169+
let mut created = Self::fake();
170170
created.start_time = None;
171171
created.train_name = Some(TrainNameChangeGroup {
172172
value: "modified_exception_train_name".to_string(),

editoast/src/models/fixtures.rs

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ use schemas::fixtures::simple_created_exception_with_change_groups;
2222
use schemas::fixtures::simple_modified_exception_with_change_groups;
2323
use schemas::infra::InfraObject;
2424
use schemas::infra::RailJson;
25-
use schemas::paced_train::ExceptionType;
2625
use schemas::paced_train::Paced;
2726
use schemas::paced_train::PacedTrainException;
2827
use schemas::paced_train::TrainNameChangeGroup;
@@ -108,29 +107,6 @@ async fn link_train_schedule_set_to_timetable(
108107
.expect("Failed to link train schedule set to timetable")
109108
}
110109

111-
pub fn create_created_exception_with_change_groups(key: &str) -> PacedTrainException {
112-
PacedTrainException {
113-
id: None,
114-
key: key.into(),
115-
exception_type: ExceptionType::Created {},
116-
disabled: false,
117-
change_groups: TrainScheduleExceptionChangeGroups::fake_created(),
118-
}
119-
}
120-
121-
pub fn create_modified_exception_with_change_groups(
122-
key: &str,
123-
occurrence_index: usize,
124-
) -> PacedTrainException {
125-
let mut exception = create_created_exception_with_change_groups(key);
126-
exception.exception_type = ExceptionType::Modified { occurrence_index };
127-
exception.change_groups.start_time = None;
128-
exception.change_groups.train_name = Some(TrainNameChangeGroup {
129-
value: "modified_exception_train_name".to_string(),
130-
});
131-
exception
132-
}
133-
134110
pub fn simple_paced_train_base() -> TrainSchedule {
135111
TrainSchedule {
136112
train_occurrence: schemas::TrainOccurrence::fake(),

editoast/src/models/train_schedule.rs

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -538,8 +538,6 @@ mod tests {
538538
use std::str::FromStr;
539539

540540
use crate::models::TrainSchedule;
541-
use crate::models::fixtures::create_created_exception_with_change_groups;
542-
use crate::models::fixtures::create_modified_exception_with_change_groups;
543541
use crate::models::fixtures::create_timetable;
544542
use crate::models::fixtures::simple_paced_train_changeset;
545543
use crate::models::fixtures::simple_sub_category;
@@ -554,15 +552,14 @@ mod tests {
554552
use pretty_assertions::assert_eq;
555553
use rstest::rstest;
556554
use schemas::TrainScheduleExceptionChangeGroups;
557-
use schemas::paced_train::PacedTrainException;
558555
use schemas::paced_train::RollingStockCategoryChangeGroup;
559556
use schemas::paced_train::StartTimeChangeGroup;
560557
use schemas::train_schedule::Comfort;
561558
use schemas::train_schedule::Distribution;
562559
use schemas::train_schedule::Margins;
563560
use schemas::train_schedule::TrainScheduleOptions;
564561

565-
pub fn create_paced_train(exceptions: Vec<PacedTrainException>) -> TrainSchedule {
562+
pub fn create_paced_train() -> TrainSchedule {
566563
TrainSchedule {
567564
id: 1,
568565
train_schedule_set_id: 1,
@@ -588,23 +585,26 @@ mod tests {
588585
time_window: chrono::Duration::try_hours(2),
589586
interval: chrono::Duration::try_minutes(30),
590587
sub_category: None,
591-
exceptions,
588+
exceptions: vec![],
592589
}
593590
}
594591

595592
#[tokio::test]
596593
async fn paced_train_main_category_apply_exception() {
597-
let mut exception = create_created_exception_with_change_groups("key_1");
598-
599-
exception.change_groups.rolling_stock_category = Some(RollingStockCategoryChangeGroup {
600-
value: Some(schemas::rolling_stock::TrainCategory::Main {
601-
main_category: schemas::rolling_stock::TrainMainCategory::FastFreightTrain,
594+
let mut exception = TrainScheduleException::fake("key_1", None);
595+
exception.change_groups = TrainScheduleExceptionChangeGroups {
596+
rolling_stock_category: Some(RollingStockCategoryChangeGroup {
597+
value: Some(schemas::rolling_stock::TrainCategory::Main {
598+
main_category: schemas::rolling_stock::TrainMainCategory::FastFreightTrain,
599+
}),
602600
}),
603-
});
601+
..Default::default()
602+
};
604603

605604
// The paced train has HighSpeedTrain
606-
let paced_train = create_paced_train(vec![exception.clone()]);
607-
let paced_train_exception = paced_train.apply_exception(&exception);
605+
let paced_train = create_paced_train();
606+
607+
let paced_train_exception = paced_train.apply_train_schedule_exception(&exception.into());
608608

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

618618
#[rstest]
619619
#[tokio::test]
620-
#[case::created(create_created_exception_with_change_groups("key_1"))]
620+
#[case::created(TrainScheduleException::fake("key_1", None))]
621621
#[tokio::test]
622-
#[case::modified(create_modified_exception_with_change_groups("key_2", 0))]
623-
async fn paced_train_apply_exception(#[case] exception: PacedTrainException) {
624-
let paced_train = create_paced_train(vec![exception.clone()]);
625-
let paced_train_exception = paced_train.apply_exception(&exception);
622+
#[case::modified(TrainScheduleException::fake("key_2", Some(0)))]
623+
async fn paced_train_apply_exception(#[case] exception: TrainScheduleException) {
624+
let exception: schemas::TrainScheduleException = exception.into();
625+
let paced_train = create_paced_train();
626+
let paced_train_exception = paced_train.apply_train_schedule_exception(&exception);
626627

627628
assert_eq!(
628629
paced_train_exception.train_name,
@@ -707,16 +708,13 @@ mod tests {
707708

708709
#[tokio::test]
709710
async fn num_base_occurrences_without_exceptions() {
710-
let paced_train = create_paced_train(vec![]);
711+
let paced_train = create_paced_train();
711712
assert_eq!(paced_train.num_base_occurrences(), 4);
712713
}
713714

714715
#[tokio::test]
715716
async fn num_base_occurrences_with_exceptions() {
716-
let paced_train = create_paced_train(vec![
717-
create_created_exception_with_change_groups("key_2"),
718-
create_modified_exception_with_change_groups("key_1", 0),
719-
]);
717+
let paced_train = create_paced_train();
720718
assert_eq!(paced_train.num_base_occurrences(), 4);
721719
}
722720

@@ -740,7 +738,7 @@ mod tests {
740738
key: Some("key_2".into()),
741739
occurrence_index: None,
742740
disabled: false,
743-
change_groups: TrainScheduleExceptionChangeGroups::fake_created(),
741+
change_groups: TrainScheduleExceptionChangeGroups::fake(),
744742
}
745743
.into();
746744
let exception_3: schemas::TrainScheduleException = TrainScheduleException {
@@ -754,7 +752,7 @@ mod tests {
754752
}
755753
.into();
756754

757-
let paced_train = create_paced_train(vec![]);
755+
let paced_train = create_paced_train();
758756
let exceptions: Vec<schemas::TrainScheduleException> = vec![
759757
exception_1.clone(),
760758
exception_2.clone(),
@@ -806,7 +804,7 @@ mod tests {
806804

807805
#[tokio::test]
808806
async fn iter_occurrences_with_modified_start_time_exception() {
809-
let mut exception_1 = create_modified_exception_with_change_groups("key_1", 1);
807+
let mut exception_1 = TrainScheduleException::fake_modified("key_1", 1);
810808
exception_1.change_groups.start_time = Some(StartTimeChangeGroup {
811809
value: DateTime::<Utc>::from_str("2025-05-15T14:31:00+02:00").unwrap(),
812810
});
@@ -821,7 +819,7 @@ mod tests {
821819
}
822820
.into();
823821

824-
let paced_train = create_paced_train(vec![]);
822+
let paced_train = create_paced_train();
825823
let occurrences: Vec<_> = paced_train
826824
.iter_occurrences_v2(std::slice::from_ref(&exception_1))
827825
.collect();

editoast/src/views/timetable/paced_train.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1725,7 +1725,6 @@ mod tests {
17251725

17261726
use crate::error::InternalError;
17271727
use crate::models;
1728-
use crate::models::fixtures::create_created_exception_with_change_groups;
17291728
use crate::models::fixtures::create_fast_rolling_stock;
17301729
use crate::models::fixtures::create_paced_train_with_exceptions;
17311730
use crate::models::fixtures::create_simple_paced_train;
@@ -2073,7 +2072,7 @@ mod tests {
20732072
create_fast_rolling_stock(&mut db_pool.get_ok(), "simulation_rolling_stock").await;
20742073
let (timetable, train_schedule_set) =
20752074
create_timetable_with_train_schedule_set(&mut db_pool.get_ok()).await;
2076-
let exception = create_created_exception_with_change_groups("created_exception_key");
2075+
let exception = TrainScheduleException::fake("created_exception_key", None);
20772076

20782077
let train_schedule_base = TrainSchedule {
20792078
train_occurrence: TrainOccurrence {

0 commit comments

Comments
 (0)