Test refactor (#124)
* Refactoring test * Test refactor * Added code to get response content * More refactoring * Code refactor
This commit was merged in pull request #124.
This commit is contained in:
+61
-90
@@ -117,9 +117,8 @@ mod tests {
|
|||||||
use common_multipart_rfc7578::client::multipart::{
|
use common_multipart_rfc7578::client::multipart::{
|
||||||
Body as MultipartBody, Form as MultipartForm,
|
Body as MultipartBody, Form as MultipartForm,
|
||||||
};
|
};
|
||||||
use std::{time::Duration, usize};
|
use std::usize;
|
||||||
use tower::ServiceExt;
|
use tower::ServiceExt;
|
||||||
use tower_http::timeout::TimeoutLayer;
|
|
||||||
|
|
||||||
mod db_mgr {
|
mod db_mgr {
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
@@ -197,6 +196,47 @@ mod tests {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mod init {
|
||||||
|
pub async fn app(pool: sqlx::PgPool) -> axum::Router {
|
||||||
|
crate::init::routes()
|
||||||
|
.await
|
||||||
|
.layer(axum::Extension(pool))
|
||||||
|
.layer(axum::extract::DefaultBodyLimit::max(1024 * 1024 * 1024))
|
||||||
|
.layer(tower_http::timeout::TimeoutLayer::new(
|
||||||
|
std::time::Duration::from_secs(300),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn song_queue_req(
|
||||||
|
app: &axum::Router,
|
||||||
|
) -> Result<axum::response::Response, std::convert::Infallible> {
|
||||||
|
// Create multipart form
|
||||||
|
let mut form = MultipartForm::default();
|
||||||
|
let _ = form.add_file("flac", "tests/Machine_gun/track01.flac");
|
||||||
|
|
||||||
|
// Create request
|
||||||
|
let content_type = form.content_type();
|
||||||
|
let body = MultipartBody::from(form);
|
||||||
|
let req = axum::http::Request::builder()
|
||||||
|
.method(axum::http::Method::POST)
|
||||||
|
.uri(crate::callers::endpoints::QUEUESONG)
|
||||||
|
.header(axum::http::header::CONTENT_TYPE, content_type)
|
||||||
|
.body(axum::body::Body::from_stream(body))
|
||||||
|
.unwrap();
|
||||||
|
app.clone().oneshot(req).await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn get_resp_data<Data>(response: axum::response::Response) -> Data
|
||||||
|
where
|
||||||
|
Data: for<'a> serde::Deserialize<'a>,
|
||||||
|
{
|
||||||
|
let body = axum::body::to_bytes(response.into_body(), usize::MAX)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
serde_json::from_slice(&body).unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_song_queue() {
|
async fn test_song_queue() {
|
||||||
let tm_pool = db_mgr::get_pool().await.unwrap();
|
let tm_pool = db_mgr::get_pool().await.unwrap();
|
||||||
@@ -214,35 +254,13 @@ mod tests {
|
|||||||
let pool = db_mgr::connect_to_db(&db_name).await.unwrap();
|
let pool = db_mgr::connect_to_db(&db_name).await.unwrap();
|
||||||
db::migrations(&pool).await;
|
db::migrations(&pool).await;
|
||||||
|
|
||||||
let app = crate::init::routes()
|
let app = init::app(pool).await;
|
||||||
.await
|
|
||||||
.layer(axum::Extension(pool))
|
|
||||||
.layer(axum::extract::DefaultBodyLimit::max(1024 * 1024 * 1024))
|
|
||||||
.layer(TimeoutLayer::new(Duration::from_secs(300)));
|
|
||||||
|
|
||||||
// Create multipart form
|
|
||||||
let mut form = MultipartForm::default();
|
|
||||||
let _ = form.add_file("flac", "tests/Machine_gun/track01.flac");
|
|
||||||
|
|
||||||
// Create request
|
|
||||||
let content_type = form.content_type();
|
|
||||||
let body = MultipartBody::from(form);
|
|
||||||
let req = axum::http::Request::builder()
|
|
||||||
.method(axum::http::Method::POST)
|
|
||||||
.uri(crate::callers::endpoints::QUEUESONG)
|
|
||||||
.header(axum::http::header::CONTENT_TYPE, content_type)
|
|
||||||
.body(axum::body::Body::from_stream(body))
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
// Send request
|
// Send request
|
||||||
match app.oneshot(req).await {
|
match song_queue_req(&app).await {
|
||||||
Ok(response) => {
|
Ok(response) => {
|
||||||
let body = axum::body::to_bytes(response.into_body(), usize::MAX)
|
let resp =
|
||||||
.await
|
get_resp_data::<crate::callers::song::response::Response>(response).await;
|
||||||
.unwrap();
|
|
||||||
println!("Body: {:?}", body);
|
|
||||||
let resp: crate::callers::song::response::Response =
|
|
||||||
serde_json::from_slice(&body).unwrap();
|
|
||||||
assert_eq!(false, resp.data.is_empty(), "Should not be empty");
|
assert_eq!(false, resp.data.is_empty(), "Should not be empty");
|
||||||
assert_eq!(false, resp.data[0].is_nil(), "Should not be empty");
|
assert_eq!(false, resp.data[0].is_nil(), "Should not be empty");
|
||||||
}
|
}
|
||||||
@@ -271,35 +289,13 @@ mod tests {
|
|||||||
let pool = db_mgr::connect_to_db(&db_name).await.unwrap();
|
let pool = db_mgr::connect_to_db(&db_name).await.unwrap();
|
||||||
db::migrations(&pool).await;
|
db::migrations(&pool).await;
|
||||||
|
|
||||||
let app = crate::init::routes()
|
let app = init::app(pool).await;
|
||||||
.await
|
|
||||||
.layer(axum::Extension(pool))
|
|
||||||
.layer(axum::extract::DefaultBodyLimit::max(1024 * 1024 * 1024))
|
|
||||||
.layer(TimeoutLayer::new(Duration::from_secs(300)));
|
|
||||||
|
|
||||||
// Create multipart form
|
|
||||||
let mut form = MultipartForm::default();
|
|
||||||
let _ = form.add_file("flac", "tests/Machine_gun/track01.flac");
|
|
||||||
|
|
||||||
// Create request
|
|
||||||
let content_type = form.content_type();
|
|
||||||
let body = MultipartBody::from(form);
|
|
||||||
let req = axum::http::Request::builder()
|
|
||||||
.method(axum::http::Method::POST)
|
|
||||||
.uri(crate::callers::endpoints::QUEUESONG)
|
|
||||||
.header(axum::http::header::CONTENT_TYPE, content_type)
|
|
||||||
.body(axum::body::Body::from_stream(body))
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
// Send request
|
// Send request
|
||||||
match app.clone().oneshot(req).await {
|
match song_queue_req(&app).await {
|
||||||
Ok(response) => {
|
Ok(response) => {
|
||||||
let body = axum::body::to_bytes(response.into_body(), usize::MAX)
|
let resp =
|
||||||
.await
|
get_resp_data::<crate::callers::song::response::Response>(response).await;
|
||||||
.unwrap();
|
|
||||||
println!("Body: {:?}", body);
|
|
||||||
let resp: crate::callers::song::response::Response =
|
|
||||||
serde_json::from_slice(&body).unwrap();
|
|
||||||
assert_eq!(false, resp.data.is_empty(), "Should not be empty");
|
assert_eq!(false, resp.data.is_empty(), "Should not be empty");
|
||||||
assert_eq!(false, resp.data[0].is_nil(), "Should not be empty");
|
assert_eq!(false, resp.data[0].is_nil(), "Should not be empty");
|
||||||
|
|
||||||
@@ -312,11 +308,10 @@ mod tests {
|
|||||||
|
|
||||||
match app.clone().oneshot(fetch_req).await {
|
match app.clone().oneshot(fetch_req).await {
|
||||||
Ok(response) => {
|
Ok(response) => {
|
||||||
let body = axum::body::to_bytes(response.into_body(), usize::MAX)
|
let resp = get_resp_data::<
|
||||||
.await
|
crate::callers::song::response::fetch_queue_song::Response,
|
||||||
.unwrap();
|
>(response)
|
||||||
let resp: crate::callers::song::response::fetch_queue_song::Response =
|
.await;
|
||||||
serde_json::from_slice(&body).unwrap();
|
|
||||||
assert_eq!(false, resp.data.is_empty(), "Should not be empty");
|
assert_eq!(false, resp.data.is_empty(), "Should not be empty");
|
||||||
}
|
}
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
@@ -349,35 +344,13 @@ mod tests {
|
|||||||
let pool = db_mgr::connect_to_db(&db_name).await.unwrap();
|
let pool = db_mgr::connect_to_db(&db_name).await.unwrap();
|
||||||
db::migrations(&pool).await;
|
db::migrations(&pool).await;
|
||||||
|
|
||||||
let app = crate::init::routes()
|
let app = init::app(pool).await;
|
||||||
.await
|
|
||||||
.layer(axum::Extension(pool))
|
|
||||||
.layer(axum::extract::DefaultBodyLimit::max(1024 * 1024 * 1024))
|
|
||||||
.layer(TimeoutLayer::new(Duration::from_secs(300)));
|
|
||||||
|
|
||||||
// Create multipart form
|
|
||||||
let mut form = MultipartForm::default();
|
|
||||||
let _ = form.add_file("flac", "tests/Machine_gun/track01.flac");
|
|
||||||
|
|
||||||
// Create request
|
|
||||||
let content_type = form.content_type();
|
|
||||||
let body = MultipartBody::from(form);
|
|
||||||
let req = axum::http::Request::builder()
|
|
||||||
.method(axum::http::Method::POST)
|
|
||||||
.uri(crate::callers::endpoints::QUEUESONG)
|
|
||||||
.header(axum::http::header::CONTENT_TYPE, content_type)
|
|
||||||
.body(axum::body::Body::from_stream(body))
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
// Send request
|
// Send request
|
||||||
match app.clone().oneshot(req).await {
|
match song_queue_req(&app).await {
|
||||||
Ok(response) => {
|
Ok(response) => {
|
||||||
let body = axum::body::to_bytes(response.into_body(), usize::MAX)
|
let resp =
|
||||||
.await
|
get_resp_data::<crate::callers::song::response::Response>(response).await;
|
||||||
.unwrap();
|
|
||||||
println!("Body: {:?}", body);
|
|
||||||
let resp: crate::callers::song::response::Response =
|
|
||||||
serde_json::from_slice(&body).unwrap();
|
|
||||||
assert_eq!(false, resp.data.is_empty(), "Should not be empty");
|
assert_eq!(false, resp.data.is_empty(), "Should not be empty");
|
||||||
assert_eq!(false, resp.data[0].is_nil(), "Should not be empty");
|
assert_eq!(false, resp.data[0].is_nil(), "Should not be empty");
|
||||||
let new_payload: serde_json::Value = serde_json::json!(
|
let new_payload: serde_json::Value = serde_json::json!(
|
||||||
@@ -409,11 +382,9 @@ mod tests {
|
|||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
Ok(response) => {
|
Ok(response) => {
|
||||||
let body = axum::body::to_bytes(response.into_body(), usize::MAX)
|
let resp =
|
||||||
.await
|
get_resp_data::<crate::callers::song::response::Response>(response)
|
||||||
.unwrap();
|
.await;
|
||||||
let resp: crate::callers::song::response::Response =
|
|
||||||
serde_json::from_slice(&body).unwrap();
|
|
||||||
assert_eq!(false, resp.data.is_empty(), "Should not be empty");
|
assert_eq!(false, resp.data.is_empty(), "Should not be empty");
|
||||||
}
|
}
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user