init push
This commit is contained in:
238
internal/repository/mysql/opc_repo_test.go
Normal file
238
internal/repository/mysql/opc_repo_test.go
Normal file
@@ -0,0 +1,238 @@
|
||||
package mysql
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
|
||||
"wucher/internal/domain/opc"
|
||||
"wucher/internal/shared/pkg/uuidv7"
|
||||
)
|
||||
|
||||
func openOpcTestDB(t *testing.T) *gorm.DB {
|
||||
t.Helper()
|
||||
dsn := fmt.Sprintf("file:opc_test_%d?mode=memory&cache=shared", time.Now().UnixNano())
|
||||
db, err := gorm.Open(sqlite.Open(dsn), &gorm.Config{
|
||||
NowFunc: func() time.Time { return time.Now().UTC() },
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("open sqlite: %v", err)
|
||||
}
|
||||
if err := db.AutoMigrate(&opc.Opc{}); err != nil {
|
||||
t.Fatalf("auto migrate: %v", err)
|
||||
}
|
||||
return db
|
||||
}
|
||||
|
||||
func TestNewOpcRepository(t *testing.T) {
|
||||
db := openOpcTestDB(t)
|
||||
repo := NewOpcRepository(db)
|
||||
if repo == nil || repo.db == nil {
|
||||
t.Fatalf("expected repository initialized")
|
||||
}
|
||||
}
|
||||
|
||||
func TestOpcRepositoryCreate(t *testing.T) {
|
||||
db := openOpcTestDB(t)
|
||||
repo := NewOpcRepository(db)
|
||||
row := &opc.Opc{Title: "Main"}
|
||||
|
||||
if err := repo.Create(context.Background(), row); err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if len(row.ID) == 0 {
|
||||
t.Fatalf("expected id set")
|
||||
}
|
||||
inactive := &opc.Opc{Title: "Inactive", IsActive: false}
|
||||
if err := repo.Create(context.Background(), inactive); err != nil {
|
||||
t.Fatalf("create inactive: %v", err)
|
||||
}
|
||||
gotInactive, err := repo.GetByID(context.Background(), inactive.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("get inactive: %v", err)
|
||||
}
|
||||
if gotInactive == nil || gotInactive.IsActive {
|
||||
t.Fatalf("expected inactive opc persisted as false")
|
||||
}
|
||||
|
||||
sqlDB, _ := db.DB()
|
||||
_ = sqlDB.Close()
|
||||
if err := repo.Create(context.Background(), &opc.Opc{Title: "AfterClose"}); err == nil {
|
||||
t.Fatalf("expected error on closed DB")
|
||||
}
|
||||
}
|
||||
|
||||
func TestOpcRepositoryUpdate(t *testing.T) {
|
||||
db := openOpcTestDB(t)
|
||||
repo := NewOpcRepository(db)
|
||||
row := &opc.Opc{Title: "Old"}
|
||||
if err := repo.Create(context.Background(), row); err != nil {
|
||||
t.Fatalf("seed create: %v", err)
|
||||
}
|
||||
|
||||
row.Title = "New"
|
||||
if err := repo.Update(context.Background(), row); err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
loaded, err := repo.GetByID(context.Background(), row.ID)
|
||||
if err != nil || loaded == nil || loaded.Title != "New" {
|
||||
t.Fatalf("expected updated row")
|
||||
}
|
||||
|
||||
sqlDB, _ := db.DB()
|
||||
_ = sqlDB.Close()
|
||||
if err := repo.Update(context.Background(), row); err == nil {
|
||||
t.Fatalf("expected error on closed DB")
|
||||
}
|
||||
}
|
||||
|
||||
func TestOpcRepositoryDelete(t *testing.T) {
|
||||
db := openOpcTestDB(t)
|
||||
repo := NewOpcRepository(db)
|
||||
row := &opc.Opc{Title: "DeleteMe"}
|
||||
if err := repo.Create(context.Background(), row); err != nil {
|
||||
t.Fatalf("seed create: %v", err)
|
||||
}
|
||||
deletedBy := uuidv7.MustBytes()
|
||||
|
||||
if err := repo.Delete(context.Background(), row.ID, deletedBy); err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
got, err := repo.GetByID(context.Background(), row.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("get by id after delete: %v", err)
|
||||
}
|
||||
if got != nil {
|
||||
t.Fatalf("expected soft deleted row hidden from GetByID")
|
||||
}
|
||||
}
|
||||
|
||||
func TestOpcRepositoryGetByID(t *testing.T) {
|
||||
t.Run("found", func(t *testing.T) {
|
||||
db := openOpcTestDB(t)
|
||||
repo := NewOpcRepository(db)
|
||||
row := &opc.Opc{Title: "Found"}
|
||||
if err := repo.Create(context.Background(), row); err != nil {
|
||||
t.Fatalf("seed create: %v", err)
|
||||
}
|
||||
|
||||
got, err := repo.GetByID(context.Background(), row.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if got == nil || got.Title != "Found" {
|
||||
t.Fatalf("expected row found")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("not found", func(t *testing.T) {
|
||||
db := openOpcTestDB(t)
|
||||
repo := NewOpcRepository(db)
|
||||
got, err := repo.GetByID(context.Background(), uuidv7.MustBytes())
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if got != nil {
|
||||
t.Fatalf("expected nil for not found")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("db error", func(t *testing.T) {
|
||||
db := openOpcTestDB(t)
|
||||
repo := NewOpcRepository(db)
|
||||
sqlDB, _ := db.DB()
|
||||
_ = sqlDB.Close()
|
||||
|
||||
if _, err := repo.GetByID(context.Background(), uuidv7.MustBytes()); err == nil {
|
||||
t.Fatalf("expected error on closed DB")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestOpcRepositoryList(t *testing.T) {
|
||||
t.Run("success without limit", func(t *testing.T) {
|
||||
db := openOpcTestDB(t)
|
||||
repo := NewOpcRepository(db)
|
||||
_ = repo.Create(context.Background(), &opc.Opc{Title: "C"})
|
||||
_ = repo.Create(context.Background(), &opc.Opc{Title: "A"})
|
||||
|
||||
rows, total, err := repo.List(context.Background(), "", "", 0, 0)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if total != 2 || len(rows) != 2 {
|
||||
t.Fatalf("expected 2 rows, total=%d len=%d", total, len(rows))
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("success with filter sort and limit", func(t *testing.T) {
|
||||
db := openOpcTestDB(t)
|
||||
repo := NewOpcRepository(db)
|
||||
_ = repo.Create(context.Background(), &opc.Opc{Title: "Main Base"})
|
||||
_ = repo.Create(context.Background(), &opc.Opc{Title: "Backup"})
|
||||
|
||||
rows, total, err := repo.List(context.Background(), "Main", "name DESC", 1, 0)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if total != 1 || len(rows) != 1 || rows[0].Title != "Main Base" {
|
||||
t.Fatalf("unexpected list result")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("count error", func(t *testing.T) {
|
||||
db := openOpcTestDB(t)
|
||||
repo := NewOpcRepository(db)
|
||||
if err := db.Migrator().DropTable(&opc.Opc{}); err != nil {
|
||||
t.Fatalf("drop table: %v", err)
|
||||
}
|
||||
|
||||
if _, _, err := repo.List(context.Background(), "", "", 10, 0); err == nil {
|
||||
t.Fatalf("expected count error")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("find error", func(t *testing.T) {
|
||||
db := openOpcTestDB(t)
|
||||
repo := NewOpcRepository(db)
|
||||
_ = repo.Create(context.Background(), &opc.Opc{Title: "Main"})
|
||||
|
||||
if _, _, err := repo.List(context.Background(), "", "name ASC, )", 10, 0); err == nil {
|
||||
t.Fatalf("expected find error from invalid sort")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("default order active sortkey first and inactive last", func(t *testing.T) {
|
||||
db := openOpcTestDB(t)
|
||||
repo := NewOpcRepository(db)
|
||||
_ = repo.Create(context.Background(), &opc.Opc{Title: "Gamma", IsActive: true})
|
||||
_ = repo.Create(context.Background(), &opc.Opc{Title: "Beta", SortKey: intPtrOpcRepo(0), IsActive: true})
|
||||
_ = repo.Create(context.Background(), &opc.Opc{Title: "Charlie", SortKey: intPtrOpcRepo(2), IsActive: true})
|
||||
_ = repo.Create(context.Background(), &opc.Opc{Title: "Alpha", SortKey: intPtrOpcRepo(1), IsActive: true})
|
||||
_ = repo.Create(context.Background(), &opc.Opc{Title: "Zulu", IsActive: false})
|
||||
_ = repo.Create(context.Background(), &opc.Opc{Title: "Bravo", SortKey: intPtrOpcRepo(9), IsActive: false})
|
||||
|
||||
rows, total, err := repo.List(context.Background(), "", "", 0, 0)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if total != 6 || len(rows) != 6 {
|
||||
t.Fatalf("unexpected total/len total=%d len=%d", total, len(rows))
|
||||
}
|
||||
|
||||
gotOrder := []string{rows[0].Title, rows[1].Title, rows[2].Title, rows[3].Title, rows[4].Title, rows[5].Title}
|
||||
wantOrder := []string{"Beta", "Alpha", "Charlie", "Gamma", "Bravo", "Zulu"}
|
||||
for i := range wantOrder {
|
||||
if gotOrder[i] != wantOrder[i] {
|
||||
t.Fatalf("unexpected default order: got=%v want=%v", gotOrder, wantOrder)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func intPtrOpcRepo(v int) *int { return &v }
|
||||
Reference in New Issue
Block a user