2026-01-21 12:34:59 +01:00
|
|
|
// Copyright (C) 2024-2026 Whiterun LLC
|
2024-10-31 10:55:29 +00:00
|
|
|
//
|
|
|
|
|
// This software is licensed under the GNU Affero General Public License (AGPL), version 3.0 or later.
|
|
|
|
|
// A copy of the license can be found in the LICENSE file or at https://www.gnu.org/licenses/agpl-3.0.html
|
|
|
|
|
|
2025-05-20 16:17:32 +02:00
|
|
|
#[allow(clippy::items_after_test_module)]
|
2024-10-31 10:55:29 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
|
mod test_utils {
|
|
|
|
|
use crate::db::DB;
|
2026-02-17 17:47:43 +01:00
|
|
|
use sqlx::sqlite::SqliteConnectOptions;
|
|
|
|
|
use sqlx::SqlitePool;
|
|
|
|
|
use std::sync::atomic::{AtomicU64, Ordering};
|
2024-10-31 10:55:29 +00:00
|
|
|
|
2026-02-17 17:47:43 +01:00
|
|
|
static DB_COUNTER: AtomicU64 = AtomicU64::new(0);
|
|
|
|
|
|
|
|
|
|
/// Create an in-memory SqlitePool-based DB for tests.
|
|
|
|
|
/// The setup function receives a `SqlitePool` to create tables and seed data.
|
|
|
|
|
/// Foreign keys are disabled to match the old rusqlite test behavior.
|
|
|
|
|
/// Each call gets a unique shared-cache in-memory database so multiple pool
|
|
|
|
|
/// connections share the same data without interfering with other tests.
|
|
|
|
|
pub async fn mock_db_pool<F, Fut>(setup_fn: F) -> DB
|
2024-10-31 10:55:29 +00:00
|
|
|
where
|
2026-02-17 17:47:43 +01:00
|
|
|
F: FnOnce(SqlitePool) -> Fut,
|
|
|
|
|
Fut: std::future::Future<Output = ()>,
|
2024-10-31 10:55:29 +00:00
|
|
|
{
|
2026-02-17 17:47:43 +01:00
|
|
|
let id = DB_COUNTER.fetch_add(1, Ordering::SeqCst);
|
|
|
|
|
let uri = format!("file:testdb_{}?mode=memory&cache=shared", id);
|
|
|
|
|
let opts = SqliteConnectOptions::new()
|
|
|
|
|
.filename(&uri)
|
|
|
|
|
.foreign_keys(false);
|
|
|
|
|
let pool = SqlitePool::connect_with(opts).await.unwrap();
|
2024-10-31 10:55:29 +00:00
|
|
|
|
2026-02-17 17:47:43 +01:00
|
|
|
setup_fn(pool.clone()).await;
|
2024-10-31 10:55:29 +00:00
|
|
|
|
|
|
|
|
DB {
|
2026-02-17 17:47:43 +01:00
|
|
|
cauldron_w: pool.clone(),
|
|
|
|
|
cauldron_r: pool.clone(),
|
|
|
|
|
bcmr_w: pool.clone(),
|
|
|
|
|
bcmr_r: pool.clone(),
|
|
|
|
|
crc20_w: pool.clone(),
|
|
|
|
|
crc20_r: pool.clone(),
|
|
|
|
|
oracle_w: pool.clone(),
|
|
|
|
|
oracle_r: pool.clone(),
|
2026-03-17 17:40:01 +01:00
|
|
|
moria_w: pool.clone(),
|
|
|
|
|
moria_r: pool.clone(),
|
2026-04-27 02:02:38 +03:00
|
|
|
ido_w: pool.clone(),
|
|
|
|
|
ido_r: pool.clone(),
|
2024-10-31 10:55:29 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
pub use test_utils::mock_db_pool;
|