feat: 实现门店套餐分配功能并统一测试基础设施
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 5m30s
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:
@@ -6,16 +6,15 @@ import (
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
"github.com/break/junhong_cmp_fiber/internal/store/postgres"
|
||||
"github.com/break/junhong_cmp_fiber/tests/testutils"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
func TestDeviceImportHandler_ProcessBatch_AllOrNothingValidation(t *testing.T) {
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
tx := newTaskTestTransaction(t)
|
||||
rdb := getTaskTestRedis(t)
|
||||
cleanTaskTestRedisKeys(t, rdb)
|
||||
|
||||
logger := zap.NewNop()
|
||||
importTaskStore := postgres.NewDeviceImportTaskStore(tx, rdb)
|
||||
@@ -145,9 +144,9 @@ func TestDeviceImportHandler_ProcessBatch_AllOrNothingValidation(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestDeviceImportHandler_ProcessImport_AllOrNothing(t *testing.T) {
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
tx := newTaskTestTransaction(t)
|
||||
rdb := getTaskTestRedis(t)
|
||||
cleanTaskTestRedisKeys(t, rdb)
|
||||
|
||||
logger := zap.NewNop()
|
||||
importTaskStore := postgres.NewDeviceImportTaskStore(tx, rdb)
|
||||
|
||||
@@ -7,16 +7,15 @@ import (
|
||||
"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"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
func TestIotCardImportHandler_ProcessImport(t *testing.T) {
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
tx := newTaskTestTransaction(t)
|
||||
rdb := getTaskTestRedis(t)
|
||||
cleanTaskTestRedisKeys(t, rdb)
|
||||
|
||||
logger := zap.NewNop()
|
||||
importTaskStore := postgres.NewIotCardImportTaskStore(tx, rdb)
|
||||
@@ -153,9 +152,9 @@ func TestIotCardImportHandler_ProcessImport(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestIotCardImportHandler_ProcessBatch(t *testing.T) {
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
tx := newTaskTestTransaction(t)
|
||||
rdb := getTaskTestRedis(t)
|
||||
cleanTaskTestRedisKeys(t, rdb)
|
||||
|
||||
logger := zap.NewNop()
|
||||
importTaskStore := postgres.NewIotCardImportTaskStore(tx, rdb)
|
||||
|
||||
121
internal/task/test_helpers_test.go
Normal file
121
internal/task/test_helpers_test.go
Normal file
@@ -0,0 +1,121 @@
|
||||
package task
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
"github.com/redis/go-redis/v9"
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/logger"
|
||||
)
|
||||
|
||||
var (
|
||||
taskTestDBOnce sync.Once
|
||||
taskTestDB *gorm.DB
|
||||
taskTestDBInitErr error
|
||||
|
||||
taskTestRedisOnce sync.Once
|
||||
taskTestRedis *redis.Client
|
||||
taskTestRedisInitErr error
|
||||
)
|
||||
|
||||
const (
|
||||
taskTestDBDSN = "host=cxd.whcxd.cn port=16159 user=erp_pgsql password=erp_2025 dbname=junhong_cmp_test sslmode=disable TimeZone=Asia/Shanghai"
|
||||
taskTestRedisAddr = "cxd.whcxd.cn:16299"
|
||||
taskTestRedisPasswd = "cpNbWtAaqgo1YJmbMp3h"
|
||||
taskTestRedisDB = 15
|
||||
)
|
||||
|
||||
func getTaskTestDB(t *testing.T) *gorm.DB {
|
||||
t.Helper()
|
||||
|
||||
taskTestDBOnce.Do(func() {
|
||||
var err error
|
||||
taskTestDB, err = gorm.Open(postgres.Open(taskTestDBDSN), &gorm.Config{
|
||||
Logger: logger.Default.LogMode(logger.Silent),
|
||||
})
|
||||
if err != nil {
|
||||
taskTestDBInitErr = fmt.Errorf("无法连接测试数据库: %w", err)
|
||||
return
|
||||
}
|
||||
|
||||
err = taskTestDB.AutoMigrate(
|
||||
&model.IotCard{},
|
||||
&model.IotCardImportTask{},
|
||||
&model.Device{},
|
||||
&model.DeviceImportTask{},
|
||||
&model.DeviceSimBinding{},
|
||||
)
|
||||
if err != nil {
|
||||
taskTestDBInitErr = fmt.Errorf("数据库迁移失败: %w", err)
|
||||
}
|
||||
})
|
||||
|
||||
if taskTestDBInitErr != nil {
|
||||
t.Skipf("跳过测试:%v", taskTestDBInitErr)
|
||||
}
|
||||
|
||||
return taskTestDB
|
||||
}
|
||||
|
||||
func getTaskTestRedis(t *testing.T) *redis.Client {
|
||||
t.Helper()
|
||||
|
||||
taskTestRedisOnce.Do(func() {
|
||||
taskTestRedis = redis.NewClient(&redis.Options{
|
||||
Addr: taskTestRedisAddr,
|
||||
Password: taskTestRedisPasswd,
|
||||
DB: taskTestRedisDB,
|
||||
})
|
||||
|
||||
ctx := context.Background()
|
||||
if err := taskTestRedis.Ping(ctx).Err(); err != nil {
|
||||
taskTestRedisInitErr = fmt.Errorf("无法连接 Redis: %w", err)
|
||||
}
|
||||
})
|
||||
|
||||
if taskTestRedisInitErr != nil {
|
||||
t.Skipf("跳过测试:%v", taskTestRedisInitErr)
|
||||
}
|
||||
|
||||
return taskTestRedis
|
||||
}
|
||||
|
||||
func newTaskTestTransaction(t *testing.T) *gorm.DB {
|
||||
t.Helper()
|
||||
|
||||
db := getTaskTestDB(t)
|
||||
tx := db.Begin()
|
||||
if tx.Error != nil {
|
||||
t.Fatalf("开启测试事务失败: %v", tx.Error)
|
||||
}
|
||||
|
||||
t.Cleanup(func() {
|
||||
tx.Rollback()
|
||||
})
|
||||
|
||||
return tx
|
||||
}
|
||||
|
||||
func cleanTaskTestRedisKeys(t *testing.T, rdb *redis.Client) {
|
||||
t.Helper()
|
||||
|
||||
ctx := context.Background()
|
||||
testPrefix := fmt.Sprintf("test:%s:", t.Name())
|
||||
|
||||
keys, _ := rdb.Keys(ctx, testPrefix+"*").Result()
|
||||
if len(keys) > 0 {
|
||||
rdb.Del(ctx, keys...)
|
||||
}
|
||||
|
||||
t.Cleanup(func() {
|
||||
keys, _ := rdb.Keys(ctx, testPrefix+"*").Result()
|
||||
if len(keys) > 0 {
|
||||
rdb.Del(ctx, keys...)
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user