Skip to content

Commit 8c161e2

Browse files
authored
Fix ForeachDocs function of pkg/couchdb (#4378)
The bug was introduced in #4377.
2 parents 5364c50 + 679bd22 commit 8c161e2

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

pkg/couchdb/bulk.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ func ForeachDocsWithCustomPagination(db prefixer.Prefixer, doctype string, limit
206206
skip = 1
207207
}
208208
req := &AllDocsRequest{
209-
StartKey: startKey,
209+
StartKey: `"` + startKey + `"`,
210210
Skip: skip,
211211
Limit: limit,
212212
}

pkg/couchdb/couchdb_test.go

+29
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package couchdb
33
import (
44
"context"
55
"encoding/json"
6+
"fmt"
67
"reflect"
78
"strings"
89
"sync"
@@ -262,6 +263,34 @@ func TestCouchdb(t *testing.T) {
262263
}
263264
})
264265

266+
t.Run("ForeachDocs", func(t *testing.T) {
267+
for i := 0; i < 5; i++ {
268+
doc := &testDoc{Test: fmt.Sprintf("foreach_%d", i)}
269+
require.NoError(t, CreateDoc(TestPrefix, doc))
270+
}
271+
272+
var results []*testDoc
273+
err := GetAllDocs(TestPrefix, TestDoctype, &AllDocsRequest{}, &results)
274+
require.NoError(t, err)
275+
var expected []string
276+
for _, result := range results {
277+
expected = append(expected, result.Test)
278+
}
279+
280+
var keys []string
281+
ForeachDocsWithCustomPagination(TestPrefix, TestDoctype, 2, func(id string, raw json.RawMessage) error {
282+
var doc testDoc
283+
err := json.Unmarshal(raw, &doc)
284+
if err != nil {
285+
return err
286+
}
287+
keys = append(keys, doc.Test)
288+
return nil
289+
})
290+
291+
assert.Equal(t, expected, keys)
292+
})
293+
265294
t.Run("ChangesSuccess", func(t *testing.T) {
266295
err := ResetDB(TestPrefix, TestDoctype)
267296
assert.NoError(t, err)

0 commit comments

Comments
 (0)