Skip to content

Commit 9a0c4ae

Browse files
committed
editoast: improve some tests of auto fix feature
1 parent e0b8640 commit 9a0c4ae

File tree

1 file changed

+95
-125
lines changed

1 file changed

+95
-125
lines changed

editoast/src/views/infra/auto_fixes.rs

+95-125
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,7 @@ mod test {
191191
use crate::schema::{
192192
ApplicableDirectionsTrackRange, BufferStop, BufferStopCache, Catenary, Detector,
193193
DetectorCache, Endpoint, ObjectRef, ObjectType, OperationalPoint, OperationalPointPart,
194-
Route, Signal, SignalCache, Slope, SpeedSection, Switch, TrackEndpoint, TrackSection,
195-
Waypoint,
194+
Route, Signal, SignalCache, SpeedSection, Switch, TrackEndpoint, TrackSection, Waypoint,
196195
};
197196
use crate::views::pagination::PaginatedResponse;
198197
use crate::views::tests::create_test_service;
@@ -602,194 +601,165 @@ mod test {
602601
#[rstest::rstest]
603602
async fn empty_object() {
604603
let app = create_test_service().await;
605-
let small_infra = small_infra(db_pool()).await;
606-
let small_infra_id = small_infra.id();
604+
let empty_infra = empty_infra(db_pool()).await;
605+
let empty_infra_id = empty_infra.id();
607606

608607
let catenary: RailjsonObject = Catenary::default().into();
609-
let req_create = get_create_operation_request(catenary.clone(), small_infra_id);
610-
assert_eq!(
611-
call_service(&app, req_create).await.status(),
612-
StatusCode::OK
613-
);
608+
let operational_point = OperationalPoint::default().into();
609+
let speed_section = SpeedSection::default().into();
614610

615-
let operational_point: RailjsonObject = OperationalPoint::default().into();
616-
let req_create = get_create_operation_request(operational_point.clone(), small_infra_id);
617-
assert_eq!(
618-
call_service(&app, req_create).await.status(),
619-
StatusCode::OK
620-
);
621-
622-
let speed_section: RailjsonObject = SpeedSection::default().into();
623-
let req_create = get_create_operation_request(speed_section.clone(), small_infra_id);
624-
assert_eq!(
625-
call_service(&app, req_create).await.status(),
626-
StatusCode::OK
627-
);
611+
for obj in [&catenary, &operational_point, &speed_section] {
612+
let req_create = get_create_operation_request(obj.clone(), empty_infra_id);
613+
assert_eq!(
614+
call_service(&app, req_create).await.status(),
615+
StatusCode::OK
616+
);
617+
}
628618

629-
let response = call_service(&app, auto_fixes_request(small_infra_id)).await;
619+
let response = call_service(&app, auto_fixes_request(empty_infra_id)).await;
630620
assert_eq!(response.status(), StatusCode::OK);
631621

632622
let operations: Vec<Operation> = read_body_json(response).await;
633-
assert!(operations.contains(&Operation::Delete(DeleteOperation {
634-
obj_id: catenary.get_id().to_string(),
635-
obj_type: ObjectType::Catenary,
636-
})));
637-
assert!(operations.contains(&Operation::Delete(DeleteOperation {
638-
obj_id: operational_point.get_id().to_string(),
639-
obj_type: ObjectType::OperationalPoint,
640-
})));
641-
assert!(operations.contains(&Operation::Delete(DeleteOperation {
642-
obj_id: speed_section.get_id().to_string(),
643-
obj_type: ObjectType::SpeedSection,
644-
})));
623+
624+
for obj in [&catenary, &operational_point, &speed_section] {
625+
assert!(operations.contains(&Operation::Delete(DeleteOperation {
626+
obj_id: obj.get_id().to_string(),
627+
obj_type: obj.get_type(),
628+
})))
629+
}
645630
}
646631

647632
#[rstest::rstest]
648633
async fn out_of_range_must_be_ignored() {
649634
let app = create_test_service().await;
650-
let small_infra = small_infra(db_pool()).await;
651-
let small_infra_id = small_infra.id();
635+
let empty_infra = empty_infra(db_pool()).await;
636+
let empty_infra_id = empty_infra.id();
637+
638+
let track: RailjsonObject = TrackSection {
639+
id: "test_track".into(),
640+
length: 1_000.0,
641+
..Default::default()
642+
}
643+
.into();
652644

653645
let catenary: RailjsonObject = Catenary {
654646
track_ranges: vec![ApplicableDirectionsTrackRange {
655-
begin: 100000000000.0,
656-
end: 100000000001.0,
647+
track: "test_track".into(),
648+
begin: 250.0,
649+
end: 1250.0,
657650
..Default::default()
658651
}],
659652
..Default::default()
660653
}
661654
.into();
662-
let req_create = get_create_operation_request(catenary.clone(), small_infra_id);
663-
assert_eq!(
664-
call_service(&app, req_create).await.status(),
665-
StatusCode::OK
666-
);
667655

668656
let operational_point: RailjsonObject = OperationalPoint {
669657
parts: vec![OperationalPointPart {
670-
position: 10000000000000.0,
658+
track: "test_track".into(),
659+
position: 1250.0,
671660
..Default::default()
672661
}],
673662
..Default::default()
674663
}
675664
.into();
676-
let req_create = get_create_operation_request(operational_point.clone(), small_infra_id);
677-
assert_eq!(
678-
call_service(&app, req_create).await.status(),
679-
StatusCode::OK
680-
);
681665

682666
let speed_section: RailjsonObject = SpeedSection {
683667
track_ranges: vec![ApplicableDirectionsTrackRange {
684-
begin: 100000000000.0,
685-
end: 100000000001.0,
668+
track: "test_track".into(),
669+
begin: 250.0,
670+
end: 1250.0,
686671
..Default::default()
687672
}],
688673
..Default::default()
689674
}
690675
.into();
691-
let req_create = get_create_operation_request(speed_section.clone(), small_infra_id);
692-
assert_eq!(
693-
call_service(&app, req_create).await.status(),
694-
StatusCode::OK
695-
);
696676

697-
let track_section: RailjsonObject = TrackSection {
698-
slopes: vec![Slope {
699-
begin: 100000000000.0,
700-
end: 100000000001.0,
701-
gradient: 0.1,
702-
}],
703-
..Default::default()
677+
for obj in [&track, &catenary, &operational_point, &speed_section] {
678+
let req_create = get_create_operation_request(obj.clone(), empty_infra_id);
679+
assert_eq!(
680+
call_service(&app, req_create).await.status(),
681+
StatusCode::OK
682+
);
704683
}
705-
.into();
706-
let req_create = get_create_operation_request(track_section.clone(), small_infra_id);
707-
assert_eq!(
708-
call_service(&app, req_create).await.status(),
709-
StatusCode::OK
710-
);
711-
712-
let response = call_service(&app, auto_fixes_request(small_infra_id)).await;
713684

685+
let response = call_service(&app, auto_fixes_request(empty_infra_id)).await;
714686
assert_eq!(response.status(), StatusCode::OK);
715687

716688
let operations: Vec<Operation> = read_body_json(response).await;
717-
assert!(!operations.contains(&Operation::Delete(DeleteOperation {
718-
obj_id: catenary.get_id().to_string(),
719-
obj_type: ObjectType::Catenary,
720-
})));
721-
assert!(!operations.contains(&Operation::Delete(DeleteOperation {
722-
obj_id: operational_point.get_id().to_string(),
723-
obj_type: ObjectType::OperationalPoint,
724-
})));
725-
assert!(!operations.contains(&Operation::Delete(DeleteOperation {
726-
obj_id: speed_section.get_id().to_string(),
727-
obj_type: ObjectType::SpeedSection,
728-
})));
729-
assert!(!operations.contains(&Operation::Delete(DeleteOperation {
730-
obj_id: track_section.get_id().to_string(),
731-
obj_type: ObjectType::TrackSection,
732-
})));
689+
690+
for obj in [&catenary, &operational_point, &speed_section] {
691+
assert!(!operations.contains(&Operation::Delete(DeleteOperation {
692+
obj_id: obj.get_id().to_string(),
693+
obj_type: obj.get_type(),
694+
})))
695+
}
733696
}
734697

735698
#[rstest::rstest]
736-
async fn out_of_range_must_be_deleted() {
699+
#[case(250., 0)]
700+
#[case(1250., 3)]
701+
async fn out_of_range_must_be_deleted(#[case] pos: f64, #[case] error_count: usize) {
737702
let app = create_test_service().await;
738-
let small_infra = small_infra(db_pool()).await;
739-
let small_infra_id = small_infra.id();
703+
let empty_infra = empty_infra(db_pool()).await;
704+
let empty_infra_id = empty_infra.id();
705+
706+
let track: RailjsonObject = TrackSection {
707+
id: "test_track".into(),
708+
length: 1_000.0,
709+
geo: geos::geojson::Geometry::new(geos::geojson::Value::LineString(vec![
710+
vec![0.0, 0.0],
711+
vec![1.0, 1.0],
712+
])),
713+
sch: geos::geojson::Geometry::new(geos::geojson::Value::LineString(vec![
714+
vec![0.0, 0.0],
715+
vec![1.0, 1.0],
716+
])),
717+
..Default::default()
718+
}
719+
.into();
740720

741721
let signal: RailjsonObject = Signal {
742-
position: 10000000000000.0,
743-
track: "TC0".into(),
722+
position: pos,
723+
track: "test_track".into(),
744724
..Default::default()
745725
}
746726
.into();
747-
let req_create = get_create_operation_request(signal.clone(), small_infra_id);
748-
assert_eq!(
749-
call_service(&app, req_create).await.status(),
750-
StatusCode::OK
751-
);
752727

753728
let detector: RailjsonObject = Detector {
754-
position: 10000000000000.0,
755-
track: "TC0".into(),
729+
position: pos,
730+
track: "test_track".into(),
756731
..Default::default()
757732
}
758733
.into();
759-
let req_create = get_create_operation_request(detector.clone(), small_infra_id);
760-
assert_eq!(
761-
call_service(&app, req_create).await.status(),
762-
StatusCode::OK
763-
);
764734

765735
let buffer_stop: RailjsonObject = BufferStop {
766-
position: 10000000000000.0,
767-
track: "TC0".into(),
736+
position: pos,
737+
track: "test_track".into(),
768738
..Default::default()
769739
}
770740
.into();
771-
let req_create = get_create_operation_request(buffer_stop.clone(), small_infra_id);
772-
assert_eq!(
773-
call_service(&app, req_create).await.status(),
774-
StatusCode::OK
775-
);
776741

777-
let response = call_service(&app, auto_fixes_request(small_infra_id)).await;
742+
for obj in [&track, &signal, &detector, &buffer_stop] {
743+
let req_create = get_create_operation_request(obj.clone(), empty_infra_id);
744+
assert_eq!(
745+
call_service(&app, req_create).await.status(),
746+
StatusCode::OK
747+
);
748+
}
778749

750+
let response = call_service(&app, auto_fixes_request(empty_infra_id)).await;
779751
assert_eq!(response.status(), StatusCode::OK);
780752

781753
let operations: Vec<Operation> = read_body_json(response).await;
782-
assert!(operations.contains(&Operation::Delete(DeleteOperation {
783-
obj_id: signal.get_id().to_string(),
784-
obj_type: ObjectType::Signal,
785-
})));
786-
assert!(operations.contains(&Operation::Delete(DeleteOperation {
787-
obj_id: detector.get_id().to_string(),
788-
obj_type: ObjectType::Detector,
789-
})));
790-
assert!(operations.contains(&Operation::Delete(DeleteOperation {
791-
obj_id: buffer_stop.get_id().to_string(),
792-
obj_type: ObjectType::BufferStop,
793-
})));
754+
assert_eq!(operations.len(), error_count);
755+
756+
if !operations.is_empty() {
757+
for obj in [&signal, &detector, &buffer_stop] {
758+
assert!(operations.contains(&Operation::Delete(DeleteOperation {
759+
obj_id: obj.get_id().to_string(),
760+
obj_type: obj.get_type(),
761+
})))
762+
}
763+
}
794764
}
795765
}

0 commit comments

Comments
 (0)