Skip to content

Commit

Permalink
editoast: refactor scenario V2 tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Wadjetz committed Jun 7, 2024
1 parent 4b360d4 commit 7bf770a
Show file tree
Hide file tree
Showing 4 changed files with 241 additions and 149 deletions.
74 changes: 66 additions & 8 deletions editoast/src/modelsv2/fixtures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::modelsv2::ElectricalProfileSet;
use crate::modelsv2::Infra;
use crate::modelsv2::Project;
use crate::modelsv2::RollingStockModel;
use crate::modelsv2::Scenario;
use crate::modelsv2::Study;
use crate::modelsv2::Tags;
use crate::views::rolling_stocks::rolling_stock_form::RollingStockForm;
Expand Down Expand Up @@ -53,6 +54,71 @@ pub async fn create_study(conn: &mut DbConnection, name: &str, project_id: i64)
.expect("Failed to create study")
}

pub async fn create_timetable(conn: &mut DbConnection) -> Timetable {
Timetable::changeset()
.electrical_profile_set_id(None)
.create(conn)
.await
.expect("Failed to create timetable")
}

pub fn scenario_changeset(
name: &str,
study_id: i64,
timetable_id: i64,
infra_id: i64,
) -> Changeset<Scenario> {
Scenario::changeset()
.name(name.to_string())
.description("test_scenario_v2 description".to_string())
.creation_date(Utc::now().naive_utc())
.last_modification(Utc::now().naive_utc())
.tags(Tags::default())
.timetable_id(timetable_id)
.study_id(study_id)
.infra_id(infra_id)
}

pub async fn create_scenario(
conn: &mut DbConnection,
name: &str,
study_id: i64,
timetable_id: i64,
infra_id: i64,
) -> Scenario {
let scenario = scenario_changeset(name, study_id, timetable_id, infra_id);
scenario
.create(conn)
.await
.expect("Failed to create scenario")
}

pub struct ScenarioFixtureSet {
pub project: Project,
pub study: Study,
pub scenario: Scenario,
pub timetable: Timetable,
pub infra: Infra,
}

pub async fn create_scenario_fixtures_set(
conn: &mut DbConnection,
name: &str,
) -> ScenarioFixtureSet {
let project = create_project(conn, "project_test_name").await;
let study = create_study(conn, "study_test_name", project.id).await;
let infra = create_empty_infra(conn).await;
let timetable = create_timetable(conn).await;
let scenario = create_scenario(conn, name, study.id, timetable.id, infra.id).await;
ScenarioFixtureSet {
project,
study,
scenario,
timetable,
infra,
}
}

pub fn fast_rolling_stock_form(name: &str) -> RollingStockForm {
let mut rolling_stock_form: RollingStockForm =
serde_json::from_str(include_str!("../tests/example_rolling_stock_1.json"))
Expand Down Expand Up @@ -151,14 +217,6 @@ pub async fn create_electrical_profile_set(conn: &mut DbConnection) -> Electrica
.expect("Failed to create electrical profile set")
}

pub async fn create_timetable(conn: &mut DbConnection) -> Timetable {
Timetable::changeset()
.electrical_profile_set_id(None)
.create(conn)
.await
.expect("Failed to create timetable")
}

pub async fn create_empty_infra(conn: &mut DbConnection) -> Infra {
Infra::changeset()
.name("empty_infra".to_owned())
Expand Down
1 change: 1 addition & 0 deletions editoast/src/modelsv2/scenario.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::modelsv2::Tags;
#[derive(Debug, Clone, ModelV2, Deserialize, Serialize, ToSchema)]
#[schema(as = ScenarioV2)]
#[model(table = crate::tables::scenario_v2)]
#[cfg_attr(test, derive(PartialEq))]
pub struct Scenario {
pub id: i64,
pub infra_id: i64,
Expand Down
Loading

0 comments on commit 7bf770a

Please sign in to comment.