Files
iotlcm/worker-svr/internal/repository/inbound/repository_test.go
T

308 lines
11 KiB
Go

package inbound
import (
"context"
"os"
"path/filepath"
"strings"
"testing"
_ "github.com/gogf/gf/contrib/drivers/sqlite/v2"
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/encoding/gjson"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gcfg"
"git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/consts"
"git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/library/batchfields"
serviceinbound "git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/service/inbound"
)
func TestBatchDetailRowClampsInvalidIdentifiersAndReasons(t *testing.T) {
task := &serviceinbound.Task{Batch: serviceinbound.Batch{BatchNo: "INB-TEST", BatchType: 103}}
row := &serviceinbound.Row{RowNo: 2, ICCID: strings.Repeat("9", 100), Reason: strings.Repeat("错", 1000)}
detail := batchDetailRow(task, row, consts.BatchDetailStatusFailed)
if detail.ItemId != "" || len([]rune(detail.ErrorReason.(string))) != batchfields.DetailReasonLimit {
t.Fatalf("invalid detail: itemID=%v, reason=%v", detail.ItemId, detail.ErrorReason)
}
if row.ICCID != strings.Repeat("9", 100) || row.Reason != strings.Repeat("错", 1000) {
t.Fatal("source row was mutated; full value must remain available to result export")
}
}
func TestSaveValidationFailureHandlesOversizedDetailsAndFallback(t *testing.T) {
ctx := context.Background()
oldAdapter := g.Cfg().GetAdapter()
adapter, err := gcfg.NewAdapterContent("batch:\n inbound:\n writeBatchSize: 1000\n")
if err != nil {
t.Fatal(err)
}
g.Cfg().SetAdapter(adapter)
defer g.Cfg().SetAdapter(oldAdapter)
for _, test := range []struct {
name string
constraint string
wantFallback bool
}{
{name: "oversized invalid row is saved", constraint: "CHECK(length(item_id) <= 64 AND length(error_reason) <= 512)"},
{name: "detail write fails", constraint: "CHECK(error_reason = 'allowed')", wantFallback: true},
} {
t.Run(test.name, func(t *testing.T) {
db, err := gdb.New(gdb.ConfigNode{Type: "sqlite", Link: "sqlite::@file(:memory:)", MaxOpenConnCount: 1})
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = db.Close(ctx) })
statements := []string{
"CREATE TABLE batch_main (batch_no TEXT PRIMARY KEY, batch_type INTEGER, batch_async_status INTEGER, total_count INTEGER, valid_count INTEGER, success_count INTEGER, fail_count INTEGER, run_result INTEGER, fail_reason TEXT, batch_info TEXT, end_time DATETIME, updated_by INTEGER)",
"CREATE TABLE batch_detail (id INTEGER PRIMARY KEY AUTOINCREMENT, batch_no TEXT, batch_type INTEGER, detail_status INTEGER, blank_out_status INTEGER, row_no INTEGER, item_id TEXT, error_reason TEXT " + test.constraint + ", snapshot TEXT, tenant_id TEXT, created_dept INTEGER, created_by INTEGER, updated_by INTEGER)",
"INSERT INTO batch_main (batch_no, batch_type, batch_async_status) VALUES ('INB-TEST', 103, 2)",
}
for _, statement := range statements {
if _, err := db.Exec(ctx, statement); err != nil {
t.Fatalf("fixture failed: %v", err)
}
}
task := &serviceinbound.Task{
Batch: serviceinbound.Batch{BatchNo: "INB-TEST", BatchType: 103},
Rows: []*serviceinbound.Row{{RowNo: 2, ICCID: strings.Repeat("9", 100), Reason: strings.Repeat("错", 1000)}},
FailureReason: "输入格式不正确",
}
if err := (&Repository{db: db}).SaveValidationFailure(ctx, task); err != nil {
t.Fatalf("failed batch must be finalized: %v", err)
}
status, err := db.Model("batch_main").Ctx(ctx).Where("batch_no", "INB-TEST").Value("batch_async_status")
if err != nil || status.Int() != consts.BatchAsyncStatusFailed {
t.Fatalf("status = %v, err = %v", status, err)
}
reason, err := db.Model("batch_main").Ctx(ctx).Where("batch_no", "INB-TEST").Value("fail_reason")
if err != nil || strings.Contains(reason.String(), "错误明细保存失败") != test.wantFallback {
t.Fatalf("fallback reason = %v, err = %v", reason, err)
}
detailCount, err := db.Model("batch_detail").Ctx(ctx).Where("batch_no", "INB-TEST").Count()
wantDetails := 1
if test.wantFallback {
wantDetails = 0
}
if err != nil || detailCount != wantDetails {
t.Fatalf("details = %d, want %d, err = %v", detailCount, wantDetails, err)
}
})
}
}
func TestResolveLocalOssPath(t *testing.T) {
uploadRoot := t.TempDir()
expectedPath := filepath.Join(uploadRoot, "000000", "user", "file", "20260721", "source.xlsx")
tests := []struct {
name string
tenantID string
storedPath string
wantPath string
wantErr bool
}{
{
name: "relative Admin path",
tenantID: "000000",
storedPath: "resource/upload/000000/user/file/20260721/source.xlsx",
wantPath: expectedPath,
},
{
name: "absolute Admin path",
tenantID: "000000",
storedPath: "/app/upload/000000/user/file/20260721/source.xlsx",
wantPath: expectedPath,
},
{
name: "default tenant",
tenantID: "",
storedPath: "resource/upload/default/user/file/20260721/source.xlsx",
wantPath: filepath.Join(uploadRoot, "default", "user", "file", "20260721", "source.xlsx"),
},
{
name: "tenant is missing",
tenantID: "000000",
storedPath: "resource/upload/000001/user/file/20260721/source.xlsx",
wantErr: true,
},
{
name: "path traversal is rejected",
tenantID: "000000",
storedPath: "resource/upload/000000/../secret.xlsx",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotPath, err := resolveLocalOssPath(uploadRoot, tt.tenantID, tt.storedPath)
if tt.wantErr {
if err == nil {
t.Fatal("expected an error")
}
return
}
if err != nil {
t.Fatalf("resolveLocalOssPath() error = %v", err)
}
if gotPath != tt.wantPath {
t.Fatalf("resolveLocalOssPath() = %q, want %q", gotPath, tt.wantPath)
}
})
}
}
func TestResolveLocalOssPathReturnsReadableFile(t *testing.T) {
uploadRoot := t.TempDir()
storedPath := "resource/upload/000000/user/file/20260721/source.xlsx"
localPath, err := resolveLocalOssPath(uploadRoot, "000000", storedPath)
if err != nil {
t.Fatalf("resolveLocalOssPath() error = %v", err)
}
if err = os.MkdirAll(filepath.Dir(localPath), 0o755); err != nil {
t.Fatalf("create directory: %v", err)
}
if err = os.WriteFile(localPath, []byte("inbound source"), 0o600); err != nil {
t.Fatalf("write source file: %v", err)
}
content, err := os.ReadFile(localPath)
if err != nil {
t.Fatalf("read source file: %v", err)
}
if string(content) != "inbound source" {
t.Fatalf("unexpected source content: %q", content)
}
}
func TestUploadRootCandidatesIncludeMountedUploadDirectory(t *testing.T) {
wd := t.TempDir()
oldWd, err := os.Getwd()
if err != nil {
t.Fatalf("get working directory: %v", err)
}
if err = os.Chdir(wd); err != nil {
t.Fatalf("change working directory: %v", err)
}
defer func() {
_ = os.Chdir(oldWd)
}()
candidates := uploadRootCandidates("resource/upload")
want := filepath.Join(wd, "upload")
for _, candidate := range candidates {
if filepath.Clean(candidate) == filepath.Clean(want) {
return
}
}
t.Fatalf("uploadRootCandidates() = %#v, want %q", candidates, want)
}
func TestResolveReadableLocalOssPathUsesMountedUploadDirectory(t *testing.T) {
wd := t.TempDir()
oldWd, err := os.Getwd()
if err != nil {
t.Fatalf("get working directory: %v", err)
}
if err = os.Chdir(wd); err != nil {
t.Fatalf("change working directory: %v", err)
}
defer func() {
_ = os.Chdir(oldWd)
}()
storedPath := "resource/upload/000000/user/file/20260813/sim-card-inbound/import/source.xlsx"
mountedPath := filepath.Join(wd, "upload", "000000", "user", "file", "20260813", "sim-card-inbound", "import", "source.xlsx")
if err = os.MkdirAll(filepath.Dir(mountedPath), 0o755); err != nil {
t.Fatalf("create mounted upload directory: %v", err)
}
if err = os.WriteFile(mountedPath, []byte("xlsx"), 0o600); err != nil {
t.Fatalf("write mounted upload file: %v", err)
}
gotPath, attempts, err := resolveReadableLocalOssPath("resource/upload", "000000", storedPath)
if err != nil {
t.Fatalf("resolveReadableLocalOssPath() error = %v, attempts = %#v", err, attempts)
}
if gotPath != mountedPath {
t.Fatalf("resolveReadableLocalOssPath() = %q, want %q, attempts = %#v", gotPath, mountedPath, attempts)
}
}
func TestBuildResultBatchInfoUsesGroupedResultFile(t *testing.T) {
task := &serviceinbound.Task{
Batch: serviceinbound.Batch{
BatchInfo: gjson.New(map[string]any{
"source": map[string]any{"inputText": "8986"},
}),
},
}
info := buildResultBatchInfo(task, "failed", 0, 0, "disk full")
if info.Get("resultFile.status").String() != "failed" {
t.Fatalf("unexpected result file status: %s", info.Get("resultFile.status").String())
}
if info.Get("resultFile.error").String() != "disk full" {
t.Fatalf("unexpected result file error: %s", info.Get("resultFile.error").String())
}
if info.Get("resultFileOssId").Int64() != 0 || info.Get("resultDownloadId").Int64() != 0 {
t.Fatalf("result file metadata should stay grouped: %s", info.MustToJsonString())
}
}
func TestParseAndBuildInboundOptionsPreserveUpstreamValidationFlag(t *testing.T) {
info := gjson.New(map[string]any{
"inboundOptions": map[string]any{
"chanKey": "chan-a",
"validateUpstreamBasicInfo": 1,
},
})
options := parseOptions(info)
if options.ValidateUpstreamBasicInfo != "1" {
t.Fatalf("ValidateUpstreamBasicInfo = %q, want 1", options.ValidateUpstreamBasicInfo)
}
task := &serviceinbound.Task{
Batch: serviceinbound.Batch{BatchInfo: info},
Options: options,
}
built := buildInboundBatchInfo(task)
if built.Get("inboundOptions.validateUpstreamBasicInfo").String() != "1" {
t.Fatalf("validate option not preserved: %s", built.MustToJsonString())
}
if built.Get("inboundOptions.chanKey").String() != "chan-a" {
t.Fatalf("chanKey not preserved: %s", built.MustToJsonString())
}
}
func TestParseSourcePrefersSourceOssId(t *testing.T) {
tests := []struct {
name string
sourceType int
wantImportID int64
wantInputID int64
}{
{name: "file source", sourceType: consts.BatchSourceTypeFile, wantImportID: 88, wantInputID: 22},
{name: "input source", sourceType: consts.BatchSourceTypeBatchInput, wantImportID: 11, wantInputID: 88},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
info := gjson.New(map[string]any{
"source": map[string]any{
"importFileOssId": int64(11),
"inputFileOssId": int64(22),
},
})
source := parseSource(info, 88, tt.sourceType)
if source.ImportFileOssID != tt.wantImportID {
t.Fatalf("ImportFileOssID = %d, want %d", source.ImportFileOssID, tt.wantImportID)
}
if source.InputFileOssID != tt.wantInputID {
t.Fatalf("InputFileOssID = %d, want %d", source.InputFileOssID, tt.wantInputID)
}
})
}
}