package aviso_test import ( "context" "database/sql" "path/filepath" "testing" "amadan.net/rastrillo/rastrillo/db" "amadan.net/rastrillo/rastrillo/migrate" "amadan.net/rastrillo/aviso" ) // openDB is a fresh on-disk rastrillo db with aviso.Schema applied — // what every store and handler test starts from. func openDB(t *testing.T) *sql.DB { t.Helper() d, err := db.Open(filepath.Join(t.TempDir(), "aviso.db"), nil) if err != nil { t.Fatalf("db.Open: %v", err) } t.Cleanup(func() { d.Close() }) if _, err := migrate.Apply(context.Background(), d, aviso.Schema); err != nil { t.Fatalf("migrate.Apply: %v", err) } return d.Writer() } func TestSchemaCreatesTheTableAndReplaysCleanly(t *testing.T) { d, err := db.Open(filepath.Join(t.TempDir(), "aviso.db"), nil) if err != nil { t.Fatal(err) } defer d.Close() for i := 0; i < 2; i++ { // second Apply must be a no-op, not a failure if _, err := migrate.Apply(context.Background(), d, aviso.Schema); err != nil { t.Fatalf("apply %d: %v", i, err) } } var name string err = d.Writer().QueryRow(`SELECT name FROM sqlite_master WHERE type='table' AND name='aviso_subscriptions'`).Scan(&name) if err != nil { t.Fatalf("table missing: %v", err) } }