Skip to content

Commit

Permalink
Revamp test folder hierarchy (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
kigawas authored Jul 23, 2024
1 parent 771e74d commit af74864
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 54 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Axum scaffold with clean architecture.

You probably don't need [Rust on Rails](https://github.com/loco-rs/loco).

Refer to this post for more details on background: <https://kigawas.me/posts/rustacean-clean-architecture-approach/>.

## Features

- [Axum](https://github.com/tokio-rs/axum) framework
Expand Down
48 changes: 18 additions & 30 deletions tests/api/mod.rs
Original file line number Diff line number Diff line change
@@ -1,41 +1,29 @@
use axum::{http::StatusCode, Router};
use http_body_util::BodyExt;

use api::setup_router;
use utils::testing::{make_get_request, make_post_request, setup_test_db};
use utils::testing::setup_test_db;

mod root;
mod user;
use root::*;
use user::*;

#[tokio::test]
async fn main() {
let db = setup_test_db("sqlite::memory:")
async fn root_main() {
let db = setup_test_db("sqlite::root?mode=memory&cache=shared")
.await
.expect("Set up db failed!");

let app = setup_router(db);
test_root(app.clone()).await;
test_post_users(app.clone()).await;
test_get_users(app).await;
}

async fn test_root(app: Router) {
let response = make_get_request(app, "/").await;
assert_eq!(response.status(), StatusCode::OK);

let body = response.into_body().collect().await.unwrap().to_bytes();
assert_eq!(&body[..], b"Hello, World from DB!");
}

async fn test_post_users(app: Router) {
let response = make_post_request(app, "/users", r#"{"username": "test"}"#.to_owned()).await;
assert_eq!(response.status(), StatusCode::CREATED);

let body = response.into_body().collect().await.unwrap().to_bytes();
assert_eq!(&body[..], br#"{"id":1,"username":"test"}"#);
test_root(app).await;
}

async fn test_get_users(app: Router) {
let response = make_get_request(app, "/users").await;
assert_eq!(response.status(), StatusCode::OK);
#[tokio::test]
async fn user_main() {
let db = setup_test_db("sqlite::user?mode=memory&cache=shared")
.await
.expect("Set up db failed!");

let body = response.into_body().collect().await.unwrap().to_bytes();
assert_eq!(&body[..], br#"{"users":[{"id":1,"username":"test"}]}"#);
let app = setup_router(db);
test_post_users(app.clone()).await;
test_post_users_error(app.clone()).await;
test_get_users(app).await;
}
12 changes: 12 additions & 0 deletions tests/api/root.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use axum::{http::StatusCode, Router};
use http_body_util::BodyExt;

use utils::testing::make_get_request;

pub(super) async fn test_root(app: Router) {
let response = make_get_request(app, "/").await;
assert_eq!(response.status(), StatusCode::OK);

let body = response.into_body().collect().await.unwrap().to_bytes();
assert_eq!(&body[..], b"Hello, World from DB!");
}
36 changes: 36 additions & 0 deletions tests/api/user.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use axum::{http::StatusCode, Router};
use http_body_util::BodyExt;
use serde_json::Value;

use utils::testing::{make_get_request, make_post_request};

pub(super) async fn test_post_users(app: Router) {
let response = make_post_request(app, "/users", r#"{"username": "test"}"#.to_owned()).await;
assert_eq!(response.status(), StatusCode::CREATED);

let body = response.into_body().collect().await.unwrap().to_bytes();
assert_eq!(&body[..], br#"{"id":1,"username":"test"}"#);
}

pub(super) async fn test_post_users_error(app: Router) {
let response = make_post_request(app, "/users", r#"{"username": "1"}"#.to_owned()).await;
assert_eq!(response.status(), StatusCode::UNPROCESSABLE_ENTITY);

let body = response.into_body().collect().await.unwrap().to_bytes();
let result: Value = serde_json::from_slice(&body).unwrap();
assert_eq!(result["message"], "Validation error");
assert_eq!(result["details"]["username"][0]["code"], "length");
assert_eq!(result["details"]["username"][0]["message"], Value::Null);
assert_eq!(
result["details"]["username"][0]["params"]["min"],
Value::Number(2.into())
)
}

pub(super) async fn test_get_users(app: Router) {
let response = make_get_request(app, "/users").await;
assert_eq!(response.status(), StatusCode::OK);

let body = response.into_body().collect().await.unwrap().to_bytes();
assert_eq!(&body[..], br#"{"users":[{"id":1,"username":"test"}]}"#);
}
30 changes: 8 additions & 22 deletions tests/app/mod.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,13 @@
use sea_orm::{DatabaseConnection, DbErr, Unchanged};

use app::services::user::create_user;
use models::domains::user;
use models::params::user::CreateUserParams;
use utils::testing::setup_test_db;

#[tokio::test]
async fn main() -> Result<(), DbErr> {
let db = setup_test_db("sqlite::memory:").await?;
test_user(&db).await?;
Ok(())
}
mod user;
use user::test_user;

async fn test_user(db: &DatabaseConnection) -> Result<(), DbErr> {
let params = CreateUserParams {
username: "test".to_string(),
};
#[tokio::test]
async fn main() {
let db = setup_test_db("sqlite::memory:")
.await
.expect("Set up db failed!");

let user = create_user(db, params).await?;
let expected = user::ActiveModel {
id: Unchanged(1),
username: Unchanged("test".to_owned()),
};
assert_eq!(user, expected);
Ok(())
test_user(&db).await;
}
18 changes: 18 additions & 0 deletions tests/app/user.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use sea_orm::{DatabaseConnection, Unchanged};

use app::services::user::create_user;
use models::domains::user;
use models::params::user::CreateUserParams;

pub(super) async fn test_user(db: &DatabaseConnection) {
let params = CreateUserParams {
username: "test".to_string(),
};

let user = create_user(db, params).await.expect("Create user failed!");
let expected = user::ActiveModel {
id: Unchanged(1),
username: Unchanged("test".to_owned()),
};
assert_eq!(user, expected);
}

0 comments on commit af74864

Please sign in to comment.