Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion iter.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,11 @@ func (i *Iter) NextPage() bool {
// Reload ignores any id passed in and creates a new reset Iter
func (i *Iter) Reload(opts ...RequestResponseOption) IterI {
newIter := *i
newIter.ListParams = &ListParams{}
newIter.Values = nil
newIter.CurrentIndex = 0
newIter.Page = 0
newIter.Error = nil
newIter.requestResponseOptions = opts
newIter.SetCursor("")
return &newIter
}
32 changes: 32 additions & 0 deletions iter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,38 @@ func TestIter_Next_PerPage_of_one(t *testing.T) {
assert.Equal(1, recordCount)
}

func TestIter_Reload(t *testing.T) {
assert := assert.New(t)
params := ListParams{PerPage: 2}
it := Iter{}
it.ListParams = &params

it.Query = func(values lib.Values, _ ...RequestResponseOption) (*[]interface{}, string, error) {
urlValues, err := values.ToValues()
assert.NoError(err)
if urlValues.Get("cursor") == "" {
ret := make([]interface{}, 2)
return &ret, "next-cursor", nil
}
ret := make([]interface{}, 2)
return &ret, "", nil
}

recordCount := 0
for it.Next() {
recordCount += 1
}
assert.Equal(4, recordCount)

reloaded := it.Reload()
reloadCount := 0
for reloaded.Next() {
reloadCount += 1
}
assert.Equal(4, reloadCount, "a reloaded iterator should iterate the full result set again")
assert.Equal(int64(2), reloaded.(*Iter).GetParams().PerPage, "Reload should preserve the original list params")
}

func TestIter_Next_No_Cursor(t *testing.T) {
assert := assert.New(t)
params := ListParams{}
Expand Down