-
Notifications
You must be signed in to change notification settings - Fork 4
/
page_test.go
47 lines (39 loc) · 1.26 KB
/
page_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package pgkit_test
import (
"strings"
"testing"
sq "github.com/Masterminds/squirrel"
"github.com/goware/pgkit/v2"
"github.com/stretchr/testify/require"
)
type T struct{}
func TestPagination(t *testing.T) {
const (
DefaultSize = 2
MaxSize = 5
Sort = "ID"
)
paginator := pgkit.NewPaginator[T](
pgkit.WithColumnFunc[T](strings.ToLower),
pgkit.WithDefaultSize[T](DefaultSize),
pgkit.WithMaxSize[T](MaxSize),
pgkit.WithSort[T](Sort),
)
page := pgkit.NewPage(0, 0)
result, query := paginator.PrepareQuery(sq.Select("*").From("t"), page)
require.Len(t, result, 0)
require.Equal(t, &pgkit.Page{Page: 1, Size: MaxSize}, page)
sql, args, err := query.ToSql()
require.NoError(t, err)
require.Equal(t, "SELECT * FROM t ORDER BY id ASC LIMIT 6 OFFSET 0", sql)
require.Empty(t, args)
result = paginator.PrepareResult(make([]T, 0), page)
require.Len(t, result, 0)
require.Equal(t, &pgkit.Page{Page: 1, Size: MaxSize}, page)
result = paginator.PrepareResult(make([]T, MaxSize), page)
require.Len(t, result, MaxSize)
require.Equal(t, &pgkit.Page{Page: 1, Size: MaxSize}, page)
result = paginator.PrepareResult(make([]T, MaxSize+2), page)
require.Len(t, result, MaxSize)
require.Equal(t, &pgkit.Page{Page: 1, Size: MaxSize, More: true}, page)
}