| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- 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-0113: 正常查询
- 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-0114: 默认分页
- 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-0115: 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-0116: 不存在的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)
- }
|