package perm import ( "testing" "time" permModel "perms-system-server/internal/model/perm" "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-0101: 正常查询 func TestPermList_Normal(t *testing.T) { ctx := ctxhelper.SuperAdminCtx() svcCtx := svc.NewServiceContext(testutil.GetTestConfig()) conn := testutil.GetTestSqlConn() now := time.Now().Unix() pc := testutil.UniqueId() var permIds []int64 for i := 0; i < 3; i++ { res, 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) id, _ := res.LastInsertId() permIds = append(permIds, id) } t.Cleanup(func() { testutil.CleanTable(ctx, conn, "`sys_perm`", permIds...) }) logic := NewPermListLogic(ctx, svcCtx) resp, err := logic.PermList(&types.PermListReq{ ProductCode: pc, Page: 1, PageSize: 10, }) require.NoError(t, err) assert.Equal(t, int64(3), resp.Total) items := resp.List.([]types.PermItem) assert.Len(t, items, 3) assert.Equal(t, pc, items[0].ProductCode) } // TC-0102: 默认分页 func TestPermList_DefaultPagination(t *testing.T) { ctx := ctxhelper.SuperAdminCtx() svcCtx := svc.NewServiceContext(testutil.GetTestConfig()) conn := testutil.GetTestSqlConn() now := time.Now().Unix() pc := testutil.UniqueId() res, 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) id, _ := res.LastInsertId() t.Cleanup(func() { testutil.CleanTable(ctx, conn, "`sys_perm`", id) }) logic := NewPermListLogic(ctx, svcCtx) resp, err := logic.PermList(&types.PermListReq{ ProductCode: pc, }) require.NoError(t, err) assert.Equal(t, int64(1), resp.Total) } // TC-0103: pageSize超过上限 func TestPermList_PageSizeExceedsLimit(t *testing.T) { ctx := ctxhelper.SuperAdminCtx() svcCtx := svc.NewServiceContext(testutil.GetTestConfig()) conn := testutil.GetTestSqlConn() now := time.Now().Unix() pc := testutil.UniqueId() res, 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) id, _ := res.LastInsertId() t.Cleanup(func() { testutil.CleanTable(ctx, conn, "`sys_perm`", id) }) logic := NewPermListLogic(ctx, svcCtx) resp, err := logic.PermList(&types.PermListReq{ ProductCode: pc, Page: 1, PageSize: 200, }) require.NoError(t, err) assert.Equal(t, int64(1), resp.Total) } // TC-0104: 不存在的productCode func TestPermList_NonExistentProductCode(t *testing.T) { ctx := ctxhelper.SuperAdminCtx() svcCtx := svc.NewServiceContext(testutil.GetTestConfig()) logic := NewPermListLogic(ctx, svcCtx) resp, err := logic.PermList(&types.PermListReq{ ProductCode: "nonexistent_" + testutil.UniqueId(), Page: 1, PageSize: 10, }) require.NoError(t, err) assert.Equal(t, int64(0), resp.Total) }