Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

api: fix wrong pageCount in /list endpoints (#1813) #1824

Merged
merged 1 commit into from
May 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion internal/core/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,10 @@ func paginate2(itemsPtr interface{}, itemsPerPage int, page int) int {
return 0
}

pageCount := (itemsLen / itemsPerPage) + 1
pageCount := (itemsLen / itemsPerPage)
if (itemsLen % itemsPerPage) != 0 {
pageCount++
}

min := page * itemsPerPage
if min >= itemsLen {
Expand Down
32 changes: 32 additions & 0 deletions internal/core/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,38 @@ func httpRequest(method string, ur string, in interface{}, out interface{}) erro
return json.NewDecoder(res.Body).Decode(out)
}

func TestPagination(t *testing.T) {
items := make([]int, 5)
for i := 0; i < 5; i++ {
items[i] = i
}

pageCount, err := paginate(&items, "1", "1")
require.NoError(t, err)
require.Equal(t, 5, pageCount)
require.Equal(t, []int{1}, items)

items = make([]int, 5)
for i := 0; i < 5; i++ {
items[i] = i
}

pageCount, err = paginate(&items, "3", "2")
require.NoError(t, err)
require.Equal(t, 2, pageCount)
require.Equal(t, []int{4}, items)

items = make([]int, 6)
for i := 0; i < 6; i++ {
items[i] = i
}

pageCount, err = paginate(&items, "3", "3")
require.NoError(t, err)
require.Equal(t, 2, pageCount)
require.Equal(t, []int{5}, items)
}

func TestAPIConfigGet(t *testing.T) {
// since the HTTP server is created and deleted multiple times,
// we can't reuse TCP connections.
Expand Down