#![cfg(feature = "snapshots")]
use std::sync::Arc;
use switchy_schema::discovery::embedded::EmbeddedMigration;
use switchy_schema_test_utils::{
MigrationSnapshotTest, MigrationTestBuilder, create_empty_in_memory,
};
#[test_log::test(switchy_async::test(real_fs))]
async fn test_simple_snapshot_example() {
MigrationSnapshotTest::new("user_migration")
.migrations_dir("./test-resources/snapshot-migrations/minimal")
.assert_schema(true)
.assert_sequence(true)
.run()
.await
.unwrap();
}
#[test_log::test(switchy_async::test(real_fs))]
async fn test_complex_integration_example() {
let db = create_empty_in_memory().await.unwrap();
let migrations: Vec<Arc<dyn switchy_schema::migration::Migration + '_>> = vec![
Arc::new(EmbeddedMigration::new(
"001_create_users".to_string(),
Some("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT NOT NULL);".into()),
Some("DROP TABLE IF EXISTS users;".into()),
)),
Arc::new(EmbeddedMigration::new(
"002_add_email".to_string(),
Some("ALTER TABLE users ADD COLUMN email TEXT;".into()),
Some("ALTER TABLE users DROP COLUMN email;".into()),
)),
];
MigrationTestBuilder::new(migrations.clone())
.with_data_before("002_add_email", |db| {
Box::pin(async move {
db.exec_raw("INSERT INTO users (name) VALUES ('test_user')")
.await
})
})
.run(db.as_ref())
.await
.unwrap();
MigrationSnapshotTest::new("data_migration_result")
.with_database(db) .expected_tables(vec!["users".to_string()])
.assert_schema(true)
.assert_data(true)
.with_data_samples("users", 5)
.run()
.await
.unwrap();
}
#[test_log::test(switchy_async::test(real_fs))]
async fn test_comprehensive_snapshot_example() {
MigrationSnapshotTest::new("comprehensive_test")
.migrations_dir("./test-resources/snapshot-migrations/comprehensive")
.assert_schema(true)
.assert_sequence(true)
.assert_data(true)
.with_data_samples("users", 3)
.with_data_samples("posts", 5)
.redact_timestamps(true)
.redact_auto_ids(true)
.with_setup(|db| {
Box::pin(async move {
db.exec_raw("CREATE TABLE IF NOT EXISTS config (key TEXT, value TEXT)")
.await?;
db.exec_raw("INSERT INTO config (key, value) VALUES ('version', '1.0')")
.await
})
})
.with_verification(|db| {
Box::pin(async move {
let query = db.select("users");
let rows = query.execute(db).await?;
let count = rows.len();
assert!(count == count); Ok(())
})
})
.run()
.await
.unwrap();
}