Skip to content

Commit

Permalink
Merge pull request #7 from mocks-rs/0.3.6
Browse files Browse the repository at this point in the history
0.3.6
  • Loading branch information
codemountains authored Oct 9, 2024
2 parents 5073931 + c203db2 commit 2e3dc2f
Show file tree
Hide file tree
Showing 11 changed files with 139 additions and 88 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mocks"
version = "0.3.5"
version = "0.3.6"
edition = "2021"
authors = ["codemountains <codemountains@gmail.com>"]
description = "Get a mock REST APIs with zero coding within seconds."
Expand Down
1 change: 1 addition & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ impl fmt::Display for MocksError {
}
}
}

impl IntoResponse for MocksError {
fn into_response(self) -> Response {
let (status, message) = match self {
Expand Down
1 change: 1 addition & 0 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ impl Server {

println!("Endpoints:");
print_endpoints(url, &storage.data);
println!();

let state = AppState::new(storage);
let router = create_router(state);
Expand Down
87 changes: 1 addition & 86 deletions src/server/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> = 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<String> = 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<String> = 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<String> = 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());
}
}
16 changes: 16 additions & 0 deletions src/server/handler/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
24 changes: 24 additions & 0 deletions src/server/handler/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> = 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());
}
}
13 changes: 13 additions & 0 deletions src/server/handler/hc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
30 changes: 30 additions & 0 deletions src/server/handler/patch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> = Path("profile".to_string());
let payload = json!({"name":"Jane Smith","age":30});
assert!(patch_one(path, State(state), Payload(payload))
.await
.is_ok());
}
}
19 changes: 19 additions & 0 deletions src/server/handler/post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> = Path("posts".to_string());
let payload = json!({"id":"01J8593X0V7Q34X011BYD92CHP","title":"posted post","views":0});
assert!(post(path, State(state), PayloadWithId(payload))
.await
.is_ok());
}
}
32 changes: 32 additions & 0 deletions src/server/handler/put.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> = 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());
}
}

0 comments on commit 2e3dc2f

Please sign in to comment.