Skip to content

Commit

Permalink
editoast: refactor rollings stocks tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Wadjetz committed Jun 4, 2024
1 parent 625d185 commit e3c1574
Show file tree
Hide file tree
Showing 5 changed files with 502 additions and 452 deletions.
54 changes: 54 additions & 0 deletions editoast/src/modelsv2/fixtures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ use crate::modelsv2::Infra;
use crate::modelsv2::Project;
use crate::modelsv2::Study;
use crate::modelsv2::Tags;
use crate::views::rolling_stocks::rolling_stock_form::RollingStockForm;

use super::RollingStockModel;

pub fn project_changeset(name: &str) -> Changeset<Project> {
Project::changeset()
Expand Down Expand Up @@ -54,6 +57,57 @@ pub async fn create_study(conn: &mut DbConnection, name: &str, project_id: i64)
.expect("Failed to create study")
}

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"))
.expect("Unable to parse exemple rolling stock");
rolling_stock_form.name = name.to_string();
rolling_stock_form
}

pub fn fast_rolling_stock_changeset(name: &str) -> Changeset<RollingStockModel> {
let mut rolling_stock_form: RollingStockForm = fast_rolling_stock_form(name);
rolling_stock_form.name = name.to_string();
let rolling_stock_model: Changeset<RollingStockModel> = rolling_stock_form.into();
rolling_stock_model.version(0)
}

pub async fn create_fast_rolling_stock(conn: &mut DbConnection, name: &str) -> RollingStockModel {
fast_rolling_stock_changeset(name)
.create(conn)
.await
.expect("Failed to create rolling stock")
}

pub fn rolling_stock_with_energy_sources_form(name: &str) -> RollingStockForm {
let mut rolling_stock_form: RollingStockForm = serde_json::from_str(include_str!(
"../tests/example_rolling_stock_2_energy_sources.json"
))
.expect("Unable to parse rolling stock with energy sources");
rolling_stock_form.name = name.to_string();
rolling_stock_form
}

pub fn rolling_stock_with_energy_sources_changeset(name: &str) -> Changeset<RollingStockModel> {
let rolling_stock_model: Changeset<RollingStockModel> =
rolling_stock_with_energy_sources_form(name).into();
rolling_stock_model.name(name.to_owned()).version(1)
}

pub async fn create_rolling_stock_with_energy_sources(
conn: &mut DbConnection,
name: &str,
) -> RollingStockModel {
rolling_stock_with_energy_sources_changeset(name)
.create(conn)
.await
.expect("Failed to create rolling stock with energy sources")
}

pub fn get_rolling_stock_with_invalid_effort_curves() -> &'static str {
include_str!("../tests/example_rolling_stock_3.json")
}

pub async fn create_empty_infra(conn: &mut DbConnection) -> Infra {
Infra::changeset()
.name("empty_infra".to_owned())
Expand Down
115 changes: 42 additions & 73 deletions editoast/src/modelsv2/rolling_stock_model.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod power_restrictions;
mod rolling_stock_usage;
pub use rolling_stock_usage::TrainScheduleScenarioStudyProject;

use std::collections::HashMap;

Expand All @@ -17,10 +18,8 @@ use editoast_schemas::rolling_stock::RollingStock;
use editoast_schemas::rolling_stock::RollingStockMetadata;
use editoast_schemas::rolling_stock::RollingStockSupportedSignalingSystems;
use power_restrictions::PowerRestriction;
pub use rolling_stock_usage::TrainScheduleScenarioStudyProject;
use serde::Deserialize;
use serde::Serialize;
use std::sync::Arc;
use utoipa::ToSchema;
use validator::Validate;
use validator::ValidationError;
Expand All @@ -29,7 +28,7 @@ use validator::ValidationErrors;
use crate::error::Result;
use crate::modelsv2::prelude::*;
use crate::modelsv2::rolling_stock_livery::RollingStockLiveryMetadataModel;
use crate::modelsv2::DbConnectionPool;
use crate::modelsv2::DbConnection;
use crate::views::rolling_stocks::RollingStockWithLiveries;

editoast_common::schemas! {
Expand Down Expand Up @@ -83,16 +82,12 @@ pub struct RollingStockModel {
}

impl RollingStockModel {
pub async fn with_liveries(
self,
db_pool: Arc<DbConnectionPool>,
) -> Result<RollingStockWithLiveries> {
pub async fn with_liveries(self, conn: &mut DbConnection) -> Result<RollingStockWithLiveries> {
use crate::tables::rolling_stock_livery::dsl as livery_dsl;
let mut conn = db_pool.get().await?;
let liveries = livery_dsl::rolling_stock_livery
.filter(livery_dsl::rolling_stock_id.eq(self.id))
.select(RollingStockLiveryMetadataModel::as_select())
.load(&mut conn)
.load(conn)
.await?;
Ok(RollingStockWithLiveries {
rolling_stock: self,
Expand Down Expand Up @@ -223,98 +218,72 @@ impl From<RollingStock> for RollingStockModelChangeset {
pub mod tests {
use rstest::*;
use serde_json::to_value;
use std::sync::Arc;
use std::ops::DerefMut;

use super::RollingStockModel;
use crate::error::InternalError;
use crate::fixtures::tests::db_pool;
use crate::fixtures::tests::get_other_rolling_stock_form;
use crate::fixtures::tests::named_fast_rolling_stock;
use crate::fixtures::tests::named_other_rolling_stock;
use crate::modelsv2::Changeset;
use crate::modelsv2::DbConnectionPool;
use crate::modelsv2::fixtures::create_fast_rolling_stock;
use crate::modelsv2::fixtures::create_rolling_stock_with_energy_sources;
use crate::modelsv2::fixtures::rolling_stock_with_energy_sources_changeset;
use crate::modelsv2::prelude::*;
use crate::modelsv2::DbConnectionPoolV2;
use crate::views::rolling_stocks::map_diesel_error;
use crate::views::rolling_stocks::RollingStockError;

pub fn get_invalid_effort_curves() -> &'static str {
include_str!("../tests/example_rolling_stock_3.json")
}

#[rstest]
async fn create_delete_rolling_stock(db_pool: Arc<DbConnectionPool>) {
use crate::modelsv2::Retrieve;
let mut db_conn = db_pool.get().await.expect("Failed to get db connection");
let name = "fast_rolling_stock_create_delete_rolling_stock";
let rolling_stock_id: i64;
{
let rolling_stock = named_fast_rolling_stock(name, db_pool.clone()).await;
rolling_stock_id = rolling_stock.id();
assert_eq!(name, rolling_stock.model.name.clone());
}

let rolling_stock = RollingStockModel::retrieve(&mut db_conn, rolling_stock_id)
.await
.unwrap();
async fn update_rolling_stock() {
let db_pool = DbConnectionPoolV2::for_tests();
let rs_name = "fast_rolling_stock_name";

assert!(rolling_stock.is_none());
}
let created_fast_rolling_stock =
create_fast_rolling_stock(db_pool.get_ok().deref_mut(), rs_name).await;

#[rstest]
async fn update_rolling_stock(db_pool: Arc<DbConnectionPool>) {
use crate::modelsv2::Update;
let mut db_conn = db_pool.get().await.expect("Failed to get db connection");
// GIVEN
let other_rs_name = "other_rolling_stock_update_rolling_stock";
let rolling_stock =
named_fast_rolling_stock("fast_rolling_stock_update_rolling_stock", db_pool.clone())
.await;
let rolling_stock_id = rolling_stock.id();
let rs_name_with_enrgy_sources_name = "other_rolling_stock_update_rolling_stock";
let rolling_stock_id = created_fast_rolling_stock.id;

let updated_rolling_stock: Changeset<RollingStockModel> =
get_other_rolling_stock_form(other_rs_name).into();
// updated_rolling_stock.id = rolling_stock_id;
let rolling_stock_with_energy_sources: Changeset<RollingStockModel> =
rolling_stock_with_energy_sources_changeset(rs_name_with_enrgy_sources_name);

// WHEN
let updated_rolling_stock = updated_rolling_stock
.update(&mut db_conn, rolling_stock_id)
let updated_rolling_stock = rolling_stock_with_energy_sources
.update(db_pool.get_ok().deref_mut(), rolling_stock_id)
.await
.unwrap()
.expect("Failed to update rolling stock")
.unwrap();

// THEN
assert_eq!(updated_rolling_stock.name, other_rs_name);
assert_eq!(updated_rolling_stock.name, rs_name_with_enrgy_sources_name);
}

#[rstest]
async fn update_rolling_stock_failure_name_already_used(db_pool: Arc<DbConnectionPool>) {
use crate::modelsv2::*;
let mut db_conn = db_pool.get().await.expect("Failed to get db connection");
async fn update_rolling_stock_failure_name_already_used() {
let db_pool = DbConnectionPoolV2::for_tests();

// GIVEN
let name = "fast_rolling_stock_update_rolling_stock_failure_name_already_used";
let _rolling_stock = named_fast_rolling_stock(name, db_pool.clone()).await;
let other_rolling_stock = named_other_rolling_stock(
"other_rolling_stock_update_rolling_stock_failure_name_already_used",
db_pool.clone(),
)
.await;
// Creating the first rolling stock
let rs_name = "fast_rolling_stock_name";
let created_fast_rolling_stock =
create_fast_rolling_stock(db_pool.get_ok().deref_mut(), rs_name).await;

let other_rolling_stock_id = other_rolling_stock.id();
let mut other_rolling_stock =
RollingStockModel::retrieve(&mut db_conn, other_rolling_stock_id)
.await
.unwrap()
.unwrap();
other_rolling_stock.name = name.to_string();
// Creating the second rolling stock
let rs_name_with_enrgy_sources_name = "fast_rolling_stock_with_energy_sources_name";
let created_fast_rolling_stock_with_energy_sources =
create_rolling_stock_with_energy_sources(
db_pool.get_ok().deref_mut(),
rs_name_with_enrgy_sources_name,
)
.await;

// WHEN
let result = other_rolling_stock
let result = created_fast_rolling_stock_with_energy_sources
.into_changeset()
.update(&mut db_conn, other_rolling_stock_id)
.update(db_pool.get_ok().deref_mut(), created_fast_rolling_stock.id)
.await
.map_err(|e| map_diesel_error(e, name));
.map_err(|e| map_diesel_error(e, rs_name));

let error: InternalError = RollingStockError::NameAlreadyUsed {
name: String::from(name),
name: String::from(rs_name),
}
.into();

Expand Down
1 change: 1 addition & 0 deletions editoast/src/views/projects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ pub mod test {
use actix_web::test::call_and_read_body_json;
use actix_web::test::call_service;
use actix_web::test::TestRequest;
use pretty_assertions::assert_eq;
use rstest::rstest;
use serde_json::json;

Expand Down
Loading

0 comments on commit e3c1574

Please sign in to comment.