补全页加分页,每页 30 行
218 行在手机上堆成一屏一行有五万多像素长,分页后第 1 页只有八千多。 页码会夹紧:?page=999 停在最后一页而不是给空白页,解析不出来回第一页。 提交后留在同一页——处理完的行会从列表里消失,停在这儿才看得见剩下的。 翻页前若这一页有改过还没提交的下拉,先确认一次(预选的建议不算「改过」)。
This commit is contained in:
@@ -168,3 +168,68 @@ func intToString(v int64) string {
|
||||
}
|
||||
return string(b)
|
||||
}
|
||||
|
||||
func TestClampPage(t *testing.T) {
|
||||
cases := []struct {
|
||||
raw string
|
||||
count int
|
||||
want int
|
||||
}{
|
||||
{"", 8, 1},
|
||||
{"1", 8, 1},
|
||||
{"8", 8, 8},
|
||||
{"999", 8, 8}, // 超了就停在最后一页,别给空白页
|
||||
{"0", 8, 1},
|
||||
{"-3", 8, 1},
|
||||
{"abc", 8, 1},
|
||||
{"3", 0, 3}, // 没有数据时不夹(总会在最后回到 1)
|
||||
}
|
||||
for _, c := range cases {
|
||||
if got := clampPage(c.raw, c.count); got != c.want {
|
||||
t.Errorf("clampPage(%q, %d) = %d,期望 %d", c.raw, c.count, got, c.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 每页只渲染 30 行;页码越界时夹回最后一页。
|
||||
func TestFillupPagination(t *testing.T) {
|
||||
f := newMovementFixture(t)
|
||||
// 造 35 个「缺类别」的地点,凑出两页
|
||||
for i := 0; i < 35; i++ {
|
||||
pk := 680000 + i
|
||||
if _, err := f.pg.ExecContext(f.ctx, `INSERT INTO places (dataset_id, src_pk, name, lat, lon)
|
||||
VALUES ($1,$2,$3,$4,$5)`, f.dsID, pk, "分页测试点", 30.0+float64(i)*0.001, 120.0); err != nil {
|
||||
t.Fatalf("造地点失败: %v", err)
|
||||
}
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
f.pg.ExecContext(f.ctx, `DELETE FROM places WHERE dataset_id=$1 AND src_pk BETWEEN 680000 AND 680099`, f.dsID)
|
||||
})
|
||||
|
||||
countRows := func(url string) (rows, pageCount int) {
|
||||
t.Helper()
|
||||
rec := httptest.NewRecorder()
|
||||
req := httptest.NewRequest("GET", url, nil).WithContext(f.ctx)
|
||||
f.srv.adminFillupPage(rec, req)
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("%s 渲染失败: %d", url, rec.Code)
|
||||
}
|
||||
body := rec.Body.String()
|
||||
return strings.Count(body, `name="cat_`), strings.Count(body, `class="pager"`)
|
||||
}
|
||||
|
||||
rows, pagers := countRows("/admin/fillup")
|
||||
if rows != fillupPerPage {
|
||||
t.Fatalf("第 1 页应有 %d 行,实际 %d", fillupPerPage, rows)
|
||||
}
|
||||
if pagers == 0 {
|
||||
t.Error("多于 1 页时应出现分页控件")
|
||||
}
|
||||
if rows, _ := countRows("/admin/fillup?page=2"); rows == 0 || rows > fillupPerPage {
|
||||
t.Fatalf("第 2 页行数异常: %d", rows)
|
||||
}
|
||||
// 越界页码要夹回最后一页,而不是给一页空白
|
||||
if rows, _ := countRows("/admin/fillup?page=9999"); rows == 0 {
|
||||
t.Error("越界页码应夹到最后一页,不该是空白页")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user