fix: 修复顺运宝分页总数误判 (#60)
This commit is contained in:
+4
-1
@@ -504,7 +504,7 @@ type StockRow struct {
|
||||
}
|
||||
|
||||
// ListPage 按日期范围翻一页货运单列表:POST /am/stock/list。
|
||||
// 返回响应内的 total,供上层核对分页期间总数有没有漂移。
|
||||
// 返回响应内的 total;HAR 证明它是当前页条数,不是筛选范围总数。
|
||||
func (c *Client) ListPage(ctx context.Context, dateFrom, dateTo string, start, pageIndex, pageSize int) ([]StockRow, int, error) {
|
||||
data, err := c.do(ctx, http.MethodPost, "/am/stock/list", nil,
|
||||
listPayload(dateFrom, dateTo, start, pageIndex, pageSize))
|
||||
@@ -522,6 +522,9 @@ func (c *Client) ListPage(ctx context.Context, dateFrom, dateTo string, start, p
|
||||
if wrap.Total == nil || *wrap.Total < 0 {
|
||||
return nil, 0, fmt.Errorf("货运单列表响应缺少合法的 total")
|
||||
}
|
||||
if *wrap.Total != len(wrap.List) {
|
||||
return nil, 0, fmt.Errorf("货运单列表响应的当前页条数为 %d,但实际返回 %d 行", *wrap.Total, len(wrap.List))
|
||||
}
|
||||
|
||||
rows := make([]StockRow, 0, len(wrap.List))
|
||||
for _, raw := range wrap.List {
|
||||
|
||||
@@ -327,7 +327,7 @@ func TestClient_ListTotal和ListPage(t *testing.T) {
|
||||
t.Fatalf("list 失败: %v", err)
|
||||
}
|
||||
if pageTotal != 1 {
|
||||
t.Fatalf("list 响应内 total 应该是 1,实际 %d", pageTotal)
|
||||
t.Fatalf("list 响应内当前页条数应该是 1,实际 %d", pageTotal)
|
||||
}
|
||||
if len(rows) != 1 || rows[0].ID != 75104587 || rows[0].Code != "260728TB95MJTQ" {
|
||||
t.Fatalf("列表结果不对: %+v", rows)
|
||||
@@ -342,6 +342,7 @@ func TestClient_ListPage要求合法Total(t *testing.T) {
|
||||
{"缺少total", map[string]any{"list": []any{}}},
|
||||
{"total类型错误", map[string]any{"list": []any{}, "total": "1"}},
|
||||
{"total为负数", map[string]any{"list": []any{}, "total": -1}},
|
||||
{"total与list长度不一致", map[string]any{"list": []any{map[string]any{"id": 1}}, "total": 0}},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
Reference in New Issue
Block a user