aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Cargo.lock1
-rw-r--r--Cargo.toml1
-rw-r--r--tests/apitests.rs67
3 files changed, 44 insertions, 25 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 08f1c11..046159b 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -499,6 +499,7 @@ version = "0.1.0"
dependencies = [
"diesel 1.4.1 (registry+https://github.com/rust-lang/crates.io-index)",
"dotenv 0.13.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"rocket 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"rocket_contrib 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)",
diff --git a/Cargo.toml b/Cargo.toml
index 53f348c..973cedb 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -17,4 +17,5 @@ default-features = false
features = ["json", "serve", "diesel_postgres_pool"]
[dev-dependencies]
+lazy_static = "~1.3.0"
serde_json = "~1.0.39"
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));