test: add repository tests to improve coverage from 46.6% to 74%
New test files: - custom_field_repository_test.go: 10 tests for CustomFieldRepository & UserCustomFieldValueRepository - login_log_repository_test.go: 3 tests for ListCursor, ListByUserIDCursor, ListAllForExport - operation_log_repository_test.go: 1 test for ListCursor - role_repository_test.go: 2 tests for GetAncestorIDs, GetAncestors - social_account_repository_test.go: 8 CRUD tests - theme_repository_test.go: 10 tests for ThemeConfigRepository - user_role_repository_test.go: 1 test for DeleteByUserAndRole Modified test files: - device_repository_test.go: Added ListAllCursor tests - user_repository_test.go: Added AdvancedSearch tests - webhook_repository_test.go: Added ListByCreatorPaginated test Updated documentation with new coverage status.
This commit is contained in:
@@ -13,6 +13,7 @@ import (
|
||||
"gorm.io/gorm/logger"
|
||||
|
||||
"github.com/user-management-system/internal/domain"
|
||||
"github.com/user-management-system/internal/pagination"
|
||||
)
|
||||
|
||||
var deviceTestCounter int64
|
||||
@@ -484,3 +485,91 @@ func createDevice(t *testing.T, repo *DeviceRepository, ctx context.Context, use
|
||||
}
|
||||
return d, nil
|
||||
}
|
||||
|
||||
// TestDeviceRepository_ListAllCursor 测试设备游标分页查询
|
||||
func TestDeviceRepository_ListAllCursor(t *testing.T) {
|
||||
db := setupDeviceTestDB(t)
|
||||
repo := NewDeviceRepository(db)
|
||||
ctx := context.Background()
|
||||
|
||||
// 创建设备(需要设置LastActiveTime以支持游标分页)
|
||||
now := time.Now()
|
||||
for i := 0; i < 5; i++ {
|
||||
repo.Create(ctx, &domain.Device{
|
||||
UserID: int64(i + 1),
|
||||
DeviceID: "cursor-device-" + string(rune('a'+i)),
|
||||
DeviceName: "设备" + string(rune('0'+i)),
|
||||
Status: domain.DeviceStatusActive,
|
||||
LastActiveTime: now.Add(-time.Duration(i) * time.Minute),
|
||||
})
|
||||
}
|
||||
|
||||
// 第一次查询,获取前3个
|
||||
devices, hasMore, err := repo.ListAllCursor(ctx, &ListDevicesParams{Offset: 0, Limit: 10}, 3, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("ListAllCursor() error = %v", err)
|
||||
}
|
||||
if len(devices) != 3 {
|
||||
t.Errorf("len(devices) = %d, want 3", len(devices))
|
||||
}
|
||||
if !hasMore {
|
||||
t.Error("hasMore should be true when more devices exist")
|
||||
}
|
||||
|
||||
// 使用游标继续查询
|
||||
lastDevice := devices[len(devices)-1]
|
||||
cursor := &pagination.Cursor{
|
||||
LastID: lastDevice.ID,
|
||||
LastValue: lastDevice.LastActiveTime,
|
||||
}
|
||||
devices2, hasMore2, err := repo.ListAllCursor(ctx, &ListDevicesParams{Offset: 0, Limit: 10}, 3, cursor)
|
||||
if err != nil {
|
||||
t.Fatalf("ListAllCursor() error = %v", err)
|
||||
}
|
||||
if len(devices2) != 2 {
|
||||
t.Errorf("len(devices2) = %d, want 2", len(devices2))
|
||||
}
|
||||
if hasMore2 {
|
||||
t.Error("hasMore2 should be false")
|
||||
}
|
||||
}
|
||||
|
||||
// TestDeviceRepository_ListAllCursor_WithFilters 测试带筛选条件的设备游标分页
|
||||
func TestDeviceRepository_ListAllCursor_WithFilters(t *testing.T) {
|
||||
db := setupDeviceTestDB(t)
|
||||
repo := NewDeviceRepository(db)
|
||||
ctx := context.Background()
|
||||
|
||||
now := time.Now()
|
||||
repo.Create(ctx, &domain.Device{
|
||||
UserID: 1,
|
||||
DeviceID: "filter-dev1",
|
||||
DeviceName: "用户1设备",
|
||||
Status: domain.DeviceStatusActive,
|
||||
LastActiveTime: now,
|
||||
})
|
||||
repo.Create(ctx, &domain.Device{
|
||||
UserID: 2,
|
||||
DeviceID: "filter-dev2",
|
||||
DeviceName: "用户2设备",
|
||||
Status: domain.DeviceStatusActive,
|
||||
LastActiveTime: now,
|
||||
})
|
||||
repo.Create(ctx, &domain.Device{
|
||||
UserID: 1,
|
||||
DeviceID: "filter-dev3",
|
||||
DeviceName: "用户1禁用设备",
|
||||
Status: domain.DeviceStatusInactive,
|
||||
LastActiveTime: now,
|
||||
})
|
||||
|
||||
// 按用户ID筛选
|
||||
status := domain.DeviceStatusActive
|
||||
devices, _, err := repo.ListAllCursor(ctx, &ListDevicesParams{UserID: 1, Status: &status, Offset: 0, Limit: 10}, 10, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("ListAllCursor() error = %v", err)
|
||||
}
|
||||
if len(devices) != 1 {
|
||||
t.Errorf("len(devices) = %d, want 1", len(devices))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user