Skip to content

Commit

Permalink
api: fix wrong pageCount in /list endpoints (#1813)
Browse files Browse the repository at this point in the history
  • Loading branch information
aler9 committed May 18, 2023
1 parent b93eed6 commit 75eca96
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
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

0 comments on commit 75eca96

Please sign in to comment.