#[cfg(test)]
mod tests {
use somnia::Migrator;
fn fixtures() -> std::path::PathBuf {
std::path::PathBuf::from("tests/fixtures/migrations")
}
#[tokio::test]
async fn run_status_and_revert() {
let db = surrealdb::engine::any::connect("mem://").await.unwrap();
db.use_ns("t").use_db("t").await.unwrap();
let migrator = Migrator::new(db.clone(), fixtures());
let total = migrator.status().await.unwrap().len();
assert_eq!(total, 3, "expected 3 fixture migrations");
let applied = migrator.run().await.unwrap();
assert_eq!(
applied.len(),
3,
"all migrations should apply on a fresh DB"
);
let mut res = db.query("SELECT name FROM widget:one;").await.unwrap();
let rows: Vec<serde_json::Value> = res.take(0).unwrap();
assert_eq!(rows.first().and_then(|r| r["name"].as_str()), Some("first"));
let status = migrator.status().await.unwrap();
assert!(status.iter().all(|m| m.applied));
let ids: Vec<&str> = status.iter().map(|m| m.id.as_str()).collect();
let mut sorted = ids.clone();
sorted.sort();
assert_eq!(ids, sorted, "migrations must be timestamp-ordered");
assert!(migrator.run().await.unwrap().is_empty());
let last = migrator.revert_last().await.unwrap();
assert_eq!(last.as_deref(), ids.last().copied());
let rest = migrator.revert_all().await.unwrap();
assert_eq!(rest.len(), 2);
let after = migrator.status().await.unwrap();
assert!(after.iter().all(|m| !m.applied));
}
}