diff options
| author | Joe Carstairs <me@joeac.net> | 2024-11-17 12:16:19 +0000 |
|---|---|---|
| committer | Joe Carstairs <me@joeac.net> | 2024-11-17 12:20:48 +0000 |
| commit | 99fa72e2b9d572e00cd232bd55a3fa24722c3134 (patch) | |
| tree | 6b50ce600fffa07a0c21b7f14d2447fe5ac3c2ed /rust/schist_models/tests/common/test_context.rs | |
| parent | 10f071447594f2bdcec84a461df9ad648f71f44b (diff) | |
Refactors workspace
Diffstat (limited to 'rust/schist_models/tests/common/test_context.rs')
| -rw-r--r-- | rust/schist_models/tests/common/test_context.rs | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/rust/schist_models/tests/common/test_context.rs b/rust/schist_models/tests/common/test_context.rs new file mode 100644 index 0000000..fb4112d --- /dev/null +++ b/rust/schist_models/tests/common/test_context.rs @@ -0,0 +1,44 @@ +use std::sync::atomic::{AtomicU64, Ordering}; + +use diesel::{Connection, SqliteConnection}; +use diesel_migrations::MigrationHarness; +use schist_queries::clear::clear; +use schist_models::migrations::MIGRATIONS; + +pub struct TestContext { + pub db_url: String, +} + +static ID_COUNTER: AtomicU64 = AtomicU64::new(0); + +impl TestContext { + pub fn new() -> Self { + let db_url = Self::next_id() + ".sqlite"; + + let connection = &mut SqliteConnection + ::establish(&db_url) + .expect("failed to connect to database"); + + connection + .run_pending_migrations(MIGRATIONS) + .expect("failed to run migrations"); + + clear(connection) + .expect("failed to clear database"); + + Self { + db_url, + } + } + + fn next_id() -> String { + ID_COUNTER.fetch_add(1, Ordering::Relaxed).to_string() + } +} + +impl Drop for TestContext { + fn drop(&mut self) { + std::fs::remove_file(&self.db_url) + .expect(format!("failed to delete database {}", self.db_url).as_str()); + } +} |
