summaryrefslogtreecommitdiff
path: root/actualbudget_to_schist_transformer/tests/common/test_context.rs
blob: e59c5f8724f66afbcb7a8f10e59f9ac4f3b4755a (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
39
40
41
42
use anyhow::Context;
use diesel::{Connection, SqliteConnection};
use diesel_migrations::MigrationHarness;
use schist_queries::clear::clear;
use schist_models::migrations::MIGRATIONS;

use super::{TEST_IN_DB_URL, TEST_OUT_DB_URL};

pub struct TestContext {
}

impl TestContext {
    pub fn new() -> Self {
        let _in_connection = &mut SqliteConnection
            ::establish(&TEST_IN_DB_URL)
            .with_context(|| format!("failed to establish connection to in database at {}", TEST_IN_DB_URL))
            .unwrap();

        let out_connection = &mut SqliteConnection
            ::establish(&TEST_OUT_DB_URL)
            .with_context(|| format!("failed to establish connection to out database at {}", TEST_OUT_DB_URL))
            .unwrap();

        out_connection
            .run_pending_migrations(MIGRATIONS)
            .expect("failed to run migrations on out database");

        clear(out_connection)
            .expect("failed to clear database");

        Self {
        }
    }
}

impl Drop for TestContext {
    fn drop(&mut self) {
        std::fs::remove_file(&TEST_OUT_DB_URL)
            .with_context(|| format!("failed to delete out database at {}", TEST_OUT_DB_URL))
            .unwrap();
    }
}