simple-web-app

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

commit f4d4bd9aebbf468a96f4bfdffa6a2dd0667cd8e3
parent 3020f927bc3b71cbcf06399a6d29e50b0d958225
Author: Silas Brack <silasbrack@gmail.com>
Date:   Sat, 13 Jun 2026 13:12:28 +0200

test: add migration version bump tests

Three tests against in-memory SQLite:
- each_migration_bumps_version: version equals migration count
- migrations_are_idempotent: running twice is a no-op
- incremental_migration_bumps_version: version >= N after migration N

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

Diffstat:
Msrc/migrations.rs | 63+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 63 insertions(+), 0 deletions(-)

diff --git a/src/migrations.rs b/src/migrations.rs @@ -85,3 +85,66 @@ pub fn run(conn: &Connection) -> Result<(), String> { eprintln!("Migrations complete. Database is now at version {}", total); Ok(()) } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn each_migration_bumps_version() { + let conn = Connection::open_in_memory().unwrap(); + conn.execute_batch("PRAGMA foreign_keys=ON;").unwrap(); + + run(&conn).unwrap(); + + let version: i64 = conn + .query_row("SELECT version FROM migration_version", [], |row| row.get(0)) + .unwrap(); + + assert_eq!( + version, + MIGRATIONS.len() as i64, + "version should equal number of migrations" + ); + } + + #[test] + fn migrations_are_idempotent() { + let conn = Connection::open_in_memory().unwrap(); + conn.execute_batch("PRAGMA foreign_keys=ON;").unwrap(); + + run(&conn).unwrap(); + run(&conn).unwrap(); // second run should be a no-op + + let version: i64 = conn + .query_row("SELECT version FROM migration_version", [], |row| row.get(0)) + .unwrap(); + + assert_eq!(version, MIGRATIONS.len() as i64); + } + + #[test] + fn incremental_migration_bumps_version() { + let conn = Connection::open_in_memory().unwrap(); + conn.execute_batch("PRAGMA foreign_keys=ON;").unwrap(); + + // Run all migrations + run(&conn).unwrap(); + + // Simulate a future migration by checking version after each step + for expected in 1..=MIGRATIONS.len() as i64 { + let version: i64 = conn + .query_row( + "SELECT version FROM migration_version WHERE version >= ?1", + [expected], + |row| row.get(0), + ) + .unwrap(); + + assert!( + version >= expected, + "version {version} should be >= {expected}" + ); + } + } +}