package main import ( "fmt" "os" "strings" "testing" ) func TestMigrationNumber(t *testing.T) { n := len(migrations) entries, err := os.ReadDir(migrationDir) if err != nil { t.Fatalf("Failed to determine number of migration files: %v", err) } expectation := len(entries) if n != expectation { t.Fatalf("Expected %d migration files but got %d.", expectation, n) } } func TestMigrationContent(t *testing.T) { for i, migration := range migrations { path := migrationPath(i) expectation := fmt.Sprintf("PRAGMA user_version = %d;", i+1) if !strings.HasPrefix(migration, expectation) { t.Fatalf("Expected '%s' at start of '%s'.", expectation, path) } n := strings.Count(migration, `user_version`) if n != 1 { t.Fatalf( "'user_version' was mentioned more than once in %s. "+ "This is likely not intended.", path, ) } } } func TestMigrations(t *testing.T) { db, _ := emptyDB(t) for i := range migrations { path := migrationPath(i) t.Logf("Test execution of %s", path) err := Migrate(db, i) if err != nil { t.Fatalf("Failed to execute migration %s: %v", path, err) } } }