All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 4m42s
新增物联网卡独立管理模块,支持单卡查询、批量导入和状态管理。主要变更包括: 功能特性: - 新增物联网卡 CRUD 接口(查询、分页列表、删除) - 支持 CSV/Excel 批量导入物联网卡 - 实现异步导入任务处理和进度跟踪 - 新增 ICCID 号码格式校验器(支持 Luhn 算法) - 新增 CSV 文件解析工具(支持编码检测和错误处理) 数据库变更: - 移除 iot_card 和 device 表的 owner_id/owner_type 字段 - 新增 iot_card_import_task 导入任务表 - 为导入任务添加运营商类型字段 测试覆盖: - 新增 IoT 卡 Store 层单元测试 - 新增 IoT 卡导入任务单元测试 - 新增 IoT 卡集成测试(包含导入流程测试) - 新增 CSV 工具和 ICCID 校验器测试 文档更新: - 更新 OpenAPI 文档(新增 7 个 IoT 卡接口) - 归档 OpenSpec 变更提案 - 更新 API 文档规范和生成器指南 Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
188 lines
5.2 KiB
Go
188 lines
5.2 KiB
Go
package task
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"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)
|
|
|
|
logger := zap.NewNop()
|
|
importTaskStore := postgres.NewIotCardImportTaskStore(tx, rdb)
|
|
iotCardStore := postgres.NewIotCardStore(tx, rdb)
|
|
|
|
handler := NewIotCardImportHandler(tx, rdb, importTaskStore, iotCardStore, logger)
|
|
ctx := context.Background()
|
|
|
|
t.Run("成功导入新ICCID", func(t *testing.T) {
|
|
task := &model.IotCardImportTask{
|
|
CarrierID: 1,
|
|
CarrierType: constants.CarrierCodeCMCC,
|
|
BatchNo: "TEST_BATCH_001",
|
|
ICCIDList: model.ICCIDListJSON{"89860012345678905001", "89860012345678905002", "89860012345678905003"},
|
|
TotalCount: 3,
|
|
}
|
|
task.Creator = 1
|
|
|
|
result := handler.processImport(ctx, task)
|
|
|
|
assert.Equal(t, 3, result.successCount)
|
|
assert.Equal(t, 0, result.skipCount)
|
|
assert.Equal(t, 0, result.failCount)
|
|
|
|
exists, _ := iotCardStore.ExistsByICCID(ctx, "89860012345678905001")
|
|
assert.True(t, exists)
|
|
})
|
|
|
|
t.Run("跳过已存在的ICCID", func(t *testing.T) {
|
|
existingCard := &model.IotCard{
|
|
ICCID: "89860012345678906001",
|
|
CardType: "data_card",
|
|
CarrierID: 1,
|
|
Status: 1,
|
|
}
|
|
require.NoError(t, iotCardStore.Create(ctx, existingCard))
|
|
|
|
task := &model.IotCardImportTask{
|
|
CarrierID: 1,
|
|
CarrierType: constants.CarrierCodeCMCC,
|
|
BatchNo: "TEST_BATCH_002",
|
|
ICCIDList: model.ICCIDListJSON{"89860012345678906001", "89860012345678906002"},
|
|
TotalCount: 2,
|
|
}
|
|
task.Creator = 1
|
|
|
|
result := handler.processImport(ctx, task)
|
|
|
|
assert.Equal(t, 1, result.successCount)
|
|
assert.Equal(t, 1, result.skipCount)
|
|
assert.Equal(t, 0, result.failCount)
|
|
assert.Len(t, result.skippedItems, 1)
|
|
assert.Equal(t, "89860012345678906001", result.skippedItems[0].ICCID)
|
|
assert.Equal(t, "ICCID 已存在", result.skippedItems[0].Reason)
|
|
})
|
|
|
|
t.Run("ICCID格式校验失败", func(t *testing.T) {
|
|
task := &model.IotCardImportTask{
|
|
CarrierID: 1,
|
|
CarrierType: constants.CarrierCodeCTCC,
|
|
BatchNo: "TEST_BATCH_003",
|
|
ICCIDList: model.ICCIDListJSON{"89860312345678907001", "898603123456789070"},
|
|
TotalCount: 2,
|
|
}
|
|
task.Creator = 1
|
|
|
|
result := handler.processImport(ctx, task)
|
|
|
|
assert.Equal(t, 0, result.successCount)
|
|
assert.Equal(t, 0, result.skipCount)
|
|
assert.Equal(t, 2, result.failCount)
|
|
assert.Len(t, result.failedItems, 2)
|
|
})
|
|
|
|
t.Run("混合场景-成功跳过和失败", func(t *testing.T) {
|
|
existingCard := &model.IotCard{
|
|
ICCID: "89860012345678908001",
|
|
CardType: "data_card",
|
|
CarrierID: 1,
|
|
Status: 1,
|
|
}
|
|
require.NoError(t, iotCardStore.Create(ctx, existingCard))
|
|
|
|
task := &model.IotCardImportTask{
|
|
CarrierID: 1,
|
|
CarrierType: constants.CarrierCodeCMCC,
|
|
BatchNo: "TEST_BATCH_004",
|
|
ICCIDList: model.ICCIDListJSON{
|
|
"89860012345678908001",
|
|
"89860012345678908002",
|
|
"invalid!iccid",
|
|
},
|
|
TotalCount: 3,
|
|
}
|
|
task.Creator = 1
|
|
|
|
result := handler.processImport(ctx, task)
|
|
|
|
assert.Equal(t, 1, result.successCount)
|
|
assert.Equal(t, 1, result.skipCount)
|
|
assert.Equal(t, 1, result.failCount)
|
|
})
|
|
|
|
t.Run("空ICCID列表", func(t *testing.T) {
|
|
task := &model.IotCardImportTask{
|
|
CarrierID: 1,
|
|
CarrierType: constants.CarrierCodeCMCC,
|
|
BatchNo: "TEST_BATCH_005",
|
|
ICCIDList: model.ICCIDListJSON{},
|
|
TotalCount: 0,
|
|
}
|
|
|
|
result := handler.processImport(ctx, task)
|
|
|
|
assert.Equal(t, 0, result.successCount)
|
|
assert.Equal(t, 0, result.skipCount)
|
|
assert.Equal(t, 0, result.failCount)
|
|
})
|
|
}
|
|
|
|
func TestIotCardImportHandler_ProcessBatch(t *testing.T) {
|
|
tx := testutils.NewTestTransaction(t)
|
|
rdb := testutils.GetTestRedis(t)
|
|
testutils.CleanTestRedisKeys(t, rdb)
|
|
|
|
logger := zap.NewNop()
|
|
importTaskStore := postgres.NewIotCardImportTaskStore(tx, rdb)
|
|
iotCardStore := postgres.NewIotCardStore(tx, rdb)
|
|
|
|
handler := NewIotCardImportHandler(tx, rdb, importTaskStore, iotCardStore, logger)
|
|
ctx := context.Background()
|
|
|
|
t.Run("验证行号正确记录", func(t *testing.T) {
|
|
existingCard := &model.IotCard{
|
|
ICCID: "89860012345678909002",
|
|
CardType: "data_card",
|
|
CarrierID: 1,
|
|
Status: 1,
|
|
}
|
|
require.NoError(t, iotCardStore.Create(ctx, existingCard))
|
|
|
|
task := &model.IotCardImportTask{
|
|
CarrierID: 1,
|
|
CarrierType: constants.CarrierCodeCMCC,
|
|
BatchNo: "TEST_BATCH_LINE",
|
|
}
|
|
task.Creator = 1
|
|
|
|
batch := []string{
|
|
"89860012345678909001",
|
|
"89860012345678909002",
|
|
"invalid",
|
|
}
|
|
result := &importResult{
|
|
skippedItems: make(model.ImportResultItems, 0),
|
|
failedItems: make(model.ImportResultItems, 0),
|
|
}
|
|
|
|
handler.processBatch(ctx, task, batch, 100, result)
|
|
|
|
assert.Equal(t, 1, result.successCount)
|
|
assert.Equal(t, 1, result.skipCount)
|
|
assert.Equal(t, 1, result.failCount)
|
|
|
|
assert.Equal(t, 101, result.skippedItems[0].Line)
|
|
assert.Equal(t, 102, result.failedItems[0].Line)
|
|
})
|
|
}
|