Skip to content
Snippets Groups Projects
Commit ce608c18 authored by Danis Kirasirov's avatar Danis Kirasirov
Browse files

Добавлен тест BatchProcessor

parent 3ced7628
No related branches found
No related tags found
No related merge requests found
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
}
...@@ -16,7 +16,7 @@ type BatchProcessor struct { ...@@ -16,7 +16,7 @@ type BatchProcessor struct {
FindPublishedOptions *FindPublishedOptions FindPublishedOptions *FindPublishedOptions
Filter *Filter Filter *Filter
limit int limit int
sort []string sort []string
processed int processed int
} }
...@@ -130,6 +130,11 @@ func (b *BatchProcessor) Do(ctx context.Context, f func(batch []*Item) error) (i ...@@ -130,6 +130,11 @@ func (b *BatchProcessor) Do(ctx context.Context, f func(batch []*Item) error) (i
return 0, err return 0, err
} }
// на случай, когда первый запрос вернул 0 элементов
if len(batch) == 0 {
break
}
if err = f(batch); err != nil { if err = f(batch); err != nil {
return 0, err return 0, err
} }
......
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)
})
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment