From 7376c32bdbd31737fba1dfa150cd3882b59e4d16 Mon Sep 17 00:00:00 2001 From: Cyb3r-Jak3 Date: Sun, 16 Apr 2023 14:59:09 -0400 Subject: [PATCH] Handle no total_pages in result info --- pages_deployment.go | 2 +- pages_deployment_test.go | 70 ++++++++++++++++++++++++++++++++++++---- pagination.go | 2 +- 3 files changed, 66 insertions(+), 8 deletions(-) diff --git a/pages_deployment.go b/pages_deployment.go index 0db086df63b..af8cc208a3f 100644 --- a/pages_deployment.go +++ b/pages_deployment.go @@ -160,7 +160,7 @@ func (api *API) ListPagesDeployments(ctx context.Context, rc *ResourceContainer, } deployments = append(deployments, r.Result...) params.ResultInfo = r.ResultInfo.Next() - if params.ResultInfo.DoneCount() || !autoPaginate { + if params.DoneCount() || !autoPaginate { break } } diff --git a/pages_deployment_test.go b/pages_deployment_test.go index b52179490d5..48ec89abb6d 100644 --- a/pages_deployment_test.go +++ b/pages_deployment_test.go @@ -11,7 +11,7 @@ import ( ) const ( - testPagesDeplyomentResponse = ` + testPagesDeploymentResponse = ` { "id": "0012e50b-fa5d-44db-8cb5-1f372785dcbe", "short_id": "0012e50b", @@ -268,7 +268,7 @@ func TestListPagesDeployments(t *testing.T) { "count": 1, "total_count": 1 } - }`, testPagesDeplyomentResponse) + }`, testPagesDeploymentResponse) } mux.HandleFunc("/accounts/"+testAccountID+"/pages/projects/test/deployments", handler) @@ -292,6 +292,64 @@ func TestListPagesDeployments(t *testing.T) { } } +func TestListPagesDeploymentsPagination(t *testing.T) { + setup() + defer teardown() + var page1Called, page2Called bool + handler := func(w http.ResponseWriter, r *http.Request) { + page := r.URL.Query().Get("page") + w.Header().Set("content-type", "application/json") + switch page { + case "1": + page1Called = true + fmt.Fprintf(w, `{ + "success": true, + "errors": [], + "messages": [], + "result": [ + %s + ], + "result_info": { + "page": 1, + "per_page": 25, + "total_count": 26, + "total_pages": 2 + } + }`, testPagesDeploymentResponse) + case "2": + page2Called = true + fmt.Fprintf(w, `{ + "success": true, + "errors": [], + "messages": [], + "result": [ + %s + ], + "result_info": { + "page": 2, + "per_page": 25, + "total_count": 26, + "total_pages": 2 + } + }`, testPagesDeploymentResponse) + default: + assert.Failf(t, "Unexpected page number", "Expected page 1 or 2, got %s", page) + return + } + } + mux.HandleFunc("/accounts/"+testAccountID+"/pages/projects/test/deployments", handler) + actual, resultInfo, err := client.ListPagesDeployments(context.Background(), AccountIdentifier(testAccountID), ListPagesDeploymentsParams{ + ProjectName: "test", + ResultInfo: ResultInfo{}, + }) + if assert.NoError(t, err) { + assert.True(t, page1Called) + assert.True(t, page2Called) + assert.Equal(t, 2, len(actual)) + assert.Equal(t, 26, resultInfo.Total) + } +} + func TestGetPagesDeploymentInfo(t *testing.T) { setup() defer teardown() @@ -305,7 +363,7 @@ func TestGetPagesDeploymentInfo(t *testing.T) { "errors": [], "messages": [], "result": %s - }`, testPagesDeplyomentResponse) + }`, testPagesDeploymentResponse) } mux.HandleFunc("/accounts/"+testAccountID+"/pages/projects/test/deployments/0012e50b-fa5d-44db-8cb5-1f372785dcbe", handler) @@ -379,7 +437,7 @@ func TestCreatePagesDeployment(t *testing.T) { "errors": [], "messages": [], "result": %s - }`, testPagesDeplyomentResponse) + }`, testPagesDeploymentResponse) } mux.HandleFunc("/accounts/"+testAccountID+"/pages/projects/test/deployments", handler) @@ -406,7 +464,7 @@ func TestRetryPagesDeployment(t *testing.T) { "errors": [], "messages": [], "result": %s - }`, testPagesDeplyomentResponse) + }`, testPagesDeploymentResponse) } mux.HandleFunc("/accounts/"+testAccountID+"/pages/projects/test/deployments/0012e50b-fa5d-44db-8cb5-1f372785dcbe/retry", handler) @@ -430,7 +488,7 @@ func TestRollbackPagesDeployment(t *testing.T) { "errors": [], "messages": [], "result": %s - }`, testPagesDeplyomentResponse) + }`, testPagesDeploymentResponse) } mux.HandleFunc("/accounts/"+testAccountID+"/pages/projects/test/deployments/0012e50b-fa5d-44db-8cb5-1f372785dcbe/rollback", handler) diff --git a/pagination.go b/pagination.go index 52d240fd19b..b45093138ef 100644 --- a/pagination.go +++ b/pagination.go @@ -22,5 +22,5 @@ func (p ResultInfo) HasMorePages() bool { // Unlike Done(), this function uses the Total and PerPage fields to determine if // there are more pages to fetch. func (p ResultInfo) DoneCount() bool { - return p.Total < p.PerPage*p.Page + return p.Total+p.PerPage < p.PerPage*p.Page }