From 9d0c0b2ae47187dbdc8113e614d34b5de5af9371 Mon Sep 17 00:00:00 2001 From: codemountains <4kz12zz@gmail.com> Date: Wed, 9 Oct 2024 21:45:23 +0900 Subject: [PATCH 1/5] :adhesive_bandage: Add line break #6 --- src/error.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/error.rs b/src/error.rs index d1ab531..112ae0a 100644 --- a/src/error.rs +++ b/src/error.rs @@ -40,6 +40,7 @@ impl fmt::Display for MocksError { } } } + impl IntoResponse for MocksError { fn into_response(self) -> Response { let (status, message) = match self { From d97c401bbc36d7c1f84834e8faa0e1995919a6b2 Mon Sep 17 00:00:00 2001 From: codemountains <4kz12zz@gmail.com> Date: Wed, 9 Oct 2024 21:47:51 +0900 Subject: [PATCH 2/5] :sparkles: Add println --- src/server.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/server.rs b/src/server.rs index 3e294aa..3afcd5e 100644 --- a/src/server.rs +++ b/src/server.rs @@ -41,6 +41,7 @@ impl Server { println!("Endpoints:"); print_endpoints(url, &storage.data); + println!(); let state = AppState::new(storage); let router = create_router(state); From b8e2d40af4a44071a539cb4f013858c7a74785f1 Mon Sep 17 00:00:00 2001 From: codemountains <4kz12zz@gmail.com> Date: Wed, 9 Oct 2024 22:06:25 +0900 Subject: [PATCH 3/5] :recycle: Relocate tests --- src/server/handler.rs | 87 +----------------------------------- src/server/handler/delete.rs | 16 +++++++ src/server/handler/get.rs | 24 ++++++++++ src/server/handler/patch.rs | 30 +++++++++++++ src/server/handler/post.rs | 19 ++++++++ src/server/handler/put.rs | 32 +++++++++++++ 6 files changed, 122 insertions(+), 86 deletions(-) diff --git a/src/server/handler.rs b/src/server/handler.rs index 0bdbf39..e1a73a7 100644 --- a/src/server/handler.rs +++ b/src/server/handler.rs @@ -7,98 +7,13 @@ pub mod put; #[cfg(test)] mod tests { - use super::super::*; - use crate::server::context::{Payload, PayloadWithId}; use crate::server::state::AppState; use crate::server::state::SharedState; use crate::storage::Storage; - use axum::extract::{Path, State}; - use serde_json::json; - fn init_state() -> SharedState { + pub(crate) fn init_state() -> SharedState { let storage = Storage::new("storage.json", false) .unwrap_or_else(|e| panic!("Failed to init storage: {}", e)); AppState::new(storage) } - - #[tokio::test] - async fn test_get_all() { - let state = init_state(); - let path: Path = Path("posts".to_string()); - assert!(get_all(path, State(state)).await.is_ok()); - } - - #[tokio::test] - async fn test_get_one() { - let state = init_state(); - let path: Path<(String, String)> = Path(( - "posts".to_string(), - "01J7BAKH37HPG116ZRRFKHBDGB".to_string(), - )); - assert!(get_one(path, State(state)).await.is_ok()); - } - - #[tokio::test] - async fn test_post() { - let state = init_state(); - let path: Path = Path("posts".to_string()); - let payload = json!({"id":"01J8593X0V7Q34X011BYD92CHP","title":"posted post","views":0}); - assert!(post(path, State(state), PayloadWithId(payload)) - .await - .is_ok()); - } - - #[tokio::test] - async fn test_put() { - let state = init_state(); - let path: Path<(String, String)> = Path(( - "posts".to_string(), - "01J7BAKH37HPG116ZRRFKHBDGB".to_string(), - )); - let payload = json!({"id":"01J7BAKH37HPG116ZRRFKHBDGB","title":"putted post","views":200}); - assert!(put(path, State(state), PayloadWithId(payload)) - .await - .is_ok()); - } - - #[tokio::test] - async fn test_put_one() { - let state = init_state(); - let path: Path = Path("profile".to_string()); - let payload = json!({"id":1,"name":"John Smith","age":25}); - assert!(put_one(path, State(state), PayloadWithId(payload)) - .await - .is_ok()); - } - - #[tokio::test] - async fn test_patch() { - let state = init_state(); - let path: Path<(String, String)> = Path(( - "posts".to_string(), - "01J7BAKH37HPG116ZRRFKHBDGB".to_string(), - )); - let payload = json!({"title":"patched post","views":200}); - assert!(patch(path, State(state), Payload(payload)).await.is_ok()); - } - - #[tokio::test] - async fn test_patch_one() { - let state = init_state(); - let path: Path = Path("profile".to_string()); - let payload = json!({"name":"Jane Smith","age":30}); - assert!(patch_one(path, State(state), Payload(payload)) - .await - .is_ok()); - } - - #[tokio::test] - async fn test_delete() { - let state = init_state(); - let path: Path<(String, String)> = Path(( - "posts".to_string(), - "01J7BAKH37HPG116ZRRFKHBDGB".to_string(), - )); - assert!(delete(path, State(state)).await.is_ok()); - } } diff --git a/src/server/handler/delete.rs b/src/server/handler/delete.rs index 905d824..cb37737 100644 --- a/src/server/handler/delete.rs +++ b/src/server/handler/delete.rs @@ -16,3 +16,19 @@ pub async fn delete( let value = state.storage.delete(&resource, &id)?; Ok((StatusCode::OK, Json(value))) } + +#[cfg(test)] +mod tests { + use crate::server::handler::delete::delete; + use axum::extract::{Path, State}; + + #[tokio::test] + async fn test_delete() { + let state = crate::server::handler::tests::init_state(); + let path: Path<(String, String)> = Path(( + "posts".to_string(), + "01J7BAKH37HPG116ZRRFKHBDGB".to_string(), + )); + assert!(delete(path, State(state)).await.is_ok()); + } +} diff --git a/src/server/handler/get.rs b/src/server/handler/get.rs index 6962761..97aaa05 100644 --- a/src/server/handler/get.rs +++ b/src/server/handler/get.rs @@ -33,3 +33,27 @@ pub async fn get_one( let value = state.storage.get_one(&resource, &id)?; Ok((StatusCode::OK, Json(value))) } + +#[cfg(test)] +mod tests { + use crate::server::handler::get::{get_all, get_one}; + use crate::server::handler::tests::init_state; + use axum::extract::{Path, State}; + + #[tokio::test] + async fn test_get_all() { + let state = init_state(); + let path: Path = Path("posts".to_string()); + assert!(get_all(path, State(state)).await.is_ok()); + } + + #[tokio::test] + async fn test_get_one() { + let state = init_state(); + let path: Path<(String, String)> = Path(( + "posts".to_string(), + "01J7BAKH37HPG116ZRRFKHBDGB".to_string(), + )); + assert!(get_one(path, State(state)).await.is_ok()); + } +} diff --git a/src/server/handler/patch.rs b/src/server/handler/patch.rs index 93ce115..4a8e2be 100644 --- a/src/server/handler/patch.rs +++ b/src/server/handler/patch.rs @@ -31,3 +31,33 @@ pub async fn patch_one( let value = state.storage.update_one(&resource, &input)?; Ok((StatusCode::OK, Json(value))) } + +#[cfg(test)] +mod tests { + use crate::server::context::Payload; + use crate::server::handler::patch::{patch, patch_one}; + use crate::server::handler::tests::init_state; + use axum::extract::{Path, State}; + use serde_json::json; + + #[tokio::test] + async fn test_patch() { + let state = init_state(); + let path: Path<(String, String)> = Path(( + "posts".to_string(), + "01J7BAKH37HPG116ZRRFKHBDGB".to_string(), + )); + let payload = json!({"title":"patched post","views":200}); + assert!(patch(path, State(state), Payload(payload)).await.is_ok()); + } + + #[tokio::test] + async fn test_patch_one() { + let state = init_state(); + let path: Path = Path("profile".to_string()); + let payload = json!({"name":"Jane Smith","age":30}); + assert!(patch_one(path, State(state), Payload(payload)) + .await + .is_ok()); + } +} diff --git a/src/server/handler/post.rs b/src/server/handler/post.rs index 79abf14..5f9b434 100644 --- a/src/server/handler/post.rs +++ b/src/server/handler/post.rs @@ -18,3 +18,22 @@ pub async fn post( let value = state.storage.insert(&resource, &input)?; Ok((StatusCode::CREATED, Json(value))) } + +#[cfg(test)] +mod tests { + use crate::server::context::PayloadWithId; + use crate::server::handler::post::post; + use crate::server::handler::tests::init_state; + use axum::extract::{Path, State}; + use serde_json::json; + + #[tokio::test] + async fn test_post() { + let state = init_state(); + let path: Path = Path("posts".to_string()); + let payload = json!({"id":"01J8593X0V7Q34X011BYD92CHP","title":"posted post","views":0}); + assert!(post(path, State(state), PayloadWithId(payload)) + .await + .is_ok()); + } +} diff --git a/src/server/handler/put.rs b/src/server/handler/put.rs index 93d6676..0dcd7dd 100644 --- a/src/server/handler/put.rs +++ b/src/server/handler/put.rs @@ -31,3 +31,35 @@ pub async fn put_one( let value = state.storage.replace_one(&resource, &input)?; Ok((StatusCode::OK, Json(value))) } + +#[cfg(test)] +mod tests { + use crate::server::context::PayloadWithId; + use crate::server::handler::put::{put, put_one}; + use crate::server::handler::tests::init_state; + use axum::extract::{Path, State}; + use serde_json::json; + + #[tokio::test] + async fn test_put() { + let state = init_state(); + let path: Path<(String, String)> = Path(( + "posts".to_string(), + "01J7BAKH37HPG116ZRRFKHBDGB".to_string(), + )); + let payload = json!({"id":"01J7BAKH37HPG116ZRRFKHBDGB","title":"putted post","views":200}); + assert!(put(path, State(state), PayloadWithId(payload)) + .await + .is_ok()); + } + + #[tokio::test] + async fn test_put_one() { + let state = init_state(); + let path: Path = Path("profile".to_string()); + let payload = json!({"id":1,"name":"John Smith","age":25}); + assert!(put_one(path, State(state), PayloadWithId(payload)) + .await + .is_ok()); + } +} From 0b0f9f319cfebc78c7ac2d3218031dfad2cc0b92 Mon Sep 17 00:00:00 2001 From: codemountains <4kz12zz@gmail.com> Date: Wed, 9 Oct 2024 22:06:58 +0900 Subject: [PATCH 4/5] :white_check_mark: Add hc test --- src/server/handler/hc.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/server/handler/hc.rs b/src/server/handler/hc.rs index a8ee8b1..a216053 100644 --- a/src/server/handler/hc.rs +++ b/src/server/handler/hc.rs @@ -4,3 +4,16 @@ use axum::response::IntoResponse; pub async fn hc() -> impl IntoResponse { StatusCode::NO_CONTENT } + +#[cfg(test)] +mod tests { + use crate::server::handler::hc::hc; + use axum::http::StatusCode; + use axum::response::IntoResponse; + + #[tokio::test] + async fn test_hc() { + let resp = hc().await.into_response(); + assert_eq!(resp.status(), StatusCode::NO_CONTENT); + } +} From c203db2b7d518a1f58e2fae71169c6dc90a0794e Mon Sep 17 00:00:00 2001 From: codemountains <4kz12zz@gmail.com> Date: Wed, 9 Oct 2024 22:12:33 +0900 Subject: [PATCH 5/5] :bookmark: 0.3.6 --- Cargo.lock | 2 +- Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0fc0c45..658853c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -438,7 +438,7 @@ dependencies = [ [[package]] name = "mocks" -version = "0.3.5" +version = "0.3.6" dependencies = [ "axum", "clap", diff --git a/Cargo.toml b/Cargo.toml index c5fd36f..787997e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mocks" -version = "0.3.5" +version = "0.3.6" edition = "2021" authors = ["codemountains "] description = "Get a mock REST APIs with zero coding within seconds."