优化结构

This commit is contained in:
Yvan
2026-08-22 20:14:29 +08:00
parent 44ab14448a
commit 699bcceeea
90 changed files with 534 additions and 298 deletions
+58
View File
@@ -0,0 +1,58 @@
package system
import (
"context"
"kra/internal/biz/system"
"testing"
)
func TestAnnouncementRepositoryKeepsRawIDQuerySemantics(t *testing.T) {
data := newTransactionTestData(t)
repo := &announcementRepo{data: data}
ctx := context.Background()
item := &system.Announcement{Title: "notice"}
if err := repo.Create(ctx, item); err != nil {
t.Fatal(err)
}
found, err := repo.Find(ctx, "1")
if err != nil {
t.Fatal(err)
}
if found.ID != item.ID {
t.Fatalf("found announcement ID = %d, want %d", found.ID, item.ID)
}
if err = repo.DeleteByIDs(ctx, nil); err != nil {
t.Fatalf("empty bulk delete must be a successful no-op: %v", err)
}
if err = repo.Delete(ctx, "not-a-number"); err != nil {
t.Fatalf("raw invalid ID must be delegated to the storage query: %v", err)
}
var count int64
if err = data.gormDB.WithContext(ctx).Model(&announcementPO{}).Count(&count).Error; err != nil {
t.Fatal(err)
}
if count != 1 {
t.Fatalf("raw invalid ID removed rows: count=%d", count)
}
}
func TestAnnouncementRepositoryPreservesSignedUserID(t *testing.T) {
data := newTransactionTestData(t)
repo := &announcementRepo{data: data}
ctx := context.Background()
userID := -1
item := &system.Announcement{Title: "notice", UserID: &userID}
if err := repo.Create(ctx, item); err != nil {
t.Fatal(err)
}
found, err := repo.Find(ctx, "1")
if err != nil {
t.Fatal(err)
}
if found.UserID == nil || *found.UserID != userID {
t.Fatalf("found user ID = %v, want %d", found.UserID, userID)
}
}