From cab86700278cb93d5f67919b39da6d8a7077d6ec Mon Sep 17 00:00:00 2001 From: John Vandenberg Date: Sat, 20 Apr 2024 11:36:00 +0800 Subject: [PATCH] Add test to unmigrate test models --- butane/tests/unmigrate.rs | 42 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 butane/tests/unmigrate.rs diff --git a/butane/tests/unmigrate.rs b/butane/tests/unmigrate.rs new file mode 100644 index 0000000..06f232a --- /dev/null +++ b/butane/tests/unmigrate.rs @@ -0,0 +1,42 @@ +//! Test the "current" migration created by the butane_test_helper due to +//! all of the other tests in the butane/tests directory. +#![cfg(test)] +use butane::db::{BackendConnection, Connection}; +use butane::migrations::{self, MemMigrations, Migration, Migrations, MigrationsMut}; +use butane_test_helper::*; + +fn unmigrate(mut connection: Connection) { + let base_dir = std::path::PathBuf::from(".butane"); + let root = base_dir.join("migrations"); + let mut disk_migrations = migrations::from_root(root); + + // this reproduces the migration created in butane_test_helper::setup_db + let disk_current = disk_migrations.current(); + let mut mem_migrations = MemMigrations::new(); + let mem_current = mem_migrations.current(); + migrations::copy_migration(disk_current, mem_current).unwrap(); + + assert!( + disk_current.db().unwrap().tables().count() != 0, + "No tables to migrate" + ); + + let backend_name = connection.backend_name(); + let backend = butane::db::get_backend(backend_name).unwrap(); + assert!( + mem_migrations + .create_migration(&nonempty::nonempty![backend], "init", None) + .expect("expected to create migration without error"), + "expected to create migration" + ); + + let migrations = mem_migrations.unapplied_migrations(&connection).unwrap(); + assert_eq!(migrations.len(), 0); + + let migration = mem_migrations.latest().unwrap(); + migration.downgrade(&mut connection).unwrap(); + + let migrations = mem_migrations.unapplied_migrations(&connection).unwrap(); + assert_eq!(migrations.len(), 1); +} +testall!(unmigrate);