Files
junhong_cmp_fiber/tests/unit/customer_account_service_test.go
huang b68e7ec013
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 15s
优化测试数据库连接管理
- 创建全局单例连接池,性能提升 6-7 倍
- 实现 NewTestTransaction/GetTestRedis/CleanTestRedisKeys
- 移除旧的 SetupTestDB/TeardownTestDB API
- 迁移所有测试文件到新方案(47 个文件)
- 添加测试连接管理规范文档
- 更新 AGENTS.md 和 README.md

性能对比:
- 旧方案:~71 秒(204 测试)
- 新方案:~10.5 秒(首次初始化 + 后续复用)
- 内存占用降低约 80%
- 网络连接数从 204 降至 1
2026-01-22 14:38:43 +08:00

434 lines
12 KiB
Go

package unit
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/break/junhong_cmp_fiber/internal/model"
"github.com/break/junhong_cmp_fiber/internal/model/dto"
"github.com/break/junhong_cmp_fiber/internal/service/customer_account"
"github.com/break/junhong_cmp_fiber/internal/store/postgres"
"github.com/break/junhong_cmp_fiber/pkg/constants"
"github.com/break/junhong_cmp_fiber/tests/testutils"
)
func createCustomerAccountTestContext(userID uint) context.Context {
ctx := context.Background()
ctx = context.WithValue(ctx, constants.ContextKeyUserID, userID)
ctx = context.WithValue(ctx, constants.ContextKeyUserType, constants.UserTypePlatform)
return ctx
}
func TestCustomerAccountService_List(t *testing.T) {
tx := testutils.NewTestTransaction(t)
rdb := testutils.GetTestRedis(t)
testutils.CleanTestRedisKeys(t, rdb)
accountStore := postgres.NewAccountStore(tx, rdb)
shopStore := postgres.NewShopStore(tx, rdb)
enterpriseStore := postgres.NewEnterpriseStore(tx, rdb)
service := customer_account.New(tx, accountStore, shopStore, enterpriseStore)
t.Run("查询账号列表-空结果", func(t *testing.T) {
ctx := createCustomerAccountTestContext(1)
req := &dto.CustomerAccountListReq{
Page: 1,
PageSize: 20,
}
result, err := service.List(ctx, req)
require.NoError(t, err)
assert.NotNil(t, result)
assert.GreaterOrEqual(t, result.Total, int64(0))
})
t.Run("查询账号列表-按用户名筛选", func(t *testing.T) {
ctx := createCustomerAccountTestContext(1)
shop := &model.Shop{
ShopName: "列表测试店铺",
ShopCode: "SHOP_LIST_001",
Level: 1,
ContactName: "联系人",
ContactPhone: "13800000001",
Status: constants.StatusEnabled,
}
shop.Creator = 1
shop.Updater = 1
err := tx.Create(shop).Error
require.NoError(t, err)
createReq := &dto.CreateCustomerAccountReq{
Username: "测试账号用户",
Phone: "13900000001",
Password: "Test123456",
ShopID: shop.ID,
}
_, err = service.Create(ctx, createReq)
require.NoError(t, err)
req := &dto.CustomerAccountListReq{
Page: 1,
PageSize: 20,
Username: "测试账号",
}
result, err := service.List(ctx, req)
require.NoError(t, err)
assert.NotNil(t, result)
assert.GreaterOrEqual(t, result.Total, int64(1))
})
t.Run("查询账号列表-按店铺筛选", func(t *testing.T) {
ctx := createCustomerAccountTestContext(1)
shop := &model.Shop{
ShopName: "筛选测试店铺",
ShopCode: "SHOP_FILTER_001",
Level: 1,
ContactName: "联系人",
ContactPhone: "13800000002",
Status: constants.StatusEnabled,
}
shop.Creator = 1
shop.Updater = 1
err := tx.Create(shop).Error
require.NoError(t, err)
createReq := &dto.CreateCustomerAccountReq{
Username: "店铺筛选账号",
Phone: "13900000002",
Password: "Test123456",
ShopID: shop.ID,
}
_, err = service.Create(ctx, createReq)
require.NoError(t, err)
req := &dto.CustomerAccountListReq{
Page: 1,
PageSize: 20,
ShopID: &shop.ID,
}
result, err := service.List(ctx, req)
require.NoError(t, err)
assert.NotNil(t, result)
assert.GreaterOrEqual(t, result.Total, int64(1))
})
}
func TestCustomerAccountService_Create(t *testing.T) {
tx := testutils.NewTestTransaction(t)
rdb := testutils.GetTestRedis(t)
testutils.CleanTestRedisKeys(t, rdb)
accountStore := postgres.NewAccountStore(tx, rdb)
shopStore := postgres.NewShopStore(tx, rdb)
enterpriseStore := postgres.NewEnterpriseStore(tx, rdb)
service := customer_account.New(tx, accountStore, shopStore, enterpriseStore)
t.Run("新增代理商账号", func(t *testing.T) {
ctx := createCustomerAccountTestContext(1)
shop := &model.Shop{
ShopName: "新增账号测试店铺",
ShopCode: "SHOP_CREATE_001",
Level: 1,
ContactName: "联系人",
ContactPhone: "13800000010",
Status: constants.StatusEnabled,
}
shop.Creator = 1
shop.Updater = 1
err := tx.Create(shop).Error
require.NoError(t, err)
req := &dto.CreateCustomerAccountReq{
Username: "新代理账号",
Phone: "13900000010",
Password: "Test123456",
ShopID: shop.ID,
}
result, err := service.Create(ctx, req)
require.NoError(t, err)
assert.NotNil(t, result)
assert.Equal(t, "新代理账号", result.Username)
assert.Equal(t, "13900000010", result.Phone)
assert.Equal(t, constants.UserTypeAgent, result.UserType)
assert.Equal(t, constants.StatusEnabled, result.Status)
})
t.Run("新增账号-手机号已存在应失败", func(t *testing.T) {
ctx := createCustomerAccountTestContext(1)
shop := &model.Shop{
ShopName: "手机号测试店铺",
ShopCode: "SHOP_CREATE_002",
Level: 1,
ContactName: "联系人",
ContactPhone: "13800000011",
Status: constants.StatusEnabled,
}
shop.Creator = 1
shop.Updater = 1
err := tx.Create(shop).Error
require.NoError(t, err)
req1 := &dto.CreateCustomerAccountReq{
Username: "账号一",
Phone: "13900000011",
Password: "Test123456",
ShopID: shop.ID,
}
_, err = service.Create(ctx, req1)
require.NoError(t, err)
req2 := &dto.CreateCustomerAccountReq{
Username: "账号二",
Phone: "13900000011",
Password: "Test123456",
ShopID: shop.ID,
}
_, err = service.Create(ctx, req2)
assert.Error(t, err)
})
t.Run("新增账号-店铺不存在应失败", func(t *testing.T) {
ctx := createCustomerAccountTestContext(1)
req := &dto.CreateCustomerAccountReq{
Username: "无效店铺账号",
Phone: "13900000012",
Password: "Test123456",
ShopID: 99999,
}
_, err := service.Create(ctx, req)
assert.Error(t, err)
})
t.Run("新增账号-未授权用户应失败", func(t *testing.T) {
ctx := context.Background()
req := &dto.CreateCustomerAccountReq{
Username: "未授权账号",
Phone: "13900000013",
Password: "Test123456",
ShopID: 1,
}
_, err := service.Create(ctx, req)
assert.Error(t, err)
})
}
func TestCustomerAccountService_Update(t *testing.T) {
tx := testutils.NewTestTransaction(t)
rdb := testutils.GetTestRedis(t)
testutils.CleanTestRedisKeys(t, rdb)
accountStore := postgres.NewAccountStore(tx, rdb)
shopStore := postgres.NewShopStore(tx, rdb)
enterpriseStore := postgres.NewEnterpriseStore(tx, rdb)
service := customer_account.New(tx, accountStore, shopStore, enterpriseStore)
t.Run("编辑账号", func(t *testing.T) {
ctx := createCustomerAccountTestContext(1)
shop := &model.Shop{
ShopName: "编辑账号测试店铺",
ShopCode: "SHOP_UPDATE_001",
Level: 1,
ContactName: "联系人",
ContactPhone: "13800000020",
Status: constants.StatusEnabled,
}
shop.Creator = 1
shop.Updater = 1
err := tx.Create(shop).Error
require.NoError(t, err)
createReq := &dto.CreateCustomerAccountReq{
Username: "待编辑账号",
Phone: "13900000020",
Password: "Test123456",
ShopID: shop.ID,
}
created, err := service.Create(ctx, createReq)
require.NoError(t, err)
newName := "编辑后账号"
updateReq := &dto.UpdateCustomerAccountRequest{
Username: &newName,
}
updated, err := service.Update(ctx, created.ID, updateReq)
require.NoError(t, err)
assert.Equal(t, "编辑后账号", updated.Username)
})
t.Run("编辑账号-不存在应失败", func(t *testing.T) {
ctx := createCustomerAccountTestContext(1)
newName := "不存在账号"
updateReq := &dto.UpdateCustomerAccountRequest{
Username: &newName,
}
_, err := service.Update(ctx, 99999, updateReq)
assert.Error(t, err)
})
}
func TestCustomerAccountService_UpdatePassword(t *testing.T) {
tx := testutils.NewTestTransaction(t)
rdb := testutils.GetTestRedis(t)
testutils.CleanTestRedisKeys(t, rdb)
accountStore := postgres.NewAccountStore(tx, rdb)
shopStore := postgres.NewShopStore(tx, rdb)
enterpriseStore := postgres.NewEnterpriseStore(tx, rdb)
service := customer_account.New(tx, accountStore, shopStore, enterpriseStore)
t.Run("修改密码", func(t *testing.T) {
ctx := createCustomerAccountTestContext(1)
shop := &model.Shop{
ShopName: "密码测试店铺",
ShopCode: "SHOP_PWD_001",
Level: 1,
ContactName: "联系人",
ContactPhone: "13800000030",
Status: constants.StatusEnabled,
}
shop.Creator = 1
shop.Updater = 1
err := tx.Create(shop).Error
require.NoError(t, err)
createReq := &dto.CreateCustomerAccountReq{
Username: "密码测试账号",
Phone: "13900000030",
Password: "OldPass123",
ShopID: shop.ID,
}
created, err := service.Create(ctx, createReq)
require.NoError(t, err)
err = service.UpdatePassword(ctx, created.ID, "NewPass456")
require.NoError(t, err)
var account model.Account
err = tx.First(&account, created.ID).Error
require.NoError(t, err)
assert.NotEqual(t, "OldPass123", account.Password)
assert.NotEqual(t, "NewPass456", account.Password)
})
t.Run("修改不存在账号密码应失败", func(t *testing.T) {
ctx := createCustomerAccountTestContext(1)
err := service.UpdatePassword(ctx, 99999, "NewPass789")
assert.Error(t, err)
})
}
func TestCustomerAccountService_UpdateStatus(t *testing.T) {
tx := testutils.NewTestTransaction(t)
rdb := testutils.GetTestRedis(t)
testutils.CleanTestRedisKeys(t, rdb)
accountStore := postgres.NewAccountStore(tx, rdb)
shopStore := postgres.NewShopStore(tx, rdb)
enterpriseStore := postgres.NewEnterpriseStore(tx, rdb)
service := customer_account.New(tx, accountStore, shopStore, enterpriseStore)
t.Run("禁用账号", func(t *testing.T) {
ctx := createCustomerAccountTestContext(1)
shop := &model.Shop{
ShopName: "状态测试店铺",
ShopCode: "SHOP_STATUS_001",
Level: 1,
ContactName: "联系人",
ContactPhone: "13800000040",
Status: constants.StatusEnabled,
}
shop.Creator = 1
shop.Updater = 1
err := tx.Create(shop).Error
require.NoError(t, err)
createReq := &dto.CreateCustomerAccountReq{
Username: "状态测试账号",
Phone: "13900000040",
Password: "Test123456",
ShopID: shop.ID,
}
created, err := service.Create(ctx, createReq)
require.NoError(t, err)
err = service.UpdateStatus(ctx, created.ID, constants.StatusDisabled)
require.NoError(t, err)
var account model.Account
err = tx.First(&account, created.ID).Error
require.NoError(t, err)
assert.Equal(t, constants.StatusDisabled, account.Status)
})
t.Run("启用账号", func(t *testing.T) {
ctx := createCustomerAccountTestContext(1)
shop := &model.Shop{
ShopName: "启用测试店铺",
ShopCode: "SHOP_STATUS_002",
Level: 1,
ContactName: "联系人",
ContactPhone: "13800000041",
Status: constants.StatusEnabled,
}
shop.Creator = 1
shop.Updater = 1
err := tx.Create(shop).Error
require.NoError(t, err)
createReq := &dto.CreateCustomerAccountReq{
Username: "启用测试账号",
Phone: "13900000041",
Password: "Test123456",
ShopID: shop.ID,
}
created, err := service.Create(ctx, createReq)
require.NoError(t, err)
err = service.UpdateStatus(ctx, created.ID, constants.StatusDisabled)
require.NoError(t, err)
err = service.UpdateStatus(ctx, created.ID, constants.StatusEnabled)
require.NoError(t, err)
var account model.Account
err = tx.First(&account, created.ID).Error
require.NoError(t, err)
assert.Equal(t, constants.StatusEnabled, account.Status)
})
t.Run("更新不存在账号状态应失败", func(t *testing.T) {
ctx := createCustomerAccountTestContext(1)
err := service.UpdateStatus(ctx, 99999, constants.StatusDisabled)
assert.Error(t, err)
})
}