package outbound import ( "errors" "strings" "testing" "git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/library/batchimport" "git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/model/entity" ) func TestValidateOutboundRowCollectsIndependentFailures(t *testing.T) { row := &Row{Iccid: "card", Msisdn: "wrong", Imsi: "wrong"} stock := &entity.TSimCardStock{CreatedDept: 200, BatchNo: "other", Msisdn: "13800008000", Imsi: "460001234567890"} reason := validateOutboundRow(row, stock, true, batchimport.OperatorContext{DeptId: 100}, true, "required") for _, want := range []string{"不属于当前机构库存", "不属于指定库存批次", "已存在正式卡记录", "MSISDN与库存记录不一致", "IMSI与库存记录不一致"} { if !strings.Contains(reason, want) { t.Fatalf("missing %q in %q", want, reason) } } rows := []*Row{{Reason: reason}, {}, {Reason: "ICCID不存在库存记录,不能出库"}} markRemainingRowsSkipped(rows) if rows[0].Reason != reason || rows[1].Reason != outboundNotExecutedReason || rows[2].Reason != "ICCID不存在库存记录,不能出库" { t.Fatalf("per-card validation results were overwritten: %+v", rows) } } func TestOutboundChunkSizesSupportFiftyThousandCards(t *testing.T) { const totalCards = 50000 if outboundDatabaseInsertChunkSize <= 0 || outboundDatabaseInsertChunkSize > 500 { t.Fatalf("outboundDatabaseInsertChunkSize = %d, want 1..500", outboundDatabaseInsertChunkSize) } if chunks := (totalCards + outboundDatabaseInsertChunkSize - 1) / outboundDatabaseInsertChunkSize; chunks != 100 { t.Fatalf("card insert chunks = %d, want 100", chunks) } } func TestAsyncFailureReasonSummarizesPlaceholderOverflow(t *testing.T) { reason := asyncFailureReason(errors.New("INSERT INTO t_sim_card_info (...) Error 1390: Prepared statement contains too many placeholders")) if reason != "新增正式卡失败:单次批量写入参数数量超过数据库限制" { t.Fatalf("unexpected reason: %q", reason) } } func TestAsyncFailureReasonFitsBatchDetailColumn(t *testing.T) { reason := asyncFailureReason(errors.New(strings.Repeat("失败原因", 300))) if len([]rune(reason)) > maxAsyncFailureReasonLength { t.Fatalf("reason length = %d, want <= %d", len([]rune(reason)), maxAsyncFailureReasonLength) } } func TestValidateOutboundIdentifiersReportsEveryDuplicate(t *testing.T) { rows := []*Row{ {RowNo: 1, Iccid: " 8986000000000000001 "}, {RowNo: 2, Iccid: "8986000000000000001"}, {RowNo: 3, Iccid: ""}, {RowNo: 4, Iccid: "8986000000000000002"}, } iccids, err := validateOutboundIdentifiers(rows) if err != nil || len(iccids) != 1 || iccids[0] != rows[3].Iccid { t.Fatalf("valid identifiers = %v, err = %v", iccids, err) } for index := 0; index < 3; index++ { if rows[index].Reason == "" { t.Fatalf("row %d lost its validation reason", index+1) } } markRemainingRowsSkipped(rows) if rows[3].Reason != outboundNotExecutedReason { t.Fatalf("valid row reason = %q", rows[3].Reason) } }