feat: 实现门店套餐分配功能并统一测试基础设施
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 5m30s

新增功能:
- 门店套餐分配管理(shop_package_allocation):支持门店套餐库存管理
- 门店套餐系列分配管理(shop_series_allocation):支持套餐系列分配和佣金层级设置
- 我的套餐查询(my_package):支持门店查询自己的套餐分配情况

测试改进:
- 统一集成测试基础设施,新增 testutils.NewIntegrationTestEnv
- 重构所有集成测试使用新的测试环境设置
- 移除旧的测试辅助函数和冗余测试文件
- 新增 test_helpers_test.go 统一任务测试辅助

技术细节:
- 新增数据库迁移 000025_create_shop_allocation_tables
- 新增 3 个 Handler、Service、Store 和对应的单元测试
- 更新 OpenAPI 文档和文档生成器
- 测试覆盖率:Service 层 > 90%

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-01-28 10:45:16 +08:00
parent 5fefe9d0cb
commit 23eb0307bb
73 changed files with 8716 additions and 4558 deletions

View File

@@ -1,87 +1,30 @@
package integration
import (
"context"
"testing"
"time"
"github.com/redis/go-redis/v9"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/testcontainers/testcontainers-go"
testcontainers_postgres "github.com/testcontainers/testcontainers-go/modules/postgres"
testcontainers_redis "github.com/testcontainers/testcontainers-go/modules/redis"
"github.com/testcontainers/testcontainers-go/wait"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"github.com/break/junhong_cmp_fiber/internal/model"
accountService "github.com/break/junhong_cmp_fiber/internal/service/account"
postgresStore "github.com/break/junhong_cmp_fiber/internal/store/postgres"
"github.com/break/junhong_cmp_fiber/pkg/constants"
"github.com/break/junhong_cmp_fiber/pkg/middleware"
"github.com/break/junhong_cmp_fiber/tests/testutils/integ"
)
// TestAccountRoleAssociation_AssignRoles 测试账号角色分配功能
func TestAccountRoleAssociation_AssignRoles(t *testing.T) {
ctx := context.Background()
// 启动 PostgreSQL 容器
pgContainer, err := testcontainers_postgres.Run(ctx,
"postgres:14-alpine",
testcontainers_postgres.WithDatabase("testdb"),
testcontainers_postgres.WithUsername("postgres"),
testcontainers_postgres.WithPassword("password"),
testcontainers.WithWaitStrategy(
wait.ForLog("database system is ready to accept connections").
WithOccurrence(2).
WithStartupTimeout(30*time.Second),
),
)
require.NoError(t, err, "启动 PostgreSQL 容器失败")
defer func() { _ = pgContainer.Terminate(ctx) }()
pgConnStr, err := pgContainer.ConnectionString(ctx, "sslmode=disable")
require.NoError(t, err)
// 启动 Redis 容器
redisContainer, err := testcontainers_redis.Run(ctx,
"redis:6-alpine",
)
require.NoError(t, err, "启动 Redis 容器失败")
defer func() { _ = redisContainer.Terminate(ctx) }()
redisHost, _ := redisContainer.Host(ctx)
redisPort, _ := redisContainer.MappedPort(ctx, "6379")
// 连接数据库
tx, err := gorm.Open(postgres.Open(pgConnStr), &gorm.Config{
Logger: logger.Default.LogMode(logger.Silent),
})
require.NoError(t, err)
// 自动迁移
err = tx.AutoMigrate(
&model.Account{},
&model.Role{},
&model.AccountRole{},
)
require.NoError(t, err)
// 连接 Redis
rdb := redis.NewClient(&redis.Options{
Addr: redisHost + ":" + redisPort.Port(),
})
env := integ.NewIntegrationTestEnv(t)
// 初始化 Store 和 Service
accountStore := postgresStore.NewAccountStore(tx, rdb)
roleStore := postgresStore.NewRoleStore(tx)
accountRoleStore := postgresStore.NewAccountRoleStore(tx, rdb)
accountStore := postgresStore.NewAccountStore(env.TX, env.Redis)
roleStore := postgresStore.NewRoleStore(env.TX)
accountRoleStore := postgresStore.NewAccountRoleStore(env.TX, env.Redis)
accService := accountService.New(accountStore, roleStore, accountRoleStore)
// 创建测试用户上下文
userCtx := middleware.SetUserContext(ctx, middleware.NewSimpleUserContext(1, constants.UserTypeSuperAdmin, 0))
// 获取超级管理员上下文
userCtx := env.GetSuperAdminContext()
t.Run("成功分配单个角色", func(t *testing.T) {
// 创建测试账号
@@ -92,7 +35,7 @@ func TestAccountRoleAssociation_AssignRoles(t *testing.T) {
UserType: constants.UserTypePlatform,
Status: constants.StatusEnabled,
}
tx.Create(account)
env.TX.Create(account)
// 创建测试角色
role := &model.Role{
@@ -100,7 +43,7 @@ func TestAccountRoleAssociation_AssignRoles(t *testing.T) {
RoleType: constants.RoleTypePlatform,
Status: constants.StatusEnabled,
}
tx.Create(role)
env.TX.Create(role)
// 分配角色
ars, err := accService.AssignRoles(userCtx, account.ID, []uint{role.ID})
@@ -119,7 +62,7 @@ func TestAccountRoleAssociation_AssignRoles(t *testing.T) {
UserType: constants.UserTypePlatform,
Status: constants.StatusEnabled,
}
tx.Create(account)
env.TX.Create(account)
// 创建多个测试角色
roles := make([]*model.Role, 3)
@@ -130,7 +73,7 @@ func TestAccountRoleAssociation_AssignRoles(t *testing.T) {
RoleType: constants.RoleTypePlatform,
Status: constants.StatusEnabled,
}
tx.Create(roles[i])
env.TX.Create(roles[i])
roleIDs[i] = roles[i].ID
}
@@ -149,7 +92,7 @@ func TestAccountRoleAssociation_AssignRoles(t *testing.T) {
UserType: constants.UserTypePlatform,
Status: constants.StatusEnabled,
}
tx.Create(account)
env.TX.Create(account)
// 创建并分配角色
role := &model.Role{
@@ -157,7 +100,7 @@ func TestAccountRoleAssociation_AssignRoles(t *testing.T) {
RoleType: constants.RoleTypePlatform,
Status: constants.StatusEnabled,
}
tx.Create(role)
env.TX.Create(role)
_, err := accService.AssignRoles(userCtx, account.ID, []uint{role.ID})
require.NoError(t, err)
@@ -178,7 +121,7 @@ func TestAccountRoleAssociation_AssignRoles(t *testing.T) {
UserType: constants.UserTypePlatform,
Status: constants.StatusEnabled,
}
tx.Create(account)
env.TX.Create(account)
// 创建并分配角色
role := &model.Role{
@@ -186,7 +129,7 @@ func TestAccountRoleAssociation_AssignRoles(t *testing.T) {
RoleType: constants.RoleTypePlatform,
Status: constants.StatusEnabled,
}
tx.Create(role)
env.TX.Create(role)
_, err := accService.AssignRoles(userCtx, account.ID, []uint{role.ID})
require.NoError(t, err)
@@ -197,7 +140,7 @@ func TestAccountRoleAssociation_AssignRoles(t *testing.T) {
// 验证角色已被软删除
var ar model.AccountRole
err = tx.Unscoped().Where("account_id = ? AND role_id = ?", account.ID, role.ID).First(&ar).Error
err = env.RawDB().Unscoped().Where("account_id = ? AND role_id = ?", account.ID, role.ID).First(&ar).Error
require.NoError(t, err)
assert.NotNil(t, ar.DeletedAt)
})
@@ -211,7 +154,7 @@ func TestAccountRoleAssociation_AssignRoles(t *testing.T) {
UserType: constants.UserTypePlatform,
Status: constants.StatusEnabled,
}
tx.Create(account)
env.TX.Create(account)
// 创建测试角色
role := &model.Role{
@@ -219,7 +162,7 @@ func TestAccountRoleAssociation_AssignRoles(t *testing.T) {
RoleType: constants.RoleTypePlatform,
Status: constants.StatusEnabled,
}
tx.Create(role)
env.TX.Create(role)
// 第一次分配
_, err := accService.AssignRoles(userCtx, account.ID, []uint{role.ID})
@@ -231,7 +174,7 @@ func TestAccountRoleAssociation_AssignRoles(t *testing.T) {
// 验证只有一条记录
var count int64
tx.Model(&model.AccountRole{}).Where("account_id = ? AND role_id = ?", account.ID, role.ID).Count(&count)
env.RawDB().Model(&model.AccountRole{}).Where("account_id = ? AND role_id = ?", account.ID, role.ID).Count(&count)
assert.Equal(t, int64(1), count)
})
@@ -241,7 +184,7 @@ func TestAccountRoleAssociation_AssignRoles(t *testing.T) {
RoleType: constants.RoleTypePlatform,
Status: constants.StatusEnabled,
}
tx.Create(role)
env.TX.Create(role)
_, err := accService.AssignRoles(userCtx, 99999, []uint{role.ID})
assert.Error(t, err)
@@ -255,7 +198,7 @@ func TestAccountRoleAssociation_AssignRoles(t *testing.T) {
UserType: constants.UserTypePlatform,
Status: constants.StatusEnabled,
}
tx.Create(account)
env.TX.Create(account)
_, err := accService.AssignRoles(userCtx, account.ID, []uint{99999})
assert.Error(t, err)
@@ -264,50 +207,16 @@ func TestAccountRoleAssociation_AssignRoles(t *testing.T) {
// TestAccountRoleAssociation_SoftDelete 测试软删除对账号角色关联的影响
func TestAccountRoleAssociation_SoftDelete(t *testing.T) {
ctx := context.Background()
env := integ.NewIntegrationTestEnv(t)
// 启动容器
pgContainer, err := testcontainers_postgres.Run(ctx,
"postgres:14-alpine",
testcontainers_postgres.WithDatabase("testdb"),
testcontainers_postgres.WithUsername("postgres"),
testcontainers_postgres.WithPassword("password"),
testcontainers.WithWaitStrategy(
wait.ForLog("database system is ready to accept connections").
WithOccurrence(2).
WithStartupTimeout(30*time.Second),
),
)
require.NoError(t, err)
defer func() { _ = pgContainer.Terminate(ctx) }()
pgConnStr, _ := pgContainer.ConnectionString(ctx, "sslmode=disable")
redisContainer, err := testcontainers_redis.Run(ctx,
"redis:6-alpine",
)
require.NoError(t, err)
defer func() { _ = redisContainer.Terminate(ctx) }()
redisHost, _ := redisContainer.Host(ctx)
redisPort, _ := redisContainer.MappedPort(ctx, "6379")
// 设置环境
tx, _ := gorm.Open(postgres.Open(pgConnStr), &gorm.Config{
Logger: logger.Default.LogMode(logger.Silent),
})
_ = tx.AutoMigrate(&model.Account{}, &model.Role{}, &model.AccountRole{})
rdb := redis.NewClient(&redis.Options{
Addr: redisHost + ":" + redisPort.Port(),
})
accountStore := postgresStore.NewAccountStore(tx, rdb)
roleStore := postgresStore.NewRoleStore(tx)
accountRoleStore := postgresStore.NewAccountRoleStore(tx, rdb)
// 初始化 Store 和 Service
accountStore := postgresStore.NewAccountStore(env.TX, env.Redis)
roleStore := postgresStore.NewRoleStore(env.TX)
accountRoleStore := postgresStore.NewAccountRoleStore(env.TX, env.Redis)
accService := accountService.New(accountStore, roleStore, accountRoleStore)
userCtx := middleware.SetUserContext(ctx, middleware.NewSimpleUserContext(1, constants.UserTypeSuperAdmin, 0))
// 获取超级管理员上下文
userCtx := env.GetSuperAdminContext()
t.Run("软删除角色后重新分配可以恢复", func(t *testing.T) {
// 创建测试数据
@@ -318,14 +227,14 @@ func TestAccountRoleAssociation_SoftDelete(t *testing.T) {
UserType: constants.UserTypePlatform,
Status: constants.StatusEnabled,
}
tx.Create(account)
env.TX.Create(account)
role := &model.Role{
RoleName: "恢复角色测试",
RoleType: constants.RoleTypePlatform,
Status: constants.StatusEnabled,
}
tx.Create(role)
env.TX.Create(role)
// 分配角色
_, err := accService.AssignRoles(userCtx, account.ID, []uint{role.ID})