Complete foundational data layer with repository implementation
- Add GORM dependencies for SQLite and PostgreSQL - Create domain models (User, Session, File) with common errors - Implement repository interfaces and database layer with migrations - Update WebApp to bootstrap with database and repositories - Add comprehensive unit tests for repository methods - Update config structure to support multiple database drivers - Extend AGENTS.md with debugging principles and dependency rules
This commit is contained in:
58
internal/repository/db_test.go
Normal file
58
internal/repository/db_test.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/dhao2001/mygo/internal/config"
|
||||
)
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user