diff options
| author | Joe Carstairs <jcarstairs@scottlogic.com> | 2025-07-28 13:22:53 +0100 |
|---|---|---|
| committer | Joe Carstairs <jcarstairs@scottlogic.com> | 2025-07-28 13:30:27 +0100 |
| commit | 2361adeb0ca77904138e3466d51c2f44cf6df613 (patch) | |
| tree | f6e7192908f99fab62061913d8a1e9d2b918414b /schist_core/schist_models/tests/common | |
| parent | c7ed1e61ce2218be0b9e60577454124c4e43a366 (diff) | |
Tidier folders
Diffstat (limited to 'schist_core/schist_models/tests/common')
| -rw-r--r-- | schist_core/schist_models/tests/common/mod.rs | 1 | ||||
| -rw-r--r-- | schist_core/schist_models/tests/common/test_context.rs | 44 |
2 files changed, 45 insertions, 0 deletions
diff --git a/schist_core/schist_models/tests/common/mod.rs b/schist_core/schist_models/tests/common/mod.rs new file mode 100644 index 0000000..5ff67da --- /dev/null +++ b/schist_core/schist_models/tests/common/mod.rs @@ -0,0 +1 @@ +pub mod test_context; diff --git a/schist_core/schist_models/tests/common/test_context.rs b/schist_core/schist_models/tests/common/test_context.rs new file mode 100644 index 0000000..fb4112d --- /dev/null +++ b/schist_core/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()); + } +} |
