MCPcopy
hub / github.com/Tencent/WeKnora / TestAPI_PaginateMergesPages

Function TestAPI_PaginateMergesPages

cli/cmd/api/api_test.go:328–371  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

326}
327
328func TestAPI_PaginateMergesPages(t *testing.T) {
329 pages := [][]byte{
330 []byte(`{"success":true,"data":[{"id":"1"},{"id":"2"}],"total":5,"page":1,"page_size":2}`),
331 []byte(`{"success":true,"data":[{"id":"3"},{"id":"4"}],"total":5,"page":2,"page_size":2}`),
332 []byte(`{"success":true,"data":[{"id":"5"}],"total":5,"page":3,"page_size":2}`),
333 }
334 idx := 0
335 svc := &fakeAPISvc{do: func(method, path string, _ any) (*http.Response, error) {
336 if idx >= len(pages) {
337 return nil, fmt.Errorf("too many calls; idx=%d", idx)
338 }
339 body := pages[idx]
340 idx++
341 return &http.Response{
342 StatusCode: 200,
343 Body: io.NopCloser(bytes.NewReader(body)),
344 Header: make(http.Header),
345 }, nil
346 }}
347
348 out, _ := iostreams.SetForTest(t)
349
350 opts := &Options{}
351 if err := runAPI(context.Background(), opts, &cmdutil.FormatOptions{Mode: cmdutil.FormatJSON}, svc, "GET", "/api/v1/knowledge-base?page=1&page_size=2", true); err != nil {
352 t.Fatalf("runAPI: %v", err)
353 }
354 var env struct {
355 OK bool `json:"ok"`
356 Data struct {
357 Data []map[string]string `json:"data"`
358 Total int `json:"total"`
359 } `json:"data"`
360 }
361 if err := json.Unmarshal(out.Bytes(), &env); err != nil {
362 t.Fatalf("unmarshal: %v\n%s", err, out.String())
363 }
364 got := env.Data
365 if len(got.Data) != 5 || got.Total != 5 {
366 t.Errorf("got %d records (total %d), want 5/5", len(got.Data), got.Total)
367 }
368 if idx != 3 {
369 t.Errorf("called %d times, want 3", idx)
370 }
371}
372
373func TestAPI_PaginateIgnoredForPOST(t *testing.T) {
374 // --paginate should be a no-op for non-GET methods (no pagination

Callers

nothing calls this directly

Calls 3

SetForTestFunction · 0.92
runAPIFunction · 0.85
StringMethod · 0.45

Tested by

no test coverage detected