Skip to content

Commit

Permalink
Handle no total_pages in result info
Browse files Browse the repository at this point in the history
  • Loading branch information
Cyb3r-Jak3 committed Apr 16, 2023
1 parent de069b5 commit 7376c32
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pages_deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
70 changes: 64 additions & 6 deletions pages_deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

const (
testPagesDeplyomentResponse = `
testPagesDeploymentResponse = `
{
"id": "0012e50b-fa5d-44db-8cb5-1f372785dcbe",
"short_id": "0012e50b",
Expand Down Expand Up @@ -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)
Expand All @@ -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()
Expand All @@ -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)
Expand Down Expand Up @@ -379,7 +437,7 @@ func TestCreatePagesDeployment(t *testing.T) {
"errors": [],
"messages": [],
"result": %s
}`, testPagesDeplyomentResponse)
}`, testPagesDeploymentResponse)
}

mux.HandleFunc("/accounts/"+testAccountID+"/pages/projects/test/deployments", handler)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pagination.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 7376c32

Please sign in to comment.