Fix Docspell search pagination
This commit is contained in:
+2
-1
@@ -122,9 +122,10 @@ func (c *DocspellClient) SearchItems(ctx context.Context, query string, limit in
|
|||||||
all = append(all, group.Items...)
|
all = append(all, group.Items...)
|
||||||
count += len(group.Items)
|
count += len(group.Items)
|
||||||
}
|
}
|
||||||
if count == 0 || count < limit {
|
if count == 0 {
|
||||||
return all, nil
|
return all, nil
|
||||||
}
|
}
|
||||||
|
offset += count - limit
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,59 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"strconv"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestSearchItemsContinuesWhenServerCapsLimit(t *testing.T) {
|
||||||
|
var offsets []int
|
||||||
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.URL.Path != "/api/v1/sec/item/search" {
|
||||||
|
t.Fatalf("unexpected path: %s", r.URL.Path)
|
||||||
|
}
|
||||||
|
var req struct {
|
||||||
|
Offset int `json:"offset"`
|
||||||
|
Limit int `json:"limit"`
|
||||||
|
}
|
||||||
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
offsets = append(offsets, req.Offset)
|
||||||
|
count := 200
|
||||||
|
if req.Offset >= 400 {
|
||||||
|
count = 0
|
||||||
|
}
|
||||||
|
items := make([]SearchItem, count)
|
||||||
|
for i := range items {
|
||||||
|
items[i] = SearchItem{ID: strconv.Itoa(req.Offset + i)}
|
||||||
|
}
|
||||||
|
_ = json.NewEncoder(w).Encode(map[string]any{
|
||||||
|
"groups": []map[string]any{{"items": items}},
|
||||||
|
"limit": 200,
|
||||||
|
"offset": req.Offset,
|
||||||
|
})
|
||||||
|
}))
|
||||||
|
defer server.Close()
|
||||||
|
|
||||||
|
client := NewDocspellClient(server.URL, "")
|
||||||
|
items, err := client.SearchItems(context.Background(), "*", 1000)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if len(items) != 400 {
|
||||||
|
t.Fatalf("expected 400 items, got %d", len(items))
|
||||||
|
}
|
||||||
|
wantOffsets := []int{0, 200, 400}
|
||||||
|
if len(offsets) != len(wantOffsets) {
|
||||||
|
t.Fatalf("expected offsets %v, got %v", wantOffsets, offsets)
|
||||||
|
}
|
||||||
|
for i := range wantOffsets {
|
||||||
|
if offsets[i] != wantOffsets[i] {
|
||||||
|
t.Fatalf("expected offsets %v, got %v", wantOffsets, offsets)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user