package main import "testing" func TestMapStatus(t *testing.T) { cases := map[string]string{ "已审核": "approved", "待审核": "pending", "被驳回": "rejected", "作废": "rejected", "": "draft", "未知": "draft", " 已审核 ": "approved", } for in, want := range cases { if got := mapStatus(in); got != want { t.Errorf("mapStatus(%q)=%q want %q", in, got, want) } } } func TestParseFloatLoose(t *testing.T) { cases := map[string]float64{ "": 0, " ": 0, "123": 123, "1,234.50": 1234.5, "abc": 0, "-9.9": -9.9, } for in, want := range cases { if got := parseFloatLoose(in); got != want { t.Errorf("parseFloatLoose(%q)=%v want %v", in, got, want) } } } func TestParseDatePtr(t *testing.T) { if parseDatePtr("") != nil { t.Error("empty should be nil") } if parseDatePtr("garbage") != nil { t.Error("garbage should be nil") } for _, s := range []string{"2023/04/04", "1998-12-08", "2023/04/04 10:30:00"} { if d := parseDatePtr(s); d == nil { t.Errorf("parseDatePtr(%q) should parse", s) } } d := parseDatePtr("2023-04-04") if d == nil || d.Time.Year() != 2023 || int(d.Time.Month()) != 4 || d.Time.Day() != 4 { t.Errorf("parseDatePtr 2023-04-04 wrong: %+v", d) } } // matchPerson:损坏的人员列容损规整到三位真人,匹配不到归 nil。 func newTestCtx() *importCtx { ic := &importCtx{} ic.personToUser = map[string]uint64{"丁国辉": 11, "刘春燕": 12, "张志磊": 13, "汤振宇": 14} ic.unmatchedOps = map[string]int{} return ic } func TestMatchPerson(t *testing.T) { ic := newTestCtx() check := func(raw string, wantID uint64, wantNil bool) { got := ic.matchPerson(raw) if wantNil { if got != nil { t.Errorf("matchPerson(%q) want nil got %v", raw, *got) } return } if got == nil || *got != wantID { t.Errorf("matchPerson(%q) want %d got %v", raw, wantID, got) } } check("丁国辉", 11, false) check("丁国GSHD", 11, false) // 容损前缀 check("张志繥SHD", 13, false) check("刘春燕", 12, false) check("汤振宇", 14, false) check("", 0, true) check("4", 0, true) // 单字符乱码 check("王五", 0, true) if ic.unmatchedOps["王五"] != 1 || ic.unmatchedOps["4"] != 1 { t.Errorf("unmatchedOps not recorded: %+v", ic.unmatchedOps) } } func TestGetColumn(t *testing.T) { cols := map[string]int{"单据编号": 0, "商品名称": 1, "数量": 2} row := []string{"RK001", " 飞天茅台 ", "12"} if got := get(row, cols, "商品名称"); got != "飞天茅台" { t.Errorf("get trimmed wrong: %q", got) } if got := get(row, cols, "不存在"); got != "" { t.Errorf("missing col should be empty: %q", got) } // 行短于列索引时不越界 if got := get([]string{"RK001"}, cols, "数量"); got != "" { t.Errorf("short row should be empty: %q", got) } }