test: add ListLogsForExportBatch test to improve coverage
This commit is contained in:
@@ -154,3 +154,54 @@ func TestLoginLogRepository_ListAllForExport(t *testing.T) {
|
||||
t.Errorf("len(logs) = %d, want 2", len(logs))
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoginLogRepository_ListLogsForExportBatch(t *testing.T) {
|
||||
db := setupLoginLogTestDB(t)
|
||||
repo := NewLoginLogRepository(db)
|
||||
ctx := context.Background()
|
||||
|
||||
// 创建多个日志
|
||||
for i := 0; i < 5; i++ {
|
||||
repo.Create(ctx, &domain.LoginLog{
|
||||
UserID: int64Ptr(1),
|
||||
LoginType: 1,
|
||||
IP: "192.168.1." + string(rune('0'+i)),
|
||||
Status: 1,
|
||||
})
|
||||
}
|
||||
|
||||
// 测试批量导出(使用cursor分页)
|
||||
// 初始查询使用一个很大的cursor来获取所有记录
|
||||
logs, hasMore, err := repo.ListLogsForExportBatch(ctx, 0, -1, nil, nil, 999999, 3)
|
||||
if err != nil {
|
||||
t.Fatalf("ListLogsForExportBatch() error = %v", err)
|
||||
}
|
||||
if len(logs) != 3 {
|
||||
t.Errorf("len(logs) = %d, want 3", len(logs))
|
||||
}
|
||||
if !hasMore {
|
||||
t.Error("hasMore should be true when more logs exist")
|
||||
}
|
||||
|
||||
// 使用cursor继续查询(使用最后一条记录的ID)
|
||||
lastID := logs[len(logs)-1].ID
|
||||
logs2, hasMore2, err := repo.ListLogsForExportBatch(ctx, 0, -1, nil, nil, lastID, 3)
|
||||
if err != nil {
|
||||
t.Fatalf("ListLogsForExportBatch() error = %v", err)
|
||||
}
|
||||
if len(logs2) != 2 {
|
||||
t.Errorf("len(logs2) = %d, want 2", len(logs2))
|
||||
}
|
||||
if hasMore2 {
|
||||
t.Error("hasMore2 should be false")
|
||||
}
|
||||
|
||||
// 测试按用户ID筛选
|
||||
logs3, _, err := repo.ListLogsForExportBatch(ctx, 1, -1, nil, nil, 999999, 10)
|
||||
if err != nil {
|
||||
t.Fatalf("ListLogsForExportBatch() error = %v", err)
|
||||
}
|
||||
if len(logs3) != 5 {
|
||||
t.Errorf("len(logs3) = %d, want 5", len(logs3))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user