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

     1  /*
     2  
     3  Copyright (c) 2022 - 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  	its := assert.New(t)
    19  
    20  	bi := &BatchIterator{BatchSize: 100}
    21  	its.False(bi.HasNext())
    22  	its.Empty(bi.Next())
    23  
    24  	bi = &BatchIterator{Items: generateBatchItems(10)}
    25  	its.True(bi.HasNext())
    26  	its.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{Items: generateBatchItems(10), Cursor: 15}
    31  	its.False(bi.HasNext())
    32  	its.Empty(bi.Next())
    33  
    34  	bi = &BatchIterator{Items: generateBatchItems(10), BatchSize: 100}
    35  	its.True(bi.HasNext())
    36  	its.Len(bi.Next(), 10)
    37  	its.False(bi.HasNext())
    38  
    39  	bi = &BatchIterator{Items: generateBatchItems(100), BatchSize: 10}
    40  	for x := 0; x < 10; x++ {
    41  		its.True(bi.HasNext())
    42  		its.Len(bi.Next(), 10, fmt.Sprintf("failed on pass %d", x))
    43  	}
    44  	its.False(bi.HasNext())
    45  
    46  	bi = &BatchIterator{Items: generateBatchItems(105), BatchSize: 10}
    47  	for x := 0; x < 10; x++ {
    48  		its.True(bi.HasNext())
    49  		its.Len(bi.Next(), 10, fmt.Sprintf("failed on pass %d", x))
    50  	}
    51  	its.True(bi.HasNext())
    52  	its.Len(bi.Next(), 5)
    53  	its.False(bi.HasNext())
    54  }
    55  
    56  func generateBatchItems(count int) []interface{} {
    57  	var output []interface{}
    58  	for x := 0; x < count; x++ {
    59  		output = append(output, fmt.Sprint(x))
    60  	}
    61  	return output
    62  }