Files
jiu/backend/cmd/import-history/main_test.go
T
wangjia ca7595b113 feat(backend): 出入库汇总近30天滚动窗口 + 跨租户安全加固
- summaryBounds:本月/近30天滚动双口径(stock-in/out Summary ?window=rolling30)
- security(SEC-001):新增 ownership.go ensureShopRef 写入侧防线(stock-in/out/finance/
  盘点建单的 warehouse/partner/product 外键归属校验);读取侧全部 Preload 补
  shop_id 作用域,finance Summary JOIN 补租户条件;回归测试 CrossTenantRefs
- security(SEC-002):release 模式 JWT 密钥为空/默认值时拒绝启动
- gofmt 对齐若干 model/cmd 文件

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JJ1g8XV1YhhmHRzhwWEW7o
2026-07-03 09:57:52 +08:00

106 lines
2.8 KiB
Go

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)
}
}