Skip to content

Commit a9e211c

Browse files
committed
feat: enhance tests for improved deserialization
1 parent 7c6ea71 commit a9e211c

File tree

4 files changed

+18
-11
lines changed

4 files changed

+18
-11
lines changed

src/om/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub struct CreateUserParams {
1313
pub confirm_password: String,
1414
}
1515

16-
#[derive(Serialize, ToSchema)]
16+
#[derive(Serialize, ToSchema, Deserialize)]
1717
pub struct UserCreated {
1818
pub id: i32,
1919
}

tests/test_ext/mod.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
use http_body_util::BodyExt;
2+
use serde::de::DeserializeOwned;
23

34
pub trait IntoValue {
4-
fn into_value(self) -> impl Future<Output = serde_json::Value>;
5+
fn into_value<T>(self) -> impl Future<Output = T>
6+
where
7+
T: DeserializeOwned;
58
}
69

710
impl IntoValue for axum::http::Response<axum::body::Body> {
8-
async fn into_value(self) -> serde_json::Value {
11+
async fn into_value<T>(self) -> T
12+
where
13+
T: DeserializeOwned,
14+
{
915
let collected = self.into_body().collect().await.unwrap();
1016
let response_in_bytes = collected.to_bytes();
1117

tests/users/create.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
use crate::setup::TestContext;
1+
use crate::{setup::TestContext, test_ext::IntoValue};
22
use axum::{
33
body::Body,
44
http::{self, Request, StatusCode},
55
};
6-
use http_body_util::BodyExt;
6+
use f5a_services::om::UserCreated;
77
use sea_orm::EntityTrait;
88
use serde_json::json;
99
use tower::ServiceExt;
@@ -80,8 +80,7 @@ async fn it_validate_required_user_params() {
8080
}
8181
],
8282
});
83-
let body_bytes = res.into_body().collect().await.unwrap().to_bytes();
84-
let body_content: serde_json::Value = serde_json::from_slice(&body_bytes).unwrap();
83+
let body_content = res.into_value::<serde_json::Value>().await;
8584

8685
assert_eq!(body_content, expected_body);
8786
}
@@ -111,14 +110,16 @@ async fn it_accepts_and_save_valid_user() {
111110
let res = app.oneshot(req).await.unwrap();
112111
assert_eq!(res.status(), StatusCode::OK);
113112

114-
let user_saved = schemas::user::Entity::find_by_id(1)
113+
let user_created = res.into_value::<UserCreated>().await;
114+
115+
let user_saved = schemas::user::Entity::find_by_id(user_created.id)
115116
.one(ctx.db.as_ref())
116117
.await
117118
.unwrap();
118119
assert!(user_saved.is_some());
119120

120121
let user_model = user_saved.unwrap();
121-
assert_eq!(user_model.id, 1);
122+
assert_eq!(user_model.id, user_created.id);
122123
assert_eq!(user_model.username, "idesoft");
123124
assert_eq!(user_model.creator_id, 1);
124125
assert!(user_model.disabled.is_positive());

tests/users/update.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use axum::{
33
http::{self, Request, StatusCode},
44
};
55
use sea_orm::EntityTrait;
6-
use serde_json::json;
6+
use serde_json::{Value, json};
77
use tower::ServiceExt;
88

99
use crate::{setup::TestContext, test_ext::IntoValue, users::migrations::insert_idesoft_user};
@@ -55,7 +55,7 @@ async fn it_validate_required_user_fields_to_update() {
5555
],
5656
"detail": "Validation failed"
5757
});
58-
assert_eq!(res.into_value().await, expected_body)
58+
assert_eq!(res.into_value::<Value>().await, expected_body)
5959
}
6060

6161
#[tokio::test]

0 commit comments

Comments
 (0)