aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorHarald Eilertsen <haraldei@anduin.net>2019-03-21 13:39:08 +0100
committerHarald Eilertsen <haraldei@anduin.net>2019-03-21 13:39:08 +0100
commit29ea39dfc319e5e0acd42b4f32f94c358a11f2a1 (patch)
treed6abca8e6ae628152a90c3bd558c01a7948c4e60 /tests
parentb4a8149dc6ed595c0bd95f5824f7048799989bdb (diff)
downloadramaskrik-social-29ea39dfc319e5e0acd42b4f32f94c358a11f2a1.tar.gz
ramaskrik-social-29ea39dfc319e5e0acd42b4f32f94c358a11f2a1.tar.bz2
ramaskrik-social-29ea39dfc319e5e0acd42b4f32f94c358a11f2a1.zip
Refactor room tests, move server and db setup to function.
Diffstat (limited to 'tests')
-rw-r--r--tests/room_tests.rs51
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"));
+ })
}