use crate::db::{Config, Database, connect};
pub struct TestDb {
db: Database,
}
impl TestDb {
pub async fn new() -> Self {
let config = Config {
path: ":memory:".to_string(),
..Default::default()
};
let db = connect(&config)
.await
.expect("failed to create test database");
Self { db }
}
pub async fn exec(self, sql: &str) -> Self {
use crate::db::ConnExt;
self.db
.conn()
.execute_raw(sql, ())
.await
.unwrap_or_else(|e| panic!("failed to execute SQL: {e}\nSQL: {sql}"));
self
}
pub async fn migrate(self, path: &str) -> Self {
crate::db::migrate(self.db.conn(), path)
.await
.unwrap_or_else(|e| panic!("failed to run migrations from '{path}': {e}"));
self
}
pub fn db(&self) -> Database {
self.db.clone()
}
}