diff options
-rw-r--r-- | tests/room_tests.rs | 51 |
1 files changed, 30 insertions, 21 deletions
diff --git a/tests/room_tests.rs b/tests/room_tests.rs index 1bd5649..1451b34 100644 --- a/tests/room_tests.rs +++ b/tests/room_tests.rs @@ -22,8 +22,10 @@ use rocket::http::ContentType; use rocket_contrib::databases::diesel::prelude::*; use serde_json; -#[test] -fn getting_rooms_from_api() { +fn server_with_db<TestFn>(f: TestFn) + where + TestFn: Fn(rocket::Rocket, ramaskrik::db::Connection) +{ let db_url = dotenv::var("TEST_DATABASE_URL") .map_err(|_| "No database! Set TEST_DATABASE_URL env var and try again.").unwrap(); @@ -32,29 +34,36 @@ fn getting_rooms_from_api() { diesel::dsl::sql_query("TRUNCATE TABLE rooms").execute(&*db).unwrap(); - use ramaskrik::schema::rooms::dsl::*; + f(server, db); +} + +#[test] +fn getting_rooms_from_api() { + server_with_db(|server, db| { + use ramaskrik::schema::rooms::dsl::*; - let new_rooms = vec![ - name.eq("Main room"), - name.eq("Small room"), - name.eq("Neverland")]; + let new_rooms = vec![ + name.eq("Main room"), + name.eq("Small room"), + name.eq("Neverland")]; - diesel::insert_into(rooms) - .values(&new_rooms) - .execute(&*db) - .unwrap(); + diesel::insert_into(rooms) + .values(&new_rooms) + .execute(&*db) + .unwrap(); - let client = rocket::local::Client::new(server).unwrap(); - let mut response = client.get("/rooms").dispatch(); - assert_eq!(response.content_type(), Some(ContentType::JSON)); + let client = rocket::local::Client::new(server).unwrap(); + let mut response = client.get("/rooms").dispatch(); + assert_eq!(response.content_type(), Some(ContentType::JSON)); - let fetched_rooms: Vec<Room> = - serde_json::from_str(&response.body_string().unwrap()).unwrap(); + let fetched_rooms: Vec<Room> = + serde_json::from_str(&response.body_string().unwrap()).unwrap(); - assert_eq!(fetched_rooms.len(), 3); + assert_eq!(fetched_rooms.len(), 3); - let room_names: Vec<&str> = fetched_rooms.iter().map(|room| room.name.as_str()).collect(); - assert!(room_names.contains(&"Main room")); - assert!(room_names.contains(&"Small room")); - assert!(room_names.contains(&"Neverland")); + let room_names: Vec<&str> = fetched_rooms.iter().map(|room| room.name.as_str()).collect(); + assert!(room_names.contains(&"Main room")); + assert!(room_names.contains(&"Small room")); + assert!(room_names.contains(&"Neverland")); + }) } |