rastrillo / aviso Public

Clone
git clone https://amadan.net/rastrillo/aviso

Plain git — no account needed to clone.

Download

Download this file

1package aviso_test
2
3import (
4 "context"
5 "database/sql"
6 "path/filepath"
7 "testing"
8
9 "amadan.net/rastrillo/rastrillo/db"
10 "amadan.net/rastrillo/rastrillo/migrate"
11
12 "amadan.net/rastrillo/aviso"
13)
14
15// openDB is a fresh on-disk rastrillo db with aviso.Schema applied —
16// what every store and handler test starts from.
17func openDB(t *testing.T) *sql.DB {
18 t.Helper()
19 d, err := db.Open(filepath.Join(t.TempDir(), "aviso.db"), nil)
20 if err != nil {
21 t.Fatalf("db.Open: %v", err)
22 }
23 t.Cleanup(func() { d.Close() })
24 if _, err := migrate.Apply(context.Background(), d, aviso.Schema); err != nil {
25 t.Fatalf("migrate.Apply: %v", err)
26 }
27 return d.Writer()
28}
29
30func TestSchemaCreatesTheTableAndReplaysCleanly(t *testing.T) {
31 d, err := db.Open(filepath.Join(t.TempDir(), "aviso.db"), nil)
32 if err != nil {
33 t.Fatal(err)
34 }
35 defer d.Close()
36 for i := 0; i < 2; i++ { // second Apply must be a no-op, not a failure
37 if _, err := migrate.Apply(context.Background(), d, aviso.Schema); err != nil {
38 t.Fatalf("apply %d: %v", i, err)
39 }
40 }
41 var name string
42 err = d.Writer().QueryRow(`SELECT name FROM sqlite_master WHERE type='table' AND name='aviso_subscriptions'`).Scan(&name)
43 if err != nil {
44 t.Fatalf("table missing: %v", err)
45 }
46}
47