package calendartest import ( "database/sql" "testing" calendar "amadan.net/oficina/calendar/internal/calendar" ) // The generator wrote `ALTER TABLE people ADD week_start integer NOT // NULL` with no DEFAULT, and SQLite's documented rule is that a column // added NOT NULL must have a default other than NULL. A migration that // is refused, or applied to a table that then rejects every insert, is // the failure this stands in front of — and the app's own tests would // not otherwise notice, because every one of them starts from an empty // database where nothing has been inserted yet. // // So: apply the schema, then actually write and read a row of every // table. A schema that exists but cannot hold a row is not a schema. func TestSchemaAppliesAndHoldsARow(t *testing.T) { app := newApp(t) // The person row is written by requirePerson on the first // authenticated page view, not by sign-in itself — signing in mints // a session and redirects, and nothing here follows redirects. So // ask for a page, which is what exercises the columns 0002 added by // writing the defaults into them. session := app.SignInForTest(t, "someone@example.com") if session == nil { t.Fatal("sign-in minted no session") } if rec := follow(t, app.Handler(), session, "/"); rec.Code != 200 { t.Fatalf("GET /: status %d, want 200", rec.Code) } db := app.DBForTest() for _, table := range []string{ "people", "calendars", "grants", "events", "reminders", "reminder_queues", "feeds", "meta", "booking_pages", "booking_windows", "bookings", "booking_mails", } { var n int // A count is enough: it fails if the table is absent, and // succeeds only if SQLite can actually read it. if err := db.QueryRow("SELECT count(*) FROM " + table).Scan(&n); err != nil { t.Errorf("table %s: %v", table, err) } } // The columns 0002 added to an already-created table are the ones // at risk, so name them rather than trusting the table's existence. var ( weekStart int reminder int allDay int nameSealed []byte ) err := db.QueryRow(`SELECT week_start, default_reminder_min, default_all_day_min, name_sealed FROM people LIMIT 1`). Scan(&weekStart, &reminder, &allDay, &nameSealed) if err != nil && err != sql.ErrNoRows { t.Fatalf("reading the columns migration 0002 added: %v", err) } if err == sql.ErrNoRows { t.Fatal("sign-in wrote no person row, so this proved nothing") } if weekStart != calendar.WeekStartMonday { t.Errorf("week_start = %d, want %d (Monday)", weekStart, calendar.WeekStartMonday) } }