forked from gobuffalo/pop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pagination_test.go
74 lines (58 loc) · 1.37 KB
/
pagination_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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package pop
import (
"net/url"
"reflect"
"testing"
"github.com/gobuffalo/nulls"
"github.com/stretchr/testify/require"
)
func Test_NewPaginator(t *testing.T) {
a := require.New(t)
p := NewPaginator(1, 10)
a.Equal(p.Offset, 0)
p = NewPaginator(2, 10)
a.Equal(p.Offset, 10)
p = NewPaginator(2, 30)
a.Equal(p.Offset, 30)
}
func Test_NewPaginatorFromParams(t *testing.T) {
a := require.New(t)
params := url.Values{}
p := NewPaginatorFromParams(params)
a.Equal(p.Page, 1)
a.Equal(p.PerPage, 20)
params.Set(PaginatorPageKey, "2")
p = NewPaginatorFromParams(params)
a.Equal(p.Page, 2)
a.Equal(p.PerPage, 20)
params.Set(PaginatorPerPageKey, "30")
p = NewPaginatorFromParams(params)
a.Equal(p.Page, 2)
a.Equal(p.PerPage, 30)
}
func Test_Pagination(t *testing.T) {
if PDB == nil {
t.Skip("skipping integration tests")
}
transaction(func(tx *Connection) {
a := require.New(t)
for _, name := range []string{"Mark", "Joe", "Jane"} {
user := User{Name: nulls.NewString(name)}
err := tx.Create(&user)
a.NoError(err)
}
u := Users{}
q := tx.Paginate(1, 2)
err := q.All(&u)
a.NoError(err)
a.Equal(len(u), 2)
p := q.Paginator
a.Equal(p.CurrentEntriesSize, 2)
a.Equal(p.TotalEntriesSize, 3)
a.Equal(p.TotalPages, 2)
u = Users{}
err = tx.Where("name = 'Mark'").All(&u)
a.NoError(err)
a.Equal(reflect.ValueOf(&u).Elem().Len(), 1)
})
}