oficina / calendar Public

Clone
git clone https://amadan.net/oficina/calendar

Plain git — no account needed to clone.

Download

Download this file

1package calendartest
2
3import (
4 "database/sql"
5 "testing"
6
7 calendar "amadan.net/oficina/calendar/internal/calendar"
8)
9
10// The generator wrote `ALTER TABLE people ADD week_start integer NOT
11// NULL` with no DEFAULT, and SQLite's documented rule is that a column
12// added NOT NULL must have a default other than NULL. A migration that
13// is refused, or applied to a table that then rejects every insert, is
14// the failure this stands in front of — and the app's own tests would
15// not otherwise notice, because every one of them starts from an empty
16// database where nothing has been inserted yet.
17//
18// So: apply the schema, then actually write and read a row of every
19// table. A schema that exists but cannot hold a row is not a schema.
20func TestSchemaAppliesAndHoldsARow(t *testing.T) {
21 app := newApp(t)
22
23 // The person row is written by requirePerson on the first
24 // authenticated page view, not by sign-in itself — signing in mints
25 // a session and redirects, and nothing here follows redirects. So
26 // ask for a page, which is what exercises the columns 0002 added by
27 // writing the defaults into them.
28 session := app.SignInForTest(t, "someone@example.com")
29 if session == nil {
30 t.Fatal("sign-in minted no session")
31 }
32 if rec := follow(t, app.Handler(), session, "/"); rec.Code != 200 {
33 t.Fatalf("GET /: status %d, want 200", rec.Code)
34 }
35
36 db := app.DBForTest()
37 for _, table := range []string{
38 "people", "calendars", "grants", "events",
39 "reminders", "reminder_queues", "feeds", "meta",
40 "booking_pages", "booking_windows", "bookings", "booking_mails",
41 } {
42 var n int
43 // A count is enough: it fails if the table is absent, and
44 // succeeds only if SQLite can actually read it.
45 if err := db.QueryRow("SELECT count(*) FROM " + table).Scan(&n); err != nil {
46 t.Errorf("table %s: %v", table, err)
47 }
48 }
49
50 // The columns 0002 added to an already-created table are the ones
51 // at risk, so name them rather than trusting the table's existence.
52 var (
53 weekStart int
54 reminder int
55 allDay int
56 nameSealed []byte
57 )
58 err := db.QueryRow(`SELECT week_start, default_reminder_min, default_all_day_min, name_sealed
59 FROM people LIMIT 1`).
60 Scan(&weekStart, &reminder, &allDay, &nameSealed)
61 if err != nil && err != sql.ErrNoRows {
62 t.Fatalf("reading the columns migration 0002 added: %v", err)
63 }
64 if err == sql.ErrNoRows {
65 t.Fatal("sign-in wrote no person row, so this proved nothing")
66 }
67 if weekStart != calendar.WeekStartMonday {
68 t.Errorf("week_start = %d, want %d (Monday)", weekStart, calendar.WeekStartMonday)
69 }
70}
71