Link coverart with song queue (#128)
* Added initial code to link coverart id and song queue id * Added more functionality * Preparing test * Finished test * Cleanup * Code formatting * Clippy warning fix
This commit was merged in pull request #128.
This commit is contained in:
+116
-19
@@ -58,7 +58,7 @@ async fn main() {
|
||||
}
|
||||
|
||||
pub mod init {
|
||||
use axum::routing::{get, post};
|
||||
use axum::routing::{get, patch, post};
|
||||
use std::time::Duration;
|
||||
use tower_http::timeout::TimeoutLayer;
|
||||
|
||||
@@ -89,6 +89,10 @@ pub mod init {
|
||||
crate::callers::endpoints::QUEUECOVERART,
|
||||
post(crate::callers::coverart::endpoint::queue),
|
||||
)
|
||||
.route(
|
||||
crate::callers::endpoints::QUEUECOVERARTLINK,
|
||||
patch(crate::callers::coverart::endpoint::link),
|
||||
)
|
||||
}
|
||||
|
||||
pub async fn app() -> axum::Router {
|
||||
@@ -269,6 +273,27 @@ mod tests {
|
||||
app.clone().oneshot(req).await
|
||||
}
|
||||
|
||||
async fn upload_coverart_queue_req(
|
||||
app: &axum::Router,
|
||||
) -> Result<axum::response::Response, std::convert::Infallible> {
|
||||
let mut form = MultipartForm::default();
|
||||
let _ = form.add_file("jpg", "tests/Machine_gun/160809_machinegun.jpg");
|
||||
|
||||
// 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::QUEUECOVERART)
|
||||
.header(axum::http::header::CONTENT_TYPE, content_type)
|
||||
.body(axum::body::Body::from_stream(body))
|
||||
.unwrap();
|
||||
|
||||
// Send request
|
||||
app.clone().oneshot(req).await
|
||||
}
|
||||
|
||||
pub async fn resp_to_bytes(
|
||||
response: axum::response::Response,
|
||||
) -> Result<axum::body::Bytes, axum::Error> {
|
||||
@@ -614,26 +639,9 @@ mod tests {
|
||||
db::migrations(&pool).await;
|
||||
|
||||
let app = init::app(pool).await;
|
||||
let mut form = MultipartForm::default();
|
||||
let _ = form.add_file("jpg", "tests/Machine_gun/160809_machinegun.jpg");
|
||||
|
||||
// Create request
|
||||
let content_type = form.content_type();
|
||||
let body = MultipartBody::from(form);
|
||||
|
||||
// Send request
|
||||
match app
|
||||
.clone()
|
||||
.oneshot(
|
||||
axum::http::Request::builder()
|
||||
.method(axum::http::Method::POST)
|
||||
.uri(crate::callers::endpoints::QUEUECOVERART)
|
||||
.header(axum::http::header::CONTENT_TYPE, content_type)
|
||||
.body(axum::body::Body::from_stream(body))
|
||||
.unwrap(),
|
||||
)
|
||||
.await
|
||||
{
|
||||
match upload_coverart_queue_req(&app).await {
|
||||
Ok(response) => {
|
||||
let resp =
|
||||
get_resp_data::<crate::callers::coverart::response::Response>(response).await;
|
||||
@@ -648,4 +656,93 @@ mod tests {
|
||||
|
||||
let _ = db_mgr::drop_database(&tm_pool, &db_name).await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_song_coverart_queue_link() {
|
||||
let tm_pool = db_mgr::get_pool().await.unwrap();
|
||||
let db_name = db_mgr::generate_db_name().await;
|
||||
|
||||
match db_mgr::create_database(&tm_pool, &db_name).await {
|
||||
Ok(_) => {
|
||||
println!("Success");
|
||||
}
|
||||
Err(err) => {
|
||||
assert!(false, "Error: {:?}", err);
|
||||
}
|
||||
}
|
||||
|
||||
let pool = db_mgr::connect_to_db(&db_name).await.unwrap();
|
||||
db::migrations(&pool).await;
|
||||
|
||||
let app = init::app(pool).await;
|
||||
|
||||
match song_queue_req(&app).await {
|
||||
Ok(response) => {
|
||||
let resp =
|
||||
get_resp_data::<crate::callers::coverart::response::Response>(response).await;
|
||||
assert_eq!(false, resp.data.is_empty(), "Should not be empty");
|
||||
let song_queue_id = resp.data[0];
|
||||
assert_eq!(false, song_queue_id.is_nil(), "Should not be empty");
|
||||
|
||||
// Send request
|
||||
match upload_coverart_queue_req(&app).await {
|
||||
Ok(response) => {
|
||||
let resp =
|
||||
get_resp_data::<crate::callers::coverart::response::Response>(response)
|
||||
.await;
|
||||
assert_eq!(false, resp.data.is_empty(), "Should not be empty");
|
||||
let coverart_id = resp.data[0];
|
||||
assert_eq!(false, coverart_id.is_nil(), "Should not be empty");
|
||||
|
||||
let payload = serde_json::json!(
|
||||
{
|
||||
"song_queue_id": song_queue_id,
|
||||
"coverart_id" : coverart_id,
|
||||
});
|
||||
|
||||
match app
|
||||
.clone()
|
||||
.oneshot(
|
||||
axum::http::Request::builder()
|
||||
.method(axum::http::Method::PATCH)
|
||||
.uri(crate::callers::endpoints::QUEUECOVERARTLINK)
|
||||
.header(axum::http::header::CONTENT_TYPE, "application/json")
|
||||
.body(axum::body::Body::from(payload.to_string()))
|
||||
.unwrap(),
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(response) => {
|
||||
let resp = get_resp_data::<
|
||||
crate::callers::coverart::response::link::Response,
|
||||
>(response)
|
||||
.await;
|
||||
assert_eq!(false, resp.data.is_empty(), "Should not be empty");
|
||||
let resp_coverart_id = resp.data[0].coverart_id;
|
||||
let resp_song_queue_id = resp.data[0].song_queue_id;
|
||||
|
||||
assert_eq!(false, resp_coverart_id.is_nil(), "Should not be empty");
|
||||
assert_eq!(
|
||||
false,
|
||||
resp_song_queue_id.is_nil(),
|
||||
"Should not be empty"
|
||||
);
|
||||
}
|
||||
Err(err) => {
|
||||
assert!(false, "Error: {:?}", err);
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
assert!(false, "Error: {:?}", err);
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
assert!(false, "Error: {:?}", err);
|
||||
}
|
||||
}
|
||||
|
||||
let _ = db_mgr::drop_database(&tm_pool, &db_name).await;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user