Added unit test for api/random

This commit is contained in:
Antoine Gersant 2018-11-11 19:20:06 -08:00
parent be1479a40c
commit 8a4c327fa8
2 changed files with 23 additions and 5 deletions

View file

@ -21,6 +21,13 @@ const TEST_MOUNT_SOURCE: &str = "test/collection";
struct TestEnvironment {
pub client: Client,
command_sender: Arc<index::CommandSender>,
db: Arc<db::DB>,
}
impl TestEnvironment {
pub fn update_index(&self) {
index::update(self.db.deref()).unwrap();
}
}
impl Drop for TestEnvironment {
@ -43,11 +50,12 @@ fn get_test_environment(db_name: &str) -> TestEnvironment {
let command_sender = index::init(db.clone());
let server =
server::get_server(5050, "/", "/api", &web_dir_path, db, command_sender.clone()).unwrap();
server::get_server(5050, "/", "/api", &web_dir_path, db.clone(), command_sender.clone()).unwrap();
let client = Client::new(server).unwrap();
TestEnvironment {
client,
command_sender,
db,
}
}
@ -286,7 +294,17 @@ fn flatten() {
#[test]
fn random() {
// TODO
let env = get_test_environment("api_random.sqlite");
let client = &env.client;
complete_initial_setup(client);
do_auth(client);
env.update_index();
let mut response = client.get("/api/random").dispatch();
assert_eq!(response.status(), Status::Ok);
let response_body = response.body_string().unwrap();
let response_json: Vec<index::Directory> = serde_json::from_str(&response_body).unwrap();
assert_eq!(response_json.len(), 2);
}
#[test]

View file

@ -109,12 +109,12 @@ pub struct Song {
pub duration: Option<i32>,
}
#[derive(Debug, Queryable, Serialize)]
#[derive(Debug, Queryable, Serialize, Deserialize)]
pub struct Directory {
#[serde(skip_serializing)]
#[serde(skip_serializing, skip_deserializing)]
id: i32,
pub path: String,
#[serde(skip_serializing)]
#[serde(skip_serializing, skip_deserializing)]
pub parent: Option<String>,
pub artist: Option<String>,
pub year: Option<i32>,