blob: 4c1b2d3f22f273e8ddde0fa88b443eabe674370e (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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")
}
|