Skip to content

Commit

Permalink
editoast: remove actix_web::web::Data from models
Browse files Browse the repository at this point in the history
  • Loading branch information
hamz2a committed May 15, 2024
1 parent 5b12cb9 commit b1fe188
Show file tree
Hide file tree
Showing 38 changed files with 280 additions and 220 deletions.
6 changes: 3 additions & 3 deletions editoast/editoast_derive/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ fn create_functions(config: &Config) -> TokenStream {
}

#[doc = #documentation]
async fn create(self, db_pool: actix_web::web::Data<crate::modelsv2::DbConnectionPool>) -> crate::error::Result<Self> {
async fn create(self, db_pool: std::sync::Arc<crate::modelsv2::DbConnectionPool>) -> crate::error::Result<Self> {
let mut conn = db_pool.get().await?;
self.create_conn(&mut conn).await
}
Expand Down Expand Up @@ -142,7 +142,7 @@ fn retrieve_functions(config: &Config) -> TokenStream {
}

#[doc = #retrieve_documentation]
async fn retrieve(db_pool: actix_web::web::Data<crate::modelsv2::DbConnectionPool>, id: i64) -> crate::error::Result<Option<Self>> {
async fn retrieve(db_pool: std::sync::Arc<crate::modelsv2::DbConnectionPool>, id: i64) -> crate::error::Result<Option<Self>> {
let mut conn = db_pool.get().await?;
Self::retrieve_conn(&mut conn, id).await
}
Expand Down Expand Up @@ -188,7 +188,7 @@ fn delete_functions(config: &Config) -> TokenStream {
}

#[doc = #documentation]
async fn delete(db_pool: actix_web::web::Data<crate::modelsv2::DbConnectionPool>, id: i64) -> crate::error::Result<bool> {
async fn delete(db_pool: std::sync::Arc<crate::modelsv2::DbConnectionPool>, id: i64) -> crate::error::Result<bool> {
let mut conn = db_pool.get().await?;
Self::delete_conn(&mut conn, id).await
}
Expand Down
54 changes: 27 additions & 27 deletions editoast/src/fixtures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
pub mod tests {
use std::io::Cursor;
use std::ops::{Deref, DerefMut};
use std::sync::Arc;

use crate::modelsv2::connection_pool::DbConnectionConfig;
use crate::modelsv2::connection_pool::DbConnectionPool;
Expand All @@ -25,7 +26,6 @@ pub mod tests {
},
};

use actix_web::web::Data;
use chrono::Utc;
use editoast_schemas::infra::ElectricalProfile;
use editoast_schemas::infra::ElectricalProfileSetData;
Expand All @@ -40,7 +40,7 @@ pub mod tests {

pub struct TestFixture<T: modelsv2::DeleteStatic<i64> + Identifiable + Send> {
pub model: T,
pub db_pool: Data<DbConnectionPool>,
pub db_pool: Arc<DbConnectionPool>,
pub infra: Option<Infra>,
}

Expand All @@ -57,7 +57,7 @@ pub mod tests {
self.model.get_id()
}

pub fn new(model: T, db_pool: Data<DbConnectionPool>) -> Self {
pub fn new(model: T, db_pool: Arc<DbConnectionPool>) -> Self {
TestFixture {
model,
db_pool,
Expand All @@ -67,7 +67,7 @@ pub mod tests {
}

impl<T: modelsv2::DeleteStatic<i64> + models::Create + Identifiable + Send> TestFixture<T> {
pub async fn create_legacy(model: T, db_pool: Data<DbConnectionPool>) -> Self {
pub async fn create_legacy(model: T, db_pool: Arc<DbConnectionPool>) -> Self {
TestFixture {
model: model.create(db_pool.clone()).await.unwrap(),
db_pool,
Expand All @@ -81,7 +81,7 @@ pub mod tests {
T: modelsv2::Model + modelsv2::DeleteStatic<i64> + Identifiable + Send,
T::Changeset: modelsv2::Create<T> + Send,
{
pub async fn create(cs: T::Changeset, db_pool: Data<DbConnectionPool>) -> Self {
pub async fn create(cs: T::Changeset, db_pool: Arc<DbConnectionPool>) -> Self {
use modelsv2::Create;
let conn = &mut db_pool.get().await.unwrap();
TestFixture {
Expand Down Expand Up @@ -111,7 +111,7 @@ pub mod tests {
}

pub trait IntoFixture: modelsv2::DeleteStatic<i64> + Identifiable + Send {
fn into_fixture(self, db_pool: Data<DbConnectionPool>) -> TestFixture<Self> {
fn into_fixture(self, db_pool: Arc<DbConnectionPool>) -> TestFixture<Self> {
TestFixture::new(self, db_pool)
}
}
Expand Down Expand Up @@ -139,13 +139,13 @@ pub mod tests {
}

#[fixture]
pub fn db_pool() -> Data<DbConnectionPool> {
pub fn db_pool() -> Arc<DbConnectionPool> {
let pg_config_url = PostgresConfig::default()
.url()
.expect("cannot get postgres config url");
let config = DbConnectionConfig::new(pg_config_url);
let pool = DbConnectionPool::builder(config).build().unwrap();
Data::new(pool)
Arc::new(pool)
}

pub fn get_fast_rolling_stock_form(name: &str) -> RollingStockForm {
Expand All @@ -166,7 +166,7 @@ pub mod tests {

pub async fn named_fast_rolling_stock(
name: &str,
db_pool: Data<DbConnectionPool>,
db_pool: Arc<DbConnectionPool>,
) -> TestFixture<RollingStockModel> {
let mut rs: Changeset<RollingStockModel> = get_fast_rolling_stock_form(name).into();
rs = rs.version(0);
Expand All @@ -188,15 +188,15 @@ pub mod tests {

pub async fn named_other_rolling_stock(
name: &str,
db_pool: Data<DbConnectionPool>,
db_pool: Arc<DbConnectionPool>,
) -> TestFixture<RollingStockModel> {
let mut rs: Changeset<RollingStockModel> = get_other_rolling_stock_form(name).into();
rs = rs.version(0);
TestFixture::create(rs, db_pool).await
}

async fn make_train_schedule(
db_pool: Data<DbConnectionPool>,
db_pool: Arc<DbConnectionPool>,
path_id: i64,
timetable_id: i64,
rolling_stock_id: i64,
Expand All @@ -221,7 +221,7 @@ pub mod tests {
#[fixture]
pub async fn train_schedule_v2(
#[future] timetable_v2: TestFixture<TimetableV2>,
db_pool: Data<DbConnectionPool>,
db_pool: Arc<DbConnectionPool>,
) -> TrainScheduleV2FixtureSet {
let timetable = timetable_v2.await;
let train_schedule = make_simple_train_schedule_v2(timetable.id(), db_pool).await;
Expand All @@ -234,7 +234,7 @@ pub mod tests {

pub async fn make_simple_train_schedule_v2(
timetable_id: i64,
db_pool: Data<DbConnectionPool>,
db_pool: Arc<DbConnectionPool>,
) -> TestFixture<TrainScheduleV2> {
let train_schedule_base: TrainScheduleBase =
serde_json::from_str(include_str!("./tests/train_schedules/simple.json"))
Expand Down Expand Up @@ -331,7 +331,7 @@ pub mod tests {

#[fixture]
pub async fn study_fixture_set(
db_pool: Data<DbConnectionPool>,
db_pool: Arc<DbConnectionPool>,
#[future] project: TestFixture<Project>,
) -> StudyFixtureSet {
let project = project.await;
Expand All @@ -351,7 +351,7 @@ pub mod tests {
}

#[fixture]
pub async fn project(db_pool: Data<DbConnectionPool>) -> TestFixture<Project> {
pub async fn project(db_pool: Arc<DbConnectionPool>) -> TestFixture<Project> {
let project_model = Project::changeset()
.name("test_project".to_owned())
.budget(Some(0))
Expand All @@ -362,7 +362,7 @@ pub mod tests {
}

#[fixture]
pub async fn timetable(db_pool: Data<DbConnectionPool>) -> TestFixture<Timetable> {
pub async fn timetable(db_pool: Arc<DbConnectionPool>) -> TestFixture<Timetable> {
let timetable_model = Timetable {
id: None,
name: Some(String::from("with_electrical_profiles")),
Expand All @@ -371,7 +371,7 @@ pub mod tests {
}

#[fixture]
pub async fn timetable_v2(db_pool: Data<DbConnectionPool>) -> TestFixture<TimetableV2> {
pub async fn timetable_v2(db_pool: Arc<DbConnectionPool>) -> TestFixture<TimetableV2> {
TestFixture::create(
TimetableV2::changeset().electrical_profile_set_id(None),
db_pool,
Expand All @@ -389,7 +389,7 @@ pub mod tests {

#[fixture]
pub async fn scenario_v2_fixture_set(
db_pool: Data<DbConnectionPool>,
db_pool: Arc<DbConnectionPool>,
#[future] timetable_v2: TestFixture<TimetableV2>,
#[future] project: TestFixture<Project>,
) -> ScenarioV2FixtureSet {
Expand All @@ -416,7 +416,7 @@ pub mod tests {
}

#[fixture]
pub async fn document_example(db_pool: Data<DbConnectionPool>) -> TestFixture<Document> {
pub async fn document_example(db_pool: Arc<DbConnectionPool>) -> TestFixture<Document> {
let img = image::open("src/tests/example_rolling_stock_image_1.gif").unwrap();
let mut img_bytes: Vec<u8> = Vec::new();
assert!(img
Expand All @@ -438,7 +438,7 @@ pub mod tests {

pub async fn rolling_stock_livery(
name: &str,
db_pool: Data<DbConnectionPool>,
db_pool: Arc<DbConnectionPool>,
) -> RollingStockLiveryFixture {
let mut rs_name = "fast_rolling_stock_".to_string();
rs_name.push_str(name);
Expand All @@ -459,7 +459,7 @@ pub mod tests {

#[fixture]
pub async fn electrical_profile_set(
db_pool: Data<DbConnectionPool>,
db_pool: Arc<DbConnectionPool>,
) -> TestFixture<ElectricalProfileSet> {
TestFixture::create(
serde_json::from_str::<Changeset<ElectricalProfileSet>>(include_str!(
Expand All @@ -473,7 +473,7 @@ pub mod tests {

#[fixture]
pub async fn dummy_electrical_profile_set(
db_pool: Data<DbConnectionPool>,
db_pool: Arc<DbConnectionPool>,
) -> TestFixture<ElectricalProfileSet> {
let ep_set_data = ElectricalProfileSetData {
levels: vec![ElectricalProfile {
Expand All @@ -490,7 +490,7 @@ pub mod tests {
}

#[fixture]
pub async fn empty_infra(db_pool: Data<DbConnectionPool>) -> TestFixture<Infra> {
pub async fn empty_infra(db_pool: Arc<DbConnectionPool>) -> TestFixture<Infra> {
TestFixture::create(
Infra::changeset()
.name("test_infra".to_owned())
Expand All @@ -500,7 +500,7 @@ pub mod tests {
.await
}

async fn make_small_infra(db_pool: Data<DbConnectionPool>) -> Infra {
async fn make_small_infra(db_pool: Arc<DbConnectionPool>) -> Infra {
let railjson: RailJson = serde_json::from_str(include_str!(
"../../tests/data/infras/small_infra/infra.json"
))
Expand All @@ -524,12 +524,12 @@ pub mod tests {
/// is made, the `editoast/tests/small_infra/small_infra.json` file should be updated
/// to the latest infra description.
#[fixture]
pub async fn small_infra(db_pool: Data<DbConnectionPool>) -> TestFixture<Infra> {
pub async fn small_infra(db_pool: Arc<DbConnectionPool>) -> TestFixture<Infra> {
TestFixture::new(make_small_infra(db_pool.clone()).await, db_pool)
}

#[fixture]
pub async fn pathfinding(db_pool: Data<DbConnectionPool>) -> TestFixture<Pathfinding> {
pub async fn pathfinding(db_pool: Arc<DbConnectionPool>) -> TestFixture<Pathfinding> {
let small_infra = make_small_infra(db_pool.clone()).await;
let pf_cs = PathfindingChangeset {
infra_id: Some(small_infra.id),
Expand Down Expand Up @@ -571,7 +571,7 @@ pub mod tests {

pub async fn train_with_simulation_output_fixture_set(
name: &str,
db_pool: Data<DbConnectionPool>,
db_pool: Arc<DbConnectionPool>,
) -> TrainScheduleWithSimulationOutputFixtureSet {
let ScenarioFixtureSet {
project,
Expand Down
5 changes: 3 additions & 2 deletions editoast/src/generated_data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use operational_point::OperationalPointLayer;
use psl_sign::PSLSignLayer;
use signal::SignalLayer;
use speed_section::SpeedSectionLayer;
use std::sync::Arc;
use switch::SwitchLayer;
use tracing::debug;
use track_section::TrackSectionLayer;
Expand Down Expand Up @@ -63,7 +64,7 @@ pub trait GeneratedData {
}

async fn refresh_pool(
pool: crate::Data<DbConnectionPool>,
pool: Arc<DbConnectionPool>,
infra: i64,
infra_cache: &InfraCache,
) -> Result<()> {
Expand All @@ -82,7 +83,7 @@ pub trait GeneratedData {

/// Refresh all the generated data of a given infra
pub async fn refresh_all(
db_pool: crate::Data<DbConnectionPool>,
db_pool: Arc<DbConnectionPool>,
infra: i64,
infra_cache: &InfraCache,
) -> Result<()> {
Expand Down
Loading

0 comments on commit b1fe188

Please sign in to comment.