diff --git a/pkg/items/dummy.go b/pkg/items/dummy.go new file mode 100644 index 0000000000000000000000000000000000000000..082f5c1f3760a7046332ce0b5eb8fe569a90ed35 --- /dev/null +++ b/pkg/items/dummy.go @@ -0,0 +1,33 @@ +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 +} + +type DummyWithOptions struct { + Items + Total int +} + +func (d *DummyWithOptions) Find(_ context.Context, _, _, _ string, _ *Filter, opts ...*FindOptions) ([]*Item, int, error) { + fo := MergeFindOptions(opts...) + limit := fo.Limit + if limit == 0 { + limit = fo.PageSize + } + return make([]*Item, limit), d.Total, nil +} diff --git a/pkg/items/pagination.go b/pkg/items/pagination.go index 6db5c3247264e9a8aaa9162a2bef6a10b0ca3366..f9461079ede21d815f77780d4d1c1adfb20bcc17 100644 --- a/pkg/items/pagination.go +++ b/pkg/items/pagination.go @@ -16,7 +16,7 @@ type BatchProcessor struct { FindPublishedOptions *FindPublishedOptions Filter *Filter - limit int + limit int sort []string processed int } @@ -130,6 +130,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..c0985d33a6288ea11b69f809439729d4258e3973 --- /dev/null +++ b/pkg/items/pagination_test.go @@ -0,0 +1,57 @@ +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) { + t.Run("EmptyResults", func(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) + }) + + t.Run("Success", func(t *testing.T) { + itemssvc := &DummyWithOptions{Items: nil, Total: 1000} + b := &BatchProcessor{ + Items: itemssvc, + SpaceID: "sp", + EnvID: environments.DefaultEnvironment, + CollectionID: "col", + FindOptions: &FindOptions{ + Regular: true, + Hidden: true, + Templates: true, + FindOptions: *options.New(0, 25), + }, + } + + var counter int + _, err := b.Do(context.Background(), func(batch []*Item) error { counter++; return nil }) + require.NoError(t, err) + assert.Equal(t, 1000/25, counter) + }) +}