aboutsummaryrefslogtreecommitdiffstats
path: root/src/test/test_helpers.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/test/test_helpers.rs')
-rw-r--r--src/test/test_helpers.rs34
1 files changed, 34 insertions, 0 deletions
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")
+}