use std::io::Write;
fn migrations_dir() -> tempfile::TempDir {
let dir = tempfile::tempdir().unwrap();
let mut f = std::fs::File::create(dir.path().join("0001_create_widgets.sql")).unwrap();
write!(
f,
"CREATE TABLE widgets (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL);"
)
.unwrap();
dir
}
#[tokio::test]
async fn test_db_runs_migrations_and_is_idempotent_within_one_process() {
let dir = migrations_dir();
let pool = larust_testing::test_db(dir.path()).await.unwrap();
sqlx::query("INSERT INTO widgets (name) VALUES (?)")
.bind("gadget")
.execute(&pool)
.await
.unwrap();
let unused_dir = migrations_dir();
let pool_again = larust_testing::test_db(unused_dir.path()).await.unwrap();
let count: (i64,) = sqlx::query_as("SELECT COUNT(*) FROM widgets")
.fetch_one(&pool_again)
.await
.unwrap();
assert_eq!(
count.0, 1,
"the second test_db() call should see the row inserted through the first \
call's pool, proving both calls share one process-wide connection"
);
}