From 29963216f159f57767989e8a47e97c6a050135b6 Mon Sep 17 00:00:00 2001 From: azzamsa Date: Mon, 22 Jan 2024 14:55:01 +0700 Subject: [PATCH] refactor: add module prefix to external functions In adherence to Rust community conventions, it is recommended to prefix external functions with the crate/module name. This practice helps prevent confusion with locally defined functions. --- tests/health/tests.rs | 10 ++++---- tests/meta/tests.rs | 6 ++--- tests/user/create_user.rs | 6 ++--- tests/user/create_user_without_full_name.rs | 6 ++--- tests/user/delete_user.rs | 12 ++++----- tests/user/duplicate_username.rs | 18 ++++++------- tests/user/find_user.rs | 6 ++--- tests/user/keep_existing_full_name.rs | 10 ++++---- tests/user/relay.rs | 28 ++++++++++----------- tests/user/update_user.rs | 10 ++++---- 10 files changed, 56 insertions(+), 56 deletions(-) diff --git a/tests/health/tests.rs b/tests/health/tests.rs index 740eaba..3b5da43 100644 --- a/tests/health/tests.rs +++ b/tests/health/tests.rs @@ -6,7 +6,7 @@ use axum::{ }; use cynic::QueryBuilder; use http_body_util::BodyExt; // for `collect` -use serde_json::{from_slice, json, to_string, Value}; +use serde_json as json; use tin::route::app; use tower::util::ServiceExt; @@ -21,13 +21,13 @@ async fn health() -> Result<()> { .method(http::Method::POST) .header(http::header::CONTENT_TYPE, mime::APPLICATION_JSON.as_ref()) .uri("/graphql") - .body(Body::from(to_string(&query)?))?; + .body(Body::from(json::to_string(&query)?))?; let response = app.oneshot(request).await?; assert_eq!(response.status(), StatusCode::OK); let body = response.into_body().collect().await?.to_bytes(); - let health_response: HealthResponse = from_slice(&body)?; + let health_response: HealthResponse = json::from_slice(&body)?; assert_eq!(health_response.data.health.status, "running"); Ok(()) @@ -43,7 +43,7 @@ async fn health_restapi() -> Result<()> { assert_eq!(response.status(), StatusCode::OK); let body = response.into_body().collect().await?.to_bytes(); - let body: Value = serde_json::from_slice(&body)?; - assert_eq!(body, json!({ "data": { "status": "running" } })); + let body: json::Value = json::from_slice(&body)?; + assert_eq!(body, json::json!({ "data": { "status": "running" } })); Ok(()) } diff --git a/tests/meta/tests.rs b/tests/meta/tests.rs index a61a442..ee679f1 100644 --- a/tests/meta/tests.rs +++ b/tests/meta/tests.rs @@ -5,7 +5,7 @@ use axum::{ }; use cynic::QueryBuilder; use http_body_util::BodyExt; // for `collect` -use serde_json::{from_slice, to_string}; +use serde_json as json; use tin::route::app; use tower::util::ServiceExt; @@ -20,13 +20,13 @@ async fn meta() -> Result<()> { .method(http::Method::POST) .header(http::header::CONTENT_TYPE, mime::APPLICATION_JSON.as_ref()) .uri("/graphql") - .body(Body::from(to_string(&query)?))?; + .body(Body::from(json::to_string(&query)?))?; let response = app.oneshot(request).await?; assert_eq!(response.status(), StatusCode::OK); let body = response.into_body().collect().await?.to_bytes(); - let meta_response: MetaResponse = from_slice(&body)?; + let meta_response: MetaResponse = json::from_slice(&body)?; let cargo_package_version = env!("CARGO_PKG_VERSION").to_string(); assert_eq!(meta_response.data.meta.version, cargo_package_version); diff --git a/tests/user/create_user.rs b/tests/user/create_user.rs index efd3188..22395d5 100644 --- a/tests/user/create_user.rs +++ b/tests/user/create_user.rs @@ -5,7 +5,7 @@ use axum::{ }; use cynic::MutationBuilder; use http_body_util::BodyExt; // for `collect` -use serde_json::{from_slice, to_string}; +use serde_json as json; use tin::route::app; use tower::util::ServiceExt; @@ -22,13 +22,13 @@ async fn create_user() -> Result<()> { .method(http::Method::POST) .header(http::header::CONTENT_TYPE, mime::APPLICATION_JSON.as_ref()) .uri("/graphql") - .body(Body::from(to_string(&query)?))?; + .body(Body::from(json::to_string(&query)?))?; let response = app.oneshot(request).await?; assert_eq!(response.status(), StatusCode::OK); let body = response.into_body().collect().await?.to_bytes(); - let user_response: CreateUserResponse = from_slice(&body)?; + let user_response: CreateUserResponse = json::from_slice(&body)?; assert_eq!(user_response.data.create_user.name, "khawa"); teardown().await?; diff --git a/tests/user/create_user_without_full_name.rs b/tests/user/create_user_without_full_name.rs index 962a875..4898031 100644 --- a/tests/user/create_user_without_full_name.rs +++ b/tests/user/create_user_without_full_name.rs @@ -5,7 +5,7 @@ use axum::{ }; use cynic::MutationBuilder; use http_body_util::BodyExt; -use serde_json::{from_slice, to_string}; +use serde_json as json; use tin::route::app; use tower::util::ServiceExt; @@ -27,13 +27,13 @@ async fn create_user_without_full_name() -> Result<()> { .method(http::Method::POST) .header(http::header::CONTENT_TYPE, mime::APPLICATION_JSON.as_ref()) .uri("/graphql") - .body(Body::from(to_string(&query)?))?; + .body(Body::from(json::to_string(&query)?))?; let response = app.oneshot(request).await?; assert_eq!(response.status(), StatusCode::OK); let body = response.into_body().collect().await?.to_bytes(); - let user_response: CreateUserResponse = from_slice(&body)?; + let user_response: CreateUserResponse = json::from_slice(&body)?; assert_eq!(user_response.data.create_user.name, "khawa"); assert_eq!(user_response.data.create_user.full_name, None); diff --git a/tests/user/delete_user.rs b/tests/user/delete_user.rs index c6430c8..bc7f391 100644 --- a/tests/user/delete_user.rs +++ b/tests/user/delete_user.rs @@ -5,7 +5,7 @@ use axum::{ }; use cynic::{MutationBuilder, QueryBuilder}; use http_body_util::BodyExt; -use serde_json::{from_slice, to_string, Value}; +use serde_json as json; use tin::route::app; use tower::{util::ServiceExt, Service}; @@ -31,7 +31,7 @@ async fn delete_user() -> Result<()> { .method(http::Method::POST) .header(http::header::CONTENT_TYPE, mime::APPLICATION_JSON.as_ref()) .uri("/graphql") - .body(Body::from(to_string(&query)?))?; + .body(Body::from(json::to_string(&query)?))?; let response = ServiceExt::>::ready(&mut app) .await? @@ -40,7 +40,7 @@ async fn delete_user() -> Result<()> { assert_eq!(response.status(), StatusCode::OK); let body = response.into_body().collect().await?.to_bytes(); - let user_response: CreateUserResponse = from_slice(&body)?; + let user_response: CreateUserResponse = json::from_slice(&body)?; assert_eq!(user_response.data.create_user.name, "khawa"); let user_id = user_response.data.create_user.id; @@ -57,7 +57,7 @@ async fn delete_user() -> Result<()> { .method(http::Method::POST) .header(http::header::CONTENT_TYPE, mime::APPLICATION_JSON.as_ref()) .uri("/graphql") - .body(Body::from(to_string(&query)?))?; + .body(Body::from(json::to_string(&query)?))?; let _ = ServiceExt::>::ready(&mut app) .await? @@ -75,14 +75,14 @@ async fn delete_user() -> Result<()> { .method(http::Method::POST) .header(http::header::CONTENT_TYPE, mime::APPLICATION_JSON.as_ref()) .uri("/graphql") - .body(Body::from(to_string(&query)?))?; + .body(Body::from(json::to_string(&query)?))?; let response = ServiceExt::>::ready(&mut app) .await? .call(request) .await?; let body = response.into_body().collect().await?.to_bytes(); - let body: Value = from_slice(&body)?; + let body: json::Value = json::from_slice(&body)?; let error_message = &body["errors"][0]["message"]; assert_eq!(error_message, "user not found"); diff --git a/tests/user/duplicate_username.rs b/tests/user/duplicate_username.rs index 593e578..1e3ac32 100644 --- a/tests/user/duplicate_username.rs +++ b/tests/user/duplicate_username.rs @@ -5,7 +5,7 @@ use axum::{ }; use cynic::MutationBuilder; use http_body_util::BodyExt; -use serde_json::{from_slice, to_string, Value}; +use serde_json as json; use tin::route::app; use tower::{util::ServiceExt, Service}; @@ -24,7 +24,7 @@ async fn duplicate_username_create() -> Result<()> { .method(http::Method::POST) .header(http::header::CONTENT_TYPE, mime::APPLICATION_JSON.as_ref()) .uri("/graphql") - .body(Body::from(to_string(&query)?))?; + .body(Body::from(json::to_string(&query)?))?; let _ = ServiceExt::>::ready(&mut app) .await? @@ -40,14 +40,14 @@ async fn duplicate_username_create() -> Result<()> { .method(http::Method::POST) .header(http::header::CONTENT_TYPE, mime::APPLICATION_JSON.as_ref()) .uri("/graphql") - .body(Body::from(to_string(&query)?))?; + .body(Body::from(json::to_string(&query)?))?; let response = ServiceExt::>::ready(&mut app) .await? .call(request) .await?; let body = response.into_body().collect().await?.to_bytes(); - let body: Value = from_slice(&body)?; + let body: json::Value = json::from_slice(&body)?; let error_message = &body["errors"][0]["message"]; assert_eq!(error_message, "username is already in use"); @@ -68,7 +68,7 @@ async fn duplicate_username_update() -> Result<()> { .method(http::Method::POST) .header(http::header::CONTENT_TYPE, mime::APPLICATION_JSON.as_ref()) .uri("/graphql") - .body(Body::from(to_string(&query)?))?; + .body(Body::from(json::to_string(&query)?))?; let response = ServiceExt::>::ready(&mut app) .await? @@ -91,7 +91,7 @@ async fn duplicate_username_update() -> Result<()> { .method(http::Method::POST) .header(http::header::CONTENT_TYPE, mime::APPLICATION_JSON.as_ref()) .uri("/graphql") - .body(Body::from(to_string(&query)?))?; + .body(Body::from(json::to_string(&query)?))?; let response = ServiceExt::>::ready(&mut app) .await? @@ -100,7 +100,7 @@ async fn duplicate_username_update() -> Result<()> { assert_eq!(response.status(), StatusCode::OK); let body = response.into_body().collect().await?.to_bytes(); - let user_response: CreateUserResponse = from_slice(&body)?; + let user_response: CreateUserResponse = json::from_slice(&body)?; let user_id = user_response.data.create_user.id; // @@ -120,14 +120,14 @@ async fn duplicate_username_update() -> Result<()> { .method(http::Method::POST) .header(http::header::CONTENT_TYPE, mime::APPLICATION_JSON.as_ref()) .uri("/graphql") - .body(Body::from(to_string(&query)?))?; + .body(Body::from(json::to_string(&query)?))?; let response = ServiceExt::>::ready(&mut app) .await? .call(request) .await?; let body = response.into_body().collect().await?.to_bytes(); - let body: Value = from_slice(&body)?; + let body: json::Value = json::from_slice(&body)?; let error_message = &body["errors"][0]["message"]; assert_eq!(error_message, "username is already in use"); diff --git a/tests/user/find_user.rs b/tests/user/find_user.rs index 9d463a1..810d8ce 100644 --- a/tests/user/find_user.rs +++ b/tests/user/find_user.rs @@ -5,7 +5,7 @@ use axum::{ }; use cynic::QueryBuilder; use http_body_util::BodyExt; -use serde_json::{from_slice, to_string, Value}; +use serde_json as json; use tin::route::app; use tower::util::ServiceExt; @@ -23,13 +23,13 @@ async fn find_user() -> Result<()> { .method(http::Method::POST) .header(http::header::CONTENT_TYPE, mime::APPLICATION_JSON.as_ref()) .uri("/graphql") - .body(Body::from(to_string(&query)?))?; + .body(Body::from(json::to_string(&query)?))?; let response = app.oneshot(request).await?; assert_eq!(response.status(), StatusCode::OK); let body = response.into_body().collect().await?.to_bytes(); - let body: Value = from_slice(&body)?; + let body: json::Value = json::from_slice(&body)?; let error_message = &body["errors"][0]["message"]; assert_eq!(error_message, "user not found"); diff --git a/tests/user/keep_existing_full_name.rs b/tests/user/keep_existing_full_name.rs index 44f6ba2..cd52002 100644 --- a/tests/user/keep_existing_full_name.rs +++ b/tests/user/keep_existing_full_name.rs @@ -5,7 +5,7 @@ use axum::{ }; use cynic::MutationBuilder; use http_body_util::BodyExt; -use serde_json::{from_slice, to_string}; +use serde_json as json; use tin::route::app; use tower::{util::ServiceExt, Service}; @@ -30,14 +30,14 @@ async fn keep_existing_full_name() -> Result<()> { .method(http::Method::POST) .header(http::header::CONTENT_TYPE, mime::APPLICATION_JSON.as_ref()) .uri("/graphql") - .body(Body::from(to_string(&query)?))?; + .body(Body::from(json::to_string(&query)?))?; let response = ServiceExt::>::ready(&mut app) .await? .call(request) .await?; let body = response.into_body().collect().await?.to_bytes(); - let user_response: CreateUserResponse = from_slice(&body)?; + let user_response: CreateUserResponse = json::from_slice(&body)?; let user_id = user_response.data.create_user.id; // // Update Only the user name @@ -54,7 +54,7 @@ async fn keep_existing_full_name() -> Result<()> { .method(http::Method::POST) .header(http::header::CONTENT_TYPE, mime::APPLICATION_JSON.as_ref()) .uri("/graphql") - .body(Body::from(to_string(&query)?))?; + .body(Body::from(json::to_string(&query)?))?; let response = ServiceExt::>::ready(&mut app) .await? @@ -64,7 +64,7 @@ async fn keep_existing_full_name() -> Result<()> { // Make sure the full name preserved // let body = response.into_body().collect().await?.to_bytes(); - let user_response: UpdateUserResponse = from_slice(&body)?; + let user_response: UpdateUserResponse = json::from_slice(&body)?; assert_eq!(user_response.data.update_user.name, "khawa1"); assert_eq!( user_response.data.update_user.full_name, diff --git a/tests/user/relay.rs b/tests/user/relay.rs index 7ca340a..b832aae 100644 --- a/tests/user/relay.rs +++ b/tests/user/relay.rs @@ -5,7 +5,7 @@ use axum::{ }; use cynic::{MutationBuilder, QueryBuilder}; use http_body_util::BodyExt; -use serde_json::{from_slice, to_string, Value}; +use serde_json as json; use tin::route::app; use tower::{util::ServiceExt, Service}; @@ -33,7 +33,7 @@ async fn no_first_no_last() -> Result<()> { let request = Request::builder() .method(http::Method::POST) .uri("/graphql") - .body(Body::from(to_string(&query)?))?; + .body(Body::from(json::to_string(&query)?))?; let response = ServiceExt::>::ready(&mut app) .await? @@ -42,7 +42,7 @@ async fn no_first_no_last() -> Result<()> { assert_eq!(response.status(), StatusCode::OK); let body = response.into_body().collect().await?.to_bytes(); - let body: Value = from_slice(&body)?; + let body: json::Value = json::from_slice(&body)?; let error_message = &body["errors"][0]["message"]; assert_eq!( error_message, @@ -66,7 +66,7 @@ async fn both_first_and_last() -> Result<()> { let request = Request::builder() .method(http::Method::POST) .uri("/graphql") - .body(Body::from(to_string(&query)?))?; + .body(Body::from(json::to_string(&query)?))?; let response = ServiceExt::>::ready(&mut app) .await? @@ -75,7 +75,7 @@ async fn both_first_and_last() -> Result<()> { assert_eq!(response.status(), StatusCode::OK); let body = response.into_body().collect().await?.to_bytes(); - let body: Value = from_slice(&body)?; + let body: json::Value = json::from_slice(&body)?; let error_message = &body["errors"][0]["message"]; assert_eq!( error_message, @@ -99,7 +99,7 @@ async fn invalid_cursor() -> Result<()> { let request = Request::builder() .method(http::Method::POST) .uri("/graphql") - .body(Body::from(to_string(&query)?))?; + .body(Body::from(json::to_string(&query)?))?; let response = ServiceExt::>::ready(&mut app) .await? @@ -108,7 +108,7 @@ async fn invalid_cursor() -> Result<()> { assert_eq!(response.status(), StatusCode::OK); let body = response.into_body().collect().await?.to_bytes(); - let body: Value = from_slice(&body)?; + let body: json::Value = json::from_slice(&body)?; let error_message = &body["errors"][0]["message"]; assert_eq!(error_message, "Invalid cursor"); Ok(()) @@ -130,7 +130,7 @@ async fn create_users() -> Result<()> { .method(http::Method::POST) .header(http::header::CONTENT_TYPE, mime::APPLICATION_JSON.as_ref()) .uri("/graphql") - .body(Body::from(to_string(&query)?))?; + .body(Body::from(json::to_string(&query)?))?; let response = ServiceExt::>::ready(&mut app) .await? @@ -156,13 +156,13 @@ async fn find_paginated_user() -> Result<()> { let request = Request::builder() .method(http::Method::POST) .uri("/graphql") - .body(Body::from(to_string(&query)?))?; + .body(Body::from(json::to_string(&query)?))?; let response = app.call(request).await?; assert_eq!(response.status(), StatusCode::OK); let body = response.into_body().collect().await?.to_bytes(); - let users_response: UsersResponse = from_slice(&body)?; + let users_response: UsersResponse = json::from_slice(&body)?; assert_eq!(users_response.data.users.total_count, 6); // // first edges @@ -184,13 +184,13 @@ async fn find_paginated_user() -> Result<()> { let request = Request::builder() .method(http::Method::POST) .uri("/graphql") - .body(Body::from(to_string(&query)?))?; + .body(Body::from(json::to_string(&query)?))?; let response = app.call(request).await?; assert_eq!(response.status(), StatusCode::OK); let body = response.into_body().collect().await?.to_bytes(); - let users_response: UsersResponse = from_slice(&body)?; + let users_response: UsersResponse = json::from_slice(&body)?; assert_eq!(users_response.data.users.edges[0].node.name, "two"); let two_cursor = users_response.data.users.edges[0].cursor.clone(); @@ -207,13 +207,13 @@ async fn find_paginated_user() -> Result<()> { let request = Request::builder() .method(http::Method::POST) .uri("/graphql") - .body(Body::from(to_string(&query)?))?; + .body(Body::from(json::to_string(&query)?))?; let response = app.call(request).await?; assert_eq!(response.status(), StatusCode::OK); let body = response.into_body().collect().await?.to_bytes(); - let users_response: UsersResponse = from_slice(&body)?; + let users_response: UsersResponse = json::from_slice(&body)?; assert_eq!(users_response.data.users.edges[0].node.name, "one"); teardown().await?; diff --git a/tests/user/update_user.rs b/tests/user/update_user.rs index 1c79df0..4cc9db6 100644 --- a/tests/user/update_user.rs +++ b/tests/user/update_user.rs @@ -5,7 +5,7 @@ use axum::{ }; use cynic::MutationBuilder; use http_body_util::BodyExt; -use serde_json::{from_slice, to_string}; +use serde_json as json; use tin::route::app; use tower::{util::ServiceExt, Service}; @@ -28,7 +28,7 @@ async fn update_user() -> Result<()> { .method(http::Method::POST) .header(http::header::CONTENT_TYPE, mime::APPLICATION_JSON.as_ref()) .uri("/graphql") - .body(Body::from(to_string(&query)?))?; + .body(Body::from(json::to_string(&query)?))?; let response = ServiceExt::>::ready(&mut app) .await? @@ -37,7 +37,7 @@ async fn update_user() -> Result<()> { assert_eq!(response.status(), StatusCode::OK); let body = response.into_body().collect().await?.to_bytes(); - let user_response: CreateUserResponse = from_slice(&body)?; + let user_response: CreateUserResponse = json::from_slice(&body)?; assert_eq!(user_response.data.create_user.name, "khawa"); let user_id = user_response.data.create_user.id; @@ -59,14 +59,14 @@ async fn update_user() -> Result<()> { .method(http::Method::POST) .header(http::header::CONTENT_TYPE, mime::APPLICATION_JSON.as_ref()) .uri("/graphql") - .body(Body::from(to_string(&query)?))?; + .body(Body::from(json::to_string(&query)?))?; let response = ServiceExt::>::ready(&mut app) .await? .call(request) .await?; let body = response.into_body().collect().await?.to_bytes(); - let user_response: UpdateUserResponse = from_slice(&body)?; + let user_response: UpdateUserResponse = json::from_slice(&body)?; assert_eq!(user_response.data.update_user.name, "haitham");