Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenArcher committed Feb 21, 2024
1 parent 54b8619 commit 891d6d8
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 4 deletions.
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"rust-analyzer.linkedProjects": [
"./Cargo.toml"
],
"rust-analyzer.showUnlinkedFileNotification": false
}
3 changes: 3 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ mod db;
mod misc;
mod response;

#[cfg(test)]
mod tests;

use actix_web::middleware::Logger;
use actix_web::{web, App, HttpServer};
use config::AppConfig;
Expand Down
8 changes: 4 additions & 4 deletions src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ pub type AppResult<T> = actix_web::Result<web::Json<OkModel<T>>, AppError>;
/// # Error JSON response model
#[derive(Serialize)]
pub struct ErrModel {
success: bool,
err: &'static str,
pub success: bool,
pub err: &'static str,
}

/// # Success JSON response model
Expand All @@ -23,8 +23,8 @@ pub struct OkModel<T>
where
T: Serialize,
{
success: bool,
data: T,
pub success: bool,
pub data: T,
}

/// # App Errors
Expand Down
1 change: 1 addition & 0 deletions src/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mod response;
35 changes: 35 additions & 0 deletions src/tests/response.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use actix_web::{http::StatusCode, web, ResponseError};

use crate::response::{AppError, AppResponse, AppResult};

#[test]
fn success() {
let response: AppResult<&str> = AppResponse::Success("success").response();
let web::Json(model) = response.unwrap();
assert_eq!(model.success, true);
assert_eq!(model.data, "success");
}

#[test]
fn bad_request() {
let response: AppResult<()> = AppResponse::Invalid("bad").response();
if let Err(e) = response {
if let AppError::Invalid { err } = e {
assert_eq!(err, "bad");
}
let resp = e.error_response();
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
} else {
panic!("response is not Err")
}
}

#[test]
fn internal_server_error() {
let response: AppResult<()> = AppResponse::InternalError("500").response();
if let Err(e) = response {
if let AppError::InternalError = e {}
let resp = e.error_response();
assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR);
}
}

0 comments on commit 891d6d8

Please sign in to comment.