-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
INTG-2800 fix batch in assignees (#408)
* INTG-2800 fix batch in assignees * INTG-2800 fix batch in assignees * INTG-2800 one more test
- Loading branch information
1 parent
612fa5a
commit d40514b
Showing
5 changed files
with
109 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package util | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
// SplitSliceInBatch splits a slice into batches and calls the callback function for each batch. | ||
func SplitSliceInBatch[T any](size int, collection []T, fn func(batch []T) error) error { | ||
if size == 0 { | ||
return fmt.Errorf("batch size cannot be 0") | ||
} | ||
|
||
for i := 0; i < len(collection); i += size { | ||
j := i + size | ||
if j > len(collection) { | ||
j = len(collection) | ||
} | ||
|
||
if err := fn(collection[i:j]); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package util_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/SafetyCulture/safetyculture-exporter/pkg/internal/util" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestSplitSliceInBatch(t *testing.T) { | ||
tests := map[string]struct { | ||
size int | ||
collection []string | ||
fn func([]string) error | ||
expectedErr error | ||
}{ | ||
"batch size is 0": { | ||
size: 0, | ||
expectedErr: fmt.Errorf("batch size cannot be 0"), | ||
}, | ||
"batch size is greater than the collection size": { | ||
size: 100, | ||
collection: []string{"a", "b", "c", "d", "e", "f", "g"}, | ||
fn: func(strings []string) error { | ||
require.True(t, len(strings) == 7) | ||
return nil | ||
}, | ||
}, | ||
"when not divisible": { | ||
size: 3, | ||
collection: []string{"a", "b", "c", "d", "e", "f", "g"}, | ||
fn: func(strings []string) error { | ||
require.True(t, len(strings) == 3 || len(strings) == 1) | ||
return nil | ||
}, | ||
}, | ||
"when is divisible": { | ||
size: 3, | ||
collection: []string{"a", "b", "c", "d", "e", "f"}, | ||
fn: func(strings []string) error { | ||
require.True(t, len(strings) == 3) | ||
return nil | ||
}, | ||
}, | ||
"when errors out": { | ||
size: 3, | ||
collection: []string{"a", "b", "c", "d", "e", "f"}, | ||
fn: func(strings []string) error { | ||
if strings[0] == "d" { | ||
return fmt.Errorf("error in processing function") | ||
} | ||
return nil | ||
}, | ||
expectedErr: fmt.Errorf("error in processing function"), | ||
}, | ||
} | ||
|
||
for name, tt := range tests { | ||
t.Run(name, func(t *testing.T) { | ||
err := util.SplitSliceInBatch(tt.size, tt.collection, tt.fn) | ||
if tt.expectedErr != nil { | ||
require.Error(t, err) | ||
assert.Equal(t, tt.expectedErr, err) | ||
} | ||
}) | ||
} | ||
} |