Skip to content

Commit 790979e

Browse files
committed
editoast: improve some tests of auto fix feature
1 parent 5f4de8f commit 790979e

File tree

1 file changed

+99
-123
lines changed

1 file changed

+99
-123
lines changed

editoast/src/views/infra/auto_fixes.rs

+99-123
Original file line numberDiff line numberDiff line change
@@ -632,195 +632,171 @@ mod test {
632632
#[rstest::rstest]
633633
async fn empty_object() {
634634
let app = create_test_service().await;
635-
let small_infra = small_infra(db_pool()).await;
636-
let small_infra_id = small_infra.id();
635+
let empty_infra = empty_infra(db_pool()).await;
636+
let empty_infra_id = empty_infra.id();
637637

638638
let catenary: RailjsonObject = Catenary::default().into();
639-
let req_create = get_create_operation_request(catenary.clone(), small_infra_id);
640-
assert_eq!(
641-
call_service(&app, req_create).await.status(),
642-
StatusCode::OK
643-
);
639+
let operational_point = OperationalPoint::default().into();
640+
let speed_section = SpeedSection::default().into();
644641

645-
let operational_point: RailjsonObject = OperationalPoint::default().into();
646-
let req_create = get_create_operation_request(operational_point.clone(), small_infra_id);
647-
assert_eq!(
648-
call_service(&app, req_create).await.status(),
649-
StatusCode::OK
650-
);
651-
652-
let speed_section: RailjsonObject = SpeedSection::default().into();
653-
let req_create = get_create_operation_request(speed_section.clone(), small_infra_id);
654-
assert_eq!(
655-
call_service(&app, req_create).await.status(),
656-
StatusCode::OK
657-
);
642+
for obj in [&catenary, &operational_point, &speed_section] {
643+
let req_create = get_create_operation_request(obj.clone(), empty_infra_id);
644+
assert_eq!(
645+
call_service(&app, req_create).await.status(),
646+
StatusCode::OK
647+
);
648+
}
658649

659-
let response = call_service(&app, auto_fixes_request(small_infra_id)).await;
650+
let response = call_service(&app, auto_fixes_request(empty_infra_id)).await;
660651
assert_eq!(response.status(), StatusCode::OK);
661652

662653
let operations: Vec<Operation> = read_body_json(response).await;
663-
assert!(operations.contains(&Operation::Delete(DeleteOperation {
664-
obj_id: catenary.get_id().to_string(),
665-
obj_type: ObjectType::Catenary,
666-
})));
667-
assert!(operations.contains(&Operation::Delete(DeleteOperation {
668-
obj_id: operational_point.get_id().to_string(),
669-
obj_type: ObjectType::OperationalPoint,
670-
})));
671-
assert!(operations.contains(&Operation::Delete(DeleteOperation {
672-
obj_id: speed_section.get_id().to_string(),
673-
obj_type: ObjectType::SpeedSection,
674-
})));
654+
655+
for obj in [&catenary, &operational_point, &speed_section] {
656+
assert!(operations.contains(&Operation::Delete(DeleteOperation {
657+
obj_id: obj.get_id().to_string(),
658+
obj_type: obj.get_type(),
659+
})))
660+
}
675661
}
676662

677663
#[rstest::rstest]
678664
async fn out_of_range_must_be_ignored() {
679665
let app = create_test_service().await;
680-
let small_infra = small_infra(db_pool()).await;
681-
let small_infra_id = small_infra.id();
666+
let empty_infra = empty_infra(db_pool()).await;
667+
let empty_infra_id = empty_infra.id();
668+
669+
let track: RailjsonObject = TrackSection {
670+
id: "test_track".into(),
671+
length: 1_000.0,
672+
slopes: vec![Slope {
673+
begin: 250.0,
674+
end: 1250.0,
675+
gradient: 0.,
676+
}],
677+
..Default::default()
678+
}
679+
.into();
682680

683681
let catenary: RailjsonObject = Catenary {
684682
track_ranges: vec![ApplicableDirectionsTrackRange {
685-
begin: 100000000000.0,
686-
end: 100000000001.0,
683+
track: "test_track".into(),
684+
begin: 250.0,
685+
end: 1250.0,
687686
..Default::default()
688687
}],
689688
..Default::default()
690689
}
691690
.into();
692-
let req_create = get_create_operation_request(catenary.clone(), small_infra_id);
693-
assert_eq!(
694-
call_service(&app, req_create).await.status(),
695-
StatusCode::OK
696-
);
697691

698692
let operational_point: RailjsonObject = OperationalPoint {
699693
parts: vec![OperationalPointPart {
700-
position: 10000000000000.0,
694+
track: "test_track".into(),
695+
position: 1250.0,
701696
..Default::default()
702697
}],
703698
..Default::default()
704699
}
705700
.into();
706-
let req_create = get_create_operation_request(operational_point.clone(), small_infra_id);
707-
assert_eq!(
708-
call_service(&app, req_create).await.status(),
709-
StatusCode::OK
710-
);
711701

712702
let speed_section: RailjsonObject = SpeedSection {
713703
track_ranges: vec![ApplicableDirectionsTrackRange {
714-
begin: 100000000000.0,
715-
end: 100000000001.0,
704+
track: "test_track".into(),
705+
begin: 250.0,
706+
end: 1250.0,
716707
..Default::default()
717708
}],
718709
..Default::default()
719710
}
720711
.into();
721-
let req_create = get_create_operation_request(speed_section.clone(), small_infra_id);
722-
assert_eq!(
723-
call_service(&app, req_create).await.status(),
724-
StatusCode::OK
725-
);
726712

727-
let track_section: RailjsonObject = TrackSection {
728-
slopes: vec![Slope {
729-
begin: 100000000000.0,
730-
end: 100000000001.0,
731-
gradient: 0.1,
732-
}],
733-
..Default::default()
713+
for obj in [&track, &catenary, &operational_point, &speed_section] {
714+
let req_create = get_create_operation_request(obj.clone(), empty_infra_id);
715+
assert_eq!(
716+
call_service(&app, req_create).await.status(),
717+
StatusCode::OK
718+
);
734719
}
735-
.into();
736-
let req_create = get_create_operation_request(track_section.clone(), small_infra_id);
737-
assert_eq!(
738-
call_service(&app, req_create).await.status(),
739-
StatusCode::OK
740-
);
741-
742-
let response = call_service(&app, auto_fixes_request(small_infra_id)).await;
743720

721+
let response = call_service(&app, auto_fixes_request(empty_infra_id)).await;
744722
assert_eq!(response.status(), StatusCode::OK);
745723

746724
let operations: Vec<Operation> = read_body_json(response).await;
747-
assert!(!operations.contains(&Operation::Delete(DeleteOperation {
748-
obj_id: catenary.get_id().to_string(),
749-
obj_type: ObjectType::Catenary,
750-
})));
751-
assert!(!operations.contains(&Operation::Delete(DeleteOperation {
752-
obj_id: operational_point.get_id().to_string(),
753-
obj_type: ObjectType::OperationalPoint,
754-
})));
755-
assert!(!operations.contains(&Operation::Delete(DeleteOperation {
756-
obj_id: speed_section.get_id().to_string(),
757-
obj_type: ObjectType::SpeedSection,
758-
})));
759-
assert!(!operations.contains(&Operation::Delete(DeleteOperation {
760-
obj_id: track_section.get_id().to_string(),
761-
obj_type: ObjectType::TrackSection,
762-
})));
725+
726+
for obj in [&track, &catenary, &operational_point, &speed_section] {
727+
assert!(!operations.contains(&Operation::Delete(DeleteOperation {
728+
obj_id: obj.get_id().to_string(),
729+
obj_type: obj.get_type(),
730+
})))
731+
}
763732
}
764733

765734
#[rstest::rstest]
766-
async fn out_of_range_must_be_deleted() {
735+
#[case(250., 0)]
736+
#[case(1250., 3)]
737+
async fn out_of_range_must_be_deleted(#[case] pos: f64, #[case] error_count: usize) {
767738
let app = create_test_service().await;
768-
let small_infra = small_infra(db_pool()).await;
769-
let small_infra_id = small_infra.id();
739+
let empty_infra = empty_infra(db_pool()).await;
740+
let empty_infra_id = empty_infra.id();
741+
742+
let track: RailjsonObject = TrackSection {
743+
id: "test_track".into(),
744+
length: 1_000.0,
745+
geo: geos::geojson::Geometry::new(geos::geojson::Value::LineString(vec![
746+
vec![0.0, 0.0],
747+
vec![1.0, 1.0],
748+
])),
749+
sch: geos::geojson::Geometry::new(geos::geojson::Value::LineString(vec![
750+
vec![0.0, 0.0],
751+
vec![1.0, 1.0],
752+
])),
753+
..Default::default()
754+
}
755+
.into();
770756

771757
let signal: RailjsonObject = Signal {
772-
position: 10000000000000.0,
773-
track: "TC0".into(),
758+
position: pos,
759+
track: "test_track".into(),
774760
..Default::default()
775761
}
776762
.into();
777-
let req_create = get_create_operation_request(signal.clone(), small_infra_id);
778-
assert_eq!(
779-
call_service(&app, req_create).await.status(),
780-
StatusCode::OK
781-
);
782763

783764
let detector: RailjsonObject = Detector {
784-
position: 10000000000000.0,
785-
track: "TC0".into(),
765+
position: pos,
766+
track: "test_track".into(),
786767
..Default::default()
787768
}
788769
.into();
789-
let req_create = get_create_operation_request(detector.clone(), small_infra_id);
790-
assert_eq!(
791-
call_service(&app, req_create).await.status(),
792-
StatusCode::OK
793-
);
794770

795771
let buffer_stop: RailjsonObject = BufferStop {
796-
position: 10000000000000.0,
797-
track: "TC0".into(),
772+
position: pos,
773+
track: "test_track".into(),
798774
..Default::default()
799775
}
800776
.into();
801-
let req_create = get_create_operation_request(buffer_stop.clone(), small_infra_id);
802-
assert_eq!(
803-
call_service(&app, req_create).await.status(),
804-
StatusCode::OK
805-
);
806777

807-
let response = call_service(&app, auto_fixes_request(small_infra_id)).await;
778+
for obj in [&track, &signal, &detector, &buffer_stop] {
779+
let req_create = get_create_operation_request(obj.clone(), empty_infra_id);
780+
assert_eq!(
781+
call_service(&app, req_create).await.status(),
782+
StatusCode::OK
783+
);
784+
}
808785

786+
let response = call_service(&app, auto_fixes_request(empty_infra_id)).await;
809787
assert_eq!(response.status(), StatusCode::OK);
810788

811789
let operations: Vec<Operation> = read_body_json(response).await;
812-
assert!(operations.contains(&Operation::Delete(DeleteOperation {
813-
obj_id: signal.get_id().to_string(),
814-
obj_type: ObjectType::Signal,
815-
})));
816-
assert!(operations.contains(&Operation::Delete(DeleteOperation {
817-
obj_id: detector.get_id().to_string(),
818-
obj_type: ObjectType::Detector,
819-
})));
820-
assert!(operations.contains(&Operation::Delete(DeleteOperation {
821-
obj_id: buffer_stop.get_id().to_string(),
822-
obj_type: ObjectType::BufferStop,
823-
})));
790+
assert_eq!(operations.len(), error_count);
791+
792+
if !operations.is_empty() {
793+
for obj in [&signal, &detector, &buffer_stop] {
794+
assert!(operations.contains(&Operation::Delete(DeleteOperation {
795+
obj_id: obj.get_id().to_string(),
796+
obj_type: obj.get_type(),
797+
})))
798+
}
799+
}
824800
}
825801

826802
#[rstest::rstest]

0 commit comments

Comments
 (0)