Skip to content

Commit 2e3dc2f

Browse files
Merge pull request #7 from mocks-rs/0.3.6
0.3.6
2 parents 5073931 + c203db2 commit 2e3dc2f

File tree

11 files changed

+139
-88
lines changed

11 files changed

+139
-88
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "mocks"
3-
version = "0.3.5"
3+
version = "0.3.6"
44
edition = "2021"
55
authors = ["codemountains <codemountains@gmail.com>"]
66
description = "Get a mock REST APIs with zero coding within seconds."

src/error.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ impl fmt::Display for MocksError {
4040
}
4141
}
4242
}
43+
4344
impl IntoResponse for MocksError {
4445
fn into_response(self) -> Response {
4546
let (status, message) = match self {

src/server.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ impl Server {
4141

4242
println!("Endpoints:");
4343
print_endpoints(url, &storage.data);
44+
println!();
4445

4546
let state = AppState::new(storage);
4647
let router = create_router(state);

src/server/handler.rs

Lines changed: 1 addition & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -7,98 +7,13 @@ pub mod put;
77

88
#[cfg(test)]
99
mod tests {
10-
use super::super::*;
11-
use crate::server::context::{Payload, PayloadWithId};
1210
use crate::server::state::AppState;
1311
use crate::server::state::SharedState;
1412
use crate::storage::Storage;
15-
use axum::extract::{Path, State};
16-
use serde_json::json;
1713

18-
fn init_state() -> SharedState {
14+
pub(crate) fn init_state() -> SharedState {
1915
let storage = Storage::new("storage.json", false)
2016
.unwrap_or_else(|e| panic!("Failed to init storage: {}", e));
2117
AppState::new(storage)
2218
}
23-
24-
#[tokio::test]
25-
async fn test_get_all() {
26-
let state = init_state();
27-
let path: Path<String> = Path("posts".to_string());
28-
assert!(get_all(path, State(state)).await.is_ok());
29-
}
30-
31-
#[tokio::test]
32-
async fn test_get_one() {
33-
let state = init_state();
34-
let path: Path<(String, String)> = Path((
35-
"posts".to_string(),
36-
"01J7BAKH37HPG116ZRRFKHBDGB".to_string(),
37-
));
38-
assert!(get_one(path, State(state)).await.is_ok());
39-
}
40-
41-
#[tokio::test]
42-
async fn test_post() {
43-
let state = init_state();
44-
let path: Path<String> = Path("posts".to_string());
45-
let payload = json!({"id":"01J8593X0V7Q34X011BYD92CHP","title":"posted post","views":0});
46-
assert!(post(path, State(state), PayloadWithId(payload))
47-
.await
48-
.is_ok());
49-
}
50-
51-
#[tokio::test]
52-
async fn test_put() {
53-
let state = init_state();
54-
let path: Path<(String, String)> = Path((
55-
"posts".to_string(),
56-
"01J7BAKH37HPG116ZRRFKHBDGB".to_string(),
57-
));
58-
let payload = json!({"id":"01J7BAKH37HPG116ZRRFKHBDGB","title":"putted post","views":200});
59-
assert!(put(path, State(state), PayloadWithId(payload))
60-
.await
61-
.is_ok());
62-
}
63-
64-
#[tokio::test]
65-
async fn test_put_one() {
66-
let state = init_state();
67-
let path: Path<String> = Path("profile".to_string());
68-
let payload = json!({"id":1,"name":"John Smith","age":25});
69-
assert!(put_one(path, State(state), PayloadWithId(payload))
70-
.await
71-
.is_ok());
72-
}
73-
74-
#[tokio::test]
75-
async fn test_patch() {
76-
let state = init_state();
77-
let path: Path<(String, String)> = Path((
78-
"posts".to_string(),
79-
"01J7BAKH37HPG116ZRRFKHBDGB".to_string(),
80-
));
81-
let payload = json!({"title":"patched post","views":200});
82-
assert!(patch(path, State(state), Payload(payload)).await.is_ok());
83-
}
84-
85-
#[tokio::test]
86-
async fn test_patch_one() {
87-
let state = init_state();
88-
let path: Path<String> = Path("profile".to_string());
89-
let payload = json!({"name":"Jane Smith","age":30});
90-
assert!(patch_one(path, State(state), Payload(payload))
91-
.await
92-
.is_ok());
93-
}
94-
95-
#[tokio::test]
96-
async fn test_delete() {
97-
let state = init_state();
98-
let path: Path<(String, String)> = Path((
99-
"posts".to_string(),
100-
"01J7BAKH37HPG116ZRRFKHBDGB".to_string(),
101-
));
102-
assert!(delete(path, State(state)).await.is_ok());
103-
}
10419
}

src/server/handler/delete.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,19 @@ pub async fn delete(
1616
let value = state.storage.delete(&resource, &id)?;
1717
Ok((StatusCode::OK, Json(value)))
1818
}
19+
20+
#[cfg(test)]
21+
mod tests {
22+
use crate::server::handler::delete::delete;
23+
use axum::extract::{Path, State};
24+
25+
#[tokio::test]
26+
async fn test_delete() {
27+
let state = crate::server::handler::tests::init_state();
28+
let path: Path<(String, String)> = Path((
29+
"posts".to_string(),
30+
"01J7BAKH37HPG116ZRRFKHBDGB".to_string(),
31+
));
32+
assert!(delete(path, State(state)).await.is_ok());
33+
}
34+
}

src/server/handler/get.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,27 @@ pub async fn get_one(
3333
let value = state.storage.get_one(&resource, &id)?;
3434
Ok((StatusCode::OK, Json(value)))
3535
}
36+
37+
#[cfg(test)]
38+
mod tests {
39+
use crate::server::handler::get::{get_all, get_one};
40+
use crate::server::handler::tests::init_state;
41+
use axum::extract::{Path, State};
42+
43+
#[tokio::test]
44+
async fn test_get_all() {
45+
let state = init_state();
46+
let path: Path<String> = Path("posts".to_string());
47+
assert!(get_all(path, State(state)).await.is_ok());
48+
}
49+
50+
#[tokio::test]
51+
async fn test_get_one() {
52+
let state = init_state();
53+
let path: Path<(String, String)> = Path((
54+
"posts".to_string(),
55+
"01J7BAKH37HPG116ZRRFKHBDGB".to_string(),
56+
));
57+
assert!(get_one(path, State(state)).await.is_ok());
58+
}
59+
}

src/server/handler/hc.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,16 @@ use axum::response::IntoResponse;
44
pub async fn hc() -> impl IntoResponse {
55
StatusCode::NO_CONTENT
66
}
7+
8+
#[cfg(test)]
9+
mod tests {
10+
use crate::server::handler::hc::hc;
11+
use axum::http::StatusCode;
12+
use axum::response::IntoResponse;
13+
14+
#[tokio::test]
15+
async fn test_hc() {
16+
let resp = hc().await.into_response();
17+
assert_eq!(resp.status(), StatusCode::NO_CONTENT);
18+
}
19+
}

src/server/handler/patch.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,33 @@ pub async fn patch_one(
3131
let value = state.storage.update_one(&resource, &input)?;
3232
Ok((StatusCode::OK, Json(value)))
3333
}
34+
35+
#[cfg(test)]
36+
mod tests {
37+
use crate::server::context::Payload;
38+
use crate::server::handler::patch::{patch, patch_one};
39+
use crate::server::handler::tests::init_state;
40+
use axum::extract::{Path, State};
41+
use serde_json::json;
42+
43+
#[tokio::test]
44+
async fn test_patch() {
45+
let state = init_state();
46+
let path: Path<(String, String)> = Path((
47+
"posts".to_string(),
48+
"01J7BAKH37HPG116ZRRFKHBDGB".to_string(),
49+
));
50+
let payload = json!({"title":"patched post","views":200});
51+
assert!(patch(path, State(state), Payload(payload)).await.is_ok());
52+
}
53+
54+
#[tokio::test]
55+
async fn test_patch_one() {
56+
let state = init_state();
57+
let path: Path<String> = Path("profile".to_string());
58+
let payload = json!({"name":"Jane Smith","age":30});
59+
assert!(patch_one(path, State(state), Payload(payload))
60+
.await
61+
.is_ok());
62+
}
63+
}

src/server/handler/post.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,22 @@ pub async fn post(
1818
let value = state.storage.insert(&resource, &input)?;
1919
Ok((StatusCode::CREATED, Json(value)))
2020
}
21+
22+
#[cfg(test)]
23+
mod tests {
24+
use crate::server::context::PayloadWithId;
25+
use crate::server::handler::post::post;
26+
use crate::server::handler::tests::init_state;
27+
use axum::extract::{Path, State};
28+
use serde_json::json;
29+
30+
#[tokio::test]
31+
async fn test_post() {
32+
let state = init_state();
33+
let path: Path<String> = Path("posts".to_string());
34+
let payload = json!({"id":"01J8593X0V7Q34X011BYD92CHP","title":"posted post","views":0});
35+
assert!(post(path, State(state), PayloadWithId(payload))
36+
.await
37+
.is_ok());
38+
}
39+
}

src/server/handler/put.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,35 @@ pub async fn put_one(
3131
let value = state.storage.replace_one(&resource, &input)?;
3232
Ok((StatusCode::OK, Json(value)))
3333
}
34+
35+
#[cfg(test)]
36+
mod tests {
37+
use crate::server::context::PayloadWithId;
38+
use crate::server::handler::put::{put, put_one};
39+
use crate::server::handler::tests::init_state;
40+
use axum::extract::{Path, State};
41+
use serde_json::json;
42+
43+
#[tokio::test]
44+
async fn test_put() {
45+
let state = init_state();
46+
let path: Path<(String, String)> = Path((
47+
"posts".to_string(),
48+
"01J7BAKH37HPG116ZRRFKHBDGB".to_string(),
49+
));
50+
let payload = json!({"id":"01J7BAKH37HPG116ZRRFKHBDGB","title":"putted post","views":200});
51+
assert!(put(path, State(state), PayloadWithId(payload))
52+
.await
53+
.is_ok());
54+
}
55+
56+
#[tokio::test]
57+
async fn test_put_one() {
58+
let state = init_state();
59+
let path: Path<String> = Path("profile".to_string());
60+
let payload = json!({"id":1,"name":"John Smith","age":25});
61+
assert!(put_one(path, State(state), PayloadWithId(payload))
62+
.await
63+
.is_ok());
64+
}
65+
}

0 commit comments

Comments
 (0)