diff options
| author | Joe Carstairs <me@joeac.net> | 2024-10-28 07:52:51 +0000 |
|---|---|---|
| committer | Joe Carstairs <me@joeac.net> | 2024-10-28 07:52:51 +0000 |
| commit | 6e9f0069f5d1e4cfbf7f9feb8536795a78c818f6 (patch) | |
| tree | 2d4751f541c6979fa73261983e3f09a925644b58 /backend/core/tests/common/test_context.rs | |
| parent | 5824c353f978a5b9ab9696451ee3014c6a614ffc (diff) | |
Integration tests for basic CRD queries
Diffstat (limited to 'backend/core/tests/common/test_context.rs')
| -rw-r--r-- | backend/core/tests/common/test_context.rs | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/backend/core/tests/common/test_context.rs b/backend/core/tests/common/test_context.rs new file mode 100644 index 0000000..f6142b0 --- /dev/null +++ b/backend/core/tests/common/test_context.rs @@ -0,0 +1,43 @@ +use std::sync::atomic::{AtomicU64, Ordering}; + +use budgeting_app_core::{queries::clear::clear, MIGRATIONS}; +use diesel::{Connection, SqliteConnection}; +use diesel_migrations::MigrationHarness; + +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()); + } +} |
