Skip to content

Commit

Permalink
editoast: remove TrainScheduleForm use in models fixtures
Browse files Browse the repository at this point in the history
Signed-off-by: Leo Valais <leo.valais97@gmail.com>
  • Loading branch information
leovalais committed Oct 7, 2024
1 parent 06d3da8 commit 2e51022
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 53 deletions.
14 changes: 4 additions & 10 deletions editoast/src/client/stdcm_search_env_commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,13 +227,9 @@ async fn show_stdcm_search_env(
#[cfg(test)]
mod tests {
use crate::{
models::{
fixtures::{
create_electrical_profile_set, create_empty_infra, create_scenario_fixtures_set,
create_timetable, create_work_schedule_group, simple_train_schedule_form,
},
train_schedule::TrainSchedule,
Changeset,
models::fixtures::{
create_electrical_profile_set, create_empty_infra, create_scenario_fixtures_set,
create_timetable, create_work_schedule_group, simple_train_schedule_changeset,
},
Create,
};
Expand All @@ -253,9 +249,7 @@ mod tests {
conn: &mut DbConnection,
) {
for start_time in start_times {
let train_schedule_changeset: Changeset<TrainSchedule> =
simple_train_schedule_form(timetable_id).into();
train_schedule_changeset
simple_train_schedule_changeset(timetable_id)
.start_time(start_time.and_utc())
.create(conn)
.await
Expand Down
14 changes: 4 additions & 10 deletions editoast/src/models/fixtures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ use crate::models::RollingStockModel;
use crate::models::Scenario;
use crate::models::Study;
use crate::models::Tags;
use crate::views::train_schedule::TrainScheduleForm;

pub fn project_changeset(name: &str) -> Changeset<Project> {
Project::changeset()
Expand Down Expand Up @@ -74,23 +73,18 @@ pub async fn create_timetable(conn: &mut DbConnection) -> Timetable {

pub fn simple_train_schedule_base() -> TrainScheduleBase {
serde_json::from_str(include_str!("../tests/train_schedules/simple.json"))
.expect("Unable to parse")
.expect("Unable to parse test train schedule")
}

pub fn simple_train_schedule_form(timetable_id: i64) -> TrainScheduleForm {
let train_schedule: TrainScheduleBase = simple_train_schedule_base();
TrainScheduleForm {
timetable_id: Some(timetable_id),
train_schedule,
}
pub fn simple_train_schedule_changeset(timetable_id: i64) -> Changeset<TrainSchedule> {
Changeset::<TrainSchedule>::from(simple_train_schedule_base()).timetable_id(timetable_id)
}

pub async fn create_simple_train_schedule(
conn: &mut DbConnection,
timetable_id: i64,
) -> TrainSchedule {
let train_schedule: Changeset<TrainSchedule> = simple_train_schedule_form(timetable_id).into();
train_schedule
simple_train_schedule_changeset(timetable_id)
.create(conn)
.await
.expect("Failed to create train schedule")
Expand Down
38 changes: 38 additions & 0 deletions editoast/src/models/train_schedule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ use editoast_schemas::train_schedule::Margins;
use editoast_schemas::train_schedule::PathItem;
use editoast_schemas::train_schedule::PowerRestrictionItem;
use editoast_schemas::train_schedule::ScheduleItem;
use editoast_schemas::train_schedule::TrainScheduleBase;
use editoast_schemas::train_schedule::TrainScheduleOptions;

use super::Model as _;

#[derive(Debug, Default, Clone, Model)]
#[model(table = editoast_models::tables::train_schedule)]
#[model(gen(ops = crud, batch_ops = crd, list))]
Expand Down Expand Up @@ -36,3 +39,38 @@ pub struct TrainSchedule {
#[model(json)]
pub options: TrainScheduleOptions,
}

impl From<TrainScheduleBase> for TrainScheduleChangeset {
fn from(
TrainScheduleBase {
train_name,
labels,
rolling_stock_name,
start_time,
path,
schedule,
margins,
initial_speed,
comfort,
constraint_distribution,
speed_limit_tag,
power_restrictions,
options,
}: TrainScheduleBase,
) -> Self {
TrainSchedule::changeset()
.comfort(comfort)
.constraint_distribution(constraint_distribution)
.initial_speed(initial_speed)
.labels(labels.into_iter().map(Some).collect())
.margins(margins)
.path(path)
.power_restrictions(power_restrictions)
.rolling_stock_name(rolling_stock_name)
.schedule(schedule)
.speed_limit_tag(speed_limit_tag.map(|s| s.0))
.start_time(start_time)
.train_name(train_name)
.options(options)
}
}
18 changes: 5 additions & 13 deletions editoast/src/views/rolling_stock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -795,9 +795,8 @@ pub mod tests {
use crate::models::fixtures::create_timetable;
use crate::models::fixtures::fast_rolling_stock_changeset;
use crate::models::fixtures::get_rolling_stock_with_invalid_effort_curves;
use crate::models::fixtures::simple_train_schedule_form;
use crate::models::fixtures::simple_train_schedule_changeset;
use crate::models::rolling_stock_model::RollingStockModel;
use crate::models::train_schedule::TrainSchedule;
use crate::views::test_app::TestApp;
use crate::views::test_app::TestAppBuilder;

Expand Down Expand Up @@ -955,20 +954,13 @@ pub mod tests {
)
.await;

let mut schedule_form_1 = simple_train_schedule_form(timetable_1.id);
let mut schedule_form_2 = simple_train_schedule_form(timetable_1.id);
schedule_form_1
.train_schedule
.rolling_stock_name
.clone_from(&rolling_stock.name);
schedule_form_2.train_schedule.rolling_stock_name = rolling_stock.name;
let train_schedule_1: Changeset<TrainSchedule> = schedule_form_1.into();
let _ = train_schedule_1
simple_train_schedule_changeset(timetable_1.id)
.rolling_stock_name(rolling_stock.name.clone())
.create(&mut db_pool.get_ok())
.await
.unwrap();
let train_schedule_2: Changeset<TrainSchedule> = schedule_form_2.into();
let _ = train_schedule_2
simple_train_schedule_changeset(timetable_1.id)
.rolling_stock_name(rolling_stock.name)
.create(&mut db_pool.get_ok())
.await
.unwrap();
Expand Down
26 changes: 6 additions & 20 deletions editoast/src/views/train_schedule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,27 +142,13 @@ pub struct TrainScheduleForm {
}

impl From<TrainScheduleForm> for TrainScheduleChangeset {
fn from(value: TrainScheduleForm) -> Self {
let TrainScheduleForm {
fn from(
TrainScheduleForm {
timetable_id,
train_schedule: ts,
} = value;

TrainSchedule::changeset()
.flat_timetable_id(timetable_id)
.comfort(ts.comfort)
.constraint_distribution(ts.constraint_distribution)
.initial_speed(ts.initial_speed)
.labels(ts.labels.into_iter().map(Some).collect())
.margins(ts.margins)
.path(ts.path)
.power_restrictions(ts.power_restrictions)
.rolling_stock_name(ts.rolling_stock_name)
.schedule(ts.schedule)
.speed_limit_tag(ts.speed_limit_tag.map(|s| s.0))
.start_time(ts.start_time)
.train_name(ts.train_name)
.options(ts.options)
train_schedule,
}: TrainScheduleForm,
) -> Self {
Self::from(train_schedule).flat_timetable_id(timetable_id)
}
}

Expand Down

0 comments on commit 2e51022

Please sign in to comment.