summaryrefslogtreecommitdiff
path: root/server/tests/common/mod.rs
blob: f88a9706db15482d3fadf78da2f8616430840e2e (plain)
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
35
36
37
38
use std::env;

use env_logger::Env;
use sqlx::{PgPool, postgres::PgPoolOptions};

pub fn test_setup() {
    dotenv::dotenv().ok();
    let _ = env_logger::Builder::from_env(
        Env::default().default_filter_or("info"),
    )
        .is_test(true)
        .try_init();
}

#[allow(dead_code)]  // Only used in ignored tests.
pub struct TestDb {
    db_pool: PgPool,
}

#[allow(dead_code)]  // Only used in ignored tests.
impl TestDb {
    pub async fn new() -> Self {
        test_setup();
        let db_url = env::var("DATABASE_URL").expect("DATABASE_URL");
        let db_pool = PgPoolOptions::new()
            .max_connections(10)
            .connect(&db_url)
            .await
        .expect("db connection");
        Self {
            db_pool
        }
    }

    pub fn db(&self) -> PgPool {
        self.db_pool.clone()
    }
}