From b79d1453c4a8f999526d2af6238a1c2570c1e495 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Tue, 2 Apr 2019 22:55:05 +0200 Subject: Renamed test file. --- tests/apitests.rs | 104 ++++++++++++++++++++++++++++++++++++++++++++++++++++ tests/room_tests.rs | 104 ---------------------------------------------------- 2 files changed, 104 insertions(+), 104 deletions(-) create mode 100644 tests/apitests.rs delete mode 100644 tests/room_tests.rs diff --git a/tests/apitests.rs b/tests/apitests.rs new file mode 100644 index 0000000..55102c5 --- /dev/null +++ b/tests/apitests.rs @@ -0,0 +1,104 @@ +/* + Social program for Ramaskrik. + Copyright (C) 2019 Harald Eilertsen + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . +*/ + +use ramaskrik; +use ramaskrik::models::{ + film::{ Film, NewFilm }, + room::Room +}; + +use rocket::http::ContentType; +use rocket_contrib::databases::diesel::prelude::*; +use serde_json; + +pub fn server_with_db(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(); + + 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(); + + 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")]; + + 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 fetched_rooms: Vec = + 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")); + }) +} + +#[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)); + + let fetched_films: Vec = + serde_json::from_str(&response.body_string().unwrap()).unwrap(); + + assert_eq!(fetched_films.len(), 3); + + let film_names: Vec<&str> = fetched_films.iter().map(|film| film.title.as_str()).collect(); + assert!(film_names.contains(&"Hellraiser")); + assert!(film_names.contains(&"Huset")); + assert!(film_names.contains(&"Skuld")); + }); +} diff --git a/tests/room_tests.rs b/tests/room_tests.rs deleted file mode 100644 index 55102c5..0000000 --- a/tests/room_tests.rs +++ /dev/null @@ -1,104 +0,0 @@ -/* - Social program for Ramaskrik. - Copyright (C) 2019 Harald Eilertsen - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see . -*/ - -use ramaskrik; -use ramaskrik::models::{ - film::{ Film, NewFilm }, - room::Room -}; - -use rocket::http::ContentType; -use rocket_contrib::databases::diesel::prelude::*; -use serde_json; - -pub fn server_with_db(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(); - - 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(); - - 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")]; - - 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 fetched_rooms: Vec = - 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")); - }) -} - -#[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)); - - let fetched_films: Vec = - serde_json::from_str(&response.body_string().unwrap()).unwrap(); - - assert_eq!(fetched_films.len(), 3); - - let film_names: Vec<&str> = fetched_films.iter().map(|film| film.title.as_str()).collect(); - assert!(film_names.contains(&"Hellraiser")); - assert!(film_names.contains(&"Huset")); - assert!(film_names.contains(&"Skuld")); - }); -} -- cgit v1.2.3