2021-09-14 12:48:27 -05:00
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
2022-11-27 13:20:29 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
2021-09-14 12:48:27 -05:00
|
|
|
|
2021-11-12 09:36:47 -05:00
|
|
|
package paginator
|
2021-09-14 12:48:27 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2021-11-12 09:36:47 -05:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-09-14 12:48:27 -05:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestPaginator(t *testing.T) {
|
|
|
|
cases := []struct {
|
2021-11-12 09:36:47 -05:00
|
|
|
db.Paginator
|
2021-09-14 12:48:27 -05:00
|
|
|
Skip int
|
|
|
|
Take int
|
|
|
|
Start int
|
|
|
|
End int
|
|
|
|
}{
|
|
|
|
{
|
2021-11-12 09:36:47 -05:00
|
|
|
Paginator: &db.ListOptions{Page: -1, PageSize: -1},
|
2021-09-14 12:48:27 -05:00
|
|
|
Skip: 0,
|
|
|
|
Take: setting.API.DefaultPagingNum,
|
|
|
|
Start: 0,
|
|
|
|
End: setting.API.DefaultPagingNum,
|
|
|
|
},
|
|
|
|
{
|
2021-11-12 09:36:47 -05:00
|
|
|
Paginator: &db.ListOptions{Page: 2, PageSize: 10},
|
2021-09-14 12:48:27 -05:00
|
|
|
Skip: 10,
|
|
|
|
Take: 10,
|
|
|
|
Start: 10,
|
|
|
|
End: 20,
|
|
|
|
},
|
|
|
|
{
|
2021-11-12 09:36:47 -05:00
|
|
|
Paginator: db.NewAbsoluteListOptions(-1, -1),
|
2021-09-14 12:48:27 -05:00
|
|
|
Skip: 0,
|
|
|
|
Take: setting.API.DefaultPagingNum,
|
|
|
|
Start: 0,
|
|
|
|
End: setting.API.DefaultPagingNum,
|
|
|
|
},
|
|
|
|
{
|
2021-11-12 09:36:47 -05:00
|
|
|
Paginator: db.NewAbsoluteListOptions(2, 10),
|
2021-09-14 12:48:27 -05:00
|
|
|
Skip: 2,
|
|
|
|
Take: 10,
|
|
|
|
Start: 2,
|
|
|
|
End: 12,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
skip, take := c.Paginator.GetSkipTake()
|
|
|
|
start, end := c.Paginator.GetStartEnd()
|
|
|
|
|
|
|
|
assert.Equal(t, c.Skip, skip)
|
|
|
|
assert.Equal(t, c.Take, take)
|
|
|
|
assert.Equal(t, c.Start, start)
|
|
|
|
assert.Equal(t, c.End, end)
|
|
|
|
}
|
|
|
|
}
|