Skip to content

Commit

Permalink
editoast: rolling stock model v2
Browse files Browse the repository at this point in the history
  • Loading branch information
Wadjetz committed Feb 13, 2024
1 parent 4105b19 commit 55afdad
Show file tree
Hide file tree
Showing 26 changed files with 604 additions and 562 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-- This file should undo anything in `up.sql`

ALTER TABLE rolling_stock
ALTER power_restrictions TYPE jsonb,
ALTER power_restrictions DROP DEFAULT,
ALTER power_restrictions DROP NOT NULL;
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
-- Your SQL goes here

UPDATE rolling_stock
SET power_restrictions = '{}'::jsonb
WHERE power_restrictions IS NULL;

ALTER TABLE rolling_stock
ALTER power_restrictions TYPE jsonb,
ALTER power_restrictions SET DEFAULT '{}',
ALTER power_restrictions SET NOT NULL;
9 changes: 5 additions & 4 deletions editoast/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1969,9 +1969,7 @@ components:
format: double
type: number
supported_signaling_systems:
items:
type: string
type: array
$ref: '#/components/schemas/RollingStockSupportedSignalingSystem'
required:
- name
- effort_curves
Expand All @@ -1986,7 +1984,6 @@ components:
- mass
- rolling_resistance
- loading_gauge
- power_restrictions
- supported_signaling_systems
type: object
RollingStockError:
Expand Down Expand Up @@ -2152,6 +2149,10 @@ components:
- number
- reference
type: object
RollingStockSupportedSignalingSystem:
items:
type: string
type: array
RollingStockWithLiveries:
allOf:
- $ref: '#/components/schemas/RollingStock'
Expand Down
4 changes: 2 additions & 2 deletions editoast/src/core/stdcm.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use serde::{Deserialize, Serialize};

use crate::models::{RollingStockModel, SpacingRequirement};

use crate::core::{pathfinding::Waypoint, simulation::SimulationResponse};
use crate::models::SpacingRequirement;
use crate::modelsv2::RollingStockModel;
use crate::schema::rolling_stock::RollingStockComfortType;
use crate::views::stdcm::AllowanceValue;

Expand Down
25 changes: 11 additions & 14 deletions editoast/src/fixtures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ pub mod tests {
use crate::models::train_schedule::Mrsp;
use crate::models::{
self, ElectricalProfileSet, Identifiable, Infra, Pathfinding, PathfindingChangeset,
Project, ResultPosition, ResultStops, ResultTrain, RollingStockLiveryModel,
RollingStockModel, Scenario, SimulationOutput, SimulationOutputChangeset, Study, Timetable,
TrainSchedule,
Project, ResultPosition, ResultStops, ResultTrain, RollingStockLiveryModel, Scenario,
SimulationOutput, SimulationOutputChangeset, Study, Timetable, TrainSchedule,
};
use crate::modelsv2::{self, Document, Model};
use crate::modelsv2::{self, Changeset, Document, Model, RollingStockModel};
use crate::schema::electrical_profiles::{ElectricalProfile, ElectricalProfileSetData};
use crate::schema::{RailJson, TrackRange};
use crate::views::infra::InfraForm;
Expand Down Expand Up @@ -113,37 +112,35 @@ pub mod tests {
Data::new(pool)
}

pub fn get_fast_rolling_stock(name: &str) -> RollingStockModel {
let mut rs: RollingStockModel =
pub fn get_fast_rolling_stock(name: &str) -> Changeset<RollingStockModel> {
let rs: Changeset<RollingStockModel> =
serde_json::from_str(include_str!("./tests/example_rolling_stock_1.json"))
.expect("Unable to parse");
rs.name = Some(name.to_string());
rs
rs.name(name.to_string())
}

pub async fn named_fast_rolling_stock(
name: &str,
db_pool: Data<DbPool>,
) -> TestFixture<RollingStockModel> {
let rs = get_fast_rolling_stock(name);
TestFixture::create_legacy(rs, db_pool).await
TestFixture::create(rs, db_pool).await
}

pub fn get_other_rolling_stock(name: &str) -> RollingStockModel {
let mut rs: RollingStockModel = serde_json::from_str(include_str!(
pub fn get_other_rolling_stock(name: &str) -> Changeset<RollingStockModel> {
let rs: Changeset<RollingStockModel> = serde_json::from_str(include_str!(
"./tests/example_rolling_stock_2_energy_sources.json"
))
.expect("Unable to parse");
rs.name = Some(name.to_string());
rs
rs.name(name.to_string())
}

pub async fn named_other_rolling_stock(
name: &str,
db_pool: Data<DbPool>,
) -> TestFixture<RollingStockModel> {
let rs = get_other_rolling_stock(name);
TestFixture::create_legacy(rs, db_pool).await
TestFixture::create(rs, db_pool).await
}

async fn make_train_schedule(
Expand Down
100 changes: 58 additions & 42 deletions editoast/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ use log::{error, info, warn};
use map::MapLayers;
use models::electrical_profiles::ElectricalProfileSet;
use models::infra::InfraError;
use models::{Retrieve, RollingStockModel};
use models::Retrieve;
use openssl::ssl::{SslConnector, SslMethod, SslVerifyMode};
use sentry::ClientInitGuard;
use std::error::Error;
Expand All @@ -66,6 +66,7 @@ use thiserror::Error;
use url::Url;
use validator::{Validate, ValidationErrorsKind};
use views::infra::InfraApiError;
use views::rolling_stocks::RollingStockForm;
use views::search::{SearchConfig, SearchConfigFinder, SearchConfigStore};

type DbPool = Pool<PgConnection>;
Expand Down Expand Up @@ -385,21 +386,22 @@ async fn import_rolling_stock(
) -> Result<(), Box<dyn Error + Send + Sync>> {
for rolling_stock_path in args.rolling_stock_path {
let rolling_stock_file = File::open(rolling_stock_path)?;
let mut rolling_stock: RollingStockModel =
let rolling_stock: RollingStockForm =
serde_json::from_reader(BufReader::new(rolling_stock_file))?;
match rolling_stock.validate() {
Ok(()) => {
println!(
"🍞 Importing rolling stock {}",
rolling_stock.name.clone().unwrap().bold()
rolling_stock.common.name.clone().bold()
);
rolling_stock.locked = Some(false);
rolling_stock.version = Some(0);
let rolling_stock = rolling_stock.create(db_pool.clone()).await?;
use crate::modelsv2::*;
let conn = &mut db_pool.get().await?;
let rolling_stock: Changeset<RollingStockModel> = rolling_stock.into();
let rolling_stock = rolling_stock.locked(false).version(0).create(conn).await?;
println!(
"✅ Rolling stock {}[{}] saved!",
rolling_stock.name.clone().unwrap().bold(),
rolling_stock.id.unwrap()
&rolling_stock.name.bold(),
&rolling_stock.id
);
}
Err(e) => {
Expand Down Expand Up @@ -742,6 +744,7 @@ mod tests {
use crate::fixtures::tests::{
db_pool, electrical_profile_set, get_fast_rolling_stock, TestFixture,
};
use crate::modelsv2::RollingStockModel;
use diesel::sql_query;
use diesel::sql_types::Text;
use diesel_async::RunQueryDsl;
Expand Down Expand Up @@ -771,13 +774,17 @@ mod tests {
// GIVEN
let rolling_stock_name =
"fast_rolling_stock_import_non_electric_rs_without_startup_and_panto_values";
let mut non_electric_rolling_stock = get_fast_rolling_stock(rolling_stock_name);
if let Some(ref mut effort_curves) = non_electric_rolling_stock.effort_curves {
effort_curves.modes.remove("25000V");
non_electric_rolling_stock.electrical_power_startup_time = Some(None);
non_electric_rolling_stock.raise_pantograph_time = Some(None);
}
let file = generate_temp_file(&non_electric_rolling_stock);
let mut non_electric_rs_changeset = get_fast_rolling_stock(rolling_stock_name);

non_electric_rs_changeset.effort_curves.as_mut().map(|ec| {
ec.0.modes.remove("25000V");
ec
});

let non_electric_rs_changeset = non_electric_rs_changeset
.electrical_power_startup_time(None)
.raise_pantograph_time(None);
let file = generate_temp_file(&non_electric_rs_changeset);
let args = ImportRollingStockArgs {
rolling_stock_path: vec![file.path().into()],
};
Expand All @@ -789,10 +796,10 @@ mod tests {
// import should not fail, as raise_panto and startup are not required for non electric
assert!(result.is_ok());
let mut conn = db_pool.get().await.unwrap();
let created_rs =
RollingStockModel::retrieve_by_name(&mut conn, rolling_stock_name.to_string())
.await
.unwrap();
use crate::modelsv2::Retrieve;
let created_rs = RollingStockModel::retrieve(&mut conn, rolling_stock_name.to_string())
.await
.unwrap();
assert!(created_rs.is_some());
TestFixture::new(created_rs.unwrap(), db_pool.clone());
}
Expand All @@ -802,11 +809,19 @@ mod tests {
// GIVEN
let rolling_stock_name =
"fast_rolling_stock_import_non_electric_rs_with_startup_and_panto_values";
let mut non_electric_rolling_stock = get_fast_rolling_stock(rolling_stock_name);
if let Some(ref mut effort_curves) = non_electric_rolling_stock.effort_curves {
effort_curves.modes.remove("25000V");
}
let file = generate_temp_file(&non_electric_rolling_stock);
let non_electric_rs_changeset = get_fast_rolling_stock(rolling_stock_name);

let effort_curves = non_electric_rs_changeset
.effort_curves
.clone()
.map(|mut ec| {
ec.0.modes.remove("25000V");
ec.0
});

let non_electric_rs_changeset = non_electric_rs_changeset.flat_effort_curves(effort_curves);

let file = generate_temp_file(&non_electric_rs_changeset);
let args = ImportRollingStockArgs {
rolling_stock_path: vec![file.path().into()],
};
Expand All @@ -817,30 +832,31 @@ mod tests {
// THEN
assert!(result.is_ok());
let mut conn = db_pool.get().await.unwrap();
let created_rs =
RollingStockModel::retrieve_by_name(&mut conn, rolling_stock_name.to_string())
.await
.unwrap();
use crate::modelsv2::Retrieve;
let created_rs = RollingStockModel::retrieve(&mut conn, rolling_stock_name.to_string())
.await
.unwrap();
assert!(created_rs.is_some());
let rs_fixture = TestFixture::new(created_rs.unwrap(), db_pool.clone());
let RollingStockModel {
electrical_power_startup_time,
raise_pantograph_time,
..
} = rs_fixture.model;
assert!(electrical_power_startup_time.unwrap().is_some());
assert!(raise_pantograph_time.unwrap().is_some());
assert!(electrical_power_startup_time.is_some());
assert!(raise_pantograph_time.is_some());
}

#[rstest]
async fn import_electric_rs_without_startup_and_panto_values(db_pool: Data<DbPool>) {
// GIVEN
let rolling_stock_name =
"fast_rolling_stock_import_electric_rs_without_startup_and_panto_values";
let mut electric_rolling_stock = get_fast_rolling_stock(rolling_stock_name);
electric_rolling_stock.electrical_power_startup_time = Some(None);
electric_rolling_stock.raise_pantograph_time = Some(None);
let file = generate_temp_file(&electric_rolling_stock.clone());
let electric_rs_changeset = get_fast_rolling_stock(rolling_stock_name);
let electric_rs_changeset = electric_rs_changeset
.electrical_power_startup_time(None)
.raise_pantograph_time(None);
let file = generate_temp_file(&electric_rs_changeset);
let args = ImportRollingStockArgs {
rolling_stock_path: vec![file.path().into()],
};
Expand All @@ -852,10 +868,10 @@ mod tests {
// it should just fail to import
assert!(result.is_err());
let mut conn = db_pool.get().await.unwrap();
let created_rs =
RollingStockModel::retrieve_by_name(&mut conn, rolling_stock_name.to_string())
.await
.unwrap();
use crate::modelsv2::Retrieve;
let created_rs = RollingStockModel::retrieve(&mut conn, rolling_stock_name.to_string())
.await
.unwrap();
assert!(created_rs.is_none());
}

Expand All @@ -877,10 +893,10 @@ mod tests {
// import should succeed, and rolling stock should have the correct values in DB
assert!(result.is_ok());
let mut conn = db_pool.get().await.unwrap();
let created_rs =
RollingStockModel::retrieve_by_name(&mut conn, rolling_stock_name.to_string())
.await
.unwrap();
use crate::modelsv2::Retrieve;
let created_rs = RollingStockModel::retrieve(&mut conn, rolling_stock_name.to_string())
.await
.unwrap();
assert!(created_rs.is_some());
let rs_fixture = TestFixture::new(created_rs.unwrap(), db_pool.clone());
let RollingStockModel {
Expand Down
2 changes: 1 addition & 1 deletion editoast/src/models/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub use self::pathfinding::*;
pub use electrical_profiles::ElectricalProfileSet;
pub use infra::Infra;
pub use projects::{Ordering, Project, ProjectWithStudies};
pub use rolling_stock::{LightRollingStockModel, RollingStockLiveryModel, RollingStockModel};
pub use rolling_stock::{LightRollingStockModel, RollingStockLiveryModel};
pub use scenario::{Scenario, ScenarioWithCountTrains, ScenarioWithDetails};
pub use study::{Study, StudyWithScenarios};
pub use text_array::TextArray;
Expand Down
2 changes: 1 addition & 1 deletion editoast/src/models/rolling_stock/light_rolling_stock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub struct LightRollingStockModel {
#[schema(value_type = LoadingGaugeType)]
loading_gauge: String,
#[schema(value_type = HashMap<String, String>)]
power_restrictions: Option<DieselJson<HashMap<String, String>>>,
power_restrictions: DieselJson<HashMap<String, String>>,
#[schema(value_type = Vec<EnergySource>)]
energy_sources: DieselJson<Vec<EnergySource>>,
locked: bool,
Expand Down
Loading

0 comments on commit 55afdad

Please sign in to comment.