Files
junhong_cmp_fiber/tests/unit/personal_customer_store_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

329 lines
9.2 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/store/postgres"
"github.com/break/junhong_cmp_fiber/pkg/constants"
"github.com/break/junhong_cmp_fiber/tests/testutils"
)
// TestPersonalCustomerStore_Create 测试创建个人客户
func TestPersonalCustomerStore_Create(t *testing.T) {
tx := testutils.NewTestTransaction(t)
rdb := testutils.GetTestRedis(t)
testutils.CleanTestRedisKeys(t, rdb)
store := postgres.NewPersonalCustomerStore(tx, rdb)
ctx := context.Background()
tests := []struct {
name string
customer *model.PersonalCustomer
wantErr bool
}{
{
name: "创建基本个人客户",
customer: &model.PersonalCustomer{
WxOpenID: "wx_openid_test_a",
WxUnionID: "wx_unionid_test_a",
Nickname: "测试用户A",
Status: constants.StatusEnabled,
},
wantErr: false,
},
{
name: "创建带微信信息的个人客户",
customer: &model.PersonalCustomer{
WxOpenID: "wx_openid_123456",
WxUnionID: "wx_unionid_abcdef",
Nickname: "测试用户B",
AvatarURL: "https://example.com/avatar.jpg",
Status: constants.StatusEnabled,
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := store.Create(ctx, tt.customer)
if tt.wantErr {
assert.Error(t, err)
} else {
require.NoError(t, err)
assert.NotZero(t, tt.customer.ID)
assert.NotZero(t, tt.customer.CreatedAt)
assert.NotZero(t, tt.customer.UpdatedAt)
}
})
}
}
// TestPersonalCustomerStore_GetByID 测试根据 ID 查询个人客户
func TestPersonalCustomerStore_GetByID(t *testing.T) {
tx := testutils.NewTestTransaction(t)
rdb := testutils.GetTestRedis(t)
testutils.CleanTestRedisKeys(t, rdb)
store := postgres.NewPersonalCustomerStore(tx, rdb)
ctx := context.Background()
// 创建测试客户
customer := &model.PersonalCustomer{
WxOpenID: "wx_openid_test_getbyid",
WxUnionID: "wx_unionid_test_getbyid",
Nickname: "测试客户",
Status: constants.StatusEnabled,
}
err := store.Create(ctx, customer)
require.NoError(t, err)
t.Run("查询存在的客户", func(t *testing.T) {
found, err := store.GetByID(ctx, customer.ID)
require.NoError(t, err)
assert.Equal(t, customer.WxOpenID, found.WxOpenID)
assert.Equal(t, customer.Nickname, found.Nickname)
})
t.Run("查询不存在的客户", func(t *testing.T) {
_, err := store.GetByID(ctx, 99999)
assert.Error(t, err)
})
}
// TestPersonalCustomerStore_GetByPhone 测试根据手机号查询
func TestPersonalCustomerStore_GetByPhone(t *testing.T) {
tx := testutils.NewTestTransaction(t)
rdb := testutils.GetTestRedis(t)
testutils.CleanTestRedisKeys(t, rdb)
store := postgres.NewPersonalCustomerStore(tx, rdb)
ctx := context.Background()
// 创建测试客户
customer := &model.PersonalCustomer{
WxOpenID: "wx_openid_test_phone",
WxUnionID: "wx_unionid_test_phone",
Nickname: "测试客户",
Status: constants.StatusEnabled,
}
err := store.Create(ctx, customer)
require.NoError(t, err)
// 创建手机号绑定记录
customerPhone := &model.PersonalCustomerPhone{
CustomerID: customer.ID,
Phone: "13800000001",
IsPrimary: true,
Status: constants.StatusEnabled,
}
err = tx.Create(customerPhone).Error
require.NoError(t, err)
t.Run("根据手机号查询", func(t *testing.T) {
found, err := store.GetByPhone(ctx, "13800000001")
require.NoError(t, err)
assert.Equal(t, customer.ID, found.ID)
assert.Equal(t, customer.Nickname, found.Nickname)
})
t.Run("查询不存在的手机号", func(t *testing.T) {
_, err := store.GetByPhone(ctx, "99900000000")
assert.Error(t, err)
})
}
// TestPersonalCustomerStore_GetByWxOpenID 测试根据微信 OpenID 查询
func TestPersonalCustomerStore_GetByWxOpenID(t *testing.T) {
tx := testutils.NewTestTransaction(t)
rdb := testutils.GetTestRedis(t)
testutils.CleanTestRedisKeys(t, rdb)
store := postgres.NewPersonalCustomerStore(tx, rdb)
ctx := context.Background()
// 创建测试客户
customer := &model.PersonalCustomer{
WxOpenID: "wx_openid_unique",
WxUnionID: "wx_unionid_unique",
Nickname: "测试客户",
Status: constants.StatusEnabled,
}
err := store.Create(ctx, customer)
require.NoError(t, err)
t.Run("根据微信OpenID查询", func(t *testing.T) {
found, err := store.GetByWxOpenID(ctx, "wx_openid_unique")
require.NoError(t, err)
assert.Equal(t, customer.ID, found.ID)
assert.Equal(t, customer.WxOpenID, found.WxOpenID)
})
t.Run("查询不存在的OpenID", func(t *testing.T) {
_, err := store.GetByWxOpenID(ctx, "nonexistent_openid")
assert.Error(t, err)
})
}
// TestPersonalCustomerStore_Update 测试更新个人客户
func TestPersonalCustomerStore_Update(t *testing.T) {
tx := testutils.NewTestTransaction(t)
rdb := testutils.GetTestRedis(t)
testutils.CleanTestRedisKeys(t, rdb)
store := postgres.NewPersonalCustomerStore(tx, rdb)
ctx := context.Background()
// 创建测试客户
customer := &model.PersonalCustomer{
WxOpenID: "wx_openid_test_update",
WxUnionID: "wx_unionid_test_update",
Nickname: "原昵称",
Status: constants.StatusEnabled,
}
err := store.Create(ctx, customer)
require.NoError(t, err)
t.Run("更新客户信息", func(t *testing.T) {
customer.Nickname = "新昵称"
customer.AvatarURL = "https://example.com/new_avatar.jpg"
err := store.Update(ctx, customer)
require.NoError(t, err)
// 验证更新
found, err := store.GetByID(ctx, customer.ID)
require.NoError(t, err)
assert.Equal(t, "新昵称", found.Nickname)
assert.Equal(t, "https://example.com/new_avatar.jpg", found.AvatarURL)
})
t.Run("绑定微信信息", func(t *testing.T) {
customer.WxOpenID = "wx_openid_new"
customer.WxUnionID = "wx_unionid_new"
err := store.Update(ctx, customer)
require.NoError(t, err)
found, err := store.GetByID(ctx, customer.ID)
require.NoError(t, err)
assert.Equal(t, "wx_openid_new", found.WxOpenID)
assert.Equal(t, "wx_unionid_new", found.WxUnionID)
})
t.Run("更新客户状态", func(t *testing.T) {
customer.Status = constants.StatusDisabled
err := store.Update(ctx, customer)
require.NoError(t, err)
found, err := store.GetByID(ctx, customer.ID)
require.NoError(t, err)
assert.Equal(t, constants.StatusDisabled, found.Status)
})
}
// TestPersonalCustomerStore_Delete 测试软删除个人客户
func TestPersonalCustomerStore_Delete(t *testing.T) {
tx := testutils.NewTestTransaction(t)
rdb := testutils.GetTestRedis(t)
testutils.CleanTestRedisKeys(t, rdb)
store := postgres.NewPersonalCustomerStore(tx, rdb)
ctx := context.Background()
// 创建测试客户
customer := &model.PersonalCustomer{
WxOpenID: "wx_openid_test_delete",
WxUnionID: "wx_unionid_test_delete",
Nickname: "待删除客户",
Status: constants.StatusEnabled,
}
err := store.Create(ctx, customer)
require.NoError(t, err)
t.Run("软删除客户", func(t *testing.T) {
err := store.Delete(ctx, customer.ID)
require.NoError(t, err)
// 验证已被软删除
_, err = store.GetByID(ctx, customer.ID)
assert.Error(t, err)
})
}
// TestPersonalCustomerStore_List 测试查询客户列表
func TestPersonalCustomerStore_List(t *testing.T) {
tx := testutils.NewTestTransaction(t)
rdb := testutils.GetTestRedis(t)
testutils.CleanTestRedisKeys(t, rdb)
store := postgres.NewPersonalCustomerStore(tx, rdb)
ctx := context.Background()
// 创建多个测试客户
for i := 1; i <= 5; i++ {
customer := &model.PersonalCustomer{
WxOpenID: testutils.GenerateUsername("wx_openid_list_", i),
WxUnionID: testutils.GenerateUsername("wx_unionid_list_", i),
Nickname: testutils.GenerateUsername("客户", i),
Status: constants.StatusEnabled,
}
err := store.Create(ctx, customer)
require.NoError(t, err)
}
t.Run("分页查询", func(t *testing.T) {
customers, total, err := store.List(ctx, nil, nil)
require.NoError(t, err)
assert.GreaterOrEqual(t, len(customers), 5)
assert.GreaterOrEqual(t, total, int64(5))
})
t.Run("带过滤条件查询", func(t *testing.T) {
filters := map[string]interface{}{
"status": constants.StatusEnabled,
}
customers, _, err := store.List(ctx, nil, filters)
require.NoError(t, err)
for _, c := range customers {
assert.Equal(t, constants.StatusEnabled, c.Status)
}
})
}
// TestPersonalCustomerStore_UniqueConstraints 测试唯一约束
func TestPersonalCustomerStore_UniqueConstraints(t *testing.T) {
tx := testutils.NewTestTransaction(t)
rdb := testutils.GetTestRedis(t)
testutils.CleanTestRedisKeys(t, rdb)
store := postgres.NewPersonalCustomerStore(tx, rdb)
ctx := context.Background()
// 创建测试客户
customer := &model.PersonalCustomer{
WxOpenID: "wx_openid_unique_test",
WxUnionID: "wx_unionid_unique_test",
Nickname: "唯一测试客户",
Status: constants.StatusEnabled,
}
err := store.Create(ctx, customer)
require.NoError(t, err)
t.Run("重复微信OpenID应失败", func(t *testing.T) {
duplicate := &model.PersonalCustomer{
WxOpenID: "wx_openid_unique_test", // 重复
WxUnionID: "wx_unionid_different",
Nickname: "另一个客户",
Status: constants.StatusEnabled,
}
err := store.Create(ctx, duplicate)
assert.Error(t, err)
})
}