From f3998376769736060d8410e5b59e7174e55f6413 Mon Sep 17 00:00:00 2001 From: gfleury Date: Thu, 19 Mar 2020 19:35:17 -0300 Subject: [PATCH] Adding tests for HasNextPage --- api_response.go | 3 +- api_response_test.go | 75 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 1 deletion(-) diff --git a/api_response.go b/api_response.go index df070f5..78e18cb 100644 --- a/api_response.go +++ b/api_response.go @@ -555,13 +555,14 @@ func NewRawAPIResponse(r *http.Response) (*APIResponse, error) { // HasNextPage returns if response is paged and has next page and where it does start func HasNextPage(response *APIResponse) (isLastPage bool, nextPageStart int) { - isLastPage = true isLastPage, ok := response.Values["isLastPage"].(bool) if ok && !isLastPage { floatStart, ok := response.Values["nextPageStart"].(float64) if ok { nextPageStart = int(floatStart) } + } else if !ok { + isLastPage = true } return !isLastPage, nextPageStart } diff --git a/api_response_test.go b/api_response_test.go index 1cfe4e7..6fa976b 100644 --- a/api_response_test.go +++ b/api_response_test.go @@ -961,3 +961,78 @@ func TestNewBitbucketAPIResponse(t *testing.T) { }) } } + +func TestHasNextPage(t *testing.T) { + type args struct { + r *APIResponse + } + tests := []struct { + name string + args args + want bool + want2 int + }{ + { + name: "Empty list", + args: args{ + r: &APIResponse{ + Values: map[string]interface{}{}, + }, + }, + want: false, + want2: 0, + }, + { + name: "Bad response", + args: args{ + r: &APIResponse{ + Values: map[string]interface{}{"values": "not an array"}, + }, + }, + want: false, + want2: 0, + }, + { + name: "Last Page", + args: args{ + r: &APIResponse{ + Values: map[string]interface{}{ + "size": 1, + "limit": 25, + "isLastPage": true, + "values": []interface{}{}, + }, + }, + }, + want: false, + want2: 0, + }, + { + name: "Hast Last Page", + args: args{ + r: &APIResponse{ + Values: map[string]interface{}{ + "size": 1, + "limit": 25, + "isLastPage": false, + "nextPageStart": float64(1203), + "values": []interface{}{}, + }, + }, + }, + want: true, + want2: 1203, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := HasNextPage(tt.args.r) + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("NewBitbucketAPIResponse() isLastPage = %v, want %v", got, tt.want) + } + if !reflect.DeepEqual(err, tt.want2) { + t.Errorf("NewBitbucketAPIResponse() nextPageStart = %v, want %v", err, tt.want2) + } + }) + } +}