github.com/blend/go-sdk@v1.20240719.1/collections/batch_iterator_test.go (about)

     1  /*
     2  
     3  Copyright (c) 2024 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package collections
     9  
    10  import (
    11  	"fmt"
    12  	"testing"
    13  
    14  	"github.com/blend/go-sdk/assert"
    15  )
    16  
    17  func Test_BatchIterator(t *testing.T) {
    18  	a := assert.New(t)
    19  
    20  	bi := &BatchIterator[string]{BatchSize: 100}
    21  	a.False(bi.HasNext())
    22  	a.Empty(bi.Next())
    23  
    24  	bi = &BatchIterator[string]{Items: generateBatchItems(10)}
    25  	a.True(bi.HasNext())
    26  	a.Empty(bi.Next())
    27  
    28  	// handle edge case where somehow the cursor gets set beyond the
    29  	// last element of the items.
    30  	bi = &BatchIterator[string]{Items: generateBatchItems(10), Cursor: 15}
    31  	a.False(bi.HasNext())
    32  	a.Empty(bi.Next())
    33  
    34  	bi = &BatchIterator[string]{Items: generateBatchItems(10), BatchSize: 100}
    35  	a.True(bi.HasNext())
    36  	a.Len(bi.Next(), 10)
    37  	a.False(bi.HasNext())
    38  
    39  	bi = &BatchIterator[string]{Items: generateBatchItems(100), BatchSize: 10}
    40  	for x := 0; x < 10; x++ {
    41  		a.True(bi.HasNext())
    42  		a.Len(bi.Next(), 10, fmt.Sprintf("failed on pass %d", x))
    43  	}
    44  	a.False(bi.HasNext())
    45  
    46  	bi = &BatchIterator[string]{Items: generateBatchItems(105), BatchSize: 10}
    47  	for x := 0; x < 10; x++ {
    48  		a.True(bi.HasNext())
    49  		a.Len(bi.Next(), 10, fmt.Sprintf("failed on pass %d", x))
    50  	}
    51  	a.True(bi.HasNext())
    52  	a.Len(bi.Next(), 5)
    53  	a.False(bi.HasNext())
    54  }
    55  
    56  func generateBatchItems(count int) (output []string) {
    57  	output = make([]string, count)
    58  	for x := 0; x < count; x++ {
    59  		output[x] = fmt.Sprint(x)
    60  	}
    61  	return output
    62  }