From 8a4c327fa8fbee2aee600153fb85119101d2328a Mon Sep 17 00:00:00 2001 From: Antoine Gersant Date: Sun, 11 Nov 2018 19:20:06 -0800 Subject: [PATCH] Added unit test for api/random --- src/api_tests.rs | 22 ++++++++++++++++++++-- src/index.rs | 6 +++--- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/api_tests.rs b/src/api_tests.rs index 57e3c5a..93e05ef 100644 --- a/src/api_tests.rs +++ b/src/api_tests.rs @@ -21,6 +21,13 @@ const TEST_MOUNT_SOURCE: &str = "test/collection"; struct TestEnvironment { pub client: Client, command_sender: Arc, + db: Arc, +} + +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 = serde_json::from_str(&response_body).unwrap(); + assert_eq!(response_json.len(), 2); } #[test] diff --git a/src/index.rs b/src/index.rs index a76cdb5..5651962 100644 --- a/src/index.rs +++ b/src/index.rs @@ -109,12 +109,12 @@ pub struct Song { pub duration: Option, } -#[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, pub artist: Option, pub year: Option,