-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
96 additions
and
54 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"}]}"#); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |