-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
get queue API working, add first tests
- Make the queue API return the current queue - Add JSON errors for e.g. 404 - Add once_cell and refactor build_test_app to fix Sled locking problems - Write first queue API tests, needs some more.
- Loading branch information
Showing
12 changed files
with
123 additions
and
53 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
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
use actix_web::{http::StatusCode, HttpResponse, ResponseError}; | ||
use std::fmt; | ||
|
||
#[derive(Debug)] | ||
pub struct AppError { | ||
pub msg: String, | ||
pub status: StatusCode, | ||
} | ||
|
||
impl AppError { | ||
pub fn new(msg: &str, status: StatusCode) -> AppError { | ||
AppError { | ||
msg: msg.to_string(), | ||
status, | ||
} | ||
} | ||
} | ||
|
||
impl fmt::Display for AppError { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
write!(f, "{:?}", self) | ||
} | ||
} | ||
|
||
impl ResponseError for AppError { | ||
// builds the actual response to send back when an error occurs | ||
fn error_response(&self) -> HttpResponse { | ||
let err_json = serde_json::json!({ "error": self.msg }); | ||
HttpResponse::build(self.status).json(err_json) | ||
} | ||
} |
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
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
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,5 +1,6 @@ | ||
pub mod api; | ||
pub mod app_state; | ||
pub mod error; | ||
pub mod filemanager; | ||
pub mod player; | ||
pub mod queue; | ||
|
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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
use actix_web::{ | ||
test::{self, read_body_json}, | ||
web::Data, | ||
}; | ||
|
||
mod helpers; | ||
|
||
use helpers::build_test_app_state; | ||
use once_cell::sync::Lazy; | ||
use pickup::{app_state::AppState, build_app}; | ||
use serde_json::{self, json}; | ||
|
||
// Make a shared app state for use in all tests, because Sled won't let us open multiple instances of the DB file | ||
// from differet threads. | ||
static APP_STATE: Lazy<Data<AppState>> = Lazy::new(build_test_app_state); | ||
|
||
#[actix_web::test] | ||
async fn test_not_found() { | ||
let app = test::init_service(build_app((*APP_STATE).clone())).await; | ||
let req = test::TestRequest::post() | ||
.uri("/queue/add") | ||
.set_json(json!({"category": "DoesNotExist"})) | ||
.to_request(); | ||
|
||
let resp = test::call_service(&app, req).await; | ||
|
||
assert_eq!(resp.status(), actix_web::http::StatusCode::NOT_FOUND); | ||
let resp_json: serde_json::Value = read_body_json(resp).await; | ||
assert_eq!( | ||
resp_json, | ||
json!({ "error": "No matching music found in the collection" }) | ||
); | ||
} | ||
|
||
#[actix_web::test] | ||
async fn test_add_artist() { | ||
let app = test::init_service(build_app((*APP_STATE).clone())).await; | ||
let req = test::TestRequest::post() | ||
.uri("/queue/add") | ||
.set_json(json!({ | ||
"category": "Music", | ||
})) | ||
.to_request(); | ||
|
||
let resp: serde_json::Value = test::call_and_read_body_json(&app, req).await; | ||
|
||
assert_eq!(resp.get("position").unwrap().as_u64().unwrap(), 0); | ||
assert_eq!(resp.get("tracks").unwrap().as_array().unwrap().len(), 5); | ||
} |