aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorHarald Eilertsen <haraldei@anduin.net>2019-03-21 10:39:16 +0100
committerHarald Eilertsen <haraldei@anduin.net>2019-03-21 10:39:16 +0100
commitb4a8149dc6ed595c0bd95f5824f7048799989bdb (patch)
treeffc490cb7e13a3abe55e176b0fd308c55da051e6 /tests
parent9c3a341e79581541fc39ceddeec28daae7dc28cf (diff)
downloadramaskrik-social-b4a8149dc6ed595c0bd95f5824f7048799989bdb.tar.gz
ramaskrik-social-b4a8149dc6ed595c0bd95f5824f7048799989bdb.tar.bz2
ramaskrik-social-b4a8149dc6ed595c0bd95f5824f7048799989bdb.zip
Actually fetch available rooms from the database.
Diffstat (limited to 'tests')
-rw-r--r--tests/room_tests.rs34
1 files changed, 31 insertions, 3 deletions
diff --git a/tests/room_tests.rs b/tests/room_tests.rs
index 292954a..1bd5649 100644
--- a/tests/room_tests.rs
+++ b/tests/room_tests.rs
@@ -17,16 +17,44 @@
*/
use ramaskrik;
+use ramaskrik::models::room::Room;
use rocket::http::ContentType;
+use rocket_contrib::databases::diesel::prelude::*;
+use serde_json;
#[test]
fn getting_rooms_from_api() {
- let db_url = dotenv::var("DATABASE_URL")
- .map_err(|_| "No database! Set DATABASE_URL env var and try again.").unwrap();
+ let db_url = dotenv::var("TEST_DATABASE_URL")
+ .map_err(|_| "No database! Set TEST_DATABASE_URL env var and try again.").unwrap();
let server = ramaskrik::build_rocket(&db_url).unwrap();
+ let db = ramaskrik::db::Connection::get_one(&server).expect("Could not get db connection");
+
+ diesel::dsl::sql_query("TRUNCATE TABLE rooms").execute(&*db).unwrap();
+
+ use ramaskrik::schema::rooms::dsl::*;
+
+ 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();
+
let client = rocket::local::Client::new(server).unwrap();
let mut response = client.get("/rooms").dispatch();
assert_eq!(response.content_type(), Some(ContentType::JSON));
- assert_eq!(response.body_string(), Some("[]".into()));
+
+ let fetched_rooms: Vec<Room> =
+ serde_json::from_str(&response.body_string().unwrap()).unwrap();
+
+ 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"));
}