use crate::db::{Config, Database, DatabasePool, PoolConfig};
use crate::error::Result;
pub struct TestPool {
pool: DatabasePool,
}
impl TestPool {
pub async fn new() -> Self {
let config = Config {
path: ":memory:".to_string(),
pool: Some(PoolConfig {
base_path: ":memory:".to_string(),
lock_shards: 4,
}),
..Default::default()
};
let pool = DatabasePool::new(&config)
.await
.expect("failed to create test pool");
Self { pool }
}
pub async fn exec(self, shard: Option<&str>, sql: &str) -> Self {
use crate::db::ConnExt;
let db = self
.pool
.conn(shard)
.await
.unwrap_or_else(|e| panic!("failed to get connection for shard {shard:?}: {e}"));
db.conn()
.execute_raw(sql, ())
.await
.unwrap_or_else(|e| panic!("failed to execute SQL: {e}\nSQL: {sql}"));
self
}
pub async fn conn(&self, shard: Option<&str>) -> Result<Database> {
self.pool.conn(shard).await
}
pub fn pool(&self) -> DatabasePool {
self.pool.clone()
}
}