From e61af34ecb5a1c3c0b11bf452bd4e126bbfc4629 Mon Sep 17 00:00:00 2001 From: Alice Khoudli Date: Wed, 9 Oct 2024 13:33:40 +0200 Subject: [PATCH] editoast: add rolling_stock deletion tests Signed-off-by: Alice Khoudli --- editoast/src/views/rolling_stock.rs | 76 +++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/editoast/src/views/rolling_stock.rs b/editoast/src/views/rolling_stock.rs index fa70d3702e1..3a97a3b400a 100644 --- a/editoast/src/views/rolling_stock.rs +++ b/editoast/src/views/rolling_stock.rs @@ -1326,4 +1326,80 @@ pub mod tests { assert_eq!(rolling_stock_exists, true); } + + #[rstest] + #[serial_test::serial] + async fn delete_unlocked_unused_rolling_stock_succeeds() { + // GIVEN + let app = TestAppBuilder::default_app(); + let db_pool = app.db_pool(); + + let rs_name = "fast_rolling_stock_name"; + let fast_rolling_stock = create_fast_rolling_stock(&mut db_pool.get_ok(), rs_name).await; + assert!(!fast_rolling_stock.locked); + + let request = + app.delete(format!("/rolling_stock/{}", fast_rolling_stock.id).as_str()); + + // WHEN + app.fetch(request).assert_status(StatusCode::NO_CONTENT); + + // THEN + let rolling_stock_exists = + RollingStockModel::exists(&mut db_pool.get_ok(), fast_rolling_stock.id) + .await + .expect("Failed to check if rolling stock exists"); + + assert_eq!(rolling_stock_exists, false); + } + + #[rstest] + #[serial_test::serial] + async fn delete_unlocked_used_rolling_stock_fails() { + // GIVEN + let app = TestAppBuilder::default_app(); + let db_pool = app.db_pool(); + + let rs_name = "fast_rolling_stock_name"; + let fast_rolling_stock = create_fast_rolling_stock(&mut db_pool.get_ok(), rs_name).await; + assert!(!fast_rolling_stock.locked); + + let project = create_project(&mut db_pool.get_ok(), &Uuid::new_v4().to_string()).await; + let study = create_study( + &mut db_pool.get_ok(), + &Uuid::new_v4().to_string(), + project.id, + ) + .await; + let timetable = create_timetable(&mut db_pool.get_ok()).await; + let infra = create_small_infra(&mut db_pool.get_ok()).await; + create_scenario( + &mut db_pool.get_ok(), + &Uuid::new_v4().to_string(), + study.id, + timetable.id, + infra.id, + ) + .await; + + simple_train_schedule_changeset(timetable.id) + .rolling_stock_name(fast_rolling_stock.name.clone()) + .create(&mut db_pool.get_ok()) + .await + .expect("Failed to create train schedule"); + + let request = + app.delete(format!("/rolling_stock/{}", fast_rolling_stock.id).as_str()); + + // WHEN + app.fetch(request).assert_status(StatusCode::CONFLICT); + + // THEN + let rolling_stock_exists = + RollingStockModel::exists(&mut db_pool.get_ok(), fast_rolling_stock.id) + .await + .expect("Failed to check if rolling stock exists"); + + assert_eq!(rolling_stock_exists, true); + } }