chore: initial commit of HouseholdBudgetBook (backend, frontend, desktop)
This commit is contained in:
commit
4a5db3f1f9
167 files changed
+21732
No files matched your search
@@ -0,0 +1,140 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestOpenConfiguresBoundedWALPool(t *testing.T) {
|
||||
conn, err := Open(filepath.Join(t.TempDir(), "book.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
if got := conn.Stats().MaxOpenConnections; got != 4 {
|
||||
t.Fatalf("MaxOpenConnections=%d, want 4", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOpenAppliesPragmasToEveryPooledConnection(t *testing.T) {
|
||||
conn, err := Open(filepath.Join(t.TempDir(), "book.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
for range sqlitePoolSize {
|
||||
pooled, err := conn.Conn(context.Background())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// 故意延迟到测试结束再释放,确保同时占满 4 个不同连接。
|
||||
defer pooled.Close()
|
||||
|
||||
var journalMode string
|
||||
var foreignKeys, busyTimeout int
|
||||
if err := pooled.QueryRowContext(context.Background(), "PRAGMA journal_mode").Scan(&journalMode); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := pooled.QueryRowContext(context.Background(), "PRAGMA foreign_keys").Scan(&foreignKeys); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := pooled.QueryRowContext(context.Background(), "PRAGMA busy_timeout").Scan(&busyTimeout); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if journalMode != "wal" || foreignKeys != 1 || busyTimeout != 5000 {
|
||||
t.Fatalf("pragmas=(%s,%d,%d), want (wal,1,5000)", journalMode, foreignKeys, busyTimeout)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestOpenAllowsWriteDuringReadTransaction(t *testing.T) {
|
||||
conn, err := Open(filepath.Join(t.TempDir(), "book.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer conn.Close()
|
||||
if _, err := conn.Exec("CREATE TABLE sample(id INTEGER PRIMARY KEY)"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
readTx, err := conn.BeginTx(context.Background(), nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer readTx.Rollback()
|
||||
var count int
|
||||
if err := readTx.QueryRow("SELECT COUNT(*) FROM sample").Scan(&count); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if _, err := conn.Exec("INSERT INTO sample(id) VALUES (1)"); err != nil {
|
||||
t.Fatalf("write while read transaction is open: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunMigrationsWithForeignKeysEnabled(t *testing.T) {
|
||||
conn, err := Open(filepath.Join(t.TempDir(), "book.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
if err := RunMigrations(conn); err != nil {
|
||||
t.Fatalf("run migrations: %v", err)
|
||||
}
|
||||
rows, err := conn.Query("PRAGMA foreign_key_check")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer rows.Close()
|
||||
if rows.Next() {
|
||||
t.Fatal("freshly migrated database contains foreign key violations")
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOpenAllowsConcurrentReads(t *testing.T) {
|
||||
conn, err := Open(filepath.Join(t.TempDir(), "book.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
if _, err := conn.Exec(`CREATE TABLE sample(id INTEGER PRIMARY KEY); INSERT INTO sample(id) VALUES (1)`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
const workers = 8
|
||||
const readsPerWorker = 20
|
||||
errs := make(chan error, workers)
|
||||
var wg sync.WaitGroup
|
||||
for range workers {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
for range readsPerWorker {
|
||||
var count int
|
||||
if err := conn.QueryRowContext(context.Background(), "SELECT COUNT(*) FROM sample").Scan(&count); err != nil {
|
||||
errs <- err
|
||||
return
|
||||
}
|
||||
if count != 1 {
|
||||
errs <- errors.New("unexpected sample count")
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
close(errs)
|
||||
for err := range errs {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user