github.com/openfga/openfga@v1.5.4-rc1/internal/mocks/mock_error_iterator.go (about) 1 package mocks 2 3 import ( 4 "context" 5 "fmt" 6 7 openfgav1 "github.com/openfga/api/proto/openfga/v1" 8 9 "github.com/openfga/openfga/pkg/storage" 10 ) 11 12 // errorTupleIterator is a mock iterator that returns error when calling next on the second Next call. 13 type errorTupleIterator struct { 14 items []*openfgav1.Tuple 15 originalLength int 16 } 17 18 func (s *errorTupleIterator) Next(ctx context.Context) (*openfgav1.Tuple, error) { 19 if ctx.Err() != nil { 20 return nil, ctx.Err() 21 } 22 23 // we want to simulate returning error after the first read 24 if len(s.items) != s.originalLength { 25 return nil, fmt.Errorf("simulated errors") 26 } 27 28 if len(s.items) == 0 { 29 return nil, nil 30 } 31 32 next, rest := s.items[0], s.items[1:] 33 s.items = rest 34 35 return next, nil 36 } 37 38 func (s *errorTupleIterator) Stop() {} 39 40 var _ storage.TupleIterator = (*errorTupleIterator)(nil) 41 42 // NewErrorTupleIterator mocks case where Next will return error after the first Next() 43 // This TupleIterator is designed to be used in tests. 44 func NewErrorTupleIterator(tuples []*openfgav1.Tuple) storage.TupleIterator { 45 iter := &errorTupleIterator{ 46 items: tuples, 47 originalLength: len(tuples), 48 } 49 50 return iter 51 }