oficina / sheets Public

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

Plain git — no account needed to clone.

Download

Download this file

1package store_test
2
3import (
4 "context"
5 "testing"
6
7 rdb "amadan.net/rastrillo/rastrillo/db"
8 "amadan.net/rastrillo/rastrillo/migrate"
9 "gorm.io/gorm"
10
11 "amadan.net/oficina/sheets/internal/sheets"
12)
13
14// openTestDB opens a real SQLite database through rastrillo's db package —
15// the same cgo-free modernc driver, WAL journal mode and pragma order the
16// app uses at runtime — and applies sheets.BootSchema onto it through the
17// same migrate.Apply call production boots with (cmd/sheets/main.go),
18// rather than GORM's AutoMigrate: a test schema built one way and a
19// production schema built another are two definitions of one schema, and
20// the whole point of BootSchema existing is that there is exactly one.
21// This is deliberately not sqlite's :memory: mode and not a hand-rolled
22// driver setup: the plaintext gate has to inspect the actual file bytes an
23// actor with bucket access would see, so the test path has to be the real
24// one.
25func openTestDB(t *testing.T, path string) *gorm.DB {
26 t.Helper()
27 d, err := rdb.Open(path, nil)
28 if err != nil {
29 t.Fatalf("open db: %v", err)
30 }
31 t.Cleanup(func() {
32 if err := d.Close(); err != nil {
33 t.Errorf("close db: %v", err)
34 }
35 })
36 if _, err := migrate.Apply(context.Background(), d, sheets.BootSchema); err != nil {
37 t.Fatalf("migrate: %v", err)
38 }
39 return d.G
40}
41
42// make32 returns a 32-byte all-zero stand-in DEK, sufficient for seal.New
43// in tests that care about sealing behaviour, not key material.
44func make32() []byte {
45 return make([]byte, 32)
46}
47
48// checkpoint forces WAL frames into the main database file with
49// PRAGMA wal_checkpoint(TRUNCATE), so a test that reads the main file
50// afterward sees settled bytes rather than data that may still be sitting
51// only in the WAL (or, before any write reaches the OS, only in a driver
52// buffer in memory).
53func checkpoint(t *testing.T, db *gorm.DB) {
54 t.Helper()
55 if err := db.Exec("PRAGMA wal_checkpoint(TRUNCATE)").Error; err != nil {
56 t.Fatalf("checkpoint: %v", err)
57 }
58}
59