diff options
author | Harald Eilertsen <haraldei@anduin.net> | 2019-04-02 22:55:59 +0200 |
---|---|---|
committer | Harald Eilertsen <haraldei@anduin.net> | 2019-04-02 22:55:59 +0200 |
commit | b694e0f342ef4f2687afdcd7424dd4a0fe350799 (patch) | |
tree | 69f494b107f53fa3e491311a565ae365b93c1dc7 | |
parent | b79d1453c4a8f999526d2af6238a1c2570c1e495 (diff) | |
download | ramaskrik-social-b694e0f342ef4f2687afdcd7424dd4a0fe350799.tar.gz ramaskrik-social-b694e0f342ef4f2687afdcd7424dd4a0fe350799.tar.bz2 ramaskrik-social-b694e0f342ef4f2687afdcd7424dd4a0fe350799.zip |
Refactor tests to always load fixtures, and sync execution.
Rust will run tests in parallel by default, which does not work
too well with using db transactions as a means to isolate changes
in the database between tests. So we have to run the database
tests one at a time, thus the mutex.
-rw-r--r-- | Cargo.lock | 1 | ||||
-rw-r--r-- | Cargo.toml | 1 | ||||
-rw-r--r-- | tests/apitests.rs | 67 |
3 files changed, 44 insertions, 25 deletions
@@ -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)", @@ -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)); |