Skip to content

Commit

Permalink
editoast: replace DbConnectionPool by V2 in views::infra::lines
Browse files Browse the repository at this point in the history
  • Loading branch information
hamz2a committed Jun 25, 2024
1 parent 9ac8715 commit d0d1086
Showing 1 changed file with 23 additions and 33 deletions.
56 changes: 23 additions & 33 deletions editoast/src/views/infra/lines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::error::Result;
use crate::infra_cache::InfraCache;
use crate::infra_cache::ObjectCache;
use crate::modelsv2::prelude::*;
use crate::modelsv2::DbConnectionPool;
use crate::modelsv2::DbConnectionPoolV2;
use crate::modelsv2::Infra;
use crate::views::infra::InfraApiError;
use crate::views::infra::InfraIdParam;
Expand Down Expand Up @@ -44,7 +44,7 @@ enum LinesErrors {
async fn get_line_bbox(
path: Path<(i64, i64)>,
infra_caches: Data<CHashMap<i64, InfraCache>>,
db_pool: Data<DbConnectionPool>,
db_pool: Data<DbConnectionPoolV2>,
) -> Result<Json<BoundingBox>> {
let (infra_id, line_code) = path.into_inner();
let line_code: i32 = line_code.try_into().unwrap();
Expand Down Expand Up @@ -73,34 +73,28 @@ async fn get_line_bbox(
#[cfg(test)]
mod test {
use actix_web::http::StatusCode;
use actix_web::test::call_service;
use actix_web::test::read_body_json;
use actix_web::test::TestRequest;
use editoast_schemas::infra::TrackSectionSncfExtension;
use editoast_schemas::primitives::Identifier;
use geos::geojson::Geometry;
use pretty_assertions::assert_eq;
use rstest::rstest;
use serde_json::json;
use std::ops::DerefMut;
use std::str::FromStr;

use super::*;
use crate::fixtures::tests::db_pool;
use crate::fixtures::tests::empty_infra;
use crate::fixtures::tests::TestFixture;
use crate::infra_cache::operation::create::apply_create_operation;
use crate::modelsv2::fixtures::create_empty_infra;
use crate::views::test_app::TestAppBuilder;
use editoast_schemas::infra::TrackSection;
use editoast_schemas::infra::TrackSectionExtensions;
// use crate::modelsv2::fixtures::create_empty_infra;
// use crate::views::test_app::TestAppBuilder;
use crate::views::tests::create_test_service;
use editoast_schemas::primitives::BoundingBox;

#[rstest]
async fn returns_correct_bbox_for_existing_line_code(
#[future] empty_infra: TestFixture<Infra>,
) {
let empty_infra = empty_infra.await;
let app = create_test_service().await;
async fn returns_correct_bbox_for_existing_line_code() {
let app = TestAppBuilder::default_app();
let db_pool = app.db_pool();
let empty_infra = create_empty_infra(db_pool.get_ok().deref_mut()).await;

let line_code = 1234;
let geometry_json = json!({
Expand All @@ -121,29 +115,26 @@ mod test {
..Default::default()
}
.into();
apply_create_operation(
&track_section,
empty_infra.id,
&mut db_pool().get().await.unwrap(),
)
.await
.expect("Failed to create track section object");
apply_create_operation(&track_section, empty_infra.id, db_pool.get_ok().deref_mut())
.await
.expect("Failed to create track section object");

let req = TestRequest::get()
let request = TestRequest::get()
.uri(format!("/infra/{}/lines/{line_code}/bbox/", empty_infra.id).as_str())
.to_request();
let response = call_service(&app, req).await;
assert_eq!(response.status(), StatusCode::OK);
let bounding_box: BoundingBox = read_body_json(response).await;
let bounding_box: BoundingBox =
app.fetch(request).assert_status(StatusCode::OK).json_into();
assert_eq!(bounding_box, BoundingBox((1., 2.), (3., 4.)));
}

#[rstest]
async fn returns_400_when_line_code_not_found(#[future] empty_infra: TestFixture<Infra>) {
let empty_infra = empty_infra.await;
let app = create_test_service().await;
async fn returns_bad_request_when_line_code_not_found() {
let app = TestAppBuilder::default_app();
let db_pool = app.db_pool();
let empty_infra = create_empty_infra(db_pool.get_ok().deref_mut()).await;

let not_existing_line_code = 123456789;
let req = TestRequest::get()
let request = TestRequest::get()
.uri(
format!(
"/infra/{}/lines/{not_existing_line_code}/bbox/",
Expand All @@ -152,7 +143,6 @@ mod test {
.as_str(),
)
.to_request();
let response = call_service(&app, req).await;
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
app.fetch(request).assert_status(StatusCode::BAD_REQUEST);
}
}

0 comments on commit d0d1086

Please sign in to comment.