Skip to content

Commit d5f25b7

Browse files
committed
editoast: improve some tests of auto fix feature
1 parent f88c4ca commit d5f25b7

File tree

1 file changed

+96
-124
lines changed

1 file changed

+96
-124
lines changed

editoast/src/views/infra/auto_fixes.rs

+96-124
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,7 @@ mod test {
194194
use crate::schema::{
195195
ApplicableDirectionsTrackRange, BufferStop, BufferStopCache, Catenary, Detector,
196196
DetectorCache, Endpoint, ObjectRef, ObjectType, OperationalPoint, OperationalPointPart,
197-
Route, Signal, SignalCache, Slope, SpeedSection, Switch, TrackEndpoint, TrackSection,
198-
Waypoint,
197+
Route, Signal, SignalCache, SpeedSection, Switch, TrackEndpoint, TrackSection, Waypoint,
199198
};
200199
use crate::views::tests::create_test_service;
201200
use actix_http::{Request, StatusCode};
@@ -565,204 +564,177 @@ mod test {
565564
#[rstest::rstest]
566565
async fn empty_object() {
567566
let app = create_test_service().await;
568-
let small_infra = small_infra(db_pool()).await;
569-
let small_infra_id = small_infra.id();
567+
let empty_infra = empty_infra(db_pool()).await;
568+
let empty_infra_id = empty_infra.id();
570569

571570
let catenary: RailjsonObject = Catenary::default().into();
572-
let req_create = get_create_operation_request(catenary.clone(), small_infra_id);
573-
assert_eq!(
574-
call_service(&app, req_create).await.status(),
575-
StatusCode::OK
576-
);
571+
let operational_point = OperationalPoint::default().into();
572+
let speed_section = SpeedSection::default().into();
577573

578-
let operational_point: RailjsonObject = OperationalPoint::default().into();
579-
let req_create = get_create_operation_request(operational_point.clone(), small_infra_id);
580-
assert_eq!(
581-
call_service(&app, req_create).await.status(),
582-
StatusCode::OK
583-
);
584-
585-
let speed_section: RailjsonObject = SpeedSection::default().into();
586-
let req_create = get_create_operation_request(speed_section.clone(), small_infra_id);
587-
assert_eq!(
588-
call_service(&app, req_create).await.status(),
589-
StatusCode::OK
590-
);
574+
for obj in [&catenary, &operational_point, &speed_section] {
575+
let req_create = get_create_operation_request(obj.clone(), empty_infra_id);
576+
assert_eq!(
577+
call_service(&app, req_create).await.status(),
578+
StatusCode::OK
579+
);
580+
}
591581

592582
let req_fix = TestRequest::get()
593-
.uri(format!("/infra/{small_infra_id}/auto_fixes").as_str())
583+
.uri(format!("/infra/{empty_infra_id}/auto_fixes").as_str())
594584
.to_request();
595585
let response = call_service(&app, req_fix).await;
596586

597587
assert_eq!(response.status(), StatusCode::OK);
598588

599589
let operations: Vec<Operation> = read_body_json(response).await;
600-
assert!(operations.contains(&Operation::Delete(DeleteOperation {
601-
obj_id: catenary.get_id().to_string(),
602-
obj_type: ObjectType::Catenary,
603-
})));
604-
assert!(operations.contains(&Operation::Delete(DeleteOperation {
605-
obj_id: operational_point.get_id().to_string(),
606-
obj_type: ObjectType::OperationalPoint,
607-
})));
608-
assert!(operations.contains(&Operation::Delete(DeleteOperation {
609-
obj_id: speed_section.get_id().to_string(),
610-
obj_type: ObjectType::SpeedSection,
611-
})));
590+
591+
for obj in [&catenary, &operational_point, &speed_section] {
592+
assert!(operations.contains(&Operation::Delete(DeleteOperation {
593+
obj_id: obj.get_id().to_string(),
594+
obj_type: obj.get_type(),
595+
})))
596+
}
612597
}
613598

614599
#[rstest::rstest]
615600
async fn out_of_range_must_be_ignored() {
616601
let app = create_test_service().await;
617-
let small_infra = small_infra(db_pool()).await;
618-
let small_infra_id = small_infra.id();
602+
let empty_infra = empty_infra(db_pool()).await;
603+
let empty_infra_id = empty_infra.id();
604+
605+
let track: RailjsonObject = TrackSection {
606+
id: "test_track".into(),
607+
length: 1_000.0,
608+
..Default::default()
609+
}
610+
.into();
619611

620612
let catenary: RailjsonObject = Catenary {
621613
track_ranges: vec![ApplicableDirectionsTrackRange {
622-
begin: 100000000000.0,
623-
end: 100000000001.0,
614+
track: "test_track".into(),
615+
begin: 250.0,
616+
end: 1250.0,
624617
..Default::default()
625618
}],
626619
..Default::default()
627620
}
628621
.into();
629-
let req_create = get_create_operation_request(catenary.clone(), small_infra_id);
630-
assert_eq!(
631-
call_service(&app, req_create).await.status(),
632-
StatusCode::OK
633-
);
634622

635623
let operational_point: RailjsonObject = OperationalPoint {
636624
parts: vec![OperationalPointPart {
637-
position: 10000000000000.0,
625+
track: "test_track".into(),
626+
position: 1250.0,
638627
..Default::default()
639628
}],
640629
..Default::default()
641630
}
642631
.into();
643-
let req_create = get_create_operation_request(operational_point.clone(), small_infra_id);
644-
assert_eq!(
645-
call_service(&app, req_create).await.status(),
646-
StatusCode::OK
647-
);
648632

649633
let speed_section: RailjsonObject = SpeedSection {
650634
track_ranges: vec![ApplicableDirectionsTrackRange {
651-
begin: 100000000000.0,
652-
end: 100000000001.0,
635+
track: "test_track".into(),
636+
begin: 250.0,
637+
end: 1250.0,
653638
..Default::default()
654639
}],
655640
..Default::default()
656641
}
657642
.into();
658-
let req_create = get_create_operation_request(speed_section.clone(), small_infra_id);
659-
assert_eq!(
660-
call_service(&app, req_create).await.status(),
661-
StatusCode::OK
662-
);
663643

664-
let track_section: RailjsonObject = TrackSection {
665-
slopes: vec![Slope {
666-
begin: 100000000000.0,
667-
end: 100000000001.0,
668-
gradient: 0.1,
669-
}],
670-
..Default::default()
644+
for obj in [&track, &catenary, &operational_point, &speed_section] {
645+
let req_create = get_create_operation_request(obj.clone(), empty_infra_id);
646+
assert_eq!(
647+
call_service(&app, req_create).await.status(),
648+
StatusCode::OK
649+
);
671650
}
672-
.into();
673-
let req_create = get_create_operation_request(track_section.clone(), small_infra_id);
674-
assert_eq!(
675-
call_service(&app, req_create).await.status(),
676-
StatusCode::OK
677-
);
678651

679652
let req_fix = TestRequest::get()
680-
.uri(format!("/infra/{small_infra_id}/auto_fixes").as_str())
653+
.uri(format!("/infra/{empty_infra_id}/auto_fixes").as_str())
681654
.to_request();
682655
let response = call_service(&app, req_fix).await;
683656

684657
assert_eq!(response.status(), StatusCode::OK);
685658

686659
let operations: Vec<Operation> = read_body_json(response).await;
687-
assert!(!operations.contains(&Operation::Delete(DeleteOperation {
688-
obj_id: catenary.get_id().to_string(),
689-
obj_type: ObjectType::Catenary,
690-
})));
691-
assert!(!operations.contains(&Operation::Delete(DeleteOperation {
692-
obj_id: operational_point.get_id().to_string(),
693-
obj_type: ObjectType::OperationalPoint,
694-
})));
695-
assert!(!operations.contains(&Operation::Delete(DeleteOperation {
696-
obj_id: speed_section.get_id().to_string(),
697-
obj_type: ObjectType::SpeedSection,
698-
})));
699-
assert!(!operations.contains(&Operation::Delete(DeleteOperation {
700-
obj_id: track_section.get_id().to_string(),
701-
obj_type: ObjectType::TrackSection,
702-
})));
660+
661+
for obj in [&catenary, &operational_point, &speed_section] {
662+
assert!(!operations.contains(&Operation::Delete(DeleteOperation {
663+
obj_id: obj.get_id().to_string(),
664+
obj_type: obj.get_type(),
665+
})))
666+
}
703667
}
704668

705669
#[rstest::rstest]
706-
async fn out_of_range_must_be_deleted() {
670+
#[case(250., 0)]
671+
#[case(1250., 3)]
672+
async fn out_of_range_must_be_deleted(#[case] pos: f64, #[case] error_count: usize) {
707673
let app = create_test_service().await;
708-
let small_infra = small_infra(db_pool()).await;
709-
let small_infra_id = small_infra.id();
674+
let empty_infra = empty_infra(db_pool()).await;
675+
let empty_infra_id = empty_infra.id();
676+
677+
let track: RailjsonObject = TrackSection {
678+
id: "test_track".into(),
679+
length: 1_000.0,
680+
geo: geos::geojson::Geometry::new(geos::geojson::Value::LineString(vec![
681+
vec![0.0, 0.0],
682+
vec![1.0, 1.0],
683+
])),
684+
sch: geos::geojson::Geometry::new(geos::geojson::Value::LineString(vec![
685+
vec![0.0, 0.0],
686+
vec![1.0, 1.0],
687+
])),
688+
..Default::default()
689+
}
690+
.into();
710691

711692
let signal: RailjsonObject = Signal {
712-
position: 10000000000000.0,
713-
track: "TC0".into(),
693+
position: pos,
694+
track: "test_track".into(),
714695
..Default::default()
715696
}
716697
.into();
717-
let req_create = get_create_operation_request(signal.clone(), small_infra_id);
718-
assert_eq!(
719-
call_service(&app, req_create).await.status(),
720-
StatusCode::OK
721-
);
722698

723699
let detector: RailjsonObject = Detector {
724-
position: 10000000000000.0,
725-
track: "TC0".into(),
700+
position: pos,
701+
track: "test_track".into(),
726702
..Default::default()
727703
}
728704
.into();
729-
let req_create = get_create_operation_request(detector.clone(), small_infra_id);
730-
assert_eq!(
731-
call_service(&app, req_create).await.status(),
732-
StatusCode::OK
733-
);
734705

735706
let buffer_stop: RailjsonObject = BufferStop {
736-
position: 10000000000000.0,
737-
track: "TC0".into(),
707+
position: pos,
708+
track: "test_track".into(),
738709
..Default::default()
739710
}
740711
.into();
741-
let req_create = get_create_operation_request(buffer_stop.clone(), small_infra_id);
742-
assert_eq!(
743-
call_service(&app, req_create).await.status(),
744-
StatusCode::OK
745-
);
712+
713+
for obj in [&track, &signal, &detector, &buffer_stop] {
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+
);
719+
}
746720

747721
let req_fix = TestRequest::get()
748-
.uri(format!("/infra/{small_infra_id}/auto_fixes").as_str())
722+
.uri(format!("/infra/{empty_infra_id}/auto_fixes").as_str())
749723
.to_request();
750724
let response = call_service(&app, req_fix).await;
751725

752726
assert_eq!(response.status(), StatusCode::OK);
753727

754728
let operations: Vec<Operation> = read_body_json(response).await;
755-
assert!(operations.contains(&Operation::Delete(DeleteOperation {
756-
obj_id: signal.get_id().to_string(),
757-
obj_type: ObjectType::Signal,
758-
})));
759-
assert!(operations.contains(&Operation::Delete(DeleteOperation {
760-
obj_id: detector.get_id().to_string(),
761-
obj_type: ObjectType::Detector,
762-
})));
763-
assert!(operations.contains(&Operation::Delete(DeleteOperation {
764-
obj_id: buffer_stop.get_id().to_string(),
765-
obj_type: ObjectType::BufferStop,
766-
})));
729+
assert_eq!(operations.len(), error_count);
730+
731+
if !operations.is_empty() {
732+
for obj in [&signal, &detector, &buffer_stop] {
733+
assert!(operations.contains(&Operation::Delete(DeleteOperation {
734+
obj_id: obj.get_id().to_string(),
735+
obj_type: obj.get_type(),
736+
})))
737+
}
738+
}
767739
}
768740
}

0 commit comments

Comments
 (0)