Files
cmautobuy/admin/handler/web/pagination_test.go
T

122 lines
4.3 KiB
Go
Raw Normal View History

2026-08-09 21:51:20 +08:00
package web
import (
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"github.com/gin-gonic/gin"
"cmautobuy/admin/model"
2026-08-11 12:04:38 +08:00
"cmautobuy/admin/service"
2026-08-09 21:51:20 +08:00
)
func listPostContext(t *testing.T, path string, values url.Values) (*gin.Context, *httptest.ResponseRecorder) {
t.Helper()
gin.SetMode(gin.TestMode)
recorder := httptest.NewRecorder()
context, _ := gin.CreateTestContext(recorder)
request := httptest.NewRequest(http.MethodPost, path, strings.NewReader(values.Encode()))
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
context.Request = request
return context, recorder
}
func assertRedirectQuery(t *testing.T, recorder *httptest.ResponseRecorder, wantPath string, want map[string]string) {
t.Helper()
location, err := url.Parse(recorder.Header().Get("Location"))
if err != nil {
t.Fatal(err)
}
if location.Path != wantPath {
t.Fatalf("跳转路径=%s,期望 %s", location.Path, wantPath)
}
for key, value := range want {
if got := location.Query().Get(key); got != value {
t.Fatalf("%s=%q,期望 %q;Location=%s", key, got, value, location.String())
}
}
}
func Test列表写操作跳转保留筛选和页码(t *testing.T) {
t.Run("蝦皮", func(t *testing.T) {
context, recorder := listPostContext(t, "/shopee/save", url.Values{
"q": {"店铺 A"}, "search_field": {"shop_name"}, "status": {"no_link"}, "unlinked": {"1"}, "page": {"2"},
})
(&Handler{}).shopeeRedirect(context, "完成")
assertRedirectQuery(t, recorder, "/shopee", map[string]string{
"q": "店铺 A", "search_field": "shop_name", "status": "no_link", "unlinked": "1", "page": "2", "msg": "完成",
})
})
t.Run("顺运宝", func(t *testing.T) {
context, recorder := listPostContext(t, "/syb/associate-pdd", url.Values{
2026-08-11 14:39:13 +08:00
"order_no": {"SYB-1"}, "shop": {"测试店铺"}, "stage": {"pdd_missing"}, "page": {"3"},
})
(&Handler{}).sybRedirect(context, "完成")
assertRedirectQuery(t, recorder, "/syb", map[string]string{
2026-08-11 14:39:13 +08:00
"order_no": "SYB-1", "shop": "测试店铺", "stage": "pdd_missing", "page": "3", "msg": "完成",
})
})
2026-08-09 21:51:20 +08:00
t.Run("PDD", func(t *testing.T) {
context, recorder := listPostContext(t, "/pdd/delete", url.Values{
"q": {"737"}, "status": {"pending"}, "page": {"3"},
})
(&Handler{}).pddRedirect(context, "完成")
assertRedirectQuery(t, recorder, "/pdd", map[string]string{
"q": "737", "status": "pending", "page": "3", "msg": "完成",
})
})
t.Run("任务", func(t *testing.T) {
context, recorder := listPostContext(t, "/tasks/delete", url.Values{
"q": {"TASK"}, "type": {"collect"}, "status": {"pending"}, "creator": {"buyer-a"}, "page": {"2"},
2026-08-09 21:51:20 +08:00
})
context.Set(currentUserKey, &model.User{Role: model.RoleAdmin})
2026-08-09 21:51:20 +08:00
(&Handler{}).taskRedirect(context, "完成")
assertRedirectQuery(t, recorder, "/tasks", map[string]string{
"q": "TASK", "type": "collect", "status": "pending", "creator": "buyer-a", "page": "2", "msg": "完成",
2026-08-09 21:51:20 +08:00
})
})
t.Run("客户端", func(t *testing.T) {
context, recorder := listPostContext(t, "/clients/assign", url.Values{
"name": {"仓库"}, "page": {"4"},
})
redirectClients(context, "完成", "")
assertRedirectQuery(t, recorder, "/clients", map[string]string{
"name": "仓库", "page": "4", "msg": "完成",
})
})
t.Run("用户", func(t *testing.T) {
context, recorder := listPostContext(t, "/users/status", url.Values{
"q": {"buyer"}, "list_status": {"active"}, "page": {"5"},
})
redirectUsers(context, "完成", "")
assertRedirectQuery(t, recorder, "/users", map[string]string{
"q": "buyer", "status": "active", "page": "5", "msg": "完成",
})
})
}
2026-08-11 12:04:38 +08:00
func Test蝦皮分页详情和状态条保留分类搜索(t *testing.T) {
2026-08-11 12:04:38 +08:00
values := url.Values{
"q": {"店铺 A"}, "search_field": {"shop_name"}, "status": {"has_link"}, "unlinked": {"1"},
2026-08-11 12:04:38 +08:00
}
detail, err := url.Parse("/shopee/detail?" + detailValuesForShopee(values, 3))
if err != nil {
t.Fatal(err)
}
for key, want := range map[string]string{
"q": "店铺 A", "search_field": "shop_name", "status": "has_link", "unlinked": "1", "page": "3",
2026-08-11 12:04:38 +08:00
} {
if got := detail.Query().Get(key); got != want {
t.Errorf("详情参数 %s=%q,期望 %q", key, got, want)
}
}
result := &service.ShopeeListResult{Total: 7, Page: 1, TotalPages: 1, IsFiltered: true}
if got := shopeeStatusLine("", result); got != "筛选结果:7 个商品 · 第 1/1 页" {
t.Errorf("资料筛选状态条=%q", got)
}
}