a18f96912d
interfaces - refactor: split UserRepository into AuthUserRepository and AdminUserRepository capabilities - refactor: split SessionRepository into AuthSessionRepository and repository-owned CLI/admin methods - refactor: replace generic Repository Update/Delete with operation-specific params and ownership predicates - refactor: replace CredentialRepository generic CRUD with passkey-specific methods - refactor: replace FileRepository generic Create/Update/Delete with UploadedFileParams, DirectoryParams, and owned soft-delete - refactor: remove repository fields from WebApp struct; repositories are now composition-time wiring only - feat: add domain error kinds ErrParentNotFound, ErrParentNotDir, ErrDirectoryNotEmpty, ErrInvalidMove - feat: add CredentialTypeAppPasskey constant - feat: add testutil.SetUserAdmin for test fixture setup that bypasses production service ports - test: add architecture test banning GORM Save in repository package - test: add capability interface contract tests ensuring each service receives the minimal interface - test: add blockingStorage helper for concurrent promotion tests - test: add preserved DSN parameter test for sqliteImmediateDSN - docs: update architecture decisions with repository write rules and capability separation - docs: update roadmap to clarify atomic single-use refresh sessions - docs: add -race test target to development docs
79 lines
1.8 KiB
Go
79 lines
1.8 KiB
Go
package repository
|
|
|
|
import (
|
|
"net/url"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/dhao2001/mygo/internal/config"
|
|
)
|
|
|
|
func TestSQLiteImmediateDSNPreservesParameters(t *testing.T) {
|
|
dsn := sqliteImmediateDSN("file:test.db?cache=shared&_busy_timeout=9000&_txlock=deferred")
|
|
base, rawQuery, found := strings.Cut(dsn, "?")
|
|
if !found || base != "file:test.db" {
|
|
t.Fatalf("DSN base = %q, want file:test.db", base)
|
|
}
|
|
query, err := url.ParseQuery(rawQuery)
|
|
if err != nil {
|
|
t.Fatalf("parse DSN query: %v", err)
|
|
}
|
|
if query.Get("cache") != "shared" || query.Get("_busy_timeout") != "9000" {
|
|
t.Fatalf("DSN did not preserve parameters: %q", dsn)
|
|
}
|
|
if query.Get("_txlock") != "immediate" {
|
|
t.Fatalf("_txlock = %q, want immediate", query.Get("_txlock"))
|
|
}
|
|
}
|
|
|
|
func TestOpenSQLite(t *testing.T) {
|
|
cfg := config.DatabaseConfig{
|
|
Driver: "sqlite3",
|
|
SQLite: config.SQLiteConfig{Path: ":memory:"},
|
|
}
|
|
|
|
db, err := Open(cfg)
|
|
if err != nil {
|
|
t.Fatalf("Open(sqlite3) = %v", err)
|
|
}
|
|
|
|
sqlDB, err := db.DB()
|
|
if err != nil {
|
|
t.Fatalf("db.DB() = %v", err)
|
|
}
|
|
if err := sqlDB.Ping(); err != nil {
|
|
t.Fatalf("ping = %v", err)
|
|
}
|
|
}
|
|
|
|
func TestOpenUnsupportedDriver(t *testing.T) {
|
|
cfg := config.DatabaseConfig{Driver: "mysql"}
|
|
_, err := Open(cfg)
|
|
if err == nil {
|
|
t.Fatal("expected error for unsupported driver, got nil")
|
|
}
|
|
}
|
|
|
|
func TestAutoMigrate(t *testing.T) {
|
|
cfg := config.DatabaseConfig{
|
|
Driver: "sqlite3",
|
|
SQLite: config.SQLiteConfig{Path: ":memory:"},
|
|
}
|
|
|
|
db, err := Open(cfg)
|
|
if err != nil {
|
|
t.Fatalf("Open = %v", err)
|
|
}
|
|
|
|
if err := AutoMigrate(db); err != nil {
|
|
t.Fatalf("AutoMigrate = %v", err)
|
|
}
|
|
|
|
// Verify tables exist
|
|
for _, table := range []string{"users", "sessions", "files"} {
|
|
if !db.Migrator().HasTable(table) {
|
|
t.Errorf("table %q not found after migration", table)
|
|
}
|
|
}
|
|
}
|