Skip to content

Commit

Permalink
editoast: replace call_service by app.fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
hamz2a committed Jun 28, 2024
1 parent 0548624 commit f8e6fef
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 28 deletions.
31 changes: 13 additions & 18 deletions editoast/src/views/infra/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,6 @@ async fn get_objects(
#[cfg(test)]
mod tests {
use actix_web::http::StatusCode;
use actix_web::test::call_service;
use actix_web::test::read_body_json;
use actix_web::test::TestRequest;
use pretty_assertions::assert_eq;
use rstest::rstest;
Expand Down Expand Up @@ -144,14 +142,12 @@ mod tests {
let db_pool = app.db_pool();
let empty_infra = create_empty_infra(db_pool.get_ok().deref_mut()).await;

let req = TestRequest::post()
let request = TestRequest::post()
.uri(format!("/infra/{}/objects/TrackSection", empty_infra.id).as_str())
.set_json(vec![""; 0])
.to_request();
assert_eq!(
call_service(&app.service, req).await.status(),
StatusCode::OK
);

app.fetch(request).assert_status(StatusCode::OK);
}

#[rstest]
Expand All @@ -175,15 +171,14 @@ mod tests {
.expect("Failed to create switch object");

// WHEN
let req = TestRequest::post()
let request = TestRequest::post()
.uri(format!("/infra/{}/objects/Switch", empty_infra.id).as_str())
.set_json(vec!["switch_001"])
.to_request();
let response = call_service(&app.service, req).await;

// THEN
assert_eq!(response.status(), StatusCode::OK);
let switch_object: Vec<ObjectQueryable> = read_body_json(response).await;
let switch_object: Vec<ObjectQueryable> =
app.fetch(request).assert_status(StatusCode::OK).json_into();
let expected_switch_object = vec![ObjectQueryable {
obj_id: switch.get_id().to_string(),
railjson: json!({
Expand All @@ -206,12 +201,12 @@ mod tests {
let db_pool = app.db_pool();
let empty_infra = create_empty_infra(db_pool.get_ok().deref_mut()).await;

let req = TestRequest::post()
let request = TestRequest::post()
.uri(format!("/infra/{}/objects/TrackSection", empty_infra.id).as_str())
.set_json(vec!["id"; 2])
.to_request();
let response = call_service(&app.service, req).await;
assert_eq!(response.status(), StatusCode::BAD_REQUEST);

app.fetch(request).assert_status(StatusCode::BAD_REQUEST);
}

#[rstest]
Expand All @@ -230,13 +225,13 @@ mod tests {
.await
.expect("Failed to create switch type object");

let req = TestRequest::post()
let request = TestRequest::post()
.uri(format!("/infra/{}/objects/SwitchType", empty_infra.id).as_str())
.set_json(vec![switch_type.id.clone()])
.to_request();
let response = call_service(&app.service, req).await;
assert_eq!(response.status(), StatusCode::OK);
let switch_type_object: Vec<ObjectQueryable> = read_body_json(response).await;

let switch_type_object: Vec<ObjectQueryable> =
app.fetch(request).assert_status(StatusCode::OK).json_into();
let expected_switch_type_object = vec![ObjectQueryable {
obj_id: switch_type.get_id().to_string(),
railjson: json!({
Expand Down
19 changes: 9 additions & 10 deletions editoast/src/views/infra/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,6 @@ async fn get_routes_nodes(
mod tests {
use actix_http::StatusCode;
use actix_web::test::call_service;
use actix_web::test::read_body_json;
use actix_web::test::TestRequest;
use pretty_assertions::assert_eq;
use rstest::rstest;
Expand Down Expand Up @@ -463,9 +462,9 @@ mod tests {
let request = TestRequest::get()
.uri(format!("/infra/{empty_infra_id}/routes/{waypoint_type}/bs_stop").as_str())
.to_request();
let response = call_service(&app.service, request).await;
assert_eq!(response.status(), StatusCode::OK);
let routes: RoutesResponse = read_body_json(response).await;

let routes: RoutesResponse = app.fetch(request).assert_status(StatusCode::OK).json_into();

assert_eq!(
routes,
RoutesResponse {
Expand All @@ -479,9 +478,9 @@ mod tests {
let request = TestRequest::get()
.uri(format!("/infra/{empty_infra_id}/routes/{waypoint_type}/detector_001").as_str())
.to_request();
let response = call_service(&app.service, request).await;
assert_eq!(response.status(), StatusCode::OK);
let routes: RoutesResponse = read_body_json(response).await;

let routes: RoutesResponse = app.fetch(request).assert_status(StatusCode::OK).json_into();

assert_eq!(
routes,
RoutesResponse {
Expand All @@ -507,9 +506,9 @@ mod tests {
.as_str(),
)
.to_request();
let response = call_service(&app.service, request).await;
assert_eq!(response.status(), StatusCode::OK);
let routes: RoutesResponse = read_body_json(response).await;

let routes: RoutesResponse = app.fetch(request).assert_status(StatusCode::OK).json_into();

assert_eq!(
routes,
RoutesResponse {
Expand Down

0 comments on commit f8e6fef

Please sign in to comment.