package role import ( "errors" "testing" "time" permModel "perms-system-server/internal/model/perm" roleModel "perms-system-server/internal/model/role" "perms-system-server/internal/model/roleperm" "perms-system-server/internal/response" "perms-system-server/internal/svc" "perms-system-server/internal/testutil" "perms-system-server/internal/testutil/ctxhelper" "perms-system-server/internal/types" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) // TC-0124: 正常查询 func TestRoleDetail_Normal(t *testing.T) { ctx := ctxhelper.SuperAdminCtx() svcCtx := svc.NewServiceContext(testutil.GetTestConfig()) conn := testutil.GetTestSqlConn() now := time.Now().Unix() pc := testutil.UniqueId() roleRes, err := svcCtx.SysRoleModel.Insert(ctx, &roleModel.SysRole{ ProductCode: pc, Name: testutil.UniqueId(), Remark: "detail test", Status: 1, PermsLevel: 1, CreateTime: now, UpdateTime: now, }) require.NoError(t, err) roleId, _ := roleRes.LastInsertId() p1Res, err := svcCtx.SysPermModel.Insert(ctx, &permModel.SysPerm{ ProductCode: pc, Name: testutil.UniqueId(), Code: testutil.UniqueId(), Status: 1, CreateTime: now, UpdateTime: now, }) require.NoError(t, err) p1Id, _ := p1Res.LastInsertId() p2Res, err := svcCtx.SysPermModel.Insert(ctx, &permModel.SysPerm{ ProductCode: pc, Name: testutil.UniqueId(), Code: testutil.UniqueId(), Status: 1, CreateTime: now, UpdateTime: now, }) require.NoError(t, err) p2Id, _ := p2Res.LastInsertId() rp1Res, err := svcCtx.SysRolePermModel.Insert(ctx, &roleperm.SysRolePerm{ RoleId: roleId, PermId: p1Id, CreateTime: now, UpdateTime: now, }) require.NoError(t, err) rp1Id, _ := rp1Res.LastInsertId() rp2Res, err := svcCtx.SysRolePermModel.Insert(ctx, &roleperm.SysRolePerm{ RoleId: roleId, PermId: p2Id, CreateTime: now, UpdateTime: now, }) require.NoError(t, err) rp2Id, _ := rp2Res.LastInsertId() t.Cleanup(func() { testutil.CleanTable(ctx, conn, "`sys_role_perm`", rp1Id, rp2Id) testutil.CleanTable(ctx, conn, "`sys_perm`", p1Id, p2Id) testutil.CleanTable(ctx, conn, "`sys_role`", roleId) }) logic := NewRoleDetailLogic(ctx, svcCtx) resp, err := logic.RoleDetail(&types.RoleDetailReq{Id: roleId}) require.NoError(t, err) assert.Equal(t, roleId, resp.Id) assert.Equal(t, pc, resp.ProductCode) assert.Equal(t, "detail test", resp.Remark) assert.ElementsMatch(t, []int64{p1Id, p2Id}, resp.PermIds) } // TC-0125: 不存在 func TestRoleDetail_NotFound(t *testing.T) { ctx := ctxhelper.SuperAdminCtx() svcCtx := svc.NewServiceContext(testutil.GetTestConfig()) logic := NewRoleDetailLogic(ctx, svcCtx) resp, err := logic.RoleDetail(&types.RoleDetailReq{Id: 999999999}) assert.Nil(t, resp) require.Error(t, err) var ce *response.CodeError require.True(t, errors.As(err, &ce)) assert.Equal(t, 404, ce.Code()) assert.Equal(t, "角色不存在", ce.Error()) }