package lifecycle import ( "context" "strings" "testing" "git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/library/batchimport" ) func TestValidateBasicRowsRejectsDuplicateIccid(t *testing.T) { rows := []batchimport.Row{ &Row{RowNo: 2, Iccid: " 8986001 "}, &Row{RowNo: 3, Iccid: "8986001"}, } err := validateBasicRows(rows) if err == nil || !strings.Contains(err.Error(), "ICCID") || !strings.Contains(err.Error(), "重复") { t.Fatalf("expected readable duplicate ICCID error, got %v", err) } } func TestValidateBasicRowsRejectsEveryEmptyIccid(t *testing.T) { rows := []batchimport.Row{ &Row{RowNo: 2, Iccid: ""}, &Row{RowNo: 5, Iccid: " "}, } err := validateBasicRows(rows) if err == nil { t.Fatal("expected empty ICCID validation error") } if !strings.Contains(err.Error(), "第2行") || !strings.Contains(err.Error(), "第5行") { t.Fatalf("expected all empty ICCID rows in error, got %v", err) } } func TestValidateBasicRowsRejectsWrongFieldCount(t *testing.T) { rows, err := ParseInputRows(context.Background(), "8986001 13800008000") if err != nil { t.Fatal(err) } err = validateBasicRows(rows) if err == nil || !strings.Contains(err.Error(), "格式") { t.Fatalf("expected wrong field count error, got %v", err) } } func TestInvalidBasicRowsStopBeforePendingPreparation(t *testing.T) { tests := []struct { name string rows []batchimport.Row }{ { name: "duplicate", rows: []batchimport.Row{ &Row{RowNo: 2, Iccid: "8986001"}, &Row{RowNo: 3, Iccid: "8986001"}, }, }, { name: "multiple empty iccid", rows: []batchimport.Row{ &Row{RowNo: 2}, &Row{RowNo: 3, Iccid: " "}, }, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { processor, err := NewProcessor(OperationSuspend) if err != nil { t.Fatal(err) } prepared := false result, err := batchimport.CreatePendingRows( context.Background(), batchimport.SubmitRequest{}, processor, tt.rows, batchimport.PendingOptions{PrepareRows: func(context.Context, []batchimport.Row) error { prepared = true return nil }}, ) if err == nil || result != nil { t.Fatalf("expected pre-persistence validation error, result=%+v err=%v", result, err) } if prepared { t.Fatal("invalid rows reached pending preparation") } }) } }