code.gitea.io/gitea@v1.21.7/models/db/paginator/paginator_test.go (about) 1 // Copyright 2021 The Gitea Authors. All rights reserved. 2 // SPDX-License-Identifier: MIT 3 4 package paginator 5 6 import ( 7 "testing" 8 9 "code.gitea.io/gitea/models/db" 10 "code.gitea.io/gitea/modules/setting" 11 12 "github.com/stretchr/testify/assert" 13 ) 14 15 func TestPaginator(t *testing.T) { 16 cases := []struct { 17 db.Paginator 18 Skip int 19 Take int 20 Start int 21 End int 22 }{ 23 { 24 Paginator: &db.ListOptions{Page: -1, PageSize: -1}, 25 Skip: 0, 26 Take: setting.API.DefaultPagingNum, 27 Start: 0, 28 End: setting.API.DefaultPagingNum, 29 }, 30 { 31 Paginator: &db.ListOptions{Page: 2, PageSize: 10}, 32 Skip: 10, 33 Take: 10, 34 Start: 10, 35 End: 20, 36 }, 37 { 38 Paginator: db.NewAbsoluteListOptions(-1, -1), 39 Skip: 0, 40 Take: setting.API.DefaultPagingNum, 41 Start: 0, 42 End: setting.API.DefaultPagingNum, 43 }, 44 { 45 Paginator: db.NewAbsoluteListOptions(2, 10), 46 Skip: 2, 47 Take: 10, 48 Start: 2, 49 End: 12, 50 }, 51 } 52 53 for _, c := range cases { 54 skip, take := c.Paginator.GetSkipTake() 55 start, end := c.Paginator.GetStartEnd() 56 57 assert.Equal(t, c.Skip, skip) 58 assert.Equal(t, c.Take, take) 59 assert.Equal(t, c.Start, start) 60 assert.Equal(t, c.End, end) 61 } 62 }