feat: 增加蝦皮店铺图片筛选 (#150)

This commit is contained in:
chengma
2026-08-11 12:04:38 +08:00
parent 2a3224b86d
commit c39f5a7912
9 changed files with 212 additions and 11 deletions
+67
View File
@@ -474,3 +474,70 @@ func TestParseShopeeStatus_认不出来当全部(t *testing.T) {
}
}
}
func TestListShopeeProducts_店铺图片三态及组合筛选(t *testing.T) {
db := newTestDB(t)
for _, product := range []struct {
id, title, shop, image string
}{
{"A", "店铺有图", "店铺 A", "https://example.invalid/a.jpg"},
{"B", "店铺无图", "店铺 B", ""},
{"C", "无店有图", "", "https://example.invalid/c.jpg"},
{"D", "资料为空白", " ", " "},
} {
seedShopeeProduct(t, db, product.id, product.title)
if _, err := db.Exec(`UPDATE shopee_products SET shopee_shop_name=?,image_url=? WHERE goods_id=?`, product.shop, product.image, product.id); err != nil {
t.Fatal(err)
}
}
cases := []struct {
name string
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"}},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
result, err := ListShopeeProducts(db, tc.filter, 1)
if err != nil {
t.Fatal(err)
}
if result.Total != len(tc.want) || len(result.Rows) != len(tc.want) {
t.Fatalf("Total=%d rows=%d,期望 %v", result.Total, len(result.Rows), tc.want)
}
for index, want := range tc.want {
if result.Rows[index].GoodsID != want {
t.Fatalf("第 %d 行=%s,期望 %s", index, result.Rows[index].GoodsID, want)
}
}
if !result.IsFiltered {
t.Fatal("使用店铺/图片条件时 IsFiltered 应为 true")
}
})
}
}
func TestParseShopeePresence_三态参数(t *testing.T) {
for _, value := range []string{"has", "missing"} {
if got := ParseShopeePresence(value); got != value {
t.Errorf("ParseShopeePresence(%q)=%q", value, got)
}
}
for _, value := range []string{"", "all", "HAS", "unknown", " "} {
if got := ParseShopeePresence(value); got != "" {
t.Errorf("ParseShopeePresence(%q)=%q,期望全部", value, got)
}
}
options := ShopeePresenceOptions("有店铺", "无店铺")
if len(options) != 3 || options[0].Value != "" || options[1].Value != "has" || options[2].Value != "missing" {
t.Fatalf("三态选项不正确:%+v", options)
}
}