feat: 统一 Admin 主列表分页 (#64)
This commit is contained in:
@@ -3,6 +3,7 @@ package service
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
@@ -12,6 +13,33 @@ import (
|
||||
"cmautobuy/admin/repository"
|
||||
)
|
||||
|
||||
func TestListPddProducts_统一每页20条并收敛越界页(t *testing.T) {
|
||||
db := newTestDB(t)
|
||||
for i := 0; i < 25; i++ {
|
||||
goodsID := fmt.Sprintf("73711653%04d", i)
|
||||
if _, err := CreatePddProduct(db,
|
||||
"https://mobile.yangkeduo.com/goods.html?goods_id="+goodsID); err != nil {
|
||||
t.Fatalf("创建第 %d 个商品失败: %v", i, err)
|
||||
}
|
||||
}
|
||||
|
||||
second, err := ListPddProducts(db, "", "", 2)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(second.Rows) != 5 || second.FilteredTotal != 25 || second.Page != 2 || second.TotalPages != 2 {
|
||||
t.Fatalf("第二页结果不对: rows=%d total=%d page=%d/%d",
|
||||
len(second.Rows), second.FilteredTotal, second.Page, second.TotalPages)
|
||||
}
|
||||
overflow, err := ListPddProducts(db, "", "", 999)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if overflow.Page != 2 || len(overflow.Rows) != 5 {
|
||||
t.Fatalf("越界页应收敛到末页: page=%d rows=%d", overflow.Page, len(overflow.Rows))
|
||||
}
|
||||
}
|
||||
|
||||
// ── 链接解析 ───────────────────────────────────────────
|
||||
|
||||
func TestParsePddGoodsID_认得的写法(t *testing.T) {
|
||||
@@ -259,7 +287,7 @@ func TestListPddProducts_规格数(t *testing.T) {
|
||||
repository.SetCollectResult(db, "100000000002", "有规格的", "", sampleSkusJSON)
|
||||
repository.SetCollectResult(db, "100000000003", "没规格的", "", `{"skus": []}`)
|
||||
|
||||
result, err := ListPddProducts(db, "", "")
|
||||
result, err := ListPddProducts(db, "", "", 1)
|
||||
if err != nil {
|
||||
t.Fatalf("查列表失败: %v", err)
|
||||
}
|
||||
@@ -287,7 +315,7 @@ func TestListPddProducts_坏掉的采集结果不影响列表打开(t *testing.T
|
||||
t.Fatalf("造坏数据失败: %v", err)
|
||||
}
|
||||
|
||||
result, err := ListPddProducts(db, "", "")
|
||||
result, err := ListPddProducts(db, "", "", 1)
|
||||
if err != nil {
|
||||
t.Fatalf("列表页应该照样打得开: %v", err)
|
||||
}
|
||||
@@ -302,7 +330,7 @@ func TestListPddProducts_按采集状态筛选(t *testing.T) {
|
||||
createProduct(t, db, "100000000002")
|
||||
repository.SetCollectResult(db, "100000000002", "已采的", "", sampleSkusJSON)
|
||||
|
||||
collected, err := ListPddProducts(db, "", model.CollectCollected)
|
||||
collected, err := ListPddProducts(db, "", model.CollectCollected, 1)
|
||||
if err != nil {
|
||||
t.Fatalf("筛选失败: %v", err)
|
||||
}
|
||||
@@ -324,12 +352,12 @@ func TestListPddProducts_按商品ID和链接搜索(t *testing.T) {
|
||||
createProduct(t, db, "737116531267")
|
||||
createProduct(t, db, "999888777666")
|
||||
|
||||
byID, _ := ListPddProducts(db, "7371165", "")
|
||||
byID, _ := ListPddProducts(db, "7371165", "", 1)
|
||||
if len(byID.Rows) != 1 || byID.Rows[0].GoodsID != "737116531267" {
|
||||
t.Errorf("按 ID 片段搜索失败,命中 %d 条", len(byID.Rows))
|
||||
}
|
||||
|
||||
byURL, _ := ListPddProducts(db, "yangkeduo", "")
|
||||
byURL, _ := ListPddProducts(db, "yangkeduo", "", 1)
|
||||
if len(byURL.Rows) != 2 {
|
||||
t.Errorf("按链接搜索应命中 2 条,实际 %d 条", len(byURL.Rows))
|
||||
}
|
||||
@@ -344,7 +372,7 @@ func TestListPddProducts_搜索词里的通配符不当通配符用(t *testing.T
|
||||
createProduct(t, db, "737116531267")
|
||||
|
||||
for _, kw := range []string{"7_7", "%"} {
|
||||
result, err := ListPddProducts(db, kw, "")
|
||||
result, err := ListPddProducts(db, kw, "", 1)
|
||||
if err != nil {
|
||||
t.Fatalf("搜 %q 出错: %v", kw, err)
|
||||
}
|
||||
@@ -359,7 +387,7 @@ func TestListPddProducts_删除的查不到(t *testing.T) {
|
||||
createProduct(t, db, "737116531267")
|
||||
DeletePddProducts(db, []string{"737116531267"})
|
||||
|
||||
result, _ := ListPddProducts(db, "", "")
|
||||
result, _ := ListPddProducts(db, "", "", 1)
|
||||
if len(result.Rows) != 0 {
|
||||
t.Errorf("软删除的不该出现在列表里,实际 %d 条", len(result.Rows))
|
||||
}
|
||||
@@ -374,7 +402,7 @@ func TestStatusLine_四个状态都列出来(t *testing.T) {
|
||||
createProduct(t, db, "100000000002")
|
||||
repository.SetCollectResult(db, "100000000002", "已采的", "", sampleSkusJSON)
|
||||
|
||||
result, _ := ListPddProducts(db, "", "")
|
||||
result, _ := ListPddProducts(db, "", "", 1)
|
||||
line := result.StatusLine()
|
||||
for _, want := range []string{"共 2 条", "已采集 1", "未采集 1", "采集中 0", "采集失败 0"} {
|
||||
if !strings.Contains(line, want) {
|
||||
@@ -444,7 +472,7 @@ func TestPdd页面_显示店铺与按颜色采样标记(t *testing.T) {
|
||||
repository.SetCollectResult(
|
||||
db, "737116531267", "测试商品", "测试旗舰店", resultJSON)
|
||||
|
||||
list, err := ListPddProducts(db, "", "")
|
||||
list, err := ListPddProducts(db, "", "", 1)
|
||||
if err != nil {
|
||||
t.Fatalf("读取列表失败: %v", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user