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(); } }