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")
	}
}
