package db_test import ( "path/filepath" "strings" "testing" "go.uber.org/fx/fxtest" "sneak.berlin/go/neoirc/internal/config" "sneak.berlin/go/neoirc/internal/db" "sneak.berlin/go/neoirc/internal/globals" "sneak.berlin/go/neoirc/internal/logger" ) const wantBusyTimeoutMS = 5000 // TestDefaultDBURLSetsWALAndBusyTimeout opens a file database // with the default URL's parameters the way the server does, // and checks the journal mode and busy timeout it ends up with. func TestDefaultDBURLSetsWALAndBusyTimeout(t *testing.T) { t.Parallel() _, params, _ := strings.Cut(config.DefaultDBURL, "?") dbURL := "file://" + filepath.Join(t.TempDir(), "state.db") + "?" + params lifecycle := fxtest.NewLifecycle(t) log, err := logger.New(lifecycle, logger.Params{ Globals: &globals.Globals{}, }) if err != nil { t.Fatal(err) } database, err := db.New(lifecycle, db.Params{ Logger: log, Config: &config.Config{DBURL: dbURL}, }) if err != nil { t.Fatal(err) } lifecycle.RequireStart() t.Cleanup(lifecycle.RequireStop) var journalMode string err = database.GetDB().QueryRowContext( t.Context(), "PRAGMA journal_mode", ).Scan(&journalMode) if err != nil { t.Fatal(err) } if journalMode != "wal" { t.Errorf("journal_mode = %q, want %q", journalMode, "wal") } var busyTimeout int err = database.GetDB().QueryRowContext( t.Context(), "PRAGMA busy_timeout", ).Scan(&busyTimeout) if err != nil { t.Fatal(err) } if busyTimeout != wantBusyTimeoutMS { t.Errorf( "busy_timeout = %d, want %d", busyTimeout, wantBusyTimeoutMS, ) } }