feat: 统一 Admin 主列表分页 (#64)

This commit is contained in:
chengma
2026-08-09 21:51:20 +08:00
parent 5c9ebd968c
commit 155dd3b4a2
23 changed files with 549 additions and 120 deletions
+77
View File
@@ -0,0 +1,77 @@
package web
import (
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"github.com/gin-gonic/gin"
)
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("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"}, "page": {"2"},
})
(&Handler{}).taskRedirect(context, "完成")
assertRedirectQuery(t, recorder, "/tasks", map[string]string{
"q": "TASK", "type": "collect", "status": "pending", "page": "2", "msg": "完成",
})
})
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": "完成",
})
})
}