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 { struct TestEnvironment {
pub client: Client, pub client: Client,
command_sender: Arc<index::CommandSender>, 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 { impl Drop for TestEnvironment {
@ -43,11 +50,12 @@ fn get_test_environment(db_name: &str) -> TestEnvironment {
let command_sender = index::init(db.clone()); let command_sender = index::init(db.clone());
let server = 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(); let client = Client::new(server).unwrap();
TestEnvironment { TestEnvironment {
client, client,
command_sender, command_sender,
db,
} }
} }
@ -286,7 +294,17 @@ fn flatten() {
#[test] #[test]
fn random() { 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] #[test]

View file

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