diff --git a/pkg/items/dummy.go b/pkg/items/dummy.go new file mode 100644 index 0000000000000000000000000000000000000000..fc1f725abc3e5f9c16164daca30f3334c16f9f86 --- /dev/null +++ b/pkg/items/dummy.go @@ -0,0 +1,17 @@ +package items + +import "context" + +type FindResultDummy struct { + Items []*Item + Total int + Error error +} +type Dummy struct { + Items + FindResult *FindResultDummy +} + +func (d *Dummy) Find(_ context.Context, _, _, _ string, _ *Filter, _ ...*FindOptions) ([]*Item, int, error) { + return d.FindResult.Items, d.FindResult.Total, d.FindResult.Error +} diff --git a/pkg/items/pagination.go b/pkg/items/pagination.go index 6fe197d7d36c1e191e300dfb2a7799d4eac58a94..84fac6da574895acf26075dd14ddd1f051c55bb3 100644 --- a/pkg/items/pagination.go +++ b/pkg/items/pagination.go @@ -128,6 +128,11 @@ func (b *BatchProcessor) Do(ctx context.Context, f func(batch []*Item) error) (i return 0, err } + // на случай, когда первый запрос вернул 0 элементов + if len(batch) == 0 { + break + } + if err = f(batch); err != nil { return 0, err } diff --git a/pkg/items/pagination_test.go b/pkg/items/pagination_test.go new file mode 100644 index 0000000000000000000000000000000000000000..bf13af39e00bd19550ddd49bae3e165b5cfa9fff --- /dev/null +++ b/pkg/items/pagination_test.go @@ -0,0 +1,35 @@ +package items + +import ( + "context" + "testing" + + "git.perx.ru/perxis/perxis-go/pkg/environments" + "git.perx.ru/perxis/perxis-go/pkg/options" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestBatchProcessor(t *testing.T) { + + itemssvc := &Dummy{FindResult: &FindResultDummy{Items: nil, Total: 0, Error: nil}} + + b := &BatchProcessor{ + Items: itemssvc, + SpaceID: "sp", + EnvID: environments.DefaultEnvironment, + CollectionID: "col", + FindOptions: &FindOptions{ + Regular: true, + Hidden: true, + Templates: true, + FindOptions: *options.NewFindOptions(0, 10), + }, + Filter: NewFilter("a > 5"), + } + + var counter int + _, err := b.Do(context.Background(), func(batch []*Item) error { counter++; return nil }) + require.NoError(t, err) + assert.Equal(t, 0, counter) +}