82 lines
2.7 KiB
Go
82 lines
2.7 KiB
Go
package sybimport_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"go-admin/app/goauto/models"
|
|
"go-admin/app/goauto/sybimport"
|
|
)
|
|
|
|
// Real samples from demo/shunyunbaoerp_stock_list.har (details[145306175],
|
|
// details[145306176]), not fabricated fixtures.
|
|
func TestParseRealSample_CleanColorAndSize(t *testing.T) {
|
|
result := sybimport.Parse("白色,L【建議50-60公斤】")
|
|
if result.Status != models.SYBParseStatusSuccess {
|
|
t.Fatalf("expected success, got %s (%s)", result.Status, result.Note)
|
|
}
|
|
if result.Color != "白色" || result.Size != "L" {
|
|
t.Fatalf("expected 白色/L, got %q/%q", result.Color, result.Size)
|
|
}
|
|
}
|
|
|
|
func TestParseRealSample_AmbiguousColorWithAnnotationAndSeparator(t *testing.T) {
|
|
result := sybimport.Parse("黑色+白色【純棉兩件裝】 簡約親膚,L【建議52.5-60公斤】")
|
|
if result.Status != models.SYBParseStatusUncertain {
|
|
t.Fatalf("expected uncertain, got %s", result.Status)
|
|
}
|
|
if result.Size != "L" {
|
|
t.Fatalf("size should still parse cleanly, got %q", result.Size)
|
|
}
|
|
if result.Color == "" {
|
|
t.Fatal("color candidate should still be reported for human review, not discarded")
|
|
}
|
|
}
|
|
|
|
func TestParseEmptyInputFails(t *testing.T) {
|
|
result := sybimport.Parse(" ")
|
|
if result.Status != models.SYBParseStatusFailed {
|
|
t.Fatalf("expected failed, got %s", result.Status)
|
|
}
|
|
if result.Color != "" || result.Size != "" {
|
|
t.Fatal("failed parse must not invent a color or size")
|
|
}
|
|
}
|
|
|
|
func TestParseNoSeparatorIsUncertainNotFailed(t *testing.T) {
|
|
result := sybimport.Parse("均碼")
|
|
if result.Status != models.SYBParseStatusUncertain {
|
|
t.Fatalf("expected uncertain (not failed) for a single token, got %s", result.Status)
|
|
}
|
|
if result.Size != "均碼" || result.Color != "" {
|
|
t.Fatalf("single token should become the size candidate only, got color=%q size=%q", result.Color, result.Size)
|
|
}
|
|
}
|
|
|
|
func TestParseNeverInventsAColorOrSizeNotInInput(t *testing.T) {
|
|
for _, raw := range []string{"", " ", "均碼", "白色,L", "黑色+白色 备注,L"} {
|
|
result := sybimport.Parse(raw)
|
|
if result.Color != "" && !containsSubstring(raw, result.Color) {
|
|
t.Fatalf("Parse(%q) invented color %q not present in input", raw, result.Color)
|
|
}
|
|
if result.Size != "" && !containsSubstring(raw, result.Size) {
|
|
t.Fatalf("Parse(%q) invented size %q not present in input", raw, result.Size)
|
|
}
|
|
}
|
|
}
|
|
|
|
func containsSubstring(haystack, needle string) bool {
|
|
for i := 0; i+len(needle) <= len(haystack); i++ {
|
|
if haystack[i:i+len(needle)] == needle {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func TestParseBothSidesEmptyAfterStrippingIsUncertain(t *testing.T) {
|
|
result := sybimport.Parse("【備註】,【備註】")
|
|
if result.Status != models.SYBParseStatusUncertain {
|
|
t.Fatalf("expected uncertain when both sides strip to empty, got %s", result.Status)
|
|
}
|
|
}
|