(t *testing.T)
| 192 | } |
| 193 | |
| 194 | func TestStreamPages_OnItemsErrorStopsPagination(t *testing.T) { |
| 195 | apiCalls := 0 |
| 196 | rt := roundTripFunc(func(req *http.Request) (*http.Response, error) { |
| 197 | apiCalls++ |
| 198 | if apiCalls == 1 { |
| 199 | return jsonResponse(map[string]interface{}{ |
| 200 | "code": 0, "msg": "ok", |
| 201 | "data": map[string]interface{}{ |
| 202 | "items": []interface{}{map[string]interface{}{"id": "1"}}, |
| 203 | "has_more": true, |
| 204 | "page_token": "next", |
| 205 | }, |
| 206 | }), nil |
| 207 | } |
| 208 | return jsonResponse(map[string]interface{}{ |
| 209 | "code": 0, "msg": "ok", |
| 210 | "data": map[string]interface{}{ |
| 211 | "items": []interface{}{map[string]interface{}{"id": "2"}}, |
| 212 | "has_more": false, |
| 213 | }, |
| 214 | }), nil |
| 215 | }) |
| 216 | |
| 217 | ac, _ := newTestAPIClient(t, rt) |
| 218 | sentinel := errors.New("stop streaming") |
| 219 | var streamedItems []interface{} |
| 220 | result, hasItems, err := ac.StreamPages(context.Background(), RawApiRequest{ |
| 221 | Method: "GET", |
| 222 | URL: "/open-apis/contact/v3/users", |
| 223 | As: "bot", |
| 224 | }, func(items []interface{}) error { |
| 225 | streamedItems = append(streamedItems, items...) |
| 226 | return sentinel |
| 227 | }, PaginationOptions{PageDelay: 0}) |
| 228 | |
| 229 | if !errors.Is(err, sentinel) { |
| 230 | t.Fatalf("err = %v, want sentinel", err) |
| 231 | } |
| 232 | if result != nil { |
| 233 | t.Fatalf("result = %#v, want nil when callback stops pagination", result) |
| 234 | } |
| 235 | if hasItems { |
| 236 | t.Fatal("hasItems = true, want false when callback stops before returning") |
| 237 | } |
| 238 | if apiCalls != 1 { |
| 239 | t.Fatalf("apiCalls = %d, want early stop after first page", apiCalls) |
| 240 | } |
| 241 | if len(streamedItems) != 1 { |
| 242 | t.Fatalf("streamedItems = %d, want first page only", len(streamedItems)) |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | func TestPaginateAll_PageLimitStopsPagination(t *testing.T) { |
| 247 | apiCalls := 0 |
nothing calls this directly
no test coverage detected