diff options
-rw-r--r-- | src/main.rs | 35 | ||||
-rw-r--r-- | src/test/test_helpers.rs | 34 | ||||
-rw-r--r-- | src/utils/mod.rs | 2 |
3 files changed, 63 insertions, 8 deletions
diff --git a/src/main.rs b/src/main.rs index b3e45ed..7d75c61 100644 --- a/src/main.rs +++ b/src/main.rs @@ -22,17 +22,38 @@ mod models; mod schema; use controllers::{home_controller, login_controller, posts_controller, users_controller}; +fn build_rocket(pool: utils::Pool) -> rocket::Rocket { + rocket::ignite() + .manage(pool) + .mount("/", home_controller::routes()) + .mount("/posts", posts_controller::routes()) + .mount("/users", users_controller::routes()) + .mount("/login", login_controller::routes()) +} + fn main() { if let Ok(dburl) = dotenv::var("DATABASE_URL") { - rocket::ignite() - .manage(utils::init_db_pool(&dburl)) - .mount("/", home_controller::routes()) - .mount("/posts", posts_controller::routes()) - .mount("/users", users_controller::routes()) - .mount("/login", login_controller::routes()) - .launch(); + let pool = utils::init_db_pool(&dburl); + build_rocket(pool).launch(); } else { eprintln!("Error: No database specified, make sure the DATABASE_URL env var is set."); }; } + +#[cfg(test)] +mod test { + mod test_helpers; + + use super::build_rocket; + use rocket::local::Client; + use rocket::http::Status; + + #[test] + fn get_landing_page() { + let pool = test_helpers::init_test_db_pool(); + let client = Client::new(build_rocket(pool)).expect("Rocket not built!"); + let response = client.get("/").dispatch(); + assert_eq!(response.status(), Status::Ok); + } +} diff --git a/src/test/test_helpers.rs b/src/test/test_helpers.rs new file mode 100644 index 0000000..4c1b2d3 --- /dev/null +++ b/src/test/test_helpers.rs @@ -0,0 +1,34 @@ +use dotenv; +use utils; +use diesel::{Connection, PgConnection}; +use r2d2::{CustomizeConnection, Pool}; +use r2d2_diesel::{ConnectionManager, Error}; + +fn test_database_url() -> String { + dotenv::var("TEST_DATABASE_URL") + .or_else(|_| dotenv::var("DATABASE_URL")) + .expect("DATABASE_URL must be set in order to run tests") +} + +#[derive(Debug)] +struct TestConnectionCustomizer{} + +impl CustomizeConnection<PgConnection, Error> for TestConnectionCustomizer { + /// Customize connections returned by the pool, so that + /// our tests don't modify the real database. + fn on_acquire(&self, conn: &mut PgConnection) -> Result<(), Error> { + conn + .begin_test_transaction() + .map_err(|e| Error::QueryError(e)) + } + + fn on_release(&self, _conn: PgConnection) {} +} + +pub fn init_test_db_pool() -> utils::Pool { + let manager = ConnectionManager::<PgConnection>::new(test_database_url()); + Pool::builder() + .connection_customizer(Box::new(TestConnectionCustomizer {})) + .build(manager) + .expect("db pool") +} diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 23b551e..b7811d4 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -4,7 +4,7 @@ use r2d2_diesel::ConnectionManager; use std::fmt::Display; // An alias to the type for a pool of Diesel PostgreSql connections. -type Pool = r2d2::Pool<ConnectionManager<PgConnection>>; +pub type Pool = r2d2::Pool<ConnectionManager<PgConnection>>; /// Initializes a database pool. pub fn init_db_pool(dburl: &str) -> Pool { |