permListLogic_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package perm
  2. import (
  3. "testing"
  4. "time"
  5. permModel "perms-system-server/internal/model/perm"
  6. "perms-system-server/internal/svc"
  7. "perms-system-server/internal/testutil"
  8. "perms-system-server/internal/testutil/ctxhelper"
  9. "perms-system-server/internal/types"
  10. "github.com/stretchr/testify/assert"
  11. "github.com/stretchr/testify/require"
  12. )
  13. // TC-0113: 正常查询
  14. func TestPermList_Normal(t *testing.T) {
  15. ctx := ctxhelper.SuperAdminCtx()
  16. svcCtx := svc.NewServiceContext(testutil.GetTestConfig())
  17. conn := testutil.GetTestSqlConn()
  18. now := time.Now().Unix()
  19. pc := testutil.UniqueId()
  20. var permIds []int64
  21. for i := 0; i < 3; i++ {
  22. res, err := svcCtx.SysPermModel.Insert(ctx, &permModel.SysPerm{
  23. ProductCode: pc, Name: testutil.UniqueId(), Code: testutil.UniqueId(),
  24. Status: 1, CreateTime: now, UpdateTime: now,
  25. })
  26. require.NoError(t, err)
  27. id, _ := res.LastInsertId()
  28. permIds = append(permIds, id)
  29. }
  30. t.Cleanup(func() {
  31. testutil.CleanTable(ctx, conn, "`sys_perm`", permIds...)
  32. })
  33. logic := NewPermListLogic(ctx, svcCtx)
  34. resp, err := logic.PermList(&types.PermListReq{
  35. ProductCode: pc, Page: 1, PageSize: 10,
  36. })
  37. require.NoError(t, err)
  38. assert.Equal(t, int64(3), resp.Total)
  39. items := resp.List.([]types.PermItem)
  40. assert.Len(t, items, 3)
  41. assert.Equal(t, pc, items[0].ProductCode)
  42. }
  43. // TC-0114: 默认分页
  44. func TestPermList_DefaultPagination(t *testing.T) {
  45. ctx := ctxhelper.SuperAdminCtx()
  46. svcCtx := svc.NewServiceContext(testutil.GetTestConfig())
  47. conn := testutil.GetTestSqlConn()
  48. now := time.Now().Unix()
  49. pc := testutil.UniqueId()
  50. res, err := svcCtx.SysPermModel.Insert(ctx, &permModel.SysPerm{
  51. ProductCode: pc, Name: testutil.UniqueId(), Code: testutil.UniqueId(),
  52. Status: 1, CreateTime: now, UpdateTime: now,
  53. })
  54. require.NoError(t, err)
  55. id, _ := res.LastInsertId()
  56. t.Cleanup(func() {
  57. testutil.CleanTable(ctx, conn, "`sys_perm`", id)
  58. })
  59. logic := NewPermListLogic(ctx, svcCtx)
  60. resp, err := logic.PermList(&types.PermListReq{
  61. ProductCode: pc,
  62. })
  63. require.NoError(t, err)
  64. assert.Equal(t, int64(1), resp.Total)
  65. }
  66. // TC-0115: pageSize超过上限
  67. func TestPermList_PageSizeExceedsLimit(t *testing.T) {
  68. ctx := ctxhelper.SuperAdminCtx()
  69. svcCtx := svc.NewServiceContext(testutil.GetTestConfig())
  70. conn := testutil.GetTestSqlConn()
  71. now := time.Now().Unix()
  72. pc := testutil.UniqueId()
  73. res, err := svcCtx.SysPermModel.Insert(ctx, &permModel.SysPerm{
  74. ProductCode: pc, Name: testutil.UniqueId(), Code: testutil.UniqueId(),
  75. Status: 1, CreateTime: now, UpdateTime: now,
  76. })
  77. require.NoError(t, err)
  78. id, _ := res.LastInsertId()
  79. t.Cleanup(func() {
  80. testutil.CleanTable(ctx, conn, "`sys_perm`", id)
  81. })
  82. logic := NewPermListLogic(ctx, svcCtx)
  83. resp, err := logic.PermList(&types.PermListReq{
  84. ProductCode: pc, Page: 1, PageSize: 200,
  85. })
  86. require.NoError(t, err)
  87. assert.Equal(t, int64(1), resp.Total)
  88. }
  89. // TC-0116: 不存在的productCode
  90. func TestPermList_NonExistentProductCode(t *testing.T) {
  91. ctx := ctxhelper.SuperAdminCtx()
  92. svcCtx := svc.NewServiceContext(testutil.GetTestConfig())
  93. logic := NewPermListLogic(ctx, svcCtx)
  94. resp, err := logic.PermList(&types.PermListReq{
  95. ProductCode: "nonexistent_" + testutil.UniqueId(),
  96. Page: 1,
  97. PageSize: 10,
  98. })
  99. require.NoError(t, err)
  100. assert.Equal(t, int64(0), resp.Total)
  101. }