Files
user-system/internal/repository/user_role_repository_test.go
long-agent 289aab2930 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.
2026-04-11 21:58:28 +08:00

37 lines
968 B
Go

package repository
import (
"context"
"testing"
"github.com/user-management-system/internal/domain"
)
func TestUserRoleRepository_DeleteByUserAndRole(t *testing.T) {
db := setupTestDB(t)
repo := NewUserRoleRepository(db)
ctx := context.Background()
// 创建用户和角色
user := &domain.User{Username: "roleuser", Password: "hash", Status: domain.UserStatusActive}
db.WithContext(ctx).Create(user)
role := &domain.Role{Code: "test_role", Name: "测试角色", Status: domain.RoleStatusEnabled}
db.WithContext(ctx).Create(role)
// 创建用户角色关联
repo.Create(ctx, &domain.UserRole{UserID: user.ID, RoleID: role.ID})
// 删除特定用户-角色关联
err := repo.DeleteByUserAndRole(ctx, user.ID, role.ID)
if err != nil {
t.Fatalf("DeleteByUserAndRole() error = %v", err)
}
// 验证已删除
exists, _ := repo.Exists(ctx, user.ID, role.ID)
if exists {
t.Error("DeleteByUserAndRole should have removed the association")
}
}