aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/apitests.rs67
1 files changed, 42 insertions, 25 deletions
diff --git a/tests/apitests.rs b/tests/apitests.rs
index 55102c5..efcbfd2 100644
--- a/tests/apitests.rs
+++ b/tests/apitests.rs
@@ -22,40 +22,69 @@ use ramaskrik::models::{
room::Room
};
+use lazy_static::lazy_static;
use rocket::http::ContentType;
use rocket_contrib::databases::diesel::prelude::*;
use serde_json;
+lazy_static! {
+ static ref MUTEX: std::sync::Mutex<()> = std::sync::Mutex::default();
+}
+
pub fn server_with_db<TestFn>(f: TestFn)
where
TestFn: Fn(rocket::Rocket, ramaskrik::db::Connection)
{
+ let _lock = MUTEX.lock();
+
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, films").execute(&*db).unwrap();
+ load_fixtures(&db);
f(server, db);
}
-#[test]
-fn getting_rooms_from_api() {
- server_with_db(|server, db| {
- use ramaskrik::schema::rooms::dsl::*;
+fn load_default_films(db: &ramaskrik::db::Connection) {
+ use ramaskrik::schema::films::dsl::*;
+
+ let new_films = vec![
+ NewFilm { title: "Hellraiser", url: Some("https://www.imdb.com/title/tt0093177") },
+ NewFilm { title: "Huset", url: Some("https://www.imdb.com/title/tt3425402") },
+ NewFilm { title: "Skuld", url: Some("https://www.imdb.com/title/tt4344456") }];
- let new_rooms = vec![
- name.eq("Main room"),
- name.eq("Small room"),
- name.eq("Neverland")];
+ diesel::insert_into(films)
+ .values(&new_films)
+ .execute(&**db)
+ .unwrap();
+}
- diesel::insert_into(rooms)
- .values(&new_rooms)
- .execute(&*db)
- .unwrap();
+fn load_default_rooms(db: &ramaskrik::db::Connection) {
+ 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();
+}
+
+fn load_fixtures(db: &ramaskrik::db::Connection) {
+ diesel::dsl::sql_query("TRUNCATE TABLE rooms, films").execute(&**db).unwrap();
+ load_default_rooms(&db);
+ load_default_films(&db);
+}
+
+#[test]
+fn getting_rooms_from_api() {
+ server_with_db(|server, db| {
let client = rocket::local::Client::new(server).unwrap();
let mut response = client.get("/rooms").dispatch();
assert_eq!(response.content_type(), Some(ContentType::JSON));
@@ -75,18 +104,6 @@ fn getting_rooms_from_api() {
#[test]
fn getting_films_from_api() {
server_with_db(|server, db| {
- use ramaskrik::schema::films::dsl::*;
-
- let new_films = vec![
- NewFilm { title: "Hellraiser", url: Some("https://www.imdb.com/title/tt0093177") },
- NewFilm { title: "Huset", url: Some("https://www.imdb.com/title/tt3425402") },
- NewFilm { title: "Skuld", url: Some("https://www.imdb.com/title/tt4344456") }];
-
- diesel::insert_into(films)
- .values(&new_films)
- .execute(&*db)
- .unwrap();
-
let client = rocket::local::Client::new(server).unwrap();
let mut response = client.get("/films").dispatch();
assert_eq!(response.content_type(), Some(ContentType::JSON));