Skip to content

Commit

Permalink
Added link page token tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Collin Shoop committed Nov 30, 2021
1 parent fda9dc5 commit 8b44329
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 13 deletions.
26 changes: 26 additions & 0 deletions godo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,32 @@ func checkCurrentPage(t *testing.T, resp *Response, expectedPage int) {
}
}

func checkNextPageToken(t *testing.T, resp *Response, expectedNextPageToken string) {
t.Helper()
links := resp.Links
pageToken, err := links.NextPageToken()
if err != nil {
t.Fatal(err)
}

if pageToken != expectedNextPageToken {
t.Fatalf("expected next page token to be '%s', was '%s'", expectedNextPageToken, pageToken)
}
}

func checkPreviousPageToken(t *testing.T, resp *Response, expectedPreviousPageToken string) {
t.Helper()
links := resp.Links
pageToken, err := links.PrevPageToken()
if err != nil {
t.Fatal(err)
}

if pageToken != expectedPreviousPageToken {
t.Fatalf("expected previous page token to be '%s', was '%s'", expectedPreviousPageToken, pageToken)
}
}

func TestDo_completion_callback(t *testing.T) {
setup()
defer teardown()
Expand Down
83 changes: 70 additions & 13 deletions links_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,25 +130,32 @@ func TestLinks_ParseMissing(t *testing.T) {

func TestLinks_ParseURL(t *testing.T) {
type linkTest struct {
name, url string
expected int
name, url string
expectedPage int
expectedPageToken string
}

linkTests := []linkTest{
{
name: "prev",
url: "https://api.digitalocean.com/v2/droplets/?page=1",
expected: 1,
name: "prev",
url: "https://api.digitalocean.com/v2/droplets/?page=1",
expectedPage: 1,
},
{
name: "last",
url: "https://api.digitalocean.com/v2/droplets/?page=5",
expected: 5,
name: "last",
url: "https://api.digitalocean.com/v2/droplets/?page=5",
expectedPage: 5,
},
{
name: "nexta",
url: "https://api.digitalocean.com/v2/droplets/?page=2",
expected: 2,
name: "next",
url: "https://api.digitalocean.com/v2/droplets/?page=2",
expectedPage: 2,
},
{
name: "page token",
url: "https://api.digitalocean.com/v2/droplets/?page=2&page_token=aaa",
expectedPage: 2,
expectedPageToken: "aaa",
},
}

Expand All @@ -158,9 +165,15 @@ func TestLinks_ParseURL(t *testing.T) {
t.Fatal(err)
}

if p != lT.expected {
if p != lT.expectedPage {
t.Errorf("expected page for '%s' to be '%d', was '%d'",
lT.url, lT.expected, p)
lT.url, lT.expectedPage, p)
}

pageToken, err := pageTokenFromURL(lT.url)
if pageToken != lT.expectedPageToken {
t.Errorf("expected pageToken for '%s' to be '%s', was '%s'",
lT.url, lT.expectedPageToken, pageToken)
}
}

Expand Down Expand Up @@ -197,3 +210,47 @@ func TestLinks_ParseEmptyString(t *testing.T) {
}
}
}

func TestLinks_NextPageToken(t *testing.T) {
t.Run("happy token", func(t *testing.T) {
checkNextPageToken(t, &Response{Links: &Links{
Pages: &Pages{
Next: "https://api.digitalocean.com/v2/droplets/?page_token=aaa",
},
}}, "aaa")
})
t.Run("empty token", func(t *testing.T) {
checkNextPageToken(t, &Response{Links: &Links{
Pages: &Pages{
Next: "https://api.digitalocean.com/v2/droplets/",
},
}}, "")
})
t.Run("no next page", func(t *testing.T) {
checkNextPageToken(t, &Response{Links: &Links{
Pages: &Pages{},
}}, "")
})
}

func TestLinks_ParseNextPageToken(t *testing.T) {
t.Run("happy token", func(t *testing.T) {
checkPreviousPageToken(t, &Response{Links: &Links{
Pages: &Pages{
Prev: "https://api.digitalocean.com/v2/droplets/?page_token=aaa",
},
}}, "aaa")
})
t.Run("empty token", func(t *testing.T) {
checkPreviousPageToken(t, &Response{Links: &Links{
Pages: &Pages{
Prev: "https://api.digitalocean.com/v2/droplets/",
},
}}, "")
})
t.Run("no next page", func(t *testing.T) {
checkPreviousPageToken(t, &Response{Links: &Links{
Pages: &Pages{},
}}, "")
})
}

0 comments on commit 8b44329

Please sign in to comment.