|
| 1 | +package paginate_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | + "github.com/tines/go-sdk/internal/paginate" |
| 8 | +) |
| 9 | + |
| 10 | +var TestCursorWithLimit = paginate.Cursor{ |
| 11 | + Meta: paginate.Meta{ |
| 12 | + CurrentPage: "https://example.com/?per_page=1&page=1", |
| 13 | + PreviousPage: "", |
| 14 | + NextPage: "https://example.com?per_page=1&page=2", |
| 15 | + NextPageNum: 2, |
| 16 | + PerPage: 1, |
| 17 | + Pages: 3, |
| 18 | + Count: 3, |
| 19 | + }, |
| 20 | + TotalRequested: 2, |
| 21 | +} |
| 22 | + |
| 23 | +var TestCursorNoLimit = paginate.Cursor{ |
| 24 | + Meta: paginate.Meta{ |
| 25 | + CurrentPage: "https://example.com/?per_page=1&page=1", |
| 26 | + PreviousPage: "", |
| 27 | + NextPage: "https://example.com?per_page=1&page=2", |
| 28 | + NextPageNum: 2, |
| 29 | + PerPage: 1, |
| 30 | + Pages: 2, |
| 31 | + Count: 2, |
| 32 | + }, |
| 33 | + TotalRequested: 0, |
| 34 | +} |
| 35 | + |
| 36 | +func TestPaginationWithLimit(t *testing.T) { |
| 37 | + assert := assert.New(t) |
| 38 | + |
| 39 | + assert.Equal(true, TestCursorWithLimit.MoreResultsAvailable(), "indicate that more results are available if next page is not nil") |
| 40 | + assert.Equal(false, TestCursorWithLimit.MaxResultsReturned(), "indicate that the maximum requested number of results has not been returned yet") |
| 41 | + assert.Equal(true, TestCursorWithLimit.ReturnMoreResults(), "indicate that more results should be returned") |
| 42 | + assert.Equal(0, TestCursorWithLimit.CurrentCounter(), "indicate that no results have been returned yet") |
| 43 | + |
| 44 | + TestCursorWithLimit.IncrementCounter() |
| 45 | + |
| 46 | + assert.Equal(1, TestCursorWithLimit.CurrentCounter(), "counter should increment by one") |
| 47 | + |
| 48 | + params := TestCursorWithLimit.GetNextPageParams() |
| 49 | + |
| 50 | + assert.Equal(map[string]interface{}{"page": "2", "per_page": "1"}, params, "next page params should get successfully extracted") |
| 51 | + |
| 52 | + TestCursorWithLimit.IncrementCounter() |
| 53 | + |
| 54 | + assert.Equal(false, TestCursorWithLimit.ReturnMoreResults(), "indicate that the maximum number of requested results have been returned") |
| 55 | +} |
| 56 | + |
| 57 | +func TestPaginationNoLimit(t *testing.T) { |
| 58 | + assert := assert.New(t) |
| 59 | + |
| 60 | + assert.Equal(false, TestCursorNoLimit.MaxResultsReturned(), "indicate that there is no maximum number of results to return") |
| 61 | + |
| 62 | + newMeta := paginate.Meta{ |
| 63 | + // Next Page info will be nil in the server response, so we don't set it here. |
| 64 | + CurrentPage: "https://example.com/?per_page=1&page=2", |
| 65 | + PreviousPage: "https://example.com?per_page=1&page=1", |
| 66 | + PerPage: 1, |
| 67 | + Pages: 2, |
| 68 | + Count: 2, |
| 69 | + } |
| 70 | + |
| 71 | + TestCursorNoLimit.UpdatePagination(newMeta) |
| 72 | + |
| 73 | + assert.Equal(false, TestCursorNoLimit.MoreResultsAvailable(), "indicate that no more results are available") |
| 74 | +} |
0 commit comments