feat: 收敛店铺管理和蝦皮分类搜索 (#212 #213)

This commit is contained in:
chengma
2026-08-13 11:47:56 +08:00
parent 1abed648a5
commit 313e9f9151
22 changed files with 389 additions and 464 deletions
+40 -26
View File
@@ -242,7 +242,7 @@ func TestListShopeeProducts_关键字匹配商品ID和名称_不匹配颜色尺
}
// 按商品名称匹配
result, err = ListShopeeProducts(db, repository.ShopeeFilter{Keyword: "背心"}, 1)
result, err = ListShopeeProducts(db, repository.ShopeeFilter{Keyword: "背心", SearchField: "title"}, 1)
if err != nil {
t.Fatalf("查询失败: %v", err)
}
@@ -251,7 +251,7 @@ func TestListShopeeProducts_关键字匹配商品ID和名称_不匹配颜色尺
}
// 颜色/尺码不参与匹配:搜"黑色"应该搜不到任何商品。
result, err = ListShopeeProducts(db, repository.ShopeeFilter{Keyword: "黑色"}, 1)
result, err = ListShopeeProducts(db, repository.ShopeeFilter{Keyword: "黑色", SearchField: "title"}, 1)
if err != nil {
t.Fatalf("查询失败: %v", err)
}
@@ -475,7 +475,7 @@ func TestParseShopeeStatus_认不出来当全部(t *testing.T) {
}
}
func TestListShopeeProducts_店铺图片三态及组合筛选(t *testing.T) {
func TestListShopeeProducts_分类搜索(t *testing.T) {
db := newTestDB(t)
for _, product := range []struct {
id, title, shop, image string
@@ -496,15 +496,9 @@ func TestListShopeeProducts_店铺图片三态及组合筛选(t *testing.T) {
filter repository.ShopeeFilter
want []string
}{
{"有店铺", repository.ShopeeFilter{Shop: "has"}, []string{"A", "B"}},
{"无店铺", repository.ShopeeFilter{Shop: "missing"}, []string{"C", "D"}},
{"有图片", repository.ShopeeFilter{Image: "has"}, []string{"A", "C"}},
{"无图片", repository.ShopeeFilter{Image: "missing"}, []string{"B", "D"}},
{"有店铺且有图片", repository.ShopeeFilter{Shop: "has", Image: "has"}, []string{"A"}},
{"无店铺且无图片", repository.ShopeeFilter{Shop: "missing", Image: "missing"}, []string{"D"}},
{"组合关键词", repository.ShopeeFilter{Keyword: "店铺有图", Shop: "has", Image: "has"}, []string{"A"}},
{"店铺名称", repository.ShopeeFilter{ShopName: "店铺 A"}, []string{"A"}},
{"店铺名称组合", repository.ShopeeFilter{ShopName: "店铺", Shop: "has", Image: "missing"}, []string{"B"}},
{"商品ID精确", repository.ShopeeFilter{Keyword: "A", SearchField: "goods_id"}, []string{"A"}},
{"商品名称包含", repository.ShopeeFilter{Keyword: "店铺有图", SearchField: "title"}, []string{"A"}},
{"店铺名称包含", repository.ShopeeFilter{Keyword: "店铺", SearchField: "shop_name"}, []string{"A", "B"}},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
@@ -521,7 +515,7 @@ func TestListShopeeProducts_店铺图片三态及组合筛选(t *testing.T) {
}
}
if !result.IsFiltered {
t.Fatal("使用店铺/图片条件时 IsFiltered 应为 true")
t.Fatal("使用分类搜索时 IsFiltered 应为 true")
}
})
}
@@ -540,7 +534,7 @@ func TestListShopeeProducts_店铺名称LIKE特殊字符按原文匹配(t *testi
}
for _, keyword := range []string{"%", "_", "!", "%店_"} {
result, err := ListShopeeProducts(db, repository.ShopeeFilter{ShopName: keyword}, 1)
result, err := ListShopeeProducts(db, repository.ShopeeFilter{Keyword: keyword, SearchField: "shop_name"}, 1)
if err != nil {
t.Fatal(err)
}
@@ -553,19 +547,39 @@ func TestListShopeeProducts_店铺名称LIKE特殊字符按原文匹配(t *testi
}
}
func TestParseShopeePresence_三态参数(t *testing.T) {
for _, value := range []string{"has", "missing"} {
if got := ParseShopeePresence(value); got != value {
t.Errorf("ParseShopeePresence(%q)=%q", value, got)
}
func TestListShopeeProducts_未登记入口只显示未关联店铺(t *testing.T) {
db := newTestDB(t)
seedShopeeProduct(t, db, "UNLINKED", "未登记商品")
seedShopeeProduct(t, db, "LINKED", "已登记商品")
if _, err := db.Exec(`INSERT INTO users(user_id,username,password_hash,role,status,password_changed_at,created_at,updated_at)
VALUES('SHOP-USER','shop-user','x','admin','active',CURRENT_TIMESTAMP,CURRENT_TIMESTAMP,CURRENT_TIMESTAMP)`); err != nil {
t.Fatal(err)
}
for _, value := range []string{"", "all", "HAS", "unknown", " "} {
if got := ParseShopeePresence(value); got != "" {
t.Errorf("ParseShopeePresence(%q)=%q,期望全部", value, got)
}
if _, err := db.Exec(`INSERT INTO shops(shop_id,display_name,normalized_name,enabled,created_by_user_id,created_at,updated_at)
VALUES('SHOP-1','店铺一','店铺一',1,'SHOP-USER',CURRENT_TIMESTAMP,CURRENT_TIMESTAMP)`); err != nil {
t.Fatal(err)
}
options := ShopeePresenceOptions("有店铺", "无店铺")
if len(options) != 3 || options[0].Value != "" || options[1].Value != "has" || options[2].Value != "missing" {
t.Fatalf("三态选项不正确:%+v", options)
if _, err := db.Exec(`UPDATE shopee_products SET shop_id='SHOP-1' WHERE goods_id='LINKED'`); err != nil {
t.Fatal(err)
}
result, err := ListShopeeProducts(db, repository.ShopeeFilter{Unlinked: true}, 1)
if err != nil {
t.Fatal(err)
}
if result.Total != 1 || len(result.Rows) != 1 || result.Rows[0].GoodsID != "UNLINKED" {
t.Fatalf("未登记结果错误:total=%d rows=%+v", result.Total, result.Rows)
}
}
func TestParseShopeeSearchField_固定白名单(t *testing.T) {
for _, value := range []string{"goods_id", "title", "shop_name"} {
if got := ParseShopeeSearchField(value); got != value {
t.Errorf("ParseShopeeSearchField(%q)=%q", value, got)
}
}
for _, value := range []string{"", "unknown", "title desc", " "} {
if got := ParseShopeeSearchField(value); got != "goods_id" {
t.Errorf("ParseShopeeSearchField(%q)=%q,期望 goods_id", value, got)
}
}
}